Numerical Methods in Computational Finance. Daniel J. Duffy

Чтение книги онлайн.

Читать онлайн книгу Numerical Methods in Computational Finance - Daniel J. Duffy страница 40

Numerical Methods in Computational Finance - Daniel J. Duffy

Скачать книгу

target="_blank" rel="nofollow" href="#ulink_4368aa32-4843-55fb-b2b7-cf0b2d8b2684">(3.22)normal upper Phi left-parenthesis t comma y comma h right-parenthesis equals one sixth left-bracket k 1 plus 2 k 2 plus 2 k 3 plus k 4 right-bracket

      where:

StartLayout 1st Row 1st Column k 1 2nd Column equals f left-parenthesis t comma y right-parenthesis 2nd Row 1st Column k 2 2nd Column equals f left-parenthesis t plus h slash 2 comma y plus one half italic h k 1 right-parenthesis 3rd Row 1st Column k 3 2nd Column equals f left-parenthesis t plus h slash 2 comma y plus one half italic h k 2 right-parenthesis 4th Row 1st Column k 4 2nd Column equals f left-parenthesis x plus h comma y plus italic h k 3 right-parenthesis period EndLayout

      Other methods are:

      Second-order Ralston method:

      and Heun's (improved Euler) method:

      This is a predictor-corrector method that also has applications to stochastic differential equations.

      In general, explicit Runge–Kutta methods are unsuitable for stiff ODEs.

      3.5.1 Code Samples in Python

      The following code is for the methods:

       Explicit Euler

       Fourth-order Runge–Kutta (RK4)

       Second-order Ralston.

       Heun (improved Euler)

       Predictor-corrector method.

      # TestFDMSchemes.py # # A catalogue of hand-crafted numerical methods for solving ODEs # # (C) Datasim Education BV 2021 # # Testing import math import numpy as np import FDMSchemes as fdm def func(t,y): return y*(1.0 - y) # logistic ode t = 0; y = 0.5 #initial time and value T = 4.0 #end of integration interval N = 4000 #number of divisions of [0,T] TOL = 1.0e-5 #for some method, a measure of the desired accuracy # y' = f(x,y) Explicit Euler method value = fdm.Euler(func,t,y,T,N,TOL) #scalar print(value) value = fdm.PredictorCorrector(func,t,y,T,N,TOL) #scalar print(value) value = fdm.RK4(func,t,y,T,N,TOL) #scalar print(value) y = 0.5 value = fdm.Heun(func,t,y,T,N,TOL) #scalar print(value) value = fdm.Ralston2(func,t,y,T,N,TOL) #scalar print(value)

      No doubt the code can be modified to make it more “pythonic” (whatever that means) and reduce the number of lines of code (at the possible expense of readability) if that is a requirement. We have also written these algorithms in C++11 as discussed in Duffy (2018).

       using value_type = double; using FunctionType = std::function<value_type (value_type)>; using FunctionType2 = std::function<value_type (value_type x, value_type y)>; class GeneralisedRiccati { private: FunctionType p, q, r; FunctionType2 n; public: GeneralisedRiccati(const FunctionType& P, const FunctionType& Q, const FunctionType& R, const FunctionType2& N) : p(P), q(Q), r(R), n(N) { } double operator() (double x, double y) { // Function object (functor) return p(x) + q(x)*y + r(x)*y*y

Скачать книгу