Partition function in Matlab - is there something missing in my code?
4 views (last 30 days)
Show older comments
Can anyone see if there is something wrong in my matlab code? My objective is to replicate this formula: http://i.stack.imgur.com/JANLf.jpg q can take value 1,2,3 and 5. I constructed my vector Xt where each element are a cumulative sum of log(1+return) at each time (t) - for stock returns - first element is normalized to log(1).
Then to compute each element Sq(T,delta t) for the four values of q this is my matlab code:
for j=1:length(dt);
E=Xt(1:dt(j):end);
EE=diff(E(2:end));
EEE=diff(E(1:end-1));
Sqone(j)=sum(abs(EE-EEE).^1);
Sqtwo(j)=sum(abs(EE-EEE).^2);
Sqthree(j)=sum(abs(EE-EEE).^3);
Sqfive(j)=sum(abs(EE-EEE).^5); end;
Is there something wrong in the code above? I am asking this because I know there is something wrong since I am not getting the expected results. I am convinced that it is due to my code posted above.
the vector dt is a vector that goes from 1 to high number - depending on the size of Xt. But my vector dt is not the problem.
Thank you for all your help!
0 Comments
Accepted Answer
Andrei Bobrov
on 5 Jun 2012
Sq = @(x,q)sum(abs(diff(x)).^q)
eg
Sq(Xt,5)
EDIT
Sq = @(x,q)sum(bsxfun(@power,abs(diff(x(:))),q(:)'));
eg
Sq(Xt,[1:3 5])
EDIT2
eg
Xt = randi(15,15,1);
dt = 1:14;
q = [1:3,5];
out = zeros(numel(dt),numel(q));
for j1 = 1:numel(dt)
out(j1,:) = sum(bsxfun(@power,abs(diff(Xt(1:dt(j1):end))),q),1);
end
variant without loop for...end
out2 = cell2mat(cellfun(@(x) sum(bsxfun(@power,abs(diff(x(:))),q),1),arrayfun(@(x)Xt(1:x:end),dt(:),'un',0),'un',0));
More Answers (2)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!