forum.alglib.net

ALGLIB forum
It is currently Mon Dec 23, 2024 5:37 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 1, 2  Next
Author Message
 Post subject: ALGLIB and C++
PostPosted: Sat Mar 26, 2011 10:21 pm 
Offline

Joined: Sat Mar 26, 2011 9:52 pm
Posts: 1
Hi!
I'm newbie in C++ and I'm trying to write a programm using C++ (not C :)).
I want to do approximation using non-linear Least squares method. But lsfitfit function requires pointer to function in global scope. It's not usefull when you use objects (because pointer to method are not equal with global function). So I cant use callback-method (using static requires pointer to source object) for calculation function value like this:
Code:
class MyClass{
private:
      void function_cx_1_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr) {
      ....
      }
public:
      FitCoeff() {
      ...
      lsfitfit(state, &function_cx_1_func);
      ...
      }
};

Maybe made some interface for this function like
Code:
class IFunction(...) {
public:
virtual double getValue(...)=0;
}

and send it to lsfitfit()?
(maybe best solution are present, I'm newbie in c++)

Also is it possible to include in distribution pre-compiled .dlls and .libs? Because adding to project lot of files from src directory are not usefull and filelist are huge.
Thx!


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Tue Mar 29, 2011 8:12 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
No, I don't think that it is possible to avoid definition of the global function. You may declare it as static (in order to move it to file-only scope), if you want.

About precompiled units - yes, it is possible to make .lib file from ALGLIB. But in any way, according to GPL, you should distribute your project with sources (both your sources and ALGLIB).


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Wed Jul 18, 2012 1:19 pm 
Offline

Joined: Wed Jul 18, 2012 1:14 pm
Posts: 9
Hello,
I have exactly the same problem. It is impossible to use global function in my case, because it destroies whole concept of classes. On some forums people recommend to use delegates in this situation, but it looks quite complicate at the moment:(

Stepan.


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Wed Jul 18, 2012 7:05 pm 
Offline

Joined: Wed Jul 18, 2012 1:14 pm
Posts: 9
I've solved previous trouble, at least it is compiled (I just call as "func = ClassName.ClassFunction()"). But now I have new problem, algorithm (lsfitfit) makes one step left, then step right, then it goes back to initial value of parameter and then it stops!


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Thu Jul 19, 2012 5:47 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Can you post your code here? I will help you to find out explanation, but I need actual code (including function being optimized).


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Thu Jul 19, 2012 2:26 pm 
Offline

Joined: Wed Jul 18, 2012 1:14 pm
Posts: 9
Code:
double function_temp(float p1, float var1)
{
   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   = 50;          // Sym Param
   int width = 128;           // Sym Param
   int height = 256;           // Sym Param

return (double)clMonteCarloAMSM.runCLKernels2(            
             p1,//gkk,         // Model Param
             b,//b,           // Model Param
             rho,         // Model Param
             sigma0,      // Model Param
             m0,          // Model Param
             k,             // Fixed Param
             lambda,      // Option Param
             initPrice,      // Option Param
             var1,//strikePrice, // Option Param
             interest,       // Option Param
             22,//noOfSum,          // Sym Param
             width,            // Sym Param
             height           // Sym Param
             );
}

Code:
void function_cx_1_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr)
{   
   func = function_temp((float)c[0],(float)x[0]);
   std::cout<<" c[0] = " << c[0] << " x[0] = " << x[0] << std::endl;
}

Code:
main(int argc, char * argv[])
{
        real_2d_array x = "[[25],[30],[35],[40],[45],[50],[55],[60],[65],[70],[75]]";
    real_1d_array y = "[24.99959, 20.01935, 15.03914, 10.07982, 5.9340054, 1.814159, 0.3633873, 0.05629168, 0.006411664, 0.000785, 0.00005]";
    real_1d_array c = "[0.99]";
/*   real_2d_array x = "[[-1],[-0.8],[-0.6],[-0.4],[-0.2],[0],[0.2],[0.4],[0.6],[0.8],[1.0]]";
    real_1d_array y = "[0.223130, 0.382893, 0.582748, 0.786628, 0.941765, 1.000000, 0.941765, 0.786628, 0.582748, 0.382893, 0.223130]";
    real_1d_array c = "[0.3]"; */
   real_1d_array bndl = "[0.00002]";
    real_1d_array bndu = "[0.99999]";
    double epsf = 0;
    double epsx = 0.0001;
    ae_int_t maxits = 0;
    ae_int_t info;
    lsfitstate state;
    lsfitreport rep;
    double diffstep = 0.0001;

    //
    // Fitting without weights
    //
    lsfitcreatef(x, y, c, diffstep, state);
    lsfitsetbc(state, bndl, bndu);
    lsfitsetcond(state, epsf, epsx, maxits);
    alglib::lsfitfit(state, function_cx_1_func);
    lsfitresults(state, info, c, rep);
    printf("%s\n", c.tostring(1).c_str()); // EXPECTED: [1.0]
}


The function clMonteCarloAMSM.runCLKernels2(...) is an integral computed by Monte Carlo technique as it is evident from the name.

The plot of the sum([runCLKernels2(gkk_real = 0.95,...,strikePrice_i) - runCLKernels2(gkk,...,,strikePrice_i),i=1..11]^2 w.r.t. gkk (minimization parameter) is:

Image

I've changed something and it makes a lot of steps, but there is a new problem. It doesn't converge to the right value (0.95). I tried different very close initial values: 0.92 -> 0.9, 0.99 -> 1.0, 0.97 -> 1.0

Thank you for your answer.


Last edited by SteSus85 on Fri Jul 20, 2012 3:01 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Fri Jul 20, 2012 5:43 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Are you trying to minimize function which somehow depends on integral computed with Monte-Carlo method? If so, it is very likely that optimizer stops because of noise in the function being optimized. Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise. Conventional optimizers can't be used on such functions. Hmmm... I recomend you to google for "noisy optimization", maybe you will find method suited for your problem.


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Fri Jul 20, 2012 3:08 pm 
Offline

Joined: Wed Jul 18, 2012 1:14 pm
Posts: 9
Sergey.Bochkanov wrote:
Are you trying to minimize function which somehow depends on integral computed with Monte-Carlo method?

Yes, the minimized function is an integral computed by Monte Carlo.
Sergey.Bochkanov wrote:
Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise.

I use always the same seed for generation pseudo-random numbers for each computation. So if I compute integral two times with the same parameters on different computers I would have exactly the same value always.
Sergey.Bochkanov wrote:
Hmmm... I recomend you to google for "noisy optimization", maybe you will find method suited for your problem.

Perhaps, I'll do it, thank you for idea.


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Mon Jul 23, 2012 10:35 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
SteSus85 wrote:
Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise.
I use always the same seed for generation pseudo-random numbers for each computation. So if I compute integral two times with the same parameters on different computers I would have exactly the same value always.

The problem is in how your integral changes when you change integration area or some other parameter. "Exact" integral is smooth with respect to changes in the integrand, Monte Carlo integral is nonsmooth. I suppose that is the source of your problems.


Top
 Profile  
 
 Post subject: Re: ALGLIB and C++
PostPosted: Mon Jul 23, 2012 9:51 pm 
Offline

Joined: Wed Jul 18, 2012 1:14 pm
Posts: 9
Sergey.Bochkanov wrote:
SteSus85 wrote:
Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise.
I use always the same seed for generation pseudo-random numbers for each computation. So if I compute integral two times with the same parameters on different computers I would have exactly the same value always.

The problem is in how your integral changes when you change integration area or some other parameter. "Exact" integral is smooth with respect to changes in the integrand, Monte Carlo integral is nonsmooth. I suppose that is the source of your problems.

Monte-Carlo integral is a sum of integrand-functions. So, if an integrand is smooth then Monte Carlo integral is also smooth. But in my case the integrand is unsmooth in certain point. So, I think you are right, unsmoothness can be a problem. I'll try to make the integrand smooth in that point!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 15 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group