//+------------------------------------------------------------------+
//| NormalDistributionExample.mq5 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//--- 正規分布を計算する関数をインクルードする
#include <Math\Stat\Normal.mqh>
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 正規分布のパラメータを設定する
double mu=5.0;
double sigma=1.0;
PrintFormat("Normal distribution with parameters mu=%G and sigma=%G, calculation examples:",mu,sigma);
//--- 間隔を設定する
double x1=mu-sigma;
double x2=mu+sigma;
//--- 確率計算のための変数
double cdf1,cdf2,probability;
//--- エラーコードを表す変数
int error_code1,error_code2;
//--- 分布関数の値を計算する
cdf1=MathCumulativeDistributionNormal(x1,mu,sigma,error_code1);
cdf2=MathCumulativeDistributionNormal(x2,mu,sigma,error_code2);
//--- エラーコードを確認する
if(error_code1==ERR_OK && error_code2==ERR_OK)
{
//--- 範囲内の確率変数の確率を計算する
probability=cdf2-cdf1;
//--- 結果を出力する
PrintFormat("1. Calculate probability of a random variable within the range of %.5f<x<%.5f",x1,x2);
PrintFormat(" Answer: Probability = %5.8f",probability);
}
//--- Find the value range of random variable x, corresponding to the 95% confidence level
probability=0.95; // 信頼確率を設定する
//--- 区間の境界での確率を設定する
double p1=(1.0-probability)*0.5;
double p2=probability+(1.0-probability)*0.5;
//--- 区間の境界を計算する
x1=MathQuantileNormal(p1,mu,sigma,error_code1);
x2=MathQuantileNormal(p2,mu,sigma,error_code2);
//--- エラーコードを確認する
if(error_code1==ERR_OK && error_code2==ERR_OK)
{
//--- 結果を出力する
PrintFormat("2. For confidence interval = %.2f, find the range of random variable",probability);
PrintFormat(" Answer: range is %5.8f <= x <=%5.8f",x1,x2);
}
PrintFormat("3. Compute the first 4 calculated and theoretical moments of the distribution");
//--- 乱数の配列を生成し、最初の4次のモーメントを計算し、理論値と比較する
int data_count=1000000; // 値の数を設定し、配列を準備する
double data[];
ArrayResize(data,data_count);
//--- 乱数値を生成して配列に格納する
for(int i=0; i<data_count; i++)
{
data[i]=MathRandomNormal(mu,sigma,error_code1);
}
//--- 計算に使われる初めの値のインデックスとデータ量を設定する
int start=0;
int count=data_count;
//--- 生成された値の最初の4次のモーメントを計算する
double mean=MathMean(data,start,count);
double variance=MathVariance(data,start,count);
double skewness=MathSkewness(data,start,count);
double kurtosis=MathKurtosis(data,start,count);
//--- 理論的モーメントを表す変数
double normal_mean=0;
double normal_variance=0;
double normal_skewness=0;
double normal_kurtosis=0;
//--- 計算されたモーメントの値を表示する
PrintFormat(" Mean Variance Skewness Kurtosis");
PrintFormat("Calculated %.10f %.10f %.10f %.10f",mean,variance,skewness,kurtosis);
//--- モーメントの理論値を計算し、得られた値と比較する
if(MathMomentsNormal(mu,sigma,normal_mean,normal_variance,normal_skewness,normal_kurtosis,error_code1))
{
PrintFormat("Theoretical %.10f %.10f %.10f %.10f",normal_mean,normal_variance,normal_skewness,normal_kurtosis);
PrintFormat("Difference %.10f %.10f %.10f %.10f",mean-normal_mean,variance-normal_variance,skewness-normal_skewness,kurtosis-normal_kurtosis);
}
}
|