CRandom/pcg_histogram.mq5
2025-09-19 20:14:50 +00:00

150 lines
12 KiB
MQL5

//+------------------------------------------------------------------+
//| unifrom.mq5 |
//| Copyright 2009-2017, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
//https://www.mql5.com/en/docs/standardlibrary/mathematics/stat/unifrom
#include <Graphics\Graphic.mqh>
#include <Math\Stat\Math.mqh>
#include <Math\Stat\Uniform.mqh>
#include <Random.mqh>
#property script_show_inputs
//--- input parameters
input double a_par=0; // distribution parameter a (lower bound)
input double b_par=10; // distribution parameter b (upper bound)
enum ENUM_PRNG_MODE
{
PRNG_MODE_DBL, // double
PRNG_MODE_INT // integer
};
input ENUM_PRNG_MODE prng_mode=PRNG_MODE_DBL; // Random number generator mode
//--- the random number generator object
CRandom pcg;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- hide the price chart
ChartSetInteger(0,CHART_SHOW,false);
////--- initialize the random number generator
// MathSrand(GetTickCount());
//--- generate a sample of the random variable
long chart=0;
string name="GraphicNormal";
int n=1000000; // the number of values in the sample
int ncells=51; // the number of intervals in the histogram
double x[]; // centers of the histogram intervals
double y[]; // the number of values from the sample falling within the interval
double data[]; // sample of random values
double max,min; // the maximum and minimum values in the sample
//--- obtain a sample from the uniform distribution
MathRandomUniform_PCG32(a_par,b_par,n,data);
//--- calculate the data to plot the histogram
CalculateHistogramArray(data,x,y,max,min,ncells);
//--- obtain the sequence boundaries and the step for plotting the theoretical curve
double step;
GetMaxMinStepValues(max,min,step);
step=MathMin(step,(max-min)/ncells);
//--- obtain the theoretically calculated data at the interval of [min,max]
double x2[];
double y2[];
MathSequence(min,max,step,x2);
MathProbabilityDensityUniform(x2,a_par,b_par,false,y2);
////--- set the scale
// double theor_max=y2[ArrayMaximum(y2)];
// double sample_max=y[ArrayMaximum(y)];
// double k=sample_max/theor_max;
// for(int i=0; i<ncells; i++)
// y[i]/=k;
//--- output charts
CGraphic graphic;
if(ObjectFind(chart,name)<0)
graphic.Create(chart,name,0,0,0,780,380);
else
graphic.Attach(chart,name);
string str="Continuous";
if(prng_mode==PRNG_MODE_INT) str="Discrete";
graphic.BackgroundMain(StringFormat("%s uniform distribution a=%G b=%G",str,a_par,b_par));
graphic.BackgroundMainSize(16);
//--- plot all curves
graphic.CurveAdd(x,y,CURVE_HISTOGRAM,"Sample").HistogramWidth(6);
//--- and now plot the theoretical curve of the distribution density
graphic.CurveAdd(x2,y2,CURVE_LINES,"Theory");
graphic.CurvePlotAll();
//--- plot all curves
graphic.Update();
}
//+------------------------------------------------------------------+
//| Calculate frequencies for data set |
//+------------------------------------------------------------------+
bool CalculateHistogramArray(const double &data[],double &intervals[],double &frequency[],
double &maxv,double &minv,int cells=10)
{
if(cells<=1) return (false);
int size=ArraySize(data);
if(size<cells*10) return (false);
minv=data[ArrayMinimum(data)];
maxv=data[ArrayMaximum(data)];
double range=maxv-minv;
double width=range/cells;
if(width==0) return false;
ArrayResize(intervals,++cells); //add last empty cell
ArrayResize(frequency,cells);
//--- define the interval centers
for(int i=0; i<cells; i++)
{
intervals[i]=minv+(i+0.5)*width;
frequency[i]=0;
}
//--- fill the frequencies of falling within the interval
for(int i=0; i<size; i++)
{
int ind=int((data[i]-minv)/width);
if(ind>=cells) ind=cells-1;
frequency[ind]++;
}
//--- set the scale to density estimate
if(prng_mode==PRNG_MODE_INT) width=1;
for(int i=0; i<cells; i++)
frequency[i]/=size*width;
return (true);
}
//+------------------------------------------------------------------+
//| Calculates values for sequence generation |
//+------------------------------------------------------------------+
void GetMaxMinStepValues(double &maxv,double &minv,double &stepv)
{
//--- calculate the absolute range of the sequence to obtain the precision of normalization
double range=MathAbs(maxv-minv);
int degree=(int)MathRound(MathLog10(range));
//--- normalize the maximum and minimum values to the specified precision
maxv=NormalizeDouble(maxv,degree);
minv=NormalizeDouble(minv,degree);
//--- sequence generation step is also set based on the specified precision
stepv=NormalizeDouble(MathPow(10,-degree),degree);
if((maxv-minv)/stepv<10)
stepv/=10.;
}
//+------------------------------------------------------------------+
//| Generates random variables from the Uniform distribution with |
//| parameters a and b. |
//+------------------------------------------------------------------+
bool MathRandomUniform_PCG32(const double a,const double b,const int data_count,double &result[])
{
//--- check upper bound
if(b<a)
return false;
//--- prepare output array and calculate random values
ArrayResize(result,data_count);
for(int i=0; i<data_count; i++)
{
//--- generate random number
if(prng_mode==PRNG_MODE_INT)
result[i]=pcg.RandomInteger((int)a,(int)b);
else
result[i]=pcg.RandomDouble(a,b);
}
return true;
}
//+------------------------------------------------------------------+