33 lines
3.2 KiB
R
33 lines
3.2 KiB
R
# Goal: An example of simulation-based inference.
|
|
# This is in the context of testing for time-series dependence in
|
|
# stock market returns data.
|
|
# The code here does the idea of Kim, Nelson, Startz (1991).
|
|
# We want to use the distribution of realworld returns data, without
|
|
# needing assumptions about normality.
|
|
# The null is lack of dependence (i.e. an efficient market).
|
|
# So repeatedly, the data is permuted, and the sample ACF is computed.
|
|
# This gives us the distribution of the ACF under H0: independence, but
|
|
# while using the empirical distribution of the returns data.
|
|
|
|
# Weekly returns on Nifty, 1/1/2002 to 31/12/2003, 104 weeks of data.
|
|
r <- c(-0.70031182197603, 0.421690133064168, -1.20098072984689, 0.143402360644984, 3.81836537549516, 3.17055939373247, 0.305580301919228, 1.23853814691852, 0.81584795095706, -1.51865139747764, -2.71223626421522, -0.784836480094242, 1.09180041170998, 0.397649587762761, -4.11309534220923, -0.263912425099111, -0.0410144239805454, 1.75756212770972, -2.3335373897992, -2.19228764624217, -3.64578978183987, 1.92535789661354, 3.45782867883164, -2.15532607229374, -0.448039988298987, 1.50124793565896, -1.45871585874362, -2.13459863369767, -6.2128068251802, -1.94482987066289, 0.751294815735637, 1.78244982829590, 1.61567494389745, 1.53557708728931, -1.53557708728931, -0.322061470004265, -2.28394919698225, 0.70399304137414, -2.93580952607737, 2.38125098034425, 0.0617697039252185, -4.14482733720716, 2.04397528093754, 0.576400673606603, 3.43072725191913, 2.96465382864843, 2.89833358015583, 1.85387040058336, 1.52136515035952, -0.637268376944444, 1.75418926224609, -0.804391905851354, -0.861816058320475, 0.576902488444109, -2.84259880663331, -1.35375536139417, 1.49096529042234, -2.05404881010045, 2.86868849528146, -0.258270670200478, -4.4515881438687, -1.73055019137092, 3.04427015714648, -2.94928202352018, 1.62081315773994, -6.83117945164824, -0.962715713711582, -1.75875847071740, 1.50330330252721, -0.0479705789653728, 3.68968303215933, -0.535807567290103, 3.94034871061182, 3.85787174417738, 0.932185956989873, 4.08598654183674, 2.27343783689715, 1.13958830440017, 2.01737201171230, -1.88131458327554, 1.97596267156648, 2.79857144562001, 2.22470306481695, 2.03212951411427, 4.95626853448883, 3.40400972901396, 3.03840139165246, -1.89863129741417, -3.70832135042951, 4.78478922155396, 4.3973589590097, 4.9667050392987, 2.99775078737081, -4.12349101552438, 3.25638269809945, 2.29683376253966, -2.64772825878214, -0.630835277076258, 4.72528848505451, 1.87368447333380, 3.17543946162564, 4.58174427843208, 3.23625985632168, 2.29777651227296)
|
|
|
|
# The 1st autocorrelation from the sample:
|
|
acf(r, 1, plot=FALSE)$acf[2]
|
|
|
|
# Obtain 1000 draws from the distribution of the 1st autocorrelation
|
|
# under the null of independence:
|
|
set.seed <- 101
|
|
simulated <- replicate(1000, acf(r[sample(1:104, replace=FALSE)], 1, plot=FALSE)$acf[2])
|
|
# At 95% --
|
|
quantile(simulated, probs=c(.025,.975))
|
|
# At 99% --
|
|
quantile(simulated, probs=c(.005,.995))
|
|
|
|
# So we can reject the null at 95% but not at 99%.
|
|
|
|
# A pretty picture.
|
|
plot(density(simulated), col="blue")
|
|
abline(v=0)
|
|
abline(v=quantile(simulated, probs=c(.025,.975)), lwd=2, col="purple")
|
|
abline(v=acf(r, 1, plot=FALSE)$acf[2], lty=2, lwd=4, col="yellow") |