|
From: Yevgeniy G. <yev...@la...> - 2022-02-16 22:35:14
|
Hi,
we're trying to use QuantLib PDE solver to calculate option prices (calls and puts). Our final code will include vol adjustment based on time, but for the first version we're trying to implement a call option price with a constant vol.
The results I'm getting don't match the expected: the red line shows intrinsic value of the option (y) depending on percent strike (x). The blue line is calculated by PDE.
[cid:image001.png@01D82358.969B6450]
Can you help finding the error?
Here's the code:
from typing import List
import QuantLib as ql
import numpy as np
import pylab as pl
class DiffusionOperator:
def __init__(self, t_max: float, x_values: List[float], cur_vals_t: List[float]):
self.__mesher = ql.FdmMesherComposite( ql.Predefined1dMesher(x_values))
self.__t_max = t_max
self.__n = len(x_values)
self.__x_values = ql.Array(x_values)
self.__cur_vals_t = ql.Array(cur_vals_t)
self.__dx = ql.FirstDerivativeOp(0, self.__mesher)
self.__ddx = ql.SecondDerivativeOp(0, self.__mesher)
self.__map = ql.TripleBandLinearOp(0, self.__mesher)
def size(self) -> int:
return 1
def setTime(self, t1: float, t2: float):
self.__map.axpyb( ql.Array(1, 1.0), self.__dx, self.__ddx.mult(self.__cur_vals_t), ql.Array())
def apply(self, r: ql.Array) -> ql.Array:
return self.__map.apply(r)
def solve_splitting(self, direction: int, r: ql.Array, dt: float) -> ql.Array:
return self.__map.solve_splitting(r, dt, 1.0)
if __name__ == "__main__":
n_points = 500; n_time_steps = 800; x_min = 0.1; x_max = 4; t_max = 0.2; bs_vol = 0.1;
x_values = np.arange(x_min, x_max + 1e-8, (x_max - x_min) / (n_points - 1))
cur_values_t = 0.5 * bs_vol**2 * x_values*x_values
y0 = np.maximum(0, 1 - x_values)
y_values = ql.Array( list(y0.copy()) )
op = DiffusionOperator(t_max, list(x_values), list(cur_values_t))
mesher = ql.FdmMesherComposite(ql.Predefined1dMesher(x_values))
solver = ql.FdmBackwardSolver(
ql.FdmLinearOpCompositeProxy(op),
ql.FdmBoundaryConditionSet(
[ql.FdmDirichletBoundary(mesher, 1, 0, ql.FdmBoundaryCondition.Lower),
ql.FdmDirichletBoundary(mesher, 0, 0, ql.FdmBoundaryCondition.Upper)] ),
ql.FdmStepConditionComposite(ql.DoubleVector(), ql.FdmStepConditionVector()),
ql.FdmSchemeDesc.CrankNicolson()
)
solver.rollback(y_values, t_max, 0.0, n_time_steps, 0)
pl.plot(x_values, y_values, '.')
pl.plot(x_values, y0, 'r-')
pl.show()
________________________________
NOTICE TO RECIPIENTS: The information in this message, including any attachments, is confidential and intended for use solely by the designated recipient(s) named above. It is the property of Laurion Capital Management LP and its affiliates ("Laurion"). If you are not the intended recipient, please return the message to the sender and delete all copies of it, including attachments, from your computer. Unauthorized use, disclosure, dissemination or copying of this message or any part hereof is strictly prohibited. No confidentiality or privilege is waived or lost by any misdirected transmission. This message is for informational purposes only. The information expressed herein may be changed at any time without notice or obligation to update. No warranty is made as to the completeness or accuracy of the information contained in this communication. Any statements or opinions made in this message do not necessarily reflect those of Laurion. This communication should not be regarded as an offer, solicitation or recommendation to sell or purchase any security or other financial product.
Email transmission cannot be guaranteed to be secure, virus-free or error-free. Therefore, we do not represent that this message is virus-free, complete or accurate and it should not be relied upon as such. Laurion accepts no liability for any damage sustained in connection with the content or transmission of this message.
All emails to and from Laurion are subject to review by certain Laurion personnel. Such communications are retained in accordance with Laurion's policies and may be produced to regulatory authorities upon request. LAURION and LAURION CAPITAL MANAGEMENT LP are registered service marks of Laurion Capital Management LP.
|