You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by en...@pnc.com on 2013/12/18 18:58:52 UTC

[math] BOBYQAOptimizer hang

I ran into a case that BOBYQAOptimizer hang in line 485. trsbox is pretty 
complex method and I'm not familiar with the algorithm. It seems going 
into infinite loop between different states.

            final double[] dsqCrvmin = trsbox(delta, gnew, xbdi, s,
                                              hs, hred);

Here is the test case for it. After reducing the value to <2.24 it hang in 
trsbox method without progress.


Can someone shed light on what might be happening, and how to deal with 
it? Thanks.


Andrew



The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp 
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.



Re: [math] BOBYQAOptimizer hang

Posted by Gilles <gi...@harfang.homelinux.org>.
Hello.

>
> Thank you for recommending LevenbergMarquardtOptimizer. It works for 
> our
> function. However, it seems to take much longer than BOBYQAOptimizer. 
> Is
> this expected as it needs to compute gradient?

It's difficult to tell.
Someone listening here might provide some theoretical insight.

But if you suspect an implementation problem, it would be a good start
to post[1] a stripped-down version of your code (preferrably in the 
form
of a Junit test).

Regards,
Gilles

[1] The Java source code file(s) can be attached with a report on the 
bug
     tracking system.

>
>
> [...]


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] BOBYQAOptimizer hang

Posted by en...@pnc.com.
Gilles,

Thank you for recommending LevenbergMarquardtOptimizer. It works for our 
function. However, it seems to take much longer than BOBYQAOptimizer. Is 
this expected as it needs to compute gradient?

Andrew



From:
Gilles <gi...@harfang.homelinux.org>
To:
<us...@commons.apache.org>
Date:
12/19/2013 07:03 PM
Subject:
Re: [math] BOBYQAOptimizer hang



On Thu, 19 Dec 2013 08:53:14 -0500, enzhong.fu@pnc.com wrote:
> Gilles,
>
> Thank you for answering. PowellOptimizer did not work very well for 
> our
> particular function, and other optimizers in scalar.noderiv are not 
> easily
> understandable.

It would be interesting to know what you tested, and what did not work 
as
you expected, in order to look for bugs in "PowellOptimizer".

For the other optimizer, it is perhaps an issue of setting good 
parameters
and initial guess...

> BOBYQAOptimizer usually works well. Do you have
> suggestions that we might work around this particular case so it does 
> not
> hang?

I have no idea.
If I had to debug it, I'd first trace the code execution to locate 
where it
hangs.

> We minimize LS with grid (x,y) having 9 points.

Did you consider using the "LevenbergMarquardtOptimizer"?


Best regards,
Gilles

>
> Andrew
>
>
> class LS implements MultivariateFunction {
>         private BS bs = new BS();
>         private double[] x;
>         private double[] y;
>         private double const1;
>         private int optionType;
>
>         public LS(double[] x, double[] y, int optionType, double 
> const1) {
>                 this.x = x;
>                 this.y = y;
>                 this.optionType = optionType;
>                 this.const1 = const1;
>         }
>
>         @Override
>         public double value(double point[]) {
>                 int len = x.length;
>                 double val = 0;
>                 for (int i=0; i<len; i++) {
>                         double yc = bs.f2(x[i], point[0]*const1, 
> point[1],
> point[2], optionType);
>                         val += Math.pow(yc-y[i], 2);
>                 }
>                 return val;
>         }
>
> }
>
> class BS {
>         public double blackScholes(double price, double strike, 
> double
> time, double vol, int optionType) {
>             double volrootime = Math.pow(time, 0.5) * vol;
>             double topline1 = Math.log(price/strike);
>             double topline2 = Math.pow(vol, 2)/2* time;
>             double topline = topline1 + topline2;
>             double d1 = topline / volrootime;
>             double d2 = d1 - volrootime;
>
>             double val = 0.0;
>             NormalDistribution normal = new NormalDistribution();
>             if (optionType==1)
>                 val = (price * normal.cumulativeProbability(d1) - 
> strike *
> normal.cumulativeProbability(d2));
>             else
>                 val = (strike * normal.cumulativeProbability(-d2) - 
> price
> * normal.cumulativeProbability(-d1));
>
>             return val;
>         }
>
>         public double f2(double x, double ah, double ai, double aj, 
> int
> ak) {
>                 return ah*bScore(x, ai, aj, ak);
>         }
>
>         public double bScore(double price, double strike, double vol, 
> int
> optionType) {
>                 return blackScholes(price, strike, 1, vol, 
> optionType);
>         }
> }
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org




The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp 
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.



Re: [math] BOBYQAOptimizer hang

Posted by Gilles <gi...@harfang.homelinux.org>.
On Thu, 19 Dec 2013 08:53:14 -0500, enzhong.fu@pnc.com wrote:
> Gilles,
>
> Thank you for answering. PowellOptimizer did not work very well for 
> our
> particular function, and other optimizers in scalar.noderiv are not 
> easily
> understandable.

It would be interesting to know what you tested, and what did not work 
as
you expected, in order to look for bugs in "PowellOptimizer".

For the other optimizer, it is perhaps an issue of setting good 
parameters
and initial guess...

> BOBYQAOptimizer usually works well. Do you have
> suggestions that we might work around this particular case so it does 
> not
> hang?

I have no idea.
If I had to debug it, I'd first trace the code execution to locate 
where it
hangs.

> We minimize LS with grid (x,y) having 9 points.

Did you consider using the "LevenbergMarquardtOptimizer"?


Best regards,
Gilles

>
> Andrew
>
>
> class LS implements MultivariateFunction {
>         private BS bs = new BS();
>         private double[] x;
>         private double[] y;
>         private double const1;
>         private int optionType;
>
>         public LS(double[] x, double[] y, int optionType, double 
> const1) {
>                 this.x = x;
>                 this.y = y;
>                 this.optionType = optionType;
>                 this.const1 = const1;
>         }
>
>         @Override
>         public double value(double point[]) {
>                 int len = x.length;
>                 double val = 0;
>                 for (int i=0; i<len; i++) {
>                         double yc = bs.f2(x[i], point[0]*const1, 
> point[1],
> point[2], optionType);
>                         val += Math.pow(yc-y[i], 2);
>                 }
>                 return val;
>         }
>
> }
>
> class BS {
>         public double blackScholes(double price, double strike, 
> double
> time, double vol, int optionType) {
>             double volrootime = Math.pow(time, 0.5) * vol;
>             double topline1 = Math.log(price/strike);
>             double topline2 = Math.pow(vol, 2)/2* time;
>             double topline = topline1 + topline2;
>             double d1 = topline / volrootime;
>             double d2 = d1 - volrootime;
>
>             double val = 0.0;
>             NormalDistribution normal = new NormalDistribution();
>             if (optionType==1)
>                 val = (price * normal.cumulativeProbability(d1) - 
> strike *
> normal.cumulativeProbability(d2));
>             else
>                 val = (strike * normal.cumulativeProbability(-d2) - 
> price
> * normal.cumulativeProbability(-d1));
>
>             return val;
>         }
>
>         public double f2(double x, double ah, double ai, double aj, 
> int
> ak) {
>                 return ah*bScore(x, ai, aj, ak);
>         }
>
>         public double bScore(double price, double strike, double vol, 
> int
> optionType) {
>                 return blackScholes(price, strike, 1, vol, 
> optionType);
>         }
> }
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] BOBYQAOptimizer hang

Posted by en...@pnc.com.
Gilles,

Thank you for answering. PowellOptimizer did not work very well for our 
particular function, and other optimizers in scalar.noderiv are not easily 
understandable. BOBYQAOptimizer usually works well. Do you have 
suggestions that we might work around this particular case so it does not 
hang? We minimize LS with grid (x,y) having 9 points.

Andrew


class LS implements MultivariateFunction {
        private BS bs = new BS();
        private double[] x;
        private double[] y;
        private double const1;
        private int optionType;

        public LS(double[] x, double[] y, int optionType, double const1) {
                this.x = x;
                this.y = y;
                this.optionType = optionType;
                this.const1 = const1;
        }

        @Override
        public double value(double point[]) {
                int len = x.length;
                double val = 0;
                for (int i=0; i<len; i++) {
                        double yc = bs.f2(x[i], point[0]*const1, point[1], 
point[2], optionType); 
                        val += Math.pow(yc-y[i], 2);
                }
                return val;
        }
 
}

class BS {
        public double blackScholes(double price, double strike, double 
time, double vol, int optionType) {
            double volrootime = Math.pow(time, 0.5) * vol;
            double topline1 = Math.log(price/strike);
            double topline2 = Math.pow(vol, 2)/2* time;
            double topline = topline1 + topline2;
            double d1 = topline / volrootime;
            double d2 = d1 - volrootime;
 
            double val = 0.0;
            NormalDistribution normal = new NormalDistribution();
            if (optionType==1)
                val = (price * normal.cumulativeProbability(d1) - strike * 
normal.cumulativeProbability(d2));
            else
                val = (strike * normal.cumulativeProbability(-d2) - price 
* normal.cumulativeProbability(-d1));
 
            return val;
        }

        public double f2(double x, double ah, double ai, double aj, int 
ak) {
                return ah*bScore(x, ai, aj, ak);
        }
 
        public double bScore(double price, double strike, double vol, int 
optionType) {
                return blackScholes(price, strike, 1, vol, optionType);
        }
}




From:
Gilles <gi...@harfang.homelinux.org>
To:
<us...@commons.apache.org>
Date:
12/18/2013 07:22 PM
Subject:
Re: [math] BOBYQAOptimizer hang



Hi.

On Wed, 18 Dec 2013 12:58:52 -0500, enzhong.fu@pnc.com wrote:
> I ran into a case that BOBYQAOptimizer hang in line 485. trsbox is 
> pretty
> complex method and I'm not familiar with the algorithm. It seems 
> going
> into infinite loop between different states.
>
>             final double[] dsqCrvmin = trsbox(delta, gnew, xbdi, s,
>                                               hs, hred);
>
> Here is the test case for it. After reducing the value to <2.24 it 
> hang in
> trsbox method without progress.
>
>
> Can someone shed light on what might be happening, and how to deal 
> with
> it? Thanks.

The short story is that BOBYQAOptimzer is not really supported.
The long story is on the bug-tracking system:
   https://issues.apache.org/jira/browse/MATH-621

Sorry,
Gilles


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org




The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp 
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.



Re: [math] BOBYQAOptimizer hang

Posted by Gilles <gi...@harfang.homelinux.org>.
Hi.

On Wed, 18 Dec 2013 12:58:52 -0500, enzhong.fu@pnc.com wrote:
> I ran into a case that BOBYQAOptimizer hang in line 485. trsbox is 
> pretty
> complex method and I'm not familiar with the algorithm. It seems 
> going
> into infinite loop between different states.
>
>             final double[] dsqCrvmin = trsbox(delta, gnew, xbdi, s,
>                                               hs, hred);
>
> Here is the test case for it. After reducing the value to <2.24 it 
> hang in
> trsbox method without progress.
>
>
> Can someone shed light on what might be happening, and how to deal 
> with
> it? Thanks.

The short story is that BOBYQAOptimzer is not really supported.
The long story is on the bug-tracking system:
   https://issues.apache.org/jira/browse/MATH-621

Sorry,
Gilles


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] BOBYQAOptimizer hang

Posted by en...@pnc.com.
The email dropped the attached file. Here is the test case code, we use 
commons-math3-3.2.

Andrew


import org.apache.commons.math3.analysis.MultivariateFunction;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.optim.InitialGuess;
import org.apache.commons.math3.optim.MaxEval;
import org.apache.commons.math3.optim.OptimizationData;
import org.apache.commons.math3.optim.PointValuePair;
import org.apache.commons.math3.optim.SimpleBounds;
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction;
import 
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.BOBYQAOptimizer;
import org.junit.Test;

public class BOBYQAOptimizerTest {
        @Test
        public void test1() {
                double[] x = new double[]{0.37215286535894443, 
0.4648383826282711, 0.5806074387095237, 0.7252090414280923, 
0.9058240020796098, 1.13142152933966, 1.413204633697474, 
1.7651664608765416, 2.204785181358541};
                double[] y = new double[]{-0.03858, -0.10265, -0.29995, 
-0.94505, -3.13887, -10.66503, -35.8148, -114.73309, -339.44331};

                double[] lower = new double[3];
                double[] upper = new double[3];
                double t1 = -511.14797785141417;        //t1 is used to 
transform the SwaptionBS function
                lower[0] = -10.0*Math.abs(t1);
                upper[0] = -lower[0];
                lower[1] = 0.02;
                upper[1] = 100.0;
                lower[2] = 0.02;
                upper[2] = 8.0;
 
                double[] p = new double[]{1.0, 1.0, 0.2995887592309402};
 
                LS ls = new LS(x,y, 1, -511.14797785141417);
                OptimizationData[] parm = new OptimizationData[]{new 
MaxEval(50000), new ObjectiveFunction(ls),
                                GoalType.MINIMIZE,      new 
InitialGuess(p), new SimpleBounds(lower, upper)};
 
                BOBYQAOptimizer op = new BOBYQAOptimizer(10);
                PointValuePair po = op.optimize(parm);
        }
 
        public static void main(String[] args) {
                BOBYQAOptimizerTest t = new BOBYQAOptimizerTest();
                t.test1();
        }
}

class LS implements MultivariateFunction {
        private BS bs = new BS();
        private double[] x;
        private double[] y;
        private double const1;
        private int optionType;

        public LS(double[] x, double[] y, int optionType, double const1) {
                this.x = x;
                this.y = y;
                this.optionType = optionType;
                this.const1 = const1;
        }

        @Override
        public double value(double point[]) {
                int len = x.length;
                double val = 0;
                for (int i=0; i<len; i++) {
                        double yc = bs.f2(x[i], point[0]*const1, point[1], 
point[2], optionType); 
                        val += Math.pow(yc-y[i], 2);
                }
                return val;
        }
 
}

class BS {
        public double blackScholes(double price, double strike, double 
time, double vol, int optionType) {
            double volrootime = Math.pow(time, 0.5) * vol;
            double topline1 = Math.log(price/strike);
            double topline2 = Math.pow(vol, 2)/2* time;
            double topline = topline1 + topline2;
            double d1 = topline / volrootime;
            double d2 = d1 - volrootime;
 
            double val = 0.0;
            NormalDistribution normal = new NormalDistribution();
            if (optionType==1)
                val = (price * normal.cumulativeProbability(d1) - strike * 
normal.cumulativeProbability(d2));
            else
                val = (strike * normal.cumulativeProbability(-d2) - price 
* normal.cumulativeProbability(-d1));
 
            return val;
        }

        public double f2(double x, double ah, double ai, double aj, int 
ak) {
                return ah*bScore(x, ai, aj, ak);
        }
 
        public double bScore(double price, double strike, double vol, int 
optionType) {
                return blackScholes(price, strike, 1, vol, optionType);
        }
}



From:
enzhong.fu@pnc.com
To:
"Commons Users List" <us...@commons.apache.org>
Date:
12/18/2013 12:59 PM
Subject:
[math] BOBYQAOptimizer hang



I ran into a case that BOBYQAOptimizer hang in line 485. trsbox is pretty 
complex method and I'm not familiar with the algorithm. It seems going 
into infinite loop between different states. 

            final double[] dsqCrvmin = trsbox(delta, gnew, xbdi, s, 
                                              hs, hred); 

Here is the test case for it. After reducing the value to <2.24 it hang in 
trsbox method without progress. 


Can someone shed light on what might be happening, and how to deal with 
it? Thanks. 


Andrew 


The contents of this email are the property of PNC. If it was not 
addressed to you, you have no legal right to read it. If you think you 
received it in error, please notify the sender. Do not forward or copy 
without permission of the sender. This message may contain an 
advertisement of a product or service and thus may constitute a commercial 
electronic mail message under US Law. The postal address for PNC is 249 
Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any 
additional advertising or promotional messages from PNC at this e-mail 
address, click here to unsubscribe. 
https://pnc.p.delivery.net/m/u/pnc/uni/p.asp 
By unsubscribing to this message, you will be unsubscribed from all 
advertising or promotional messages from PNC. Removing your e-mail address 
from this mailing list will not affect your subscription to alerts, 
e-newsletters or account servicing e-mails.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org



The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp 
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.