Login  Register

Re: using quantlib in Matlab

Posted by steven e. pav on Nov 08, 2010; 5:47pm
URL: http://quantlib.414.s1.nabble.com/using-quantlib-in-Matlab-tp9078p9080.html

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