Time series analysis by James D Hamilton - its considered to be the bible on time series analysis, pretty much covers all the theory , it doesnt have any code or pseudocode
Introduction to Time Series with R by Metcalfe & Coweperwait
Analysis of Financial Time Series by Tsay
Aim and Motivation
Would be exploring time series analysis in the context of finance
Implementing code from the book by Tsay , would be using the book by Hamilton and Metcalfe as reference
Aim to understand different time series models in R.
Pt=Pt−1(1+Rt)⟹Rt=PtPt−Pt−1 where Pt and Rt are the price and return at time t respectively Continuously compounded return or log return
rt=ln(1+Rt) Dividend Payment
rt=ln(Pt+Dt)−ln(Pt−1) Where Dt is dividend payment between time t-1 and time t .
Definition 2 (Multiperiod gross return) Similiary we can define multiperiod simple return by Rt[k]=exp[k1j=0∑k−1(1+Rt−j)]−1 where exp is the exponentential function and k is total number periods the asset was held for.
Log return would be rt[k]=rt+rt−1+.....+rt−k+1
Where p is the portfolio , i the ith asset and wi the weight of the ith asset in the portfolio
* Load data and header =T give the 1st row of the data file , that is the names of the cloumns of the data set
# da contains the return for 5 five stocks and indexes namely IBM ,value-weighted , equal-weighted and S&P composite index from 1970 to 2008library(fBasics)da =read.table("TST/d-ibm3dx7008.txt",header = T) dim(da) # dimensions for the data
[1] 9845 5
da[1:5,]# first five rows
ABCDEFGHIJ0123456789
Date
<int>
rtn
<dbl>
vwretd
<dbl>
ewretd
<dbl>
sprtrn
<dbl>
1
19700102
0.000686
0.012137
0.033450
0.010211
2
19700105
0.009596
0.006375
0.018947
0.004946
3
19700106
0.000679
-0.007233
-0.005776
-0.006848
4
19700107
0.000678
-0.001272
0.003559
-0.002047
5
19700108
0.002034
0.000564
0.002890
0.000540
tail(da)#Last 5 rows
ABCDEFGHIJ0123456789
Date
<int>
rtn
<dbl>
vwretd
<dbl>
ewretd
<dbl>
sprtrn
<dbl>
9840
20081223
-0.016953
-0.007618
-0.008332
-0.009717
9841
20081224
-0.000993
0.004463
0.005254
0.005781
9842
20081226
0.010060
0.007170
0.011629
0.005356
9843
20081229
-0.000984
-0.004481
-0.016514
-0.003873
9844
20081230
0.028308
0.024951
0.021692
0.024407
9845
20081231
0.007301
0.017513
0.036731
0.014158
ibm = da[,2] # IBM simple returnssibm = ibm*100#Percentage simple returns b =basicStats(sibm)s1 =skewness(sibm)t = s1/sqrt(6/9845) #definition for test statistic here 9845 is N pv =2*(1-pnorm(t)) # Calculate p-valuecat("skewness =", s1,"\nTest Satistic = ",t,"\np-value = ", pv)
skewness = 0.06139878
Test Satistic = 2.487093
p-value = 0.01287919
#IBM = ibm[, 2] # IBM simple return values#IBM = ts(IBM,frequency = 250 ,start = c(1970,1))#plot.ts(IBM)#acf(IBM,lag=250,ylim= c(-0.05,0.05))#ACF is covered in detail down below , the x axis label is fraction of unit time #libm = ibm[,2]*100#length(libm)#mean(libm)#t.test(libm)#k1 = kurtosis(libm)#s1 =skewness(libm)#t3 = (s1^2)/(6/9845)#t2 = ((k1)^2)/(24/9845)#3t2#a = t2+t3#a#normalTest(libm,method = 'jb')
Linear Time Series
Stationarity
Foundation of time series analysis is Stationarity. A time series {rt} is said to be strictly stationary if the joint distribution of (rt1,rt2,rt3,....,rtk) is identical to that of (rt1+t,rt2+t,rt3+t,....,rtk+t) for all t , where k is an arbitrary positive integer and (t1,t2,t3,....,tk) is collection of k positive integers. Strict stationarity is very hard to verify emperically A time series is said to be weakly stationary if the mean of rt and the covariance between rt and rt−l are time invariant where l is an arbitrary integer. Weak stationarity enables us to make inference concerning future observations
E(tt)=μ
Cov(rt,rt−l)=γl
Weak stationarity is commonly studied and more practical.
The covariance γl=Cov(rt,rt−l) is called the lag-l autocovariance of rt. From the above defintion it follows that
γ0=Var(rt)
γ−l=γl
Autocorrelation Function
Consider a weakly stationary return series rt. When the linear dependence between rt and its past values rt−l is of interest , the concept of correlation is generalised to autocorrelation. The correlation coefficient between rt and rt−l is called the lag-l autocorrelation of rt and is commonly denoted by ρl , which under the weak stationarity assumption is a function of l only. Its defined by ρl=Var(rt)Var(rt−l)Cov(rt,rt−l)=Var(rt)Cov(rt,rt−l)=γ0γl
Its easy to see that
ρ0=1
ρl=ρ−l
−1≤ρl≤1
For a given sample of returns {rt}t=1T , let rˉ be the sample mean. Then the lag-1 autocorrelation of rt is ρ^1=∑t=2T(rt−rˉ)2∑t=2T(rt−rˉ)(rt−1−rˉ)
The conditions under which ρ^ is a consistent estimator of ρ1 are as follows :
The returns {rt} is an independent and identically distributed sequence
E(rt2)<∞
Then ρ^1 is asymptotically normal with mean zero and variance 1/T
Testing
Let H0:ρ1=0 be the null hypothesis versus Ha:ρ1=0 the alternative hypothesis. The test statistic is the usual t ratio (check the statistics section for further description) , which is Tρ^1 and follows asymptotically the standard normal distribution. The null hypothesis H0 is rejected if the t ratio is large in magnitude or equivalently , the p value of the t ratio is small , lets say less than 0.05