-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysesecond.m
More file actions
213 lines (205 loc) · 5.56 KB
/
Copy pathAnalysesecond.m
File metadata and controls
213 lines (205 loc) · 5.56 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
%% data retrieval
tic
c1=readtable('AAPL.csv');
%c1(2528,:)=[];%for AAPLold
% logarithmic returns of close prices
CPS=struct2array(load('CPS_361_613_2007'));
log_rets=tick2ret(CPS,datetime(c1{:,1}),'Continuous');
[m,n]=size(log_rets);
%% adjusting data for function
%prices
adjc=zeros(m+1,n);
for i=1:n
adjc(:,i)=benford_data(CPS(:,i),'ALL',10);
end
emc_bec=zeros(10,n);
emp_bec=zeros(10,n);
for i=1:n
bec=benford_extract(adjc(:,i),'ALL',10,'2ND');
emp_bec(:,i)=bec{:,7};
emc_bec(:,i)=bec{:,6};
end
mean_emp_be=zeros(10,1);
for i=1:10
adder=0;
for j=1:n
adder=adder+emp_bec(i,j);
end
mean_emp_be(i)=adder/n;
end
%log returns
emc_belrc=zeros(10,n);
emp_belrc=zeros(10,n);
for i=1:n
belrc=benford_extract(log_rets(:,i),'ALL',10,'2ND');
emp_belrc(:,i)=belrc{:,7};
emc_belrc(:,i)=belrc{:,6};
end
mean_lr_emp_be=zeros(10,1);
for i=1:10
adder=0;
for j=1:n
adder=adder+emp_belrc(i,j);
end
mean_lr_emp_be(i)=adder/n;
end
%% figure overall empirical probability distributions
x=0:9;
y=0.11112*ones(size(x));
figure(1)
hold on
grid on
plot(x,mean_lr_emp_be,'b-o',x,mean_emp_be,'c-^','LineWidth',1.5)
plot(x,bec{:,3},'--k','LineWidth',1)
plot(x,y,'Color',[100 100 100]/256,'LineStyle','--','LineWidth',1)
hold off;
ylabel('\it{P_d}')
xlabel('First Significant Digit')
title('Overall Empirical Probability Distributions')
legend('Observed Log Return Values','Obeserved Prices Values','Second Law Distribution','Uniform Distribution')
%% Chi-Square goodness-of-fit test
prices_multiplier=sum(emc_bec,'all');
log_multiplier=sum(emc_belrc,'all');
% Overall Log Return against Benford Dist.
CSGOF_lr_be=0;
for i=1:10
CSGOF_lr_be=CSGOF_lr_be+(((mean_lr_emp_be(i)*log_multiplier-belrc{i,3}*log_multiplier)^2)/(belrc{i,3}*log_multiplier));
end
% Overal Prices against Benford Dist.
CSGOF_p_be=0;
for i=1:10
CSGOF_p_be=CSGOF_p_be+(((mean_emp_be(i)*prices_multiplier-bec{i,3}*prices_multiplier)^2)/(bec{i,3}*prices_multiplier));
end
%Overall Log Return against Uniform Dist.
CSGOF_lr_un=0;
for i=1:10
CSGOF_lr_un=CSGOF_lr_un+(((mean_lr_emp_be(i)*log_multiplier-0.11112*log_multiplier)^2)/(0.11112*log_multiplier));
end
%Overal Prices against Uniform Dist.
CSGOF_p_un=0;
for i=1:10
CSGOF_p_un=CSGOF_p_un+(((mean_emp_be(i)*prices_multiplier-0.11112*prices_multiplier)^2)/(0.11112*prices_multiplier));
end
%% day-by-day analysis of log returns
dbd_emc_belrc=zeros(10,m);
dbd_emp_belrc=zeros(10,m);
for i=1:m
belrc=benford_extract(log_rets(i,:),'ALL',10,'2ND');
dbd_emc_belrc(:,i)=belrc{:,6};
dbd_emp_belrc(:,i)=belrc{:,7};
end
%% chi-square day-by-day log returns against benford
CSGOF_dbd=zeros(1,m);
dbd_multiplier=zeros(1,m);
accepted5=0;
accepted1=0;
AL5=16.919;
AL1=21.666;
rejecteddays5=0;
rejecteddays1=0;
rday1=zeros(1,m);
rday5=zeros(1,m);
t=1;
s=1;
for j=1:m
for i=1:10
dbd_multiplier(j)=dbd_multiplier(j)+dbd_emc_belrc(i,j);
end
end
for j=1:m
for i=1:10
CSGOF_dbd(j)=CSGOF_dbd(j)+(((dbd_emc_belrc(i,j)-belrc{i,3}*dbd_multiplier).^2)/(belrc{i,3}*dbd_multiplier));
end
if CSGOF_dbd(j) <= AL1
accepted1=accepted1+1;
else
rejecteddays1=rejecteddays1+1;
rday1(s)=j;
s=s+1;
end
if CSGOF_dbd(j) <= AL5
accepted5=accepted5+1;
else
rejecteddays5=rejecteddays5+1;
rday5(t)=j;
t=t+1;
end
end
rday1=rday1(rday1~=0);
rday5=rday5(rday5~=0);
%% Consecutive rejection analysis for 5%
consecutive5=diff(rday5)==1;
consecutive1=diff(rday1)==1;
lc5=max(size(consecutive5));
lc1=max(size(consecutive1));
consec_no=zeros(20,2);
i=1;
c=1;
b=1;
while i<=lc5
if consecutive5(i)==1
c=2;
i=i+1;
while i<lc5 && consecutive5(i)==1
c=c+1;
i=i+1;
end
consec_no(c,1)=consec_no(c,1)+1;
else
consec_no(1,1)=consec_no(1,1)+1;
i=i+1;
end
end
i=1;
while i<=lc1
if consecutive1(i)==1
b=2;
i=i+1;
while i<lc1 && consecutive1(i)==1
b=b+1;
i=i+1;
end
consec_no(b,2)=consec_no(b,2)+1;
else
consec_no(1,2)=consec_no(1,2)+1;
i=i+1;
end
end
[sortedX, sortedInds] = sort(CSGOF_dbd(:),'descend');
%%
figure(2)
dates=(datetime(c1{:,1}));
dates(m,:)=[];
plotdates=linspace(dates(1),dates(end),m/121);
scatter(dates,CSGOF_dbd,10,([50,0,153]/256))
yline(AL5,'Color',[57 255 20]/256,'lineWidth',3)
grid on
xticks(plotdates)
xtickformat('dd-MMM-yyyy')
xtickangle(45)
xlim([dates(1),dates(end)])
xlabel('Date')
ylabel('Chi-Square')
ylim([0,sortedX(1)+5])
yticks(0:5:sortedX(1)+5)
title('Chi-Square Values of the Day-by-Day Analysis of Returns')
format bank;
varnames={'Reference Probability Distribution','Chi-Square w.r.t. Prices','Chi-Square w.r.t. Returns'};
T=table(['Benford';'Uniform'],[CSGOF_p_be;CSGOF_p_un],[CSGOF_lr_be;CSGOF_lr_un],'VariableNames',varnames) %#ok<NOPTS>
figure(3)
hold on
plot(x,dbd_emp_belrc(:,sortedInds(end)),'b-o',x,dbd_emp_belrc(:,sortedInds(1)),'c-^','LineWidth',1.5)
plot(x,bec{:,3},'--k','LineWidth',1)
plot(x,y,'Color',[100 100 100]/256,'LineStyle','--','LineWidth',1)
grid on
ylabel('\it{P_d}')
xlabel('First Significant Digit')
title('Most and Least Accepted Days')
legend('Most Accepted Day','Least Accepted Day','Second Law Distribution','Uniform Distribution')
hold off
%%
top45rd=string([45,1]);
for i=1:45
top45rd(i)=datestr(dates(sortedInds(i)+1,1));
end
toc