-
Notifications
You must be signed in to change notification settings - Fork 1
/
Data.py
138 lines (104 loc) · 8.76 KB
/
Data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
""" This Python scripts downloads all the required data for each Systemic Risk Paper
1. Malanhobis Distance
2. Correlation Surprise
3. Absorption Ratio
.... """
#STAGE1: Import Python libraries
import pandas.io.data as pdio #Functions from pandas.io.data extract data from various Internet sources into a DataFrame. Currently the following sources are supported: Yahoo Finance, Google Finance, FRED, Kenneth French's library, World Bank and Google Analytics
import numpy as np #NumPy is an extension to the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions to operate on these arrays
import pandas as pd #Pandas is a software library written for the Python programming language for data manipulation and analysis.
import pandas.io.data as web
import systemicRiskMeasures as srm
#STAGE2: Download Required Data
# MahalanoBis Distance
#Figure 4
#symbols = ['^GSPC','EFA','^TYX','TIP','VNQ','^DJC'] #An example of the type of data that would be required for Mahalanobis Distance inputs
#Start_Date,End_Date='10/20/1973','12/8/2014' #Date Range for required returns. In the format of MM/DD/YY
#Historical_Prices = pdio.get_data_yahoo(symbols,start= Start_Date,end= End_Date)# Download Historical Prices using symbols as a list of yahoo tickers over the defined start date-end date
#Historical_Prices.save('returns')
#symbols = ['^TNX'] #An example of the type of data that would be required for Mahalanobis Distance inputs
#Start_Date,End_Date='01/01/1998','1/29/2010' #Date Range for required returns. In the format of MM/DD/YY
#Historical_Prices = pdio.get_data_yahoo(symbols,start= Start_Date,end= End_Date)# Download Historical Prices using symbols as a list of yahoo tickers over the defined start date-end date
#Historical_Prices.save('Treasury_Bonds_AR_Exhibit_9') #Save Figure 4's data as 'returns'
#Figure 5 MD
symbols = ['XWD.TO','^RUT','^GSPC','IWF','IWD','HDG'] #An example of the type of data that would be required for Figure 5 inputs
Start_Date,End_Date='1/1/1993','12/8/2014' #Date Range for required returns. In the format of MM/DD/YY
Historical_Pricess = pdio.get_data_yahoo(symbols,start= Start_Date,end= End_Date)#Download Historical Prices using symbols as a list of yahoo tickers over the defined start date-end date
Historical_Pricess_closed=Historical_Pricess['Adj Close'].dropna() # From Historical Prices extract the Adjusted CLose Returns
Figure5= pd.DataFrame(index=Historical_Pricess_closed.index) #Create Figure 5 DataFrame with Historical_Prices dates as index.
Figure5['World Equties']=Historical_Pricess_closed['HDG'].values #Append World Equties to DataFrame
Figure5['Small-Large']=Historical_Pricess_closed['^RUT'].values-Historical_Pricess_closed['^GSPC'].values#Append Small-Large to DataFrame
Figure5['Growth-Value']=Historical_Pricess_closed['IWF'].values-Historical_Pricess_closed['IWD'].values#Append Growth-Value to DataFrame
Figure5['Hedge Funds']=Historical_Pricess_closed['HDG'].values #Append Hedge Funds to DataFrame
Figure5.save('returnsMD_Figure5') #save Figure 5's DataFrame as returns Mahalanobis Distance Figure 5
#Table 1
Global_Assets=pd.read_csv('Global_Assets.csv', index_col=0)
Global_Assets_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=Global_Assets)
Global_Assets_timeseries_dataframe.save('Table1_Global_Assets')
US_Assets=pd.read_csv('US_Assets.csv', index_col=0)
US_Assets_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=US_Assets)
US_Assets_timeseries_dataframe.save('Table1_US_Assets')
Currency=pd.read_csv('Currency.csv', index_col=0)
Currency_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=Currency)
Currency_timeseries_dataframe.save('Table1_Currency')
#Table 2
Table_2_Asset_Classes=pd.read_csv('MahDis_Table_2.csv', index_col=0)
Asset_Classes_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=Table_2_Asset_Classes)
Asset_Classes_timeseries_dataframe_log_returns=srm.logreturns(Returns=Asset_Classes_timeseries_dataframe)
Asset_Classes_timeseries_dataframe_log_returns.save('Table2_Asset_Classes')
#Mean-variance optimisation
symbols = ['^GSPC'] #An example of the type of data that would be required for Figure 5 inputs
Start_Date='1/1/1980' #Date Range for required returns. In the format of MM/DD/YY
Historical_Pricess = pdio.get_data_yahoo(symbols,start= Start_Date)#Download Historical Prices using symbols as a list of yahoo tickers over the defined start date-end date
Historical_Pricess_closed=Historical_Pricess['Adj Close'].dropna()
Historical_Pricess_closed.save('S&P_500')
symbols = ['^VIX'] #An example of the type of data that would be required for Figure 5 inputs
Start_Date,End_Date='2/01/1977','31/12/2014' #Date Range for required returns. In the format of MM/DD/YY
Historical_Pricess = pdio.get_data_yahoo(symbols,start= Start_Date,end= End_Date)#Download Historical Prices using symbols as a list of yahoo tickers over the defined start date-end date
Historical_Pricess_closed=Historical_Pricess['Adj Close'].dropna()
Historical_Pricess_closed.save('^VIX')
symbols = ['^GSPC', '^TYX'] #An example of the type of data that would be required for Figure 5 inputs
Start_Date='15/02/1977' #Date Range for required returns. In the format of MM/DD/YY
Historical_Pricess = pdio.get_data_yahoo(symbols,start= Start_Date)#Download Historical Prices using symbols as a list of yahoo tickers over the defined start date-end date
Historical_Pricess_closed=Historical_Pricess['Adj Close'].dropna()
Historical_Pricess_closed.save('Probit_portfolio')
#Correlation Surprise
#Exhibit 5: US Equities, European Equities, Currencies
#Equities
US_Equities=pd.read_csv('CorrSur_USEquities.csv', index_col=0)
US_Equities_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=US_Equities)
US_Equities_timeseries_dataframe.save('Exhibit5_US_Equities')
#European Equtiies
Euro_Equities=pd.read_csv('CorrSur_EuropeanEquities.csv', index_col=0)
Euro_Equities_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=Euro_Equities)
Euro_Equities_timeseries_dataframe.save('Exhibit5_Euro_Equities')
#Currency
Currency=pd.read_csv('CorrSur_Currency.csv', index_col=0)
Currency_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=Currency)
Currency_timeseries_dataframe.save('Exhibit5_Currency')
#tstart= '10/20/2000' #An example of the type of data required for Exhibit 5 Currency
#tend= '10/20/2014'
#Currency= web.DataReader(["EXUSEU","EXUSUK"], 'fred', tstart, tend)
#Currency.save('CorrelationSurprise_Exhibit5_Currency')
#Images for Article
#Mahalanobis Data
EM_Asia=pd.read_csv('EM_Asia_xIndia.csv', index_col=0)
EM_Asia_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=EM_Asia)
EM_Asia_timeseries_dataframe.save('EM_Asia_xIndia')
USsectors=pd.read_csv('USsectors.csv', index_col=0)
USsectors_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=USsectors)
USsectors_timeseries_dataframe.save('USsectors')
UKsectors=pd.read_csv('UKsectors.csv', index_col=0)
UKsectors_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=UKsectors)
UKsectors_timeseries_dataframe.save('UKsectors')
JPNsectors=pd.read_csv('JAPsectors.csv', index_col=0)
JPNsectors_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=JPNsectors)
JPNsectors_timeseries_dataframe.save('JPNsectors')
CANsectors=pd.read_csv('CANsectors.csv', index_col=0)
CANsectors_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=CANsectors)
CANsectors_timeseries_dataframe.save('CANsectors')
####################################################################
"""Probit"""
USARECM=pd.read_csv('USARECM.csv', index_col=0)
USARECM_timeseries_dataframe=srm.CreateDataFrameWithTimeStampIndex(DataFrame=USARECM)
USARECM_timeseries_dataframe.save('USARECM')