oslib/tst/graficos_2866/MQL5/Scripts/DistributionsGraphics/DemoFDistribution.mq5

117 lines
9.6 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:15:18 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| DemoFDistribution.mq5 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include <Graphics\Graphic.mqh>
#include <Math\Stat\F.mqh>
#include <Math\Stat\Math.mqh>
#property script_show_inputs
//--- input parameters
input double nu_1=100; // ?5@2>5 G8A;> AB5?5=59 A2>1>4K
input double nu_2=100; // 2B>@>5 G8A;> AB5?5=59 A2>1>4K
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- >B:;NG8< ?>:07 F5=>2>3> 3@0D8:0
ChartSetInteger(0,CHART_SHOW,false);
//--- 8=8F80;878@C5< 35=5@0B>@ A;CG09=KE G8A5;
MathSrand(GetTickCount());
//--- A35=5@8@C5< 2K1>@:C A;CG09=>9 25;8G8=K
long chart=0;
string name="GraphicNormal";
int n=1000000; // :>;8G5AB2> 7=0G5=89 2 2K1>@:5
int ncells=51; // :>;8G5AB2> 8=B5@20;>2 2 38AB>3@0<<5
double x[]; // F5=B@K 8=B5@20;>2 38AB>3@0<<K
double y[]; // :>;8G5AB2> 7=0G5=89 87 2K1>@:8, ?>?02H8E 2 8=B5@20;
double data[]; // 2K1>@:0 A;CG09=KE 7=0G5=89
double max,min; // <0:A8<0;L=>5 8 <8=8<0;L=>5 7=0G5=8O 2 2K1>@:5
//--- ?>;CG8< 2K1>@:C 87 F-@0A?@545;5=8O
MathRandomF(nu_1,nu_2,n,data);
//--- @0AAG8B05< 40==K5 4;O ?>AB@>5=8O 38AB>3@0<<K
CalculateHistogramArray(data,x,y,max,min,ncells);
//--- ?>;CG8< 3@0=8FK ?>A;54>20B5;L=>AB8 8 H03 4;O ?>AB@>5=8O B5>@5B8G5A:>9 :@82>9
double step;
GetMaxMinStepValues(max,min,step);
step=MathMin(step,(max-min)/ncells);
//--- ?>;CG8< B5>@5B8G5A:8 @0AAG8B0==K5 40==K5 =0 8=B5@20;5 [min,max]
double x2[];
double y2[];
MathSequence(min,max,step,x2);
MathProbabilityDensityF(x2,nu_1,nu_2,false,y2);
//--- <0AHB018@C5<
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;
//---
CGraphic graphic;
if(ObjectFind(chart,name)<0)
graphic.Create(chart,name,0,0,0,780,380);
else
graphic.Attach(chart,name);
graphic.BackgroundMain(StringFormat("F-distribution nu1=%G nu2=%G",nu_1,nu_2));
graphic.BackgroundMainSize(16);
//--- plot all curves
graphic.CurveAdd(x,y,CURVE_HISTOGRAM,"Sample").HistogramWidth(4);
//--- 0 B5?5@L ?>AB@>8< B5>@5B8G5A:CN :@82CN ?;>B=>AB8 @0A?@545;5=8O
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,const 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);
ArrayResize(frequency,cells);
//--- 704048< F5=B@K 8=B5@20;>2
for(int i=0; i<cells; i++)
{
intervals[i]=minv+(i+0.5)*width;
frequency[i]=0;
}
//--- 70?>;=8< G0AB>BK ?>?040=8O 2 8=B5@20;
for(int i=0; i<size; i++)
{
int ind=int((data[i]-minv)/width);
if(ind>=cells) ind=cells-1;
frequency[ind]++;
}
return (true);
}
//+------------------------------------------------------------------+
//| Calculates values for sequence generation |
//+------------------------------------------------------------------+
void GetMaxMinStepValues(double &maxv,double &minv,double &stepv)
{
//--- 2KG8A;8< 01A>;NB=K9 @07<0E ?>A;54>20B5;L=>AB8, GB>1K ?>;CG8BL B>G=>ABL =>@<0;870F88
double range=MathAbs(maxv-minv);
int degree=(int)MathRound(MathLog10(range));
//--- =>@<0;87C5< <0:A. 8 <8=. 7=0G5=8O A 7040==>9 B>G=>ABLN
maxv=NormalizeDouble(maxv,degree);
minv=NormalizeDouble(minv,degree);
//--- H03 35=5@0F88 ?>A;54>20B5;L=>AB8 B0:65 704048< >B 7040==>9 B>G=>AB8
stepv=NormalizeDouble(MathPow(10,-degree),degree);
if((maxv-minv)/stepv<10)
stepv/=10.;
}
//+------------------------------------------------------------------+