计算赫斯特指数

时间:2011-05-25 07:38:36

标签: delphi math

你好=)对不起我的英语,提前 我有一个任务是通过线性回归的方法计算赫斯特指数。我有解决方案的文字说明。它看起来很容易,但总是得到值,从0..1范围出来。通常,值为1.9或类似的值。有时它会得到接近于零的负值。 我查了大约一千次代码,但看不出错误。

var
max_z,min_z,x_m:real; //max and min of cumulative sum and mean value of X for every Tau
st,ss,sst,st2 :real;
Al, Herst: real;
x_vr:array of double;   //a piece of array with length=tau
i, j, nach: integer;
begin
    //file opening and getting values of X array are in another function
    nach:=3;    //initial value of tau
    Setlength(ln_rs,l-nach); //length of  ln(R/S) array
    Setlength(ln_t,l-nach);  //length of  ln(tau) array
    Setlength(r,l-nach);   //length of  R array
    Setlength(s,l-nach);   //length of S array


      //Let's start
    for tau:=nach to l do  //we will change tau
    begin
        Setlength(x_vr,tau+1); //set new local array (length=tau)
        for i:=0 to length(x_vr)-1 do
            x_vr[i]:=x[i];

        x_m:=Mean(x_vr);    //mean value
        Setlength(y,tau+1);   //length of array of difference from mean value
        Setlength(z,tau+1);   //length of array of cumulative sum

        for i:=0 to tau do
            y[i]:=x_vr[i]-x_m;      //difference from mean value

        z[0]:=y[0];
        for i:=1 to tau do      //cumulative sum
             for j :=i downto 0 do
                z[i]:=z[i]+y[j];

        max_z:=z[0];
        for i:=1 to tau do        //max of cumulative sum
            max_z:=max(max_z,z[i]);

        min_z:=z[0];
        for i:=1 to tau do        //min of cumulative sum
            min_z:=min(min_z,z[i]);

        r[tau-nach]:=max_z-min_z;    //R value
        s[tau-nach]:=0;
        for i:=0 to tau do
            s[tau-nach]:=power(y[i],2)+s[tau-nach];         //S value

        s[tau-nach]:=sqrt(s[tau-nach]/(tau+1));

        //new array values
        ln_rs[tau-nach]:=Ln(R[tau-nach]/S[tau-nach]);   // ln(R/S)
        ln_t[tau-nach]:=ln(tau);                        // ln (tau)

    end;    //End of calculating

    //Method of Least squares
    for i:=0 to length(ln_rs)-1 do  
        st:=st+ln_t[i];

    st:=(1/length(ln_rs))*st;

    for i:=0 to length(ln_rs)-1 do
        ss:=ss+ln_rs[i];

    ss:=(1/length(ln_rs))*ss;

    for i:=0 to length(ln_rs)-1 do
        sst:=sst+ln_t[i]*ln_rs[i];

    sst:=(1/length(ln_rs))*sst;

    for i:=0 to length(ln_rs)-1 do
        st2:=st2+ln_t[i]*ln_t[i];

    st2:=(1/length(ln_rs))*st2;


    Herst:=(sst-st*ss)/(st2-st*st);      //coefficient of approximal function
    al:=ss-st*Herst;

谢谢大家=)

P.S。

 for tau:=nach to l do

有L,而不是1.L是X阵列的长度。 L> nach总是在最后一步,当l = nach时。

P.P.S。 伙计们,这很有用。但价值观不对。他们从射程出去。也许,算法中有错误。或许我滑了一步。

上次更新

这是神秘的,但我只改变了计算阵列Z的方法,它开始正常工作.... 谢谢大家=)

2 个答案:

答案 0 :(得分:2)

我首先看到的是:

nach := 3;    
for tau := nach to l do  //w

这很重要。并且因为nach> 1,所以不会执行此循环的主体。

如果你期望倒计时。使用downto变体。倒计时:

for tau := nach downto l do  //w

答案 1 :(得分:0)

鉴于主循环(for tau)从nach迭代到l,前四个SetLength调用应设置l - nach + 1的长度,而不是l - nach

相关问题