Libraries: ALGLIB - Numerical Analysis Library - page 5

 

Hi!


I am completely new to ALGLIB. So, I was trying to perform the following task, which is to calculate k² using a script. I can easly do that in Excel, but have no idea how to use a non-linear eqution model with one boxed constraint.


void OnStart()
  {
   const double x[22] = {28.98, 30.48, 30.98, 31.48, 31.98, 32.48, 32.98, 33.48, 33.98, 34.48, 34.98, 35.48, 35.98, 36.48, 36.98, 37.48, 37.98, 38.48, 38.98, 39.98, 40.98, 41.98};
   const double y[22] = {8.26, 7.26, 5.71, 6.15, 5.90, 5.15, 5.03, 4.30, 4.15, 3.30, 2.90, 2.71, 1.96, 1.57, 1.27, 0.90, 0.70, 0.47, 0.33, 0.20, 0.09, 0.06};
   const double w[22] = {2, 8, 2, 2, 2, 1, 11, 12, 24, 12, 6, 12, 21, 7, 74, 51, 159, 385, 171, 109, 18, 18}; //weights
   const double x0 = 37.74; //central value theorem

//model equation
//y = (1/2)*((x^2-2x0x+x0^2+4k^2)^(1/2) + x+x0) - x
//k^2 was calculated as 0.582018800686398, after using Solver in Excel where total error was a minimized sum of w(y-y0)^2
//box constraint k^2>0;
  };




Thanks in advance for reading!

 
Arthur Albano #:

Hi!


I am completely new to ALGLIB. So, I was trying to perform the following task, which is to calculate k² using a script. I can easly do that in Excel, but have no idea how to use a non-linear eqution model with one boxed constraint.





Thanks in advance for reading!


So far, looks ugly...


//+------------------------------------------------------------------+
//|                                                AlgLibExample.mq5 |
//|                                                    Arthur Albano |
//|                       https://www.mql5.com/en/users/arthuralbano |
//+------------------------------------------------------------------+
#property copyright "Arthur Albano"
#property link      "https://www.mql5.com/en/users/arthuralbano"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

#include <Math\Alglib\alglib.mqh>
#include <Math\Alglib\interpolation.mqh>
#include <Arrays\ArrayDouble.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   const double x0 = 37.74; //central value theorem

//model equation
//f(x,k²,x0) = (1/2)*((x^2-2x0x+x0^2+4k^2)^(1/2) + x+x0) - x
//k^2 was calculated as 0.582018800686398, after using Solver in Excel
//box contraint k^2>0;

// https://www.alglib.net/translator/man/manual.cpp.html#example_lsfit_d_nlfb

   const double x[22] = {28.98, 30.48, 30.98, 31.48, 31.98, 32.48, 32.98, 33.48, 33.98, 34.48, 34.98, 35.48, 35.98, 36.48, 36.98, 37.48, 37.98, 38.48, 38.98, 39.98, 40.98, 41.98};
   double w[22] = {2, 8, 2, 2, 2, 1, 11, 12, 24, 12, 6, 12, 21, 7, 74, 51, 159, 385, 171, 109, 18, 18}; //weights
   double y[22] = {8.26, 7.26, 5.71, 6.15, 5.90, 5.15, 5.03, 4.30, 4.15, 3.30, 2.90, 2.71, 1.96, 1.57, 1.27, 0.90, 0.70, 0.47, 0.33, 0.20, 0.09, 0.06};
   
   const int n = ArraySize(x);

   double c[1] = {x0 / 100.};
   CLSFitState state;

   CMatrixDouble xx(n, 1);
   for(int i = 0; i < n; i++)
     {
      xx[i].Set(0,x[i]);
     };

   CLSFit::LSFitCreateWFGH(xx, y, w, c, n,1,1,state);

   const double epsf = 0.000001;
   const double epsx = 0.000001;
   const int maxits = 0;
   CLSFit::LSFitSetCond(state,epsf,epsx,maxits);
   
   double bndl[1] = {0.};
   double bndu[1] = {x0 / 100.};
   CLSFit::LSFitSetBC(state,bndl,bndu);
   
   CNDimensional_PFunc *Pfunc = new CNDimensional_PFunc;

   //CAlglib::LSFitFit(state,Pfunc,,,rep,state);
   
   int info;
   CLSFitReport rep;
   CLSFit::LSFitResults(state,info,c,rep);
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double func(const double _x, const double _k, const double _x0)
  {
   return((1. / 2.) * (pow(pow(_x, 2.) - 2 * _x0 * _x + pow(_x0, 2.) + 4 * pow(_k, 2.), 1. / 2.) + _x + _x0) - _x);
  };
//+------------------------------------------------------------------+
 

Done.

Basically copy & paste from AlgLib TestInterface.mqh.

//+------------------------------------------------------------------+
//|                                                   AlgLibTest.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

#include <Math\Alglib\alglib.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   Print(TEST_LSFit_D_NLF());
  };

//+------------------------------------------------------------------+
//| Derived class from CNDimensional_PFunc                           |
//+------------------------------------------------------------------+
class CNDimensional_CX_1_Func : public CNDimensional_PFunc
  {
public:
   //--- constructor, destructor
                     CNDimensional_CX_1_Func(void);
                    ~CNDimensional_CX_1_Func(void);
   //--- method
   virtual void      PFunc(double &c[], double &x[], double &func, CObject &obj);
  };
//+------------------------------------------------------------------+
//| Constructor without parameters                                   |
//+------------------------------------------------------------------+
CNDimensional_CX_1_Func::CNDimensional_CX_1_Func(void)
  {

  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CNDimensional_CX_1_Func::~CNDimensional_CX_1_Func(void)
  {

  }
//+------------------------------------------------------------------+
//| This callback calculates f(c,x), where x is a                    |
//| position on X-axis and c is adjustable parameter                 |
//+------------------------------------------------------------------+
void CNDimensional_CX_1_Func::PFunc(double &c[], double &x[], double &func, CObject &obj)
  {
   double x0 = 37.74;
   func = (1.0/2.0)*(sqrt(x[0]*x[0] - 2.0*x[0]*x0 + x0*x0 + 4*c[0]) + x[0] + x0) - x[0];
  }

//+------------------------------------------------------------------+
//| Nonlinear fitting using function value only                      |
//+------------------------------------------------------------------+
double TEST_LSFit_D_NLF()
  {
//--- create variables
   CMatrixDouble           x;
   double                  y[];
   double                  c[];
   double                  w[];
   CObject                 obj;
   CNDimensional_CX_1_Func fcx1func;
   CNDimensional_Rep       frep;
   x.Resize(22,1);
//--- initialization
   x[0].Set(0,28.98);
   x[1].Set(0,30.48);
   x[2].Set(0,30.98);
   x[3].Set(0,31.48);
   x[4].Set(0,31.98);
   x[5].Set(0,32.48);
   x[6].Set(0,32.98);
   x[7].Set(0,33.48);
   x[8].Set(0,33.98);
   x[9].Set(0,34.48);
   x[10].Set(0,34.98);
   x[11].Set(0,35.48);
   x[12].Set(0,35.98);
   x[13].Set(0,36.48);
   x[14].Set(0,36.98);
   x[15].Set(0,37.48);
   x[16].Set(0,37.98);
   x[17].Set(0,38.48);
   x[18].Set(0,38.98);
   x[19].Set(0,39.98);
   x[20].Set(0,40.98);
   x[21].Set(0,41.98);
//--- allocation
   ArrayResize(y,22);
//--- initialization
   y[0]=8.26;
   y[1]=7.26;
   y[2]=5.71;
   y[3]=6.15;
   y[4]=5.90;
   y[5]=5.15;
   y[6]=5.03;
   y[7]=4.30;
   y[8]=4.15;
   y[9]=3.30;
   y[10]=2.90;
   y[11]=2.71;
   y[12]=1.96;
   y[13]=1.57;
   y[14]=1.27;
   y[15]=0.90;
   y[16]=0.70;
   y[17]=0.47;
   y[18]=0.33;
   y[19]=0.20;
   y[20]=0.09;
   y[21]=0.06;
//--- allocation
   ArrayResize(c,1);
//--- initialization
   c[0] =  0.5;
//--- create variables
   int maxits=0;
   int info;
   CLSFitStateShell state;
   double diffstep=0.0001;
//--- Fitting withweights
   CAlglib::LSFitCreateF(x,y,c,diffstep,state);
//--- function call
   double epsf = 0.0000001;
   double epsx = 0.0000001;
   CAlglib::LSFitSetCond(state,epsf,epsx,maxits);
//--- function call
   CAlglib::LSFitFit(state,fcx1func,frep,0,obj);
//--- function call
   CLSFitReportShell rep;
   CAlglib::LSFitResults(state,info,c,rep);
//--- Fitting with weights
//--- (you can change weights and see how it changes result)
   ArrayResize(w,22);
//--- initialization
   w[0]=2;
   w[1]=8;
   w[2]=2;
   w[3]=2;
   w[4]=2;
   w[5]=1;
   w[6]=11;
   w[7]=12;
   w[8]=24;
   w[9]=12;
   w[10]=6;
   w[11]=12;
   w[12]=21;
   w[13]=7;
   w[14]=74;
   w[15]=51;
   w[16]=159;
   w[17]=385;
   w[18]=171;
   w[19]=109;
   w[20]=18;
   w[21]=18;
//--- Clear weights
//   ArrayFill(w,0,ArraySize(w),1);
//--- function call
   CAlglib::LSFitCreateWF(x,y,w,c,diffstep,state);
//--- function call
   CAlglib::LSFitSetCond(state,epsf,epsx,maxits);
//--- function call
   CAlglib::LSFitFit(state,fcx1func,frep,0,obj);
//--- function call
   CAlglib::LSFitResults(state,info,c,rep);
   return(c[0]);
  }
//+------------------------------------------------------------------+
 
Please reset a fresh version, it doesn't compile!(
 
Koshtenko #:
Please reset the latest version, it doesn't compile!(

Use the standard version of Alglib, which has been supplied with the terminal for a long time. The class of complex numbers is adapted in it:


 
qingyouwei #:
How can i multiply two matrix use the lib alglib ?

How can i multiply two matrix use the lib alglib?

Can you compile ALGLIB and pass it?

My compiled the following error: 'complex' - unexpected token complex.mqh 37 8

'complex' - unexpected token complex.mqh 37 8

'{' - name expected complex.mqh 38 3

'complex' - unexpected token, probably type is missing? complex.mqh 79 1

'im' - unexpected token, probably type is missing? complex.mqh 79 31

'im' - class type expected complex.mqh 79 31

'}' - semicolon expected complex.mqh 82 3

'}' - expressions are not allowed on a global scope complex.mqh 82 3

'complex' - unexpected token, probably type is missing? complex.mqh 86 1

'im' - unexpected token, probably type is missing? complex.mqh 86 41

'im' - variable already defined complex.mqh 86 41

see previous declaration of variable 'im' complex.mqh 79 31

'im' - class type expected complex.mqh 86

 

When are we going to add spectral analysis functions
SSA ?
ALGLIB has them, but MT still does not.
https://www.alglib.net/translator/man/manual.cpp.html#example_ssa_d_basic

 
Ivan Shishikin #:

When are we going to add spectral analysis functions
SSA ?
ALGLIB has them, but MT still does not.
https://www.alglib.net/translator/man/manual.cpp.html#example_ssa_d_basic

Maybe until the standard library will not be added (and it may not be soon), the existing sources(1, 2) will be fine?

 
unfortunately there is no code for predicates.
there is only the model itself. but that is not enough.
 
Ivan Shishikin #:

When are we going to add spectral analysis functions
SSA ?
ALGLIB has them, but MT still does not.
https://www.alglib.net/translator/man/manual.cpp.html#example_ssa_d_basic

Imho, there is no guarantee that it will appear at all ... Maybe, instead of waiting, it is better to use them directly in C-code?