ScalerByLeo/Src/Scaler/Bench/Robust.mq5
2026-09-15 16:39:51 -05:00

159 lines
4.2 KiB
MQL5

//+------------------------------------------------------------------+
//| Robust.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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define PERCENTILE(vec, l, h, w) ((l == h) ? (vec[l]) : ((vec[l] * (1.0 - w)) + (vec[h] * w)))
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
__forceinline void MakePer(const double rank, ulong&l, ulong&h, double& w)
{
l = (ulong)MathFloor(rank);
h = (ulong)MathCeil(rank);
w = rank - l;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool V2(const matrix& X, double& checksum)
{
//---
const ulong cols = X.Cols();
const ulong rows = X.Rows();
const double rs_1 = double(rows - 1);
//---
ulong l50, h50;
double w50;
MakePer(0.50 * rs_1, l50, h50, w50);
ulong l25, h25;
double w25;
MakePer(0.25 * rs_1, l25, h25, w25);
ulong l75, h75;
double w75;
MakePer(0.75 * rs_1, l75, h75, w75);
//---
for(ulong c = 0; c < cols; c++)
{
//---
vector col = X.Col(c);
col.Sort(SORT_ASCENDING);
//---
const double median_v = PERCENTILE(col, l50, h50, w50);
const double iqr_v = PERCENTILE(col, l75, h75, w75) - PERCENTILE(col, l25, h25, w25);
//---
if(fabs(iqr_v) < DBL_EPSILON)
{
return false;
}
//---
// m_medians[c] = median_v;
// m_iqrs[c] = iqr_v;
// m_iqrs_inv[c] = 1.0 / iqr_v;
checksum = (median_v) + (iqr_v) + (1.0 / iqr_v);
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Normal(const matrix& X, double& checksum)
{
//---
const ulong cols = X.Cols();
const ulong rows = X.Rows();
const double rs_1 = double(rows - 1);
//---
for(ulong c = 0; c < cols; c++)
{
//---
const vector col = X.Col(c);
const double median_v = col.Median();
const double iqr_v = col.Percentile(75) - col.Percentile(25);
//---
if(fabs(iqr_v) < DBL_EPSILON)
{
return false;
}
//---
// m_medians[c] = median_v;
// m_iqrs[c] = iqr_v;
// m_iqrs_inv[c] = 1.0 / iqr_v;
checksum = (median_v) + (iqr_v) + (1.0 / iqr_v);
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
matrix Ran()
{
matrix v(64, 64);
for(int i = 0; i < 64; i++)
for(int x = 0; x < 64; x++)
v[i][x] = (double)MathRand();
return v;
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
//---
MathSrand(10);
double vs = 0.0;
ulong a = GetMicrosecondCount();
for(int i = 1; i < 10000; i++)
{
matrix v = Ran();
double v1;
Normal(v, v1);
vs += v1;
}
ulong b = GetMicrosecondCount();
Print("native: ", (b - a), " vs : ", vs);
vs = 0.0;
MathSrand(10);
a = GetMicrosecondCount();
for(int i = 1; i < 10000; i++)
{
matrix v = Ran();
double v1;
V2(v, v1);
vs += v1;
}
b = GetMicrosecondCount();
Print("v2: ", (b - a), " vs : ", vs);
}
//+------------------------------------------------------------------+