Linear Regression Slope - Help - page 2

 

.

Hi WHRoeder,

 

I've already converted it to an 1D version.

But this TheilSen function still's confuse me... 

In the linear regression slope function we have a value return but the TheilSen function does not have any return.

I'm not used to use functions like that, but I presume that the objective of that function is to calculate all the loops in order to get the "m" and "b" values.

If so... Why does the function call have the parameters "m" and "b"?!? If that is what we want to get?!?

TheilSen(m, b, ...

Sory about this  (probably) stupid questions but I realy dont understand the meaning of the "m" and "b" in the function call... :(

 
It returns the parameters of a line: y = m x + b. Just like your linear regression would calculate both terms had you included both. You are only interested in the slope (m) not the intercept.
 

.

 Yes.. That I understand..

 What confuse me is why does we need to call that function with those parameters?!

 What I want to say is this:

.

 Why is the funtion call like this:

 TheilSen(m, b, Close, RegLin_Period, iBar);

 And not like this:

 TheilSen(Close, RegLin_Period, iBar);

 .

 What are "m" and "b" doing there?!?!

.

Once again, sorry about this noobie questions and thank you for your patience!

 
strutch:


 What are "m" and "b" doing there?!?!

It's a linear equation . . .  simple mathematics.
 
strutch: What are "m" and "b" doing there?!?!
Because it returns TWO values. Functions can only return ONE
 

.

Sorry guys... I'm not explaing myself...

I understand what does "m" and "b" mean.

I belive that I understand the way the function works.

I  think that when it runs the "m" and "b" values are calculated.

What I don't understand is why are "m" and "b" in the function parameters.

.

 For example in this simple function...

example( double a, double b, double c)
       {
        return ( a + b / c )
       }

 "a", "b" and "c" are parameters that will be used in the function calculation.

In the TheilSen  function "m" and "b" are the first two parameters but I don´t see where they are used!

Simplifying... I'm reading that function like this:

example( double m, double b, double x, double y)
       {
        m = x + y
        b = x + x
       }

i.e. when we call that function we are given unecessary "m" and "b" paramenters

.

 

strutch: i.e. when we call that function we are given unnecessary "m" and "b" parameters

In your example(), m and b are unnecessary. In my function m and b are passed by reference and return values.

example( double m, double b, double x, double y)
       {
        m = x + y
        b = x + x
       }
double m=1, b=2; example(m,b, 3,4); Print(m," ",b); // Outputs 1 2
example( double& m, double& b, double x, double y)
       {
        m = x + y
        b = x + x
       }
double m=1, b=2; example(m,b, 3,4); Print(m," ",b); // Outputs 7 6
 

Perfect... Now I understood!!! :)

 Sorry about this but I didn't know about the "&" importance.

 .

Meahnwhile, i'm trying (attached file) to follow your advices but it doesn't work... :( 

.

 
strutch:

 

Perfect... Now I understood!!! :)

 Sorry about this but I didn't know about the "&" importance.

Passing variables by reference . . .  https://www.mql5.com/en/forum/117210
 
strutch:

 

Perfect... Now I understood!!! :)

 Sorry about this but I didn't know about the "&" importance.

 .

Meahnwhile, i'm trying (attached file) to follow your advices but it doesn't work... :( 

.


well it can't work as your statíc double slopes[]  has a length of zero:

void TheilSen(double& m, double& b, double v[], int n1, int iBeg=0){
   //{Theil–Sen estimator of a set of two-dimensional points (xi,yi) is the
   // median m of the slopes (yj - yi)/(xj - xi)
   // http://en.wikipedia.org/wiki/Theil-Sen_estimator
   // 2     2:1                                                 1
   // 3     2:1, 3:1, 3:2                                       3
   // 4     2:1, 3:1, 3:2, 4:1, 4:2, 4:3                        6
   //}5     2:1, 3:1, 3:2, 4:1, 4:2, 4:3  5:1, 5:2, 5:3, 5:4   10
   static double slopes[];   
   int nReq = n1 * (n1-1) / 2;
/*   if(ArraySize(slopes) < nReq) if(ArrayResize(slopes, nReq) <= 0){ // <= ERROR is here, do not disable
      DisableTrading("ArrayResize(TheilSen, " + nReq + ") Failed: "
                                                +GetLastError() );   return;  } */
   int   nSlopes = 0;
   for(int i=iBeg + n1 - 1; i >  iBeg; i--)
      for (int j=i - 1;     j >= iBeg; j--){
         slopes[nSlopes] = (v[i] - v[j]) / (i-j);   nSlopes++;        }
         // now all slopes[..] are 0.000000 :(
   m = Median(slopes, nSlopes);
   for(i=0; i < n1; i++)   slopes[i] = v[iBeg+i] - m * i;
   b = Median(slopes, n1);
}



this helps:

void TheilSen(double& m, double& b, double v[], int n1, int iBeg=0){
   //{Theil–Sen estimator of a set of two-dimensional points (xi,yi) is the
   // median m of the slopes (yj - yi)/(xj - xi)
   // http://en.wikipedia.org/wiki/Theil-Sen_estimator
   // 2     2:1                                                 1
   // 3     2:1, 3:1, 3:2                                       3
   // 4     2:1, 3:1, 3:2, 4:1, 4:2, 4:3                        6
   //}5     2:1, 3:1, 3:2, 4:1, 4:2, 4:3  5:1, 5:2, 5:3, 5:4   10
   static double slopes[];
   int nReq = n1 * (n1-1) / 2;
   if(ArraySize(slopes) < nReq) if(ArrayResize(slopes, nReq) <= 0){
      Print("ArrayResize(TheilSen, " + nReq + ") Failed: "
                                                +GetLastError() );   return;  }
   int   nSlopes = 0;
   for(int i=iBeg + n1 - 1; i >  iBeg; i--)
      for (int j=i - 1;     j >= iBeg; j--){
         slopes[nSlopes] = (v[i] - v[j]) / (i-j);   nSlopes++;        }
   m = Median(slopes, nSlopes);
   for(i=0; i < n1; i++)   slopes[i] = v[iBeg+i] - m * i;
   b = Median(slopes, n1);
}
Reason: