Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Hurst Exponent

Tags:

math

delphi

Hi there =) And sorry for my English, in advance I have a task to calculate hurst exponent by method of linear regression. And I have text description of solution. It looks very easy, but always i get values, that go out from range 0..1. Usually, value is 1.9 or something similar. Sometimes it gets negative value that is close to zero. I have looked over code about thousand times but couldn't see a mistake.

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;

Thanks everybody =)

P.S.

 for tau:=nach to l do

There is L, not 1. And L is Length of X array. And L>nach always besides last step, when l=nach.

P.P.S. It works, guys. But values are not right. And they go out from range. Maybe, there is mistake in algorithm. Or maybe I skiped some step.

Last Update

It's mystic, but i only changed method of calculating array Z and it started works correctly.... Thanks all =)

like image 363
Letoile Avatar asked May 25 '11 07:05

Letoile


1 Answers

First thing I see:

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

This counts up. And because nach>1, the body of this loop won't be executed.

If you expect to count down. Use the downto variant. To count down:

for tau := nach downto l do  //w
like image 175
Toon Krijthe Avatar answered Oct 02 '22 02:10

Toon Krijthe