While working on Quantopian.com, I have had to do a fair bit of learning in various areas
- Learn Statistics
- Operating on Data in Pandas
- Trading Models
This is my attempt at documenting some of my learnings for my own and others.
Fundamentals Data Operations
Calculate Z-Score for specific fundamental fields in the Quantopian fundamentals DataFrame.
from scipy import stats
#Get rows you want
d=fund_df.loc[['pe_ratio','ev_to_ebitda']]
#Transpose it
d=d.T
#Drop NANs
d=d.dropna()
#Apply stats.zscore on that data
d=d.apply(stats.zscore)
#transpose it back
d=d.T
#rename the columns as needed
zscore=d.rename({'ev_to_ebitda':'ev_to_ebitda_zscore','pe_ratio':'pe_ratio_zscore'})
Calculate Z-Score for specific fundamental fields in the Quantopian fundamentals DataFrame, but do it group-wise by Morning Start Sector Code
from scipy import stats
#Transpose it
d=fund_df.T
#Drop NANs
d=d.dropna()
#Groupby Morning Star Code
d=d.groupby('morningstar_sector_code')
#Get rows you want
d=d[['pe_ratio','ev_to_ebitda']]
#Transform with stats.zscore on that grouped data
d=d.transform(stats.zscore)
#transpose it back
d=d.T
#rename the columns as needed
gzscore=d.rename({'ev_to_ebitda':'ev_to_ebitda_gzscore','pe_ratio':'pe_ratio_gzscore'})
Add these rows to back to fundamentals data
fund_df = pandas.concat([fund_df,zscore,gzscore])
Copyright (c) Sarvi Shanmugham
No comments:
Post a Comment