声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 12853|回复: 28

[计算数学] 求R/S中hurst指数的计算程序

[复制链接]
发表于 2007-5-29 17:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
各位高手,小妹最近要做R/S分析,需要求解hurst指数,望各位高手能给一个直接运行的程序
ps:小妹不懂matlab

[ 本帖最后由 咕噜噜 于 2007-6-14 19:00 编辑 ]
回复
分享到:

使用道具 举报

发表于 2007-5-30 04:20 | 显示全部楼层
 楼主| 发表于 2007-5-30 10:21 | 显示全部楼层

回复 #2 gghhjj 的帖子

这个程序如何输入数据,
运行这个.m文件后出现
??? Error using ==> RSana123
You should provide a time series.

还有就是输出的logRS is the log(R/S)应该不是最后的hurst指数吧


附程序:
RS分形算法matlab代码实现,既利用RS方法计算时间序列的分形hurst指数

[Copy to clipboard] [ - ]CODE:
function [logRS,logERS,V]=RSana(x,n,method,q)
%Syntax: [logRS,logERS,V]=RSana(x,n,method,q)
%____________________________________________
%
% Performs R/S analysis on a time series.
%
% logRS is the log(R/S).
% logERS is the Expectation of log(R/S).
% V is the V statistic.
% x is the time series.
% n is the vector with the sub-periods.
% method can take one of the following values
%  'Hurst' for the Hurst-Mandelbrot variation.
%  'Lo' for the Lo variation.
%  'MW' for the Moody-Wu variation.
%  'Parzen' for the Parzen variation.
% q can be either
%  a (non-negative) integer.
%  'auto' for the Lo's suggested value.
%
%
% References:
%
% Peters E (1991): Chaos and Order in the Capital Markets. Willey
%
% Peters E (1996): Fractal Market Analysis. Wiley
%
% Lo A (1991): Long term memory in stock market prices. Econometrica
% 59: 1279-1313
%
% Moody J, Wu L (1996): Improved estimates for Rescaled Range and Hurst
% exponents. Neural Networks in Financial Engineering, eds. Refenes A-P
% Abu-Mustafa Y, Moody J, Weigend A: 537-553, Word Scientific
%
% Hauser M (1997): Semiparametric and nonparametric testing for long
% memory: A Monte Carlo study. Empirical Economics 22: 247-271
%
%
% Alexandros Leontitsis
% Department of Education
% University of Ioannina
% 45110 - Dourouti
% Ioannina
% Greece
%
% University e-mail: me00743@cc.uoi.gr
% Lifetime e-mail: leoaleq@yahoo.com
% Homepage: http://www.geocities.com/CapeCanaveral/Lab/1421
%
% 1 Jan 2004.

if nargin<1 | isempty(x)==1
   error('You should provide a time series.');
else
   % x must be a vector
   if min(size(x))>1
      error('Invalid time series.');
   end
   x=x(:);
   % N is the time series length
   N=length(x);
end

if nargin<2 | isempty(n)==1
   n=1;
else
   % n must be either a scalar or a vector
   if min(size(n))>1
      error('n must be either a scalar or a vector.');
   end
   % n must be integer
   if n-round(n)~=0
       error('n must be integer.');
   end
   % n must be positive
   if n<=0
      error('n must be positive.');
   end
end

if nargin<4 | isempty(q)==1
   q=0;
else
    if q=='auto'
        t=autocorr(x,1);
        t=t(2);
        q=((3*N/2)^(1/3))*(2*t/(1-t^2))^(2/3);
    else
        % q must be a scalar
        if sum(size(q))>2
            error('q must be scalar.');
        end
        % q must be integer
        if q-round(q)~=0
            error('q must be integer.');
        end
        % q must be positive
        if q<0
            error('q must be positive.');
        end
    end
end


for i=1:length(n)
   
    % Calculate the sub-periods
    a=floor(N/n(i));
   
    % Make the sub-periods matrix
    X=reshape(x(1:a*n(i)),n(i),a);
   
    % Estimate the mean of each sub-period
    ave=mean(X);
   
    % Remove the mean from each sub-period
    cumdev=X-ones(n(i),1)*ave;
   
    % Estimate the cumulative deviation from the mean
    cumdev=cumsum(cumdev);
   
    % Estimate the standard deviation
    switch method
    case 'Hurst'
        % Hurst-Mandelbrot variation
        stdev=std(X);
    case 'Lo'
        % Lo variation
        for j=1:a
            sq=0;
            for k=0:q
                v(k+1)=sum(X(k+1:n(i),j)'*X(1:n(i)-k,j))/(n(i)-1);
                if k>0
                    sq=sq+(1-k/(q+1))*v(k+1);
                end
            end
            stdev(j)=sqrt(v(1)+2*sq);
        end
    case 'MW'
        % Moody-Wu variation
        for j=1:a
            sq1=0;
            sq2=0;
            for k=0:q
                v(k+1)=sum(X(k+1:n(i),j)'*X(1:n(i)-k,j))/(n(i)-1);
                if k>0
                    sq1=sq1+(1-k/(q+1))*(n(i)-k)/n(i)/n(i);
                    sq2=sq2+(1-k/(q+1))*v(k+1);
                end
            end
            stdev(j)=sqrt((1+2*sq1)*v(1)+2*sq2);
        end
    case 'Parzen'
        % Parzen variation
        if mod(q,2)~=0
            error('For the "Parzen" variation q must be dived by 2.');
        end
        for j=1:a
            sq1=0;
            sq2=0;
            for k=0:q
                v(k+1)=sum(X(k+1:n(i),j)'*X(1:n(i)-k,j))/(n(i)-1);
                if k>0 & k<=q/2
                    sq1=sq1+(1-6*(k/q)^2+6*(k/q)^3)*v(k+1);
                elseif k>0 & k>q/2
                    sq2=sq2+(1-(k/q)^3)*v(k+1);
                end
            end
            stdev(j)=sqrt(v(1)+2*sq1+2*sq2);
        end
    otherwise
        error('You should provide another value for "method".');
    end
   
    % Estiamte the rescaled range
    rs=(max(cumdev)-min(cumdev))./stdev;
   
    clear stdev
   
    % Take the logarithm of the mean R/S
    logRS(i,1)=log10(mean(rs));
   
    if nargout>1
        
        % Initial calculations fro the log(E(R/S))
        j=1:n(i)-1;
        s=sqrt((n(i)-j)./j);
        s=sum(s);
        
        % The estimation of log(E(R/S))
        logERS(i,1)=log10(s/sqrt(n(i)*pi/2));
        
        % Other estimations of log(E(R/S))
        %logERS(i,1)=log10((n(i)-0.5)/n(i)*s/sqrt(n(i)*pi/2));
        %logERS(i,1)=log10(sqrt(n(i)*pi/2));
        
    end
   
    if nargout>2
        % Estimate V
        V(i,1)=mean(rs)/sqrt(n(i));
    end

end
发表于 2007-5-31 06:48 | 显示全部楼层
你没输入变量x,n,method,q,怎么运行?
真晕,居然会犯这样的错误

看看matlab的function该怎么用吧,别人帮不了你
发表于 2007-5-31 08:34 | 显示全部楼层


看看基础的书
 楼主| 发表于 2007-6-1 07:53 | 显示全部楼层

回复 #4 gghhjj 的帖子

请问method和q应该输入什么值?
% method can take one of the following values
%  'Hurst' for the Hurst-Mandelbrot variation.
%  'Lo' for the Lo variation.
%  'MW' for the Moody-Wu variation.
%  'Parzen' for the Parzen variation.
% q can be either
%  a (non-negative) integer.
%  'auto' for the Lo's suggested value.
发表于 2007-6-1 08:04 | 显示全部楼层
原帖由 无水1324 于 2007-5-31 08:34 发表


看看基础的书
 楼主| 发表于 2007-6-1 08:30 | 显示全部楼层

回复 #7 无水1324 的帖子

时间紧迫,来不及重新学matlab,望高手指点迷津
发表于 2007-6-1 12:21 | 显示全部楼层
在调用的时候写为:
q=   ;x= ; n= ;method= ;
  [logRS,logERS,V]=RSana(x,n,method,q)
就可以了
% q
 楼主| 发表于 2007-6-2 15:20 | 显示全部楼层

回复 #9 无水1324 的帖子

谢谢!已经计算出来了
但是最后的ans是hurst指数吗?
发表于 2007-6-2 15:37 | 显示全部楼层
最后的ans应该有三个
[logRS,logERS,V]
 楼主| 发表于 2007-6-2 15:53 | 显示全部楼层

回复 #11 无水1324 的帖子

好奇怪,只有一个值啊
发表于 2007-6-2 15:58 | 显示全部楼层
只有一个,那就是你需要的了
发表于 2007-6-3 08:59 | 显示全部楼层
结果是什么要看你怎么调用RSana了

把调用部分贴出来看看
 楼主| 发表于 2007-6-3 11:58 | 显示全部楼层

回复 #14 gghhjj 的帖子

>> x=[0.210,0.325,0.251,0.255,0.235,0.264,0.241,0.218,0.252,0.287,0.298,0.282,0.326,0.309,0.370,0.492,0.561,0.631,0.453,0.685,0.696,0.665]

x =

  Columns 1 through 9

    0.2100    0.3250    0.2510    0.2550    0.2350    0.2640    0.2410    0.2180    0.2520

  Columns 10 through 18

    0.2870    0.2980    0.2820    0.3260    0.3090    0.3700    0.4920    0.5610    0.6310

  Columns 19 through 22

    0.4530    0.6850    0.6960    0.6650

>> RSana(x,5,'Hurst',6)

ans =

    0.1965
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 03:19 , Processed in 0.080594 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表