pandas & NumPypandas ו-NumPy
The two foundational Python libraries for working with numbers and time series, used by practically every quant.שתי ספריות היסוד של Python לעבודה עם מספרים וסדרות עתיות, ומשתמשים בהן כמעט כל הקוונטים.
What is it?מה זה?
NumPy provides fast multi-dimensional arrays and vectorized math, the base layer of scientific Python. pandas builds on it with the DataFrame and Series: labeled tables with powerful date and time indexing, resampling, rolling windows, joins and group operations. pandas was in fact originally created inside a quantitative investment firm to analyze financial data.NumPy מספקת מערכים רב-ממדיים מהירים וחישובים וקטוריים, ומהווה את שכבת הבסיס של Python המדעי. pandas בנויה מעליה עם DataFrame ו-Series: טבלאות עם תוויות, אינדקס תאריכים וזמנים חזק, resampling, חלונות מתגלגלים, מיזוגים ופעולות קבוצתיות. למעשה, pandas נוצרה במקור בתוך חברת השקעות כמותית כדי לנתח נתונים פיננסיים.
Why it matters for quantsלמה זה חשוב לקוואנטים
Almost everything a quant does starts with a table of prices indexed by date: computing returns, volatility, moving averages, correlations and signals. pandas makes these operations one-liners and handles the messy details of dates, missing values and aligning different series. NumPy handles the heavy numerical lifting for things like portfolio math and simulations. Most other tools on this site, including yfinance and vectorbt, return or consume pandas objects, so fluency here pays off everywhere.כמעט כל מה שקוונט עושה מתחיל בטבלת מחירים עם אינדקס של תאריכים: חישוב תשואות, תנודתיות, ממוצעים נעים, קורלציות וסיגנלים. pandas הופכת את הפעולות האלה לשורה אחת ומטפלת בפרטים המסובכים של תאריכים, ערכים חסרים ויישור סדרות שונות. NumPy מטפלת בחישובים הנומריים הכבדים, כמו מתמטיקה של תיקים וסימולציות. רוב הכלים האחרים באתר, כולל yfinance ו-vectorbt, מחזירים או מקבלים אובייקטים של pandas, כך ששליטה בה משתלמת בכל מקום.
What quants look forמה קוואנטים מחפשים שם
- pct_change() and np.log to compute simple and log returnspct_change() ו-np.log לחישוב תשואות פשוטות ולוגריתמיות
- rolling() windows for moving averages, rolling volatility and rolling correlationsחלונות rolling() לממוצעים נעים, תנודתיות מתגלגלת וקורלציות מתגלגלות
- resample() to convert daily data to weekly or monthlyresample() להמרת נתונים יומיים לשבועיים או לחודשיים
- shift() to lag signals correctly and avoid look-ahead biasshift() כדי להשהות סיגנלים נכון ולהימנע מ-look-ahead bias
- Vectorized NumPy operations instead of Python loops for speedפעולות וקטוריות של NumPy במקום לולאות Python, בשביל מהירות
How to get startedאיך מתחילים
- Install both with: pip install pandas numpy (they come preinstalled in Colab and Kaggle).התקינו את שתיהן עם: pip install pandas numpy (הן מותקנות מראש ב-Colab וב-Kaggle).
- Work through the official '10 minutes to pandas' guide in the pandas docs.עברו על המדריך הרשמי '10 minutes to pandas' בתיעוד של pandas.
- Load a price series (from a CSV or yfinance) into a DataFrame with a DatetimeIndex.טענו סדרת מחירים (מקובץ CSV או מ-yfinance) ל-DataFrame עם DatetimeIndex.
- Compute daily returns, a 20-day moving average and annualized volatility, then plot them.חשבו תשואות יומיות, ממוצע נע של 20 יום ותנודתיות שנתית, ואז שרטטו אותם.
Code exampleדוגמת קוד
import numpy as np
import pandas as pd
dates = pd.bdate_range("2024-01-01", periods=250)
prices = pd.Series(100 * np.cumprod(1 + np.random.normal(0, 0.01, 250)), index=dates)
returns = prices.pct_change().dropna()
sma20 = prices.rolling(20).mean()
ann_vol = returns.std() * np.sqrt(252)
print(f"Annualized volatility: {ann_vol:.2%}")
Uses a simulated random price series so it runs offline. Replace prices with real data to analyze an actual asset.משתמש בסדרת מחירים אקראית מדומה כדי שירוץ בלי אינטרנט. החליפו את prices בנתונים אמיתיים כדי לנתח נכס אמיתי.
Tips & pitfallsטיפים ומלכודות
- When turning a signal into positions, shift it by one bar (signal.shift(1)) so today's trade only uses information available yesterday.כשהופכים סיגנל לפוזיציות, הזיזו אותו בבר אחד (signal.shift(1)) כך שהעסקה של היום תשתמש רק במידע שהיה זמין אתמול.
- Rolling windows produce NaN at the start. Decide explicitly how to handle them instead of letting them silently drop rows.חלונות מתגלגלים מייצרים NaN בהתחלה. החליטו במפורש איך לטפל בהם במקום לתת להם להוריד שורות בשקט.
- Avoid looping over DataFrame rows. Vectorized operations are often orders of magnitude faster.הימנעו מלולאות על שורות של DataFrame. פעולות וקטוריות מהירות לעיתים קרובות בסדרי גודל.
Relatedקשור
Sourcesמקורות
Quant Israel is not affiliated with this service or company. Details may change — check the official site. Nothing here is investment advice.Quant Israel אינה קשורה לשירות או לחברה הזו. הפרטים עשויים להשתנות — בדקו באתר הרשמי. שום דבר כאן אינו ייעוץ השקעות.