94 lines
3.6 KiB
MQL5
94 lines
3.6 KiB
MQL5
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| Learning.mq5 |
|
||
|
|
//| Eugene |
|
||
|
|
//| https://www.mql5.com |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#property copyright "Eugene"
|
||
|
|
#property link "https://www.mql5.com"
|
||
|
|
#property version "1.00"
|
||
|
|
#include <CategoricalHMM\CategoricalHMM.mqh>
|
||
|
|
#include <Graphics\Graphic.mqh>
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| Script program start function |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
void OnStart()
|
||
|
|
{
|
||
|
|
int n_states = 2;
|
||
|
|
int n_emissions = 6;
|
||
|
|
int n_samples = 300;
|
||
|
|
|
||
|
|
//--- 1. Создаем модель (seed=99 для повторяемости) ---
|
||
|
|
CCategoricalHMM model(n_states, n_emissions, 99);
|
||
|
|
|
||
|
|
// --- Задаем параметры модели ---
|
||
|
|
// --- 2. Устанавливаем матрицу переходов (Tr) ---
|
||
|
|
matrix tr = {{0.95, 0.05},
|
||
|
|
{0.1, 0.9}
|
||
|
|
};
|
||
|
|
|
||
|
|
// --- 3.Устанавливаем матрицу эмиссий (E) ---
|
||
|
|
matrix e = {{1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6, 1.0/6},
|
||
|
|
{0.1, 0.1, 0.1, 0.1, 0.1, 0.5}
|
||
|
|
};
|
||
|
|
|
||
|
|
// --- 4. Генерируем последовательность наблюдений и скрытых состояний ---
|
||
|
|
vector y,z;
|
||
|
|
model.Sample(n_samples, tr, e, y, z);
|
||
|
|
|
||
|
|
// --- 5. Создаем новую модель и обучаем на сгенерированых данных ---
|
||
|
|
CCategoricalHMM hmm(n_states, n_emissions);
|
||
|
|
// --- В качестве начальных параметров зададим: ---
|
||
|
|
matrix initialTR = {{0.7, 0.3},
|
||
|
|
{0.3, 0.7}
|
||
|
|
};
|
||
|
|
matrix initialE = {{0.15, 0.05, 0.1, 0.1, 0.4, 0.2},
|
||
|
|
{0.05, 0.15, 0.1, 0.1, 0.3, 0.3}
|
||
|
|
};
|
||
|
|
|
||
|
|
hmm.m_TR = initialTR;
|
||
|
|
hmm.m_E = initialE;
|
||
|
|
|
||
|
|
double tol = 1e-6;
|
||
|
|
if(hmm.Fit(y,100, tol, true))
|
||
|
|
{
|
||
|
|
Print("\n--- Результаты ---");
|
||
|
|
PrintFormat("Final LogLikelihood: %.6f", hmm.m_logliks[hmm.m_logliks.Size() - 1]);
|
||
|
|
Print("Estimated TR:\n", hmm.m_TR);
|
||
|
|
Print("True TR:\n", tr);
|
||
|
|
Print("Estimated E:\n", hmm.m_E);
|
||
|
|
Print("True E:\n", e);
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- БЛОК ВИЗУАЛИЗАЦИИ ---
|
||
|
|
int plotLimit = (int)hmm.m_logliks.Size();
|
||
|
|
double x[], y_[];
|
||
|
|
ArrayResize(x, plotLimit);
|
||
|
|
ArrayResize(y_, plotLimit);
|
||
|
|
for(int i = 0; i < plotLimit; i++)
|
||
|
|
{
|
||
|
|
x[i] = i;
|
||
|
|
y_[i] = hmm.m_logliks[i];
|
||
|
|
}
|
||
|
|
ChartSetInteger(0,CHART_SHOW,false);
|
||
|
|
CGraphic graphic;
|
||
|
|
ulong width = ChartGetInteger(0,CHART_WIDTH_IN_PIXELS);
|
||
|
|
ulong height = ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS);
|
||
|
|
graphic.Create(0,"HMM_States_Plot",0,0,0,int(width),int(height));
|
||
|
|
CCurve *curve = graphic.CurveAdd(x, y_, ColorToARGB(clrDodgerBlue, 255), CURVE_LINES,"LL");
|
||
|
|
curve.LinesStyle(STYLE_SOLID);
|
||
|
|
curve.LinesWidth(2);
|
||
|
|
graphic.BackgroundMainColor(ColorToARGB(clrBlack,255));
|
||
|
|
graphic.BackgroundMainSize(24);
|
||
|
|
graphic.BackgroundMain("EM Training Progress");
|
||
|
|
graphic.XAxis().Name("Iteration");
|
||
|
|
graphic.XAxis().NameSize(18);
|
||
|
|
graphic.YAxis().Name("Log Likelihood");
|
||
|
|
graphic.YAxis().NameSize(18);
|
||
|
|
graphic.CurvePlotAll();
|
||
|
|
graphic.Update();
|
||
|
|
Sleep(5*1000);
|
||
|
|
ChartSetInteger(0,CHART_SHOW,true);
|
||
|
|
graphic.Destroy();
|
||
|
|
ChartRedraw(0);
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|