ScalerByLeo/Src/Scaler/Bench/Std.mq5

171 lines
3.9 KiB
MQL5

2026-09-15 11:21:44 -05:00
//+------------------------------------------------------------------+
//| Std.mq5 |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool StandartVec(const vector& in, vector& out)
{
double std = in.Std();
if(std < DBL_EPSILON)
return false;
std = 1.0 / std;
//---
const double mean = in.Mean();
out = (in - mean) * std;
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool OnePassWelford(const vector& X, vector& out)
{
//---
const ulong fs = X.Size();
double mean_val = 0.0;
double m2 = 0.0;
for(ulong i = 0; i < fs; i++)
{
const double v = X[i];
out[i] = v;
//---
const double del = v - mean_val;
mean_val += del / (i + 1);
//---
const double del2 = v - mean_val;
//---
m2 += del * del2;
}
//--- calc std
double std = m2 / fs; // var
//--- check std
std = MathSqrt(std);
if(std < DBL_EPSILON)
return false;
//--- inv
std = 1.0 / std; // inversa
//--- final
out -= mean_val;
out *= std;
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool TwoPassStd(const vector &X, vector &out)
{
const ulong n = X.Size();
if(n == 0)
return false;
//---
const double s_inv = 1.0 / double(n);
//---
double mean_val = 0.0;
for(ulong i = 0; i < n; i++)
mean_val += X[i];
mean_val *= s_inv;
//---
double sumSq = 0.0;
out.Resize(n);
for(ulong i = 0; i < n; i++)
{
const double d = X[i] - mean_val;
out[i] = d;
sumSq += d * d;
}
const double var = sumSq * s_inv;
//---
double std = MathSqrt(var);
if(std < DBL_EPSILON)
return false;
std = 1.0 / std;
//--- final: centrado + escalado, vectorizado
out *= std;
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector Ran()
{
vector v(1000);
for(int i = 0; i < 1000; i++)
{
v[i] = (double)MathRand();
}
return v;
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
MathSrand(10);
double vs = 0.0;
ulong a = GetMicrosecondCount();
for(int i = 1; i < 100000; i++)
{
vector v = Ran();
StandartVec(v, v);
vs += v[0];
}
ulong b = GetMicrosecondCount();
Print("native: ", (b - a), " vs : ", vs);
vs = 0.0;
MathSrand(10);
a = GetMicrosecondCount();
for(int i = 1; i < 100000; i++)
{
vector v = Ran();
OnePassWelford(v, v);
vs += v[0];
}
b = GetMicrosecondCount();
Print("1p: ", (b - a), " vs : ", vs);
vs = 0.0;
MathSrand(10);
a = GetMicrosecondCount();
for(int i = 1; i < 100000; i++)
{
vector v = Ran();
TwoPassStd(v, v);
vs += v[0];
}
b = GetMicrosecondCount();
Print("2p: ", (b - a), " vs : ", vs);
}
//+------------------------------------------------------------------+