Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/paper_analysis/data/preparedata_monthly.R
1433 views
1
2
3
4
setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs")
5
6
library(zoo);
7
8
# Load the data
9
start = as.Date("1975-12-31") ; end = as.Date("2010-06-30") ;
10
dates = seq.Date(as.Date(start) + 1, as.Date(end) + 1, by ="month") - 1
11
12
13
# S&P GSCI
14
15
data = as.data.frame(read.csv( file = "data/SPGSCIall.csv",skip=5,sep=","))
16
colnames(data) = c("Date","SPGSCI")
17
SPGSCI = zoo( data$SPGSCI , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
18
SPGSCI = window( SPGSCI , start = start , end = end );
19
monthlySPGSCI = aggregate(SPGSCI, as.yearmon , tail , 1 );
20
monthlyR_SPGSCI = diff(monthlySPGSCI)/as.vector(lag(monthlySPGSCI))
21
monthlyR_SPGSCI = zoo( as.vector(monthlyR_SPGSCI) , order.by = dates[2:length(dates)])
22
23
# S&P 500 total returns
24
25
data = as.data.frame(read.csv( file = "data/SP500all.csv",skip=5,sep=","))
26
colnames(data) = c("Date","SP500")
27
SP500 = zoo( data$SP500 , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
28
SP500= window( SP500 , start = start , end = end );
29
monthlySP500 = aggregate(SP500, as.yearmon , tail , 1 );
30
monthlyR_SP500 = diff(monthlySP500)/as.vector(lag(monthlySP500))
31
32
# we still need the returns on the total return index for the period Jan 1976-Jan 1988
33
34
data = as.data.frame(read.csv( file = "data/SP500returns.csv",skip=5,sep=","))
35
colnames(data) = c("Date","SP500")
36
SP500 = zoo( data$SP500 , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
37
SP500= window( SP500 , start = start , end = "1988-01-31" );
38
monthlyRSP500bis = aggregate(SP500, as.yearmon , tail , 1 );
39
Z = merge( monthlyRSP500bis , monthlyR_SP500 )
40
monthlyR_SP500 = replace(Z$monthlyRSP500bis, is.na( Z$monthlyRSP500bis) , 0) + replace( Z$monthlyR_SP500, is.na( Z$monthlyR_SP500) , 0)
41
monthlyR_SP500 = zoo( as.vector(monthlyR_SP500) , order.by = dates[2:length(dates)])
42
43
# MSCI EAFE
44
45
data = as.data.frame(read.csv( file = "data/EAFEall.csv",skip=5,sep=","))
46
colnames(data) = c("Date","EAFE")
47
EAFE = zoo( data$EAFE , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
48
EAFE = window( EAFE , start = start , end = end );
49
monthlyEAFE= aggregate(EAFE, as.yearmon , tail , 1 );
50
monthlyR_EAFE = diff(monthlyEAFE)/as.vector(lag(monthlyEAFE))
51
monthlyR_EAFE = zoo( as.vector(monthlyR_EAFE) , order.by = dates[2:length(dates)])
52
53
54
# NAREIT
55
56
data = as.data.frame(read.csv( file = "data/nareit.csv",skip=0,sep=","))
57
colnames(data) = c("Date","NAREIT")
58
monthlyNAREIT = zoo( as.numeric(data$NAREIT) , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
59
monthlyNAREIT = window( monthlyNAREIT , start = start , end = end );
60
monthlyR_NAREIT = diff(monthlyNAREIT)/as.vector(lag(monthlyNAREIT))
61
monthlyR_NAREIT = zoo( as.vector(monthlyR_NAREIT) , order.by = dates[2:length(dates)])
62
63
# Bond
64
65
data = as.data.frame(read.csv( file = "data/MLDOMMASTERall.csv",skip=5,sep=","))
66
colnames(data) = c("Date","bond")
67
bond = zoo( data$bond , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
68
bond = window( bond , start = start , end = end );
69
monthlybond= aggregate(bond, as.yearmon , tail , 1 );
70
monthlyR_bond = diff(monthlybond)/as.vector(lag(monthlybond))
71
monthlyR_bond = zoo( as.vector(monthlyR_bond) , order.by = dates[2:length(dates)])
72
73
# Inflation
74
75
data = as.data.frame(read.csv( file = "data/CPI.csv",skip=5,sep=","))
76
colnames(data) = c("Date","CPI")
77
CPI = zoo( data$CPI , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
78
CPI = window( CPI , start = start , end = end );
79
monthlyCPI = aggregate(CPI, as.yearmon , tail , 1 );
80
monthlyInflation = diff(monthlyCPI)/as.vector(lag(monthlyCPI))
81
monthlyInflation = zoo( as.vector(monthlyInflation) , order.by = dates[2:length(dates)])
82
83
84
# Middle interest rate on secondary market 3-month treasury bill:
85
86
data = as.data.frame(read.csv( file = "data/Tbillall.csv",skip=5,sep=","))
87
colnames(data) = c("Date","TBill")
88
TBill = zoo( data$TBill , order.by = as.Date( as.character(data$Date) , format = "%m/%d/%Y" ) )
89
TBill = window( TBill , start = start , end = end );
90
monthlyTBill = aggregate(TBill, as.yearmon , tail , 1 );
91
#data$TBill[(as.character(data$TBill)=="#NA")]=NA;
92
# The rates listed on Treasury bills are annualized discount rates, assuming a year has 360 days.
93
# T-bill discount rate = [face value-bill price]*(360/number of days until maturity)
94
# A three month T-Bill has a maturity of 91 days
95
# The bond equivalent yield associated to the T-Bill is
96
# T-bill yield = [ (face value - bill price)/bill price ]x(365/number of days until maturity)
97
# The annualized yield is then (take face value of 1000):
98
# bill price = 100 - (91/360)*rate
99
# yield = {[(91/360)*rate]/[100 - (91/360)*rate]}*(365/91)
100
# See e.g. Mizrach, Bruce and Neely, Christopher J.,The Microstructure of the U.S. Treasury Market(April 2008). FRB St. Louis Working Paper No. 2007-052B. Available at SSRN: http://ssrn.com/abstract=1070226
101
102
# Bond-equivalent annualized yield
103
monthlyTBill = (91/360)*(365/91)*monthlyTBill/(100-monthlyTBill*(91/360) ) ;
104
# Make it monthly
105
monthlyTBill = monthlyTBill/12;
106
monthlyTBill = zoo( as.vector(monthlyTBill) , order.by = dates[2:length(dates)])
107
108
# Mixed portfolio
109
110
data = merge.zoo( monthlyR_bond , monthlyR_SP500 , monthlyR_NAREIT, monthlyR_SPGSCI , monthlyTBill,monthlyInflation , monthlyR_EAFE)
111
colnames(data) = c( "Bond" , "SP500", "NAREIT" , "SPGSCI" ,"TBill","Inflation", "EAFE" )
112
data = na.locf(data);
113
head(data); plot(data); tail(data)
114
write.zoo( data , file="data/data.txt" , row.names=F )
115
write.csv( data , file="data/data.csv" , row.names=F )
116
#save( data , file = paste( getwd(),"/data/equitybondscommodity/equitybondscommodity.Rdata" , sep="") )
117
118
119
120