AnnuitySolve Examples

Start up

Off[General::spell1]

<< AnnuitySolve`

Example 1: Getting started. One-factor model

Setup

\(\(pde\  = \ \ k \((m - x)\) D[A[x, t], \ x]\  + \ 
          x\ s^2/2\ D[A[x, t], \ {x, \ 2}]\  - 
	D[A[x, t], \ t]\  - \ 
          x\ A[x, t]\  + \ ν\ x\ s^2/2\ D[A[x, t], \ x]^2/
              A[x, t];\)
\)
Npde[
          k_,  ν_]  =   pde  + 1 /.  {m  ->  1/20,  
            s  ->  1/5};
\(\(Nbondpde[k_, \ ν_]\  = \ 
        pde\  /. \ {m\  -> \ 1/20, \ s\  -> \ 1/5};\)
\)
x0  =  1/20;

Solve

First part: Getting started

Solve linear pde with k = 1, 3rd-order polynomial

Asoln  =  
        NAnnuitySolve[Npde[1,  0],  
          A,  {t,  0,  1000},  {x,  x0},   3];

Plot the solution

Plot3D[
        Asoln[x, t],  {x,  0,   .2},  {t,  0,  1000}];

How well does the solution fit the pde?

resid  =  ResidualFromPDE[Npde[1,  0],   Asoln];
Plot3D[
        Abs[resid[x, t]],  {x,  0,   .2},  {t,  0,  
          1000}];

Second part: More complicated

Loop over values of k. (Series solution with low k converges slowly.)

\(\(Atab\  = \ 
        Table[
	
          NAnnuitySolve[Npde[10^\((\(-i\))\), \ 0], \ 
            A, \ {t, \ 0, \ 1000}, {x, x0}, \ \ order], 
	{i, \ 0, \ 
            3}, \ {order, \ 1, \ 5}];\)\)

Get annuity series from bond prices

Asoln  =  
        NAnnuitySolve[Npde[1/1000,  0],  
          A,   {t,  0,  1000},  {x,  x0}, 3];
resid  =  ResidualFromPDE[Npde[1/1000, 0],  Asoln];
\(\(Bsoln\  = \ 
        NAnnuitySolve[Nbondpde[1/1000, 0], \ 
          A, \ \ {t, \ 0, \ 1000}, {x, x0}, \ 1, 
	
          FunctionalForm\  -> \ Exp];\)\)
Bcoeffs  =  
        NBondToAnnuitySeries[Bsoln[x, t], {t,  0,  1000},  {x, x0},  
          5];

Use Bcoeffs to fix series solution

\(\(Asoln1\  = \ 
        NAnnuitySolve[Npde[1/1000, 0], \ A, \ {t, \ 0, \ 1000}, \ {x, x0}, 
          3, 
	PolynomialOrderDifferential\  -> \ 2, \ 
          DifferentialLoadings\  -> \ Bcoeffs];\)\)
resid1  =  
        ResidualFromPDE[Npde[1/1000, 0],  Asoln1];

Compare the two solutions

Plot3D[
        Asoln[x, t],  {x,  0,   .2},  {t,  0,  1000}];
Plot3D[
        Asoln1[x, t],  {x,  0,   .2},  {t,  0,  1000}];
Plot3D[
        Abs[resid[x, t]],  {x,  0,   .2},  {t,  0,  1000}];
Plot3D[
        Abs[resid1[x, t]],  {x,  0,   .2},  {t,  0,  
          1000}];

Example 2: Two-factor model

Setup

\(\(pde2\  = \ \ 1\  - \ \((x1\  + \ x2)\)*
            A[x1, \ x2, \ t]\  - \ \(\(Derivative[0, \ 0, \ 1]\)[A]\)[x1, \ 
            x2, \ t]\  + 
\ \ \((k2*\((m2\  - \ x2)\)\  - \ 
                sX2*x2*λ2)\)*\(\(Derivative[0, \ 1, \ 0]\)[A]\)[x1, \ 
              x2, \ t]\  + 
\ \ \((k1*\((m1\  - \ x1)\)\  - \ 
                sX1*x1*λ1)\)*\(\(Derivative[1, \ 0, \ 0]\)[A]\)[x1, \ 
              x2, \ t]\  - 
\ \ \((ν*\((\((sX2^2*
                          x2*\(\(Derivative[0, \ 1, \ 0]\)[A]\)[x1, \ x2, \ 
                              t]^2)\)/
                      A[x1, \ x2, \ t]^2\  + 
\ \ \((sX1^2*
                          x1*\(\(Derivative[1, \ 0, \ 0]\)[A]\)[x1, \ x2, \ 
                              t]^2)\)/A[x1, \ x2, \ t]^2)\))\)/
            2\  + 
\ \ \((sX2^2*
                  x2*\(\(Derivative[0, \ 2, \ 0]\)[A]\)[x1, \ x2, \ 
                    t]\  + 
\ \ \ \ \ sX1^2*
                  x1*\(\(Derivative[2, \ 0, \ 0]\)[A]\)[x1, \ x2, \ t])\)/
            2\ ;\)
\)
\(\(nrule2\  = \ {k1\  -> \ 97/125, \ m1\  -> \ 3209/100000, \ 
          sX1\  -> \ 82/625, 
\ \ λ1\  -> \ \(-593\)/5000, \ 
          k2\  -> \ 8991/10000000, 
\ \ m2\  -> \ 1061/50000, \ 
          sX2\  -> \ 2657/50000, 
\ \ λ2\  -> \ \(-4151\)/100000, \ 
          k3\  -> \ 3/500, \ 
          m3\  -> \ 1/100, 
\ \ sX3\  -> \ 
            9/50, \ λ3\  -> \ \(-13\)/100, \ ν\  -> \ 
            0}\ ;\)
\)
npde2  =  pde2  /.  nrule2;
xargs  =  
        Sequence  @@  (Transpose[{{x1,  x2},  {m1,  m2}}]  /.  
              nrule2);

Solve

Asoln2  =  
        NAnnuitySolve[npde2,  A,  {t,  0,  10000},  xargs,  
          3];

Get annuity series from bond prices

Bsoln2  =  
        NAnnuitySolve[npde2  -  1,  A, {t,  0,  1000},  xargs,  1,  
          FunctionalForm  ->  Exp];
Bcoeffs2  =  
        NBondToAnnuitySeries[Bsoln2[x1,  x2,  t],  {t,  0,  1000},  
          xargs,  5];

Use Bcoeffs to fix series solution

\(\(Asoln3\  = \ 
        NAnnuitySolve[npde2, \ A, {t, \ 0, \ 1000}, xargs, \ 3, \ 
          PolynomialOrderDifferential\  -> \ 2, 
	
          DifferentialLoadings\  -> \ Bcoeffs2];\)\)

Compare with direct bond series at t = 1000

\(\(bondseries\  = \ 
        MakePolynomial[{x1, x2} - {m1, m2}\  /. \ nrule2, \ t, 
	5]\  /. \ 
          Bcoeffs2;\)\)
\(\(Plot3D[
        Asoln3[x1, \ x2, \ 1000]\  - \ bondseries\  /. \ 
          t\  -> \ 1000, 
	{x1, \ 0, \  .2}, \ {x2, \ 
          0, \  .2}];\)\)

Example 3: Symbolic solution to Vasicek bond prices.

\(\(pde\  = \ 
        k \((m - x)\) D[B[x, t], \ x]\  + \ 
          s^2/2\ D[B[x, t], \ {x, \ 2}]\  - 
	D[B[x, t], \ t]\  - \ 
          x\ B[x, t];\)\)

Bsoln  =  
      AnnuitySolve[pde,  B,  t,  {x,  m},   1,  
        FunctionalForm  ->  Exp]

ResidualFromPDE[pde,  Bsoln][x, t] // Simplify

BondToAnnuitySeries[Bsoln[x, t] /. s -> 0,  t,  {x,  m},  
      2]

Example 4: Symbolic solution for 1st 3 terms for annuity with deterministic state variable.

Compare this to the last part of the previous example.

pde  =  
        1  +  k (m - x) D[A[x, t],  x]  -  D[A[x, t],  t]  -  
          x A[x, t];

Asoln  =  
      Simplify  /@  AnnuitySolve[pde,  A,  t, {x,  m},  2][x, 
          t]