Info

This question is closed. Reopen it to edit or answer.

How to code conducting tests to different time periods?

1 view (last 30 days)
Anders
Anders on 29 Dec 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi!
Im trying to conduct a ttest and ftest on a dataset using a movingsum as an signal on the dataset, and I want the months used in the sum to go from 2-15 and test for each one of these periods. I started to duplicate the code for n=2 to fit n=3, but thought that there might be a more clever way to do this. Does anyone know of one? If you look at the code below, you should get the idea. NOTE; I also want to gather all the results from the tests in 2 matrices, so I can easily access these.
Any help would be greatly greatly appreciated as it would save me alot of time! Thanks in advance!
load austrial.txt % load the data matrix from the file
mkt = austrial(:,2); % market return in the second column
w = log(1 + mkt/100)
numElementsToSum = 2;
mSum = conv(w, ones(numElementsToSum, 1), 'valid');
movsum = mSum(1:end-1);
returns = mkt(numElementsToSum+1:end)
Buytwo=returns(movsum>0);
Selltwo=returns(movsum<0);
ExRetBuytwo = mean(Buytwo)
ExRetSelltwo = mean(Selltwo)
StdBuytwo = std(Buytwo)
StdSelltwo = std(Selltwo)
[h,ptwomean] = ttest2(Buytwo,Selltwo,0.05,'right','unequal');
[h,ptwovar] = vartest2(Buytwo,Selltwo,0.05,'left');
numElementsToSum = 3;
mSum = conv(w, ones(numElementsToSum, 1), 'valid');
movsumthree = mSum(1:end-1);
returns = mkt(numElementsToSum+1:end)
Buythree=returns(movsumthree>0);
Sellthree=returns(movsumthree<0);
ExRetBuythree = mean(Buythree)
ExRetSellthree = mean(Sellthree)
StdBuythree = std(Buythree)
StdSellthree = std(Sellthree)
[h,pthreemean] = ttest2(Buytthree,Sellthree,0.05,'right','unequal');
[h,pthreevar] = vartest2(Buythree,Sellthree,0.05,'left');
pmean= [ptwomean,pthreemean]
pvar= [ptwovar,pthreevar]

Answers (1)

Walter Roberson
Walter Roberson on 29 Dec 2012
Guessing here about which values you wish to save:
nvals = 2:15;
for nidx = 1 : length(nvals)
numElementsToSum = nvals(nidx);
mSum = conv(w, ones(numElementsToSum, 1), 'valid');
movsum = mSum(1:end-1);
returns = mkt(numElementsToSum+1:end)
Buytwo=returns(movsum>0);
Selltwo=returns(movsum<0);
ExRetBuytwo = mean(Buytwo)
ExRetSelltwo = mean(Selltwo)
StdBuytwo = std(Buytwo)
StdSelltwo = std(Selltwo)
[h, ptwomean(nidx)] = ttest2(Buytwo,Selltwo,0.05,'right','unequal');
[h, ptwovar(nidx)] = vartest2(Buytwo,Selltwo,0.05,'left');
end
plot(nvals, ptwomean, 'b', nvals, ptwovar, 'g:')
  1 Comment
Anders
Anders on 30 Dec 2012
Edited: Anders on 30 Dec 2012
Thanks for the answer Walter! but I cant seem to get it working. When I implement your answer, I get the following;
mkt = austrial(:,2);
w = log(1 + mkt/100)
nvals = 2:15;
nidx = 1 : length(nvals);
numElementsToSum = nvals(nidx);
mSum = conv(w, ones(numElementsToSum, 1), 'valid');
movsum = mSum(1:end-1);
returns = mkt(numElementsToSum+1:end)
Buy=returns(movsum>0);
Sell=returns(movsum<0);
ExRetBuy = mean(Buy);
ExRetSell = mean(Sell);
StdBuy = std(Buy);
StdSell = std(Sell);
[h, pmean(nidx)] = ttest2(Buy,Sell,0.05,'right','unequal');
[h, pvar(nidx)] = vartest2(Buy,Sell,0.05,'left');
plot(nvals, pmean, 'b', nvals, pvar, 'g:')
which gives me 14 values for both pmean and pvar as Im looking for, but they are all the same value. It just repeats the values from the tests from n=2. Is there something I have misunderstood with your answer?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!