请问如何用MATLAB计算大盘的HURST指数?能给出详细的步骤吗?程序如下,说的详细点,谢谢!
很多人都想知道股市Hurst指数变化,但是股软没有这个功能,我自己找了一个hurst的函数工具,写了一个计算股市的,比较简单,可以实现对股市的hurst指数计算,发到这里共享一下,供抛砖引玉,希望理想的高手多多指点,这个程序有些问题,算出来的指数会大于1,我暂时没有办法,请高手多多指教。
使用的时候,注意参数n的调节,n大了曲线会比较平滑,i是开始计算日
r=x(3000:end,2);意思是取数据文件的第二列的大盘指数,从3000行开始。
999999.txt是数据文件,可以从通达信导出
程序在matlab下执行没有问题,别的不敢保证
附图可以这么看,hurst指数小于0.7时,行情将发生反转,逐渐增加时今天的行情对明天的行情影响增加,小于是反之,数据截止到昨天。
n取的是10,也就是10天的移动hurst指数
复制内容到剪贴板 代码:%
%clear;
tic;
x=load('999999.txt');
r=x(3000:end,2);
%r=zscore(r);
qishu=length(r);
n=12;
i=100;
h=zeros(qishu-1,1);
for i=i-n:qishu;
data=reshape(r(i-n+1:i,1),1,n);
%rs=polyfit(log10(i-n:i)',RSana(r,i-n:i,'Hurst',1),1);
rs=hurst_exponent(data);
h(i,1)=rs(:,1);
%h(i,1)=hurst_exponent(data);
end
subplot(2,1,1); plot(h(100:end,1))
grid on;
title('HURST指数')
subplot(2,1,2); plot(r(100:end,1))
title('上证指数')
hold on;
grid on;
toc;
以下的代码请保存为hurst_exponent.m
复制内容到剪贴板 代码:%Hurst 指数的计算
% The Hurst exponent
%--------------------------------------------------------------------------
% The first 20 lines of code are a small test driver.
% You can delete or comment out this part when you are done validating the
% function to your satisfaction.
%
% Bill Davidson, quellen@
% 13 Nov
% function []=hurst_exponent()
% disp('testing Hurst calculation');
%
% % n=100;
% % data=rand(1,n);
% load gx.txt
% for n=1:967;
% data(1,n)=sum(gx(n,2:7));
% end
% %data=reshape(data,1,967);
% plot(data);
%
% hurst=estimate_hurst_exponent(data);
%
% [s,err]=sprintf('Hurst exponent = %.2f',hurst);disp(s);
%--------------------------------------------------------------------------
% This function does dispersional analysis on a data series, then does a
% Matlab polyfit to a log-log plot to estimate the Hurst exponent of the
% series.
%
% This algorithm is far faster than a full-blown implementation of Hurst's
% algorithm.I got the idea from a 2000 PhD dissertation by Hendrik J
% Blok, and I make no guarantees whatsoever about the rigor of this approach
% or the accuracy of results.Use it at your own risk.
%
% Bill Davidson
% 21 Oct
function [hurst] = hurst_exponent(data0) % data set
data=data0; % make a local copy
[M,npoints]=size(data0);
yvals=zeros(1,npoints);
xvals=zeros(1,npoints);
data2=zeros(1,npoints);
index=0;
binsize=1;
while npoints>4
y=std(data);
index=index+1;
xvals(index)=binsize;
yvals(index)=binsize*y;
npoints=fix(npoints/2);
binsize=binsize*2;
for ipoints=1:npoints % average adjacent points in pairs
data2(ipoints)=(data(2*ipoints)+data((2*ipoints)-1))*0.5;
end
data=data2(1:npoints);
end % while
xvals=xvals(1:index);
yvals=yvals(1:index);
logx=log(xvals);
logy=log(yvals);
p2=polyfit(logx,logy,1);
hurst=p2(1); % Hurst exponent is the slope of the linear fit of log-log plot
return;