hello; I am trying to use the quantlib library from Matlab. As a test case, I adapted the EquityOption.cpp example code from the quantlib webpage to a cpp file which would generate the price of an American Put option with parameters given by the user, using a few different pricing engines. Before writing the mex, I had created a standalone version which accepted input from std::cin, and this ran fine. However, when I run the code from matlab, the results always come back as 0. I do not know a lot about the guts of quantlib, and the lazy evaluation methods being used. However, it seems that when I run the standalone, it takes about 1 second or so (there are some binomial lattice engines being used), whereas when I run the mex file from matlab, it returns zeros instantaneously. I suspect somehow that the << operator in the standalone (copied from EquityOption.cpp) forces the option to be evaluated under the given engine, but this is not being done in my matlab mex. Am I wrong about this? Do I have to force evaluation somehow? As a snippet of code, I have things like this in the mex: americanOptionPut.setPricingEngine(boost::shared_ptr<PricingEngine>( new BinomialVanillaEngine<JarrowRudd>(bsmProcess,timeSteps))); P[iii] = (double)americanOptionPut.NPV(); //puts the results in the output whereas in the standalone it looks more like: americanOptionPut.setPricingEngine(boost::shared_ptr<PricingEngine>( new BinomialVanillaEngine<JarrowRudd>(bsmProcess,timeSteps))); std::cout << "the result is " << americanOptionPut.NPV() << std::endl; any hints? thanks, --sep [ Steven E. Pav {bikes/bitters/linux} nerd ] [ google voice: 909.SHBYCHF shabbychef a.t gmail d0t com ] [ parent of tinyurl.com/oskarpix ] [ a palindrome: wondered now ] ------------------------------------------------------------------------------ The Next 800 Companies to Lead America's Growth: New Video Whitepaper David G. Thomson, author of the best-selling book "Blueprint to a Billion" shares his insights and actions to help propel your business during the next growth cycle. Listen Now! http://p.sf.net/sfu/SAP-dev2dev _______________________________________________ QuantLib-dev mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-dev |
A real easy way of talking to Quantlib from Matlab is to call the java
wrappers for Quantlib. Matlab is written in Java and so it is easy to load classes from there and call them. Robert Philipp Synapse Financial Engineering 703.623.4063 (mobile) 703.537.0119 (fax) [hidden email] On 11/5/2010 7:02 PM, steven e. pav wrote: > > > hello; > > > I am trying to use the quantlib library from Matlab. As a test case, I > adapted the EquityOption.cpp example code from the quantlib webpage to a cpp > file which would generate the price of an American Put option with parameters > given by the user, using a few different pricing engines. Before writing the > mex, I had created a standalone version which accepted input from std::cin, and > this ran fine. > However, when I run the code from matlab, the results always come back as 0. I > do not know a lot about the guts of quantlib, and the lazy evaluation methods > being used. However, it seems that when I run the standalone, it takes about 1 > second or so (there are some binomial lattice engines being used), whereas when > I run the mex file from matlab, it returns zeros instantaneously. I suspect > somehow that the<< operator in the standalone (copied from EquityOption.cpp) > forces the option to be evaluated under the given engine, but this is not being > done in my matlab mex. > Am I wrong about this? Do I have to force evaluation somehow? As a snippet of > code, I have things like this in the mex: > > americanOptionPut.setPricingEngine(boost::shared_ptr<PricingEngine>( > new BinomialVanillaEngine<JarrowRudd>(bsmProcess,timeSteps))); > P[iii] = (double)americanOptionPut.NPV(); //puts the results in the output > > whereas in the standalone it looks more like: > americanOptionPut.setPricingEngine(boost::shared_ptr<PricingEngine>( > new BinomialVanillaEngine<JarrowRudd>(bsmProcess,timeSteps))); > std::cout<< "the result is "<< americanOptionPut.NPV()<< std::endl; > > > any hints? thanks, > > > --sep > > > [ Steven E. Pav {bikes/bitters/linux} nerd ] > [ google voice: 909.SHBYCHF shabbychef a.t gmail d0t com ] > [ parent of tinyurl.com/oskarpix ] > [ a palindrome: wondered now ] > > > ------------------------------------------------------------------------------ > The Next 800 Companies to Lead America's Growth: New Video Whitepaper > David G. Thomson, author of the best-selling book "Blueprint to a > Billion" shares his insights and actions to help propel your > business during the next growth cycle. Listen Now! > http://p.sf.net/sfu/SAP-dev2dev > _______________________________________________ > QuantLib-dev mailing list > [hidden email] > https://lists.sourceforge.net/lists/listinfo/quantlib-dev ------------------------------------------------------------------------------ The Next 800 Companies to Lead America's Growth: New Video Whitepaper David G. Thomson, author of the best-selling book "Blueprint to a Billion" shares his insights and actions to help propel your business during the next growth cycle. Listen Now! http://p.sf.net/sfu/SAP-dev2dev _______________________________________________ QuantLib-dev mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-dev |
In reply to this post by steven e. pav
On Nov 6, 2010, at 12:02 AM, steven e. pav wrote: > I am trying to use the quantlib library from Matlab. As a test case, I > adapted the EquityOption.cpp example code from the quantlib webpage > to a cpp > file which would generate the price of an American Put option with > parameters > given by the user, using a few different pricing engines. Before > writing the > mex, I had created a standalone version which accepted input from > std::cin, and > this ran fine. > However, when I run the code from matlab, the results always come > back as 0. I > do not know a lot about the guts of quantlib, and the lazy > evaluation methods > being used. However, it seems that when I run the standalone, it > takes about 1 > second or so (there are some binomial lattice engines being used), > whereas when > I run the mex file from matlab, it returns zeros instantaneously. It sounds like the library thinks the instrument is expired and skips the calculation. Are you settings the evaluation date correctly? (Look for where Settings::instance().evaluationDate() is set in the standalone.) Luigi ------------------------------------------------------------------------------ The Next 800 Companies to Lead America's Growth: New Video Whitepaper David G. Thomson, author of the best-selling book "Blueprint to a Billion" shares his insights and actions to help propel your business during the next growth cycle. Listen Now! http://p.sf.net/sfu/SAP-dev2dev _______________________________________________ QuantLib-dev mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-dev |
On Sat, 6 Nov 2010, Luigi Ballabio wrote:
>> I am trying to use the quantlib library from Matlab. As a test case, I >> adapted the EquityOption.cpp example code from the quantlib webpage to a >> cpp >> file which would generate the price of an American Put option with >> parameters >> given by the user, using a few different pricing engines. Before writing >> the >> mex, I had created a standalone version which accepted input from std::cin, >> and >> this ran fine. >> However, when I run the code from matlab, the results always come back as >> 0. I > > It sounds like the library thinks the instrument is expired and skips the > calculation. > Are you settings the evaluation date correctly? (Look for where > Settings::instance().evaluationDate() is set in the standalone.) I do not think that is the problem. I tried an even simpler version of the code, quoted here: #ifdef IS_MATLAB #include <mex.h> #endif // the only header you need to use QuantLib #include <ql/quantlib.hpp> #include <iostream> #include <iomanip> using namespace QuantLib; #if defined(QL_ENABLE_SESSIONS) namespace QuantLib { Integer sessionId() { return 0; } } #endif #ifdef IS_MATLAB void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) { #else int main(int argc, char* argv[]) { #endif // set up dates Calendar calendar = TARGET(); Date todaysDate(15, May, 1998); Date settlementDate(17, May, 1998); Settings::instance().evaluationDate() = todaysDate; // our options Option::Type type(Option::Put); Real underlying = 36; Real strike = 40; Spread dividendYield = 0.00; Rate riskFreeRate = 0.06; Volatility volatility = 0.20; Date maturity(17, May, 1999); DayCounter dayCounter = Actual365Fixed(); std::cout << "Option type = " << type << std::endl; std::cout << "Maturity = " << maturity << std::endl; std::cout << "Underlying price = " << underlying << std::endl; std::cout << "Strike = " << strike << std::endl; std::cout << "Risk-free interest rate = " << io::rate(riskFreeRate) << std::endl; std::cout << "Dividend yield = " << io::rate(dividendYield) << std::endl; std::cout << "Volatility = " << io::volatility(volatility) << std::endl; std::string method; boost::shared_ptr<Exercise> europeanExercise( new EuropeanExercise(maturity)); Handle<Quote> underlyingH( boost::shared_ptr<Quote>(new SimpleQuote(underlying))); // bootstrap the yield/dividend/vol curves Handle<YieldTermStructure> flatTermStructure( boost::shared_ptr<YieldTermStructure>( new FlatForward(settlementDate, riskFreeRate, dayCounter))); Handle<YieldTermStructure> flatDividendTS( boost::shared_ptr<YieldTermStructure>( new FlatForward(settlementDate, dividendYield, dayCounter))); Handle<BlackVolTermStructure> flatVolTS( boost::shared_ptr<BlackVolTermStructure>( new BlackConstantVol(settlementDate, calendar, volatility, dayCounter))); boost::shared_ptr<StrikedTypePayoff> payoff( new PlainVanillaPayoff(type, strike)); boost::shared_ptr<BlackScholesMertonProcess> bsmProcess( new BlackScholesMertonProcess(underlyingH, flatDividendTS, flatTermStructure, flatVolTS)); // options VanillaOption europeanOption(payoff, europeanExercise); // Analytic formulas: // Black-Scholes for European method = "Black-Scholes"; europeanOption.setPricingEngine(boost::shared_ptr<PricingEngine>( new AnalyticEuropeanEngine(bsmProcess))); std::cout << method << ": " << europeanOption.NPV() << std::endl; #ifdef IS_MATLAB return; #else return 1; #endif } when I compile this as a standalone using the -UIS_MATLAB compiler flag, and run it, I get the results: Option type = Put Maturity = May 17th, 1999 Underlying price = 36 Strike = 40 Risk-free interest rate = 6.000000 % Dividend yield = 0.000000 % Volatility = 20.000000 % Black-Scholes: 3.84431 when I compile with -DIS_MATLAB into a Matlab mex file, and run it from within Matlab, I get the following: Option type = Put Maturity = May 17th, 1999 Underlying price = 36.000000 Strike = 40.000000 Risk-free interest rate = 6.000000 % Dividend yield = 0.000000 % Volatility = 20.000000 % Black-Scholes: 0.000000 everything is the same except the results from NPV(), and the representation of the price and strike (e.g. 36 vs. 36.000000). --sep [ Steven E. Pav {bikes/bitters/linux} nerd ] [ google voice: 909.SHBYCHF shabbychef a.t gmail d0t com ] [ parent of tinyurl.com/oskarpix ] [ a palindrome: zoos sure Russo Oz ] ------------------------------------------------------------------------------ The Next 800 Companies to Lead America's Growth: New Video Whitepaper David G. Thomson, author of the best-selling book "Blueprint to a Billion" shares his insights and actions to help propel your business during the next growth cycle. Listen Now! http://p.sf.net/sfu/SAP-dev2dev _______________________________________________ QuantLib-dev mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-dev |
On Mon, 2010-11-08 at 09:47 -0800, steven e. pav wrote:
> > It sounds like the library thinks the instrument is expired and skips the > > calculation. > > Are you settings the evaluation date correctly? (Look for where > > Settings::instance().evaluationDate() is set in the standalone.) > > I do not think that is the problem. I tried an even simpler version of the > code [...] Hmm. In this case, I'm afraid I have no idea. You can try stepping through the code in a debugger, if Matlab allows it, or at least adding some print statement in the performCalculation() method of the instrument and the calculate() method of the engine to try and see what's happening... Luigi -- There are two ways to write error-free programs; only the third one works. -- unknown ------------------------------------------------------------------------------ Centralized Desktop Delivery: Dell and VMware Reference Architecture Simplifying enterprise desktop deployment and management using Dell EqualLogic storage and VMware View: A highly scalable, end-to-end client virtualization framework. Read more! http://p.sf.net/sfu/dell-eql-dev2dev _______________________________________________ QuantLib-dev mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-dev |
Free forum by Nabble | Edit this page |