forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTU_mom_hypothesisTest.m
113 lines (79 loc) · 3.21 KB
/
TU_mom_hypothesisTest.m
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
clear;
% load('../Data/inputDataOHLCDaily_20120511', 'syms', 'tday', 'cl');
load('inputDataOHLCDaily_20120511', 'syms', 'tday', 'cl');
idx=strmatch('TU', syms, 'exact');
tday=tday(:, idx);
cl=cl(:, idx);
lookback=250;
holddays=25;
longs=cl > backshift(lookback, cl) ;
shorts=cl < backshift(lookback, cl) ;
pos=zeros(length(cl), 1);
for h=0:holddays-1
long_lag=backshift(h, longs);
long_lag(isnan(long_lag))=false;
long_lag=logical(long_lag);
short_lag=backshift(h, shorts);
short_lag(isnan(short_lag))=false;
short_lag=logical(short_lag);
pos(long_lag)=pos(long_lag)+1;
pos(short_lag)=pos(short_lag)-1;
end
marketRet=(cl-backshift(1, cl))./backshift(1, cl);
marketRet(~isfinite(marketRet))=0;
ret=backshift(1, pos).*marketRet/holddays;
ret(isnan(ret))=0;
% Gaussian hypothesis test
fprintf(1, 'Gaussian Test statistic=%4.2f\n', mean(ret)/std(ret)*sqrt(length(ret)));
% Gaussian Test statistic=2.93
% Randomized market returns hypothesis test
moments={mean(marketRet), std(marketRet), skewness(marketRet), kurtosis(marketRet)};
numSampleAvgretBetterOrEqualObserved=0;
for sample=1:10000
marketRet_sim=pearsrnd(moments{:}, length(marketRet), 1);
cl_sim=cumprod(1+marketRet_sim)-1;
longs_sim=cl_sim > backshift(lookback, cl_sim) ;
shorts_sim=cl_sim < backshift(lookback, cl_sim) ;
pos_sim=zeros(length(cl_sim), 1);
for h=0:holddays-1
long_sim_lag=backshift(h, longs_sim);
long_sim_lag(isnan(long_sim_lag))=false;
long_sim_lag=logical(long_sim_lag);
short_sim_lag=backshift(h, shorts_sim);
short_sim_lag(isnan(short_sim_lag))=false;
short_sim_lag=logical(short_sim_lag);
pos_sim(long_sim_lag)=pos_sim(long_sim_lag)+1;
pos_sim(short_sim_lag)=pos_sim(short_sim_lag)-1;
end
ret_sim=backshift(1, pos_sim).*marketRet_sim/holddays;
ret_sim(~isfinite(ret_sim))=0;
if (mean(ret_sim)>= mean(ret))
numSampleAvgretBetterOrEqualObserved=numSampleAvgretBetterOrEqualObserved+1;
end
end
fprintf(1, 'Randomized prices: p-value=%f\n', numSampleAvgretBetterOrEqualObserved/10000);
% p-value=0.027500
% Randomized entry trades hypothesis test
numSampleAvgretBetterOrEqualObserved=0;
for sample=1:100000
P=randperm(length(longs));
longs_sim=longs(P);
shorts_sim=shorts(P);
pos_sim=zeros(length(cl), 1);
for h=0:holddays-1
long_sim_lag=backshift(h, longs_sim);
long_sim_lag(isnan(long_sim_lag))=false;
long_sim_lag=logical(long_sim_lag);
short_sim_lag=backshift(h, shorts_sim);
short_sim_lag(isnan(short_sim_lag))=false;
short_sim_lag=logical(short_sim_lag);
pos(long_sim_lag)=pos(long_sim_lag)+1;
pos(short_sim_lag)=pos(short_sim_lag)-1;
end
ret_sim=backshift(1, pos_sim).*marketRet/holddays;
ret_sim(isnan(ret_sim))=0;
if (mean(ret_sim)>= mean(ret))
numSampleAvgretBetterOrEqualObserved=numSampleAvgretBetterOrEqualObserved+1;
end
end
fprintf(1, 'Randomized trades: p-value=%f\n', numSampleAvgretBetterOrEqualObserved/100000);