-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from tournierjc/FdmBermudanSwaptionHW
Fdm bermudan swaption hw
- Loading branch information
Showing
11 changed files
with
616 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/QLNet/Methods/Finitedifferences/Meshers/FdmSimpleProcess1dMesher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright (C) 2017 Jean-Camille Tournier (jean-camille.tournier@avivainvestors.com) | ||
This file is part of QLNet Project https://github.com/amaggiulli/qlnet | ||
QLNet is free software: you can redistribute it and/or modify it | ||
under the terms of the QLNet license. You should have received a | ||
copy of the license along with this program; if not, license is | ||
available online at <http://qlnet.sourceforge.net/License.html>. | ||
QLNet is a based on QuantLib, a free-software/open-source library | ||
for financial quantitative analysts and developers - http://quantlib.org/ | ||
The QuantLib license is available online at http://quantlib.org/license.shtml. | ||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the license for more details. | ||
*/ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace QLNet | ||
{ | ||
/// <summary> | ||
/// One-dimensional grid mesher | ||
/// </summary> | ||
public class FdmSimpleProcess1DMesher : Fdm1dMesher | ||
{ | ||
public FdmSimpleProcess1DMesher(int size, | ||
StochasticProcess1D process, | ||
double maturity, int tAvgSteps = 10, | ||
double epsilon = 0.0001, | ||
double? mandatoryPoint = null) | ||
: base(size) | ||
{ | ||
locations_ = new InitializedList<double>(locations_.Count, 0.0); | ||
for (int l=1; l<=tAvgSteps; ++l) | ||
{ | ||
double t = (maturity*l)/tAvgSteps; | ||
|
||
double mp = (mandatoryPoint != null) ? mandatoryPoint.Value | ||
: process.x0(); | ||
|
||
double qMin = Math.Min(Math.Min(mp, process.x0()), | ||
process.evolve(0, process.x0(), t, | ||
new InverseCumulativeNormal().value(epsilon))); | ||
double qMax = Math.Max(Math.Max(mp, process.x0()), | ||
process.evolve(0, process.x0(), t, | ||
new InverseCumulativeNormal().value(1 - epsilon))); | ||
|
||
double dp = (1 - 2 * epsilon) / (size - 1); | ||
double p = epsilon; | ||
locations_[0] += qMin; | ||
|
||
for (int i=1; i < size-1; ++i) | ||
{ | ||
p += dp; | ||
locations_[i] += process.evolve(0, process.x0(), t, | ||
new InverseCumulativeNormal().value(p)); | ||
} | ||
locations_[locations_.Count - 1] += qMax; | ||
} | ||
locations_ = locations_.Select(x => x / tAvgSteps).ToList(); | ||
for (int i=0; i < size-1; ++i) | ||
{ | ||
dminus_[i+1] = dplus_[i] = locations_[i+1] - locations_[i]; | ||
} | ||
|
||
dplus_[dplus_.Count - 1] = null; | ||
dminus_[0] = null; | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
src/QLNet/Methods/Finitedifferences/Operators/FdmHullWhiteOp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright (C) 2017 Jean-Camille Tournier (jean-camille.tournier@avivainvestors.com) | ||
This file is part of QLNet Project https://github.com/amaggiulli/qlnet | ||
QLNet is free software: you can redistribute it and/or modify it | ||
under the terms of the QLNet license. You should have received a | ||
copy of the license along with this program; if not, license is | ||
available online at <http://qlnet.sourceforge.net/License.html>. | ||
QLNet is a based on QuantLib, a free-software/open-source library | ||
for financial quantitative analysts and developers - http://quantlib.org/ | ||
The QuantLib license is available online at http://quantlib.org/license.shtml. | ||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the license for more details. | ||
*/ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace QLNet | ||
{ | ||
public class FdmHullWhiteOp : FdmLinearOpComposite | ||
{ | ||
public FdmHullWhiteOp(FdmMesher mesher, | ||
HullWhite model, | ||
int direction) | ||
{ | ||
x_ = mesher.locations(direction); | ||
dzMap_ = new TripleBandLinearOp(new FirstDerivativeOp(direction, mesher).mult(-1.0 * x_ * model.a()).add( | ||
new SecondDerivativeOp(direction, mesher).mult(0.5 * model.sigma() * model.sigma() | ||
* new Vector(mesher.layout().size(), 1.0)))); | ||
mapT_ = new TripleBandLinearOp(direction, mesher); | ||
direction_ = direction; | ||
model_ = model; | ||
} | ||
public override int size() { return 1; } | ||
|
||
//! Time \f$t1 <= t2\f$ is required | ||
public override void setTime(double t1, double t2) | ||
{ | ||
OneFactorModel.ShortRateDynamics dynamics = model_.dynamics(); | ||
|
||
double phi = 0.5* ( dynamics.shortRate(t1, 0.0) | ||
+ dynamics.shortRate(t2, 0.0)); | ||
|
||
mapT_.axpyb(new Vector(), dzMap_, dzMap_, -1.0 * (x_ + phi)); | ||
} | ||
|
||
public override Vector apply(Vector r) | ||
{ | ||
return mapT_.apply(r); | ||
} | ||
|
||
public override Vector apply_mixed(Vector r) { | ||
Vector retVal = new Vector(r.size(), 0.0); | ||
return retVal; | ||
} | ||
|
||
public override Vector apply_direction(int direction, Vector r) { | ||
if (direction == direction_) | ||
return mapT_.apply(r); | ||
else { | ||
Vector retVal = new Vector(r.size(), 0.0); | ||
return retVal; | ||
} | ||
} | ||
public override Vector solve_splitting(int direction, Vector r, double s) { | ||
if (direction == direction_) | ||
return mapT_.solve_splitting(r, s, 1.0); | ||
else { | ||
Vector retVal = new Vector(r.size(), 0.0); | ||
return retVal; | ||
} | ||
} | ||
public override Vector preconditioner(Vector r, double s) { return solve_splitting(direction_, r, s); } | ||
|
||
public override List<SparseMatrix> toMatrixDecomp() | ||
{ | ||
List<SparseMatrix> retVal = new InitializedList<SparseMatrix>(1, mapT_.toMatrix()); | ||
return retVal; | ||
} | ||
|
||
#region IOperator interface | ||
public override IOperator identity(int size) { return null; } | ||
public override Vector applyTo(Vector v) { return new Vector(); } | ||
public override Vector solveFor(Vector rhs) { return new Vector(); } | ||
|
||
public override IOperator multiply(double a, IOperator D) { return null; } | ||
public override IOperator add(IOperator A, IOperator B) { return null; } | ||
public override IOperator subtract(IOperator A, IOperator B) { return null; } | ||
|
||
public override bool isTimeDependent() { return false; } | ||
public override void setTime(double t) { } | ||
public override object Clone() { return this.MemberwiseClone(); } | ||
#endregion | ||
|
||
protected HullWhite model_; | ||
protected Vector x_; | ||
protected TripleBandLinearOp dzMap_; | ||
protected TripleBandLinearOp mapT_; | ||
protected int direction_; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/QLNet/Methods/Finitedifferences/Solvers/FdmHullWhiteSolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright (C) 2017 Jean-Camille Tournier (jean-camille.tournier@avivainvestors.com) | ||
This file is part of QLNet Project https://github.com/amaggiulli/qlnet | ||
QLNet is free software: you can redistribute it and/or modify it | ||
under the terms of the QLNet license. You should have received a | ||
copy of the license along with this program; if not, license is | ||
available online at <http://qlnet.sourceforge.net/License.html>. | ||
QLNet is a based on QuantLib, a free-software/open-source library | ||
for financial quantitative analysts and developers - http://quantlib.org/ | ||
The QuantLib license is available online at http://quantlib.org/license.shtml. | ||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the license for more details. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace QLNet | ||
{ | ||
public class FdmHullWhiteSolver : LazyObject | ||
{ | ||
public FdmHullWhiteSolver( | ||
Handle<HullWhite> model, | ||
FdmSolverDesc solverDesc, | ||
FdmSchemeDesc schemeDesc = null) | ||
{ | ||
solverDesc_ = solverDesc; | ||
schemeDesc_ = schemeDesc ?? new FdmSchemeDesc().Hundsdorfer(); | ||
model_ = model; | ||
model_.registerWith(update); | ||
} | ||
|
||
public double valueAt(double s) | ||
{ | ||
calculate(); | ||
return solver_.interpolateAt(s); | ||
} | ||
public double deltaAt(double s) { | ||
return 0.0; | ||
} | ||
public double gammaAt(double s) { | ||
return 0.0; | ||
} | ||
public double thetaAt(double s) { | ||
return 0.0; | ||
} | ||
|
||
protected override void performCalculations() | ||
{ | ||
FdmHullWhiteOp op = new FdmHullWhiteOp( | ||
solverDesc_.mesher, model_.currentLink(), 0); | ||
|
||
solver_ = new Fdm1DimSolver(solverDesc_, schemeDesc_, op); | ||
} | ||
|
||
protected Handle<HullWhite> model_; | ||
protected FdmSolverDesc solverDesc_; | ||
protected FdmSchemeDesc schemeDesc_; | ||
protected Fdm1DimSolver solver_; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.