forum.alglib.net

ALGLIB forum
It is currently Mon Dec 23, 2024 6:04 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 14 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: ALGLIB and C++
PostPosted: Fri Sep 28, 2012 1:06 pm 
Offline

Joined: Wed Jul 18, 2012 1:14 pm
Posts: 9
Hello.
I got it work more or less. It works very fast and precisely for 1 parameter, quite good for 2 parameters, more or less good for 3 parameters in case of good guess and with scaling. I have the biggest trouble for the case of 4 parameters. It makes first step, which is often worth than initial guess and then it makes very very small steps w.r.t. all parameters. I guess it could be because the values of function are much bigger than the differences of it from step to step. Could it be a problem? How to treat it? Or may be I do scaling incorrect?

One of examples (correct answer is c[0]=3, c[1]=1.4,c[2]=0.95):
Image

p.s. I hope I managed to descibe my problem.


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Sun Sep 30, 2012 7:57 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Can you post your code here? I need: a) data you used for fitting, b) function being fitted. It is better to reproduce algorithm behavior on real data.


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Mon Oct 01, 2012 6:11 am 
Offline

Joined: Wed Jul 18, 2012 1:14 pm
Posts: 9
Sergey.Bochkanov wrote:
Can you post your code here? I need: a) data you used for fitting, b) function being fitted. It is better to reproduce algorithm behavior on real data.

a)
Code:
double function_temp(float p1, float p2, float p3, float p4, float var1)
{
   //float gkk = 0.95f;
   //float b = 3.f;         // Model Param
   float rho = 0.3f;         // Model Param
   //float sigma0 = 0.02f;      // Model Param
   //float m0 = 1.4f;          // Model Param
   int k = 6;             // Fixed Param  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   float lambda = -0.1f;      // Option Param
   float initPrice = 50.f;      // Option Param
//   float strikePrice   = 65.f; // Option Param
   float interest = 0.00018f;       // Option Param
   int noOfSum   = 300;          // Sym Param
   int width = 128;           // Sym Param
   int height = 256;// 256;           // Sym Param
   float temp;
return (double)clMonteCarloAMSM(            
             p3,//gkk,         // Model Param
             p1,//b,           // Model Param
             rho,         // Model Param
             p4,//sigma0,      // Model Param
             p2,          // Model Param
             k,             // Fixed Param
             lambda,      // Option Param
             initPrice,      // Option Param
             var1,//strikePrice, // Option Param
             interest,       // Option Param
             noOfSum,//noOfSum,          // Sym Param
             width,            // Sym Param
             height,           // Sym Param
             &temp);
//   return exp(-p1*pow(var1, 2));
  std::cout<<" temp = " << (double)temp <<std::endl;
}

void function_cx_1_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr)
{
    // this callback calculates f(c,x)=exp(-c0*sqr(x0))
    // where x is a position on X-axis and c is adjustable parameter
    //func = pow((float)x[0] - (float)c[0],2);

//   func = function_temp2((float)c[0],(float)x[0]);
   func = function_temp((float)c[0],(float)c[1],(float)c[2], (float)c[3], (float)x[0]);
   if (x[0]==50) {std::cout<<" c[0] = " << c[0]<<" c[1] = " << c[1] << " c[2] = " << c[2] <<" c[3] = " << c[3] << " x[0] = " << x[0] << " func = " << (double)func <<std::endl;}
}

int
main(int argc, char * argv[])
{

real_2d_array x = "[[30],[32],[34],[36],[38],[40],[42],[44],[46],[48],[50],[52],[54],[56],[58],[60],[62],[64],[66],[68],[70]]";
real_1d_array y = "[21.7538, 19.9783, 18.254, 16.5923, 15.004, 13.4978, 12.0822, 10.7645, 9.54836, 8.43455, 7.42138, 6.50722, 5.68772, 4.95832, 4.31183, 3.74198, 3.24245, 2.80582, 2.42521, 2.09441, 1.80822]";


   real_1d_array c = "[3.1, 1.38, 0.97, 0.03]";
   //real_1d_array c = "[3.0739, 1.4057, 0.9557]";

    real_1d_array bndl = "[2.0, 1.2, 0.9, 0.001]";
    real_1d_array bndu = "[10.0, 1.6, 0.9999, 0.06]";
   //real_1d_array s = "[0.8e+1, 1.0, 1.5]";
    real_1d_array s = "[8.0e-7, 0.4e-7, 0.25e-7, 0.05e-7]";

    double epsf = 0; 
    double epsx = 0.00001;  // condition on step size
    ae_int_t maxits = 0;
    ae_int_t info;
    lsfitstate state;
    lsfitreport rep;

    double diffstep =  1.0e-2;

    //
    // Fitting without weights
    //
    lsfitcreatef(x, y, c, diffstep, state);
    lsfitsetcond(state, epsf, epsx, maxits);
    lsfitsetbc(state, bndl, bndu);
   lsfitsetscale(state, s);
   //alglib::lsfitsetxrep(state, true);
    alglib::lsfitfit(state, function_cx_1_func);
    lsfitresults(state, info, c, rep);
   //printf("%d\n", int(state));
    std::cout<< "C = " << c.tostring(4).c_str() << std::endl;


There are prices computed by the model by using Monte Carlo (in vector y) and strike-price (in vector x). So, it is not really "real" data, but it is generated data.

b) It is not so easy. I do calibration of option prices which are calculated by using Monte-Carlo Method (as I wrote above). I can write my model, but I think it would quite difficult to reproduce it.


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Mon Oct 01, 2012 11:36 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
It is very hard to tell without debugging actual code, but I suspect that it is somehow connected with the fact that you optimize function which is calculated using Monte-Carlo methods. As I previously told, Monte-Carlo methods do not guarantee smoothness. And even when run in the deterministic setting (same seed value is used every time) they result in function with many sharp local extrema. I suppose that algorithm stalls in one of these extrema.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group