Why my explicit FDM code isn't working?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Why my explicit FDM code isn't working?

Student T
Hi,

I attach my entire code at the end of the mail. I want to try the explicit FDM scheme to price a simple option. It worked if I change my code from `ExplictEuler` to `CrankNicolson`, but it reports NA for the option NPV if I used the explicit scheme.

Why can't I simply change to the explicit scheme?





#include <ql/quantlib.hpp>


using namespace QuantLib;


boost::shared_ptr<YieldTermStructure> flatRate(const Date& today, const boost::shared_ptr<Quote>& forward, const DayCounter& dc)

{

    return boost::shared_ptr<YieldTermStructure>(new FlatForward(today, Handle<Quote>(forward), dc));

}


boost::shared_ptr<YieldTermStructure> flatRate(const Date& today, Rate forward, const DayCounter& dc)

{

    return flatRate(today, boost::shared_ptr<Quote>(new SimpleQuote(forward)), dc);

}


boost::shared_ptr<YieldTermStructure> flatRate(const boost::shared_ptr<Quote>& forward, const DayCounter& dc)

{

    return boost::shared_ptr<YieldTermStructure>(new FlatForward(0, NullCalendar(), Handle<Quote>(forward), dc));

}


boost::shared_ptr<YieldTermStructure> flatRate(Rate forward, const DayCounter& dc)

{

    return flatRate(boost::shared_ptr<Quote>(new SimpleQuote(forward)), dc);

}


boost::shared_ptr<GeneralizedBlackScholesProcess>

    makeProcess(const boost::shared_ptr<Quote>& u,

            const boost::shared_ptr<YieldTermStructure>& q,

            const boost::shared_ptr<YieldTermStructure>& r,

            const boost::shared_ptr<BlackVolTermStructure>& vol)

{

    return boost::shared_ptr<BlackScholesMertonProcess>(

            new BlackScholesMertonProcess(Handle<Quote>(u),

                                          Handle<YieldTermStructure>(q),

                                          Handle<YieldTermStructure>(r),

                                          Handle<BlackVolTermStructure>(vol)));

}


boost::shared_ptr<BlackVolTermStructure> flatVol(const Date& today, const boost::shared_ptr<Quote>& vol, const DayCounter& dc)

{

    return boost::shared_ptr<BlackVolTermStructure>(new BlackConstantVol(today, NullCalendar(), Handle<Quote>(vol), dc));

}


boost::shared_ptr<BlackVolTermStructure> flatVol(const Date& today, Volatility vol, const DayCounter& dc)

{

    return flatVol(today, boost::shared_ptr<Quote>(new SimpleQuote(vol)), dc);

}


boost::shared_ptr<BlackVolTermStructure> flatVol(const boost::shared_ptr<Quote>& vol, const DayCounter& dc)

{

    return boost::shared_ptr<BlackVolTermStructure>(new BlackConstantVol(0, NullCalendar(), Handle<Quote>(vol), dc));

}


boost::shared_ptr<BlackVolTermStructure> flatVol(Volatility vol, const DayCounter& dc)

{

    return flatVol(boost::shared_ptr<Quote>(new SimpleQuote(vol)), dc);

}


int main(int argc, char ** argv)

{

    Date today = Date(1, Jan, 2015);


    Settings::instance().evaluationDate() = today;

    

    boost::shared_ptr<SimpleQuote> spot(new SimpleQuote(100.0));

    boost::shared_ptr<YieldTermStructure> qTS    = flatRate(today, 0.05, Actual360());

    boost::shared_ptr<YieldTermStructure> rTS    = flatRate(today, 0.05, Actual360());

    boost::shared_ptr<BlackVolTermStructure> vTS = flatVol (today, 0.05, Actual360());


    boost::shared_ptr<GeneralizedBlackScholesProcess> stochProcess = makeProcess(spot, qTS, rTS, vTS);

    

    boost::shared_ptr<PricingEngine> engine = boost::shared_ptr<PricingEngine>(

            new FDEuropeanEngine<ExplicitEuler>(stochProcess, 1000, 1000));


    boost::shared_ptr<StrikedTypePayoff> payoff(new PlainVanillaPayoff(Option::Call, 100.0));

    boost::shared_ptr<Exercise> exercise(new EuropeanExercise(Date(1, Jan, 2016)));

    

    EuropeanOption option(payoff, exercise);

    option.setPricingEngine(engine);


    std::cout << option.NPV() << std::endl;

}


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Why my explicit FDM code isn't working?

Peter Caspers-4
Hi Ted,

explicit Euler is known to be unstable (exploding solutions) for certain discretizations. Others may work (like 100,100 instead of 1000,1000) but it’s probably not best practice to use this scheme anyway. So it has nothing to do with QL or your code, it just confirms the theory.

Regards
Peter

Am 17.02.2016 um 14:07 schrieb Ted Wong <[hidden email]>:

Hi,

I attach my entire code at the end of the mail. I want to try the explicit FDM scheme to price a simple option. It worked if I change my code from `ExplictEuler` to `CrankNicolson`, but it reports NA for the option NPV if I used the explicit scheme.

Why can't I simply change to the explicit scheme?





#include <ql/quantlib.hpp>


using namespace QuantLib;


boost::shared_ptr<YieldTermStructure> flatRate(const Date& today, const boost::shared_ptr<Quote>& forward, const DayCounter& dc)

{

    return boost::shared_ptr<YieldTermStructure>(new FlatForward(today, Handle<Quote>(forward), dc));

}


boost::shared_ptr<YieldTermStructure> flatRate(const Date& today, Rate forward, const DayCounter& dc)

{

    return flatRate(today, boost::shared_ptr<Quote>(new SimpleQuote(forward)), dc);

}


boost::shared_ptr<YieldTermStructure> flatRate(const boost::shared_ptr<Quote>& forward, const DayCounter& dc)

{

    return boost::shared_ptr<YieldTermStructure>(new FlatForward(0, NullCalendar(), Handle<Quote>(forward), dc));

}


boost::shared_ptr<YieldTermStructure> flatRate(Rate forward, const DayCounter& dc)

{

    return flatRate(boost::shared_ptr<Quote>(new SimpleQuote(forward)), dc);

}


boost::shared_ptr<GeneralizedBlackScholesProcess>

    makeProcess(const boost::shared_ptr<Quote>& u,

            const boost::shared_ptr<YieldTermStructure>& q,

            const boost::shared_ptr<YieldTermStructure>& r,

            const boost::shared_ptr<BlackVolTermStructure>& vol)

{

    return boost::shared_ptr<BlackScholesMertonProcess>(

            new BlackScholesMertonProcess(Handle<Quote>(u),

                                          Handle<YieldTermStructure>(q),

                                          Handle<YieldTermStructure>(r),

                                          Handle<BlackVolTermStructure>(vol)));

}


boost::shared_ptr<BlackVolTermStructure> flatVol(const Date& today, const boost::shared_ptr<Quote>& vol, const DayCounter& dc)

{

    return boost::shared_ptr<BlackVolTermStructure>(new BlackConstantVol(today, NullCalendar(), Handle<Quote>(vol), dc));

}


boost::shared_ptr<BlackVolTermStructure> flatVol(const Date& today, Volatility vol, const DayCounter& dc)

{

    return flatVol(today, boost::shared_ptr<Quote>(new SimpleQuote(vol)), dc);

}


boost::shared_ptr<BlackVolTermStructure> flatVol(const boost::shared_ptr<Quote>& vol, const DayCounter& dc)

{

    return boost::shared_ptr<BlackVolTermStructure>(new BlackConstantVol(0, NullCalendar(), Handle<Quote>(vol), dc));

}


boost::shared_ptr<BlackVolTermStructure> flatVol(Volatility vol, const DayCounter& dc)

{

    return flatVol(boost::shared_ptr<Quote>(new SimpleQuote(vol)), dc);

}


int main(int argc, char ** argv)

{

    Date today = Date(1, Jan, 2015);


    Settings::instance().evaluationDate() = today;

    

    boost::shared_ptr<SimpleQuote> spot(new SimpleQuote(100.0));

    boost::shared_ptr<YieldTermStructure> qTS    = flatRate(today, 0.05, Actual360());

    boost::shared_ptr<YieldTermStructure> rTS    = flatRate(today, 0.05, Actual360());

    boost::shared_ptr<BlackVolTermStructure> vTS = flatVol (today, 0.05, Actual360());


    boost::shared_ptr<GeneralizedBlackScholesProcess> stochProcess = makeProcess(spot, qTS, rTS, vTS);

    

    boost::shared_ptr<PricingEngine> engine = boost::shared_ptr<PricingEngine>(

            new FDEuropeanEngine<ExplicitEuler>(stochProcess, 1000, 1000));


    boost::shared_ptr<StrikedTypePayoff> payoff(new PlainVanillaPayoff(Option::Call, 100.0));

    boost::shared_ptr<Exercise> exercise(new EuropeanExercise(Date(1, Jan, 2016)));

    

    EuropeanOption option(payoff, exercise);

    option.setPricingEngine(engine);


    std::cout << option.NPV() << std::endl;

}

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users