Jump to content

Need help with Hurst Exponent

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Letoile

Letoile

    Newbie

  • Members
  • Pip
  • 5 posts
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 =)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Have you validated any of your intermediate calculations? For example, is Mean calculating correctly? Do you notice any rounding errors that may be accumulating that could be correcting by doing all calculations with higher precision variables, such as extended instead of real/double?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Letoile

Letoile

    Newbie

  • Members
  • Pip
  • 5 posts
So.. i verified this values, tried different functions. and values were identical. But when i changed method of calculating cumulative sum - it started works correctly!

Solution is found. Thanks =)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I'm glad I could help :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users