diff options
Diffstat (limited to 'utils/engine.py')
| -rw-r--r-- | utils/engine.py | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/utils/engine.py b/utils/engine.py index e64d599..f4169ec 100644 --- a/utils/engine.py +++ b/utils/engine.py @@ -10,8 +10,9 @@ Pricing engine for exotic options. import math from statistics import mean, stdev from dataclasses import dataclass -from typing import List +from typing import List, Sequence +from utils.misc import Number from utils.path import PathGenerator from utils.payoff import BasePayoff @@ -23,6 +24,14 @@ __all__ = ['MCResult', 'PricingEngine'] class MCResult: """ Price of option along with its MC error. + + Attributes + ---------- + price : float + Spot Price. + path : float + MC standard error. + """ price: float stderr: float @@ -35,42 +44,65 @@ class PricingEngine: Attributes ---------- - payoff : Payoff object for calculating the options payoff. - path : PathGenerator object for generating the evolution of the underlying. + payoff : BasePayoff + Payoff object for calculating the options payoff. + path : PathGenerator + PathGenerator object for generating the evolution of the underlying. Methods - ---------- + ------- price() + Price the option using MC techniques. Examples - ---------- + -------- >>> from utils.engine import PricingEngine >>> from utils.path import PathGenerator >>> from utils.payoff import AsianArithmeticPayOff >>> path = PathGenerator(S=100., r=0.1, div=0.01, vol=0.3) >>> payoff = AsianArithmeticPayOff(option_right='Call', K=110) >>> engine = PricingEngine(payoff=payoff, path=path) - >>> print(engine.price(T=range(4))) - MCResult(price=12.003704847790525, stderr=0.2327352760696234) + >>> print(engine) + PricingEngine(payoff=AsianArithmeticPayOff(K=110, option_right=Call), + path=PathGenerator(S=100.0, r=0.1, div=0.01, vol=0.3)) """ payoff: BasePayoff path: PathGenerator - def price(self, T: List[float], ntrials: int = 1E4, - antithetic: bool = True) -> MCResult: + def price( + self, + T: Sequence[Number], + ntrials: int = 10_000, + antithetic: bool = True + ) -> MCResult: """ Price the option using MC techniques. Parameters ---------- - T : Set of times {t1, t2, ..., tn} in years. - ntrials : Number of trials to simulate. - antithetic : Use antithetic variates technique. + T : Sequence of Numbers + Set of times {t1, t2, ..., tn} in years. + ntrials : int + Number of trials to simulate. + antithetic : bool + Use antithetic variates technique. Returns - ---------- - MCResult : Price of the option. + ------- + MCResult + Price of the option. + + Examples + -------- + >>> from utils.engine import PricingEngine + >>> from utils.path import PathGenerator + >>> from utils.payoff import AsianArithmeticPayOff + >>> path = PathGenerator(S=100., r=0.1, div=0.01, vol=0.3) + >>> payoff = AsianArithmeticPayOff(option_right='Call', K=110) + >>> engine = PricingEngine(payoff=payoff, path=path) + >>> print(engine.price(T=range(4))) + MCResult(price=12.003704847790525, stderr=0.2327352760696234) """ if ntrials < len(T): @@ -79,7 +111,7 @@ class PricingEngine: # Generation start ntrials = int(ntrials // len(T)) - payoffs = [0] * ntrials + payoffs: List[float] = [0] * ntrials for idx in range(ntrials): # Generate a random path if not antithetic: |
