Linear regression channel - page 13

 
Задача: Подсчет среднего значения и дисперсии числового ряда
Задача: Подсчет среднего значения и дисперсии числового ряда
  • votes: 2
  • 2015.10.16
  • Google+
  • purecodecpp.com
Эта задача имеет очень большое практическое применение: в статистической обработке данных, обработке временных рядов, в цифровой обработке сигналов применительно к цифровым отсчётам сигнала. Постановка задачи такая: – вводится последовательность (вещественных) чисел … – нужно просчитать, в итоге, среднее значение и дисперсию (или СКО...
 

I'll give it a try.

 

Here's 1

Here's the calculation:

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   int start;
   if(prev_calculated==0){
      start=period;
      double ms=0;
      for(int i=0;i<period;i++){
         ms+=close[i];
      }
      ma[period-1]=ms/period;
   }
   else{
      start=prev_calculated-1;
   }

   for(int i=start;i<rates_total;i++){      
      //ma[i]=ma[i-1]+(-close[i-period]+close[i])/period;      
      double s1=0;
      double s2=0;
      for(int j=i-period+1;j<=i;j++){
         s1+=close[j];
         s2+=close[j]*close[j];
      }
      s1/=period;
      s2=s2/period-s1*s1;
      Label1Buffer[i]=s2;
   }

   return(rates_total);
  }

Not an accelerated algorithm to check the formula. Maybe I did something wrong?

Files:
stdX4.mq5  6 kb
 
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      period=14;
//--- indicator buffers
double         Label1Buffer[];

int OnInit()
  {
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);


//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   static double ms,ms2;
   int start;
   if(prev_calculated==0)
     {
      start=period;
      ms=0; ms2=0;
      for(int i=0;i<period;i++)
        {
         ms +=close[i];
         ms2+=close[i]*close[i];
        }
     }
   else
     {
      start=prev_calculated-1;
     }

   for(int i=start;i<rates_total;i++)
     {
      double Ms =ms +(-close[i-period]+close[i]);
      double Ms2=ms2+(-close[i-period]*close[i-period]+close[i]*close[i]);
      double s=Ms/period;
      Label1Buffer[i]=sqrt(s*s+(Ms2-2*Ms*s)/period);
      ms=Ms;
      ms2=Ms2;
     }
   return(rates_total);
  }
 
Yuriy Asaulenko:
Not with you betting. 2.days of silence, please.
You won't be getting any Hennessy anyway.) But you'll spoil my dinner.

Drinking's bad for you. Sorry. I was here first. ))

 

Google "Moving Standard Deviation"

https://www.johndcook.com/blog/standard_deviation/

Accurately computing running variance
  • www.johndcook.com
The most direct way of computing sample variance or standard deviation can have severe numerical problems. Mathematically, sample variance can be computed as follows. The most obvious way to compute variance then would be to have two sums: one to accumulate the sum of the x‘s and another to accumulate the sums of the squares of the x‘s. If the...
 
Dmitry Fedoseev:

Here's

Here's the calculation:

Not an accelerated algorithm to check the formula. Maybe you did something wrong?

You don't take into account the arrival of new data and the need to delete old data (out of the window).

Nikolai does. see the code below your message.

 

So... my final message.

Tell Yuri to message me in person where to deliver it, or buy a certificate, or whatever.

Be seeing you. I'll be back in a month.

 
Nikolai Semko:

it's not healthy to drink. Sorry. I was the first. ))

You're welcome. I was not going to race. Just asked not to interfere, and those who know, shut up.
The algorithm, yes, is practically equivalent, I wrote about it on page 1-2 of the thread.
Dimitri, sorry, though proven, I can't accept Hennessy, to my regret.
The code, I suppose, makes no sense to write. Come Semko, and that's it... Priority wanted)). As if anyone but Dimitri doubted it.
I will have to go to the shop myself.
 
Dmitry Fedoseev:

So... my final message.

Tell Yuri to message me in person where to deliver it, or buy a certificate, or whatever.

Be seeing you. I'll be back in a month.

I envy you. Fruitful work!
My message was that this can be done not only with RMS for a simple waving machine, but also for a polynomial of any degree.
True, the formula grows exponentially as the degree of the polynomial increases.

Reason: