Hurst calculation

Hi there =) And sorry for my English, in advance I have a task to calculate the hurst index by linear regression. And I have a text description of the solution. It looks very easy, but always I get values ​​that fall outside the range of 0..1. Typically, the value is 1.9 or something similar. Sometimes it gets a negative value close to zero. I looked at the code about a thousand times, but did not see an error.

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 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 to everyone =)

PS

  for tau:=nach to l do 

There is L, not 1. And L is the length of the array X. And L> nach is always, except for the last step, when l = nach.

PPS It works guys. But the values ​​are incorrect. And they are out of range. There may be an error in the algorithm. Or maybe I missed some step.

Last update

This is mysticism, but I just changed the method of calculating the array Z, and it started working correctly .... Thanks to everyone =)

+6
source share
3 answers

The first thing I see:

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

This is a count. And since nach> 1, the body of this loop will not be executed.

If you expect a countdown. Use the downto option. For countdown:

 for tau := nach downto l do //w 
+1
source

Given that the main loop ( for tau ) iterates from nach to l , the first four SetLength calls should set the length l - nach + 1 instead of l - nach .

0
source

If the line

 z[i]:=z[i]+y[j]; 

not to be

 z[i]:=z[i - 1]+y[j]; 

?

0
source

Source: https://habr.com/ru/post/888987/


All Articles