qdpmc.engine.monte_carlo.MonteCarlo

class qdpmc.engine.monte_carlo.MonteCarlo(batch_size: int, num_iter: int, caller=None)[source]

Bases: object

A Monte Carlo engine for valuing path-dependent options.

Parameters regarding the simulation are specified here. This engine implements vectorization, which significantly enhances algorithm speed.

Parameters
  • batch_size (int) – An integer telling the engine how many paths should be generated in each iteration.

  • num_iter (int) – Number of iterations. The product of batch_size and num_iter is the total number of paths generated.

Methods

__init__(batch_size, num_iter[, caller])

A Monte Carlo engine for valuing path-dependent options.

calc(option, process[, request_greeks, …])

Calculates the present value of an option given a market process.

single_iter_caller(option, process[, …])

This returns a function that takes seed as its sole parameter.

Attributes

caller

Default caller used to implement Monte Carlo simulation.

most_recent_entropy

The most recently used entropy.

calc(option: qdpmc.structures.base.StructureMC, process: qdpmc.model.market_process.BlackScholes, request_greeks=False, fd_steps=None, fd_scheme=None, entropy=None, caller=None, caller_args=None)[source]

Calculates the present value of an option given a market process.

Parameters
  • option – The option structure that needs to be calculated.

  • process – An object containing information about the process driving the dynamics of the underlying asset. Currently this value must be a BlackScholes object.

  • request_greeks (bool) – Whether to calculate and return Greek letters.

  • fd_steps (dict) – A dictionary controlling steps of finite difference. Default is None, corresponding to {“ds”: 0.01, dr: “0.0001”, dv: “0.01”}. Note that ds is in log scale.

  • fd_scheme (dict) – Scheme of finite difference for calculating Greek letters. Default is {“Delta”: “central”, “Rho”: “central”, “Vega”: “central”}. This does not affect Gamma, which is always calculated using the central difference scheme.

  • entropy (int or None) – Controls random numbers. Must be an integer.

  • caller (callable()) – caller used to implement computation. Default is None, corresponding to self.caller. caller must take at least two arguments: caller(calc_once_give_seed, seed list, …). Moreover, caller should return a list of results, not a scalar. See below for more details.

  • caller_args (dict) – parameters passed to caller. Default is None, equivalent to dict(n_jobs=cpu_counts).

Returns

scalar or dict – PV or dict of Greek letters.

Examples

The default caller is roughly equivalent to:

def caller(calc, seedsequence, /, **kwargs):
    calc = joblib.delayed(calc)
    if "n_jobs" not in kwargs:
        kwargs["n_jobs"] = cpu_counts()
    with joblib.Parallel(**kwargs) as parallel:
        res = parallel(calc(seed) for seed in seedsequence)
    return res

To implement Monte Carlo with your own caller, for example one with no parallel computation, define the caller as follows:

def mycaller(calc, seedsequence):
    return [calc(s) for s in seedsequence]

You may also set the default caller so you do not need to specify caller every time calc is called.

mc.caller = mycaller

To revert to the joblib caller, delete the caller or set it to None:

del mc.caller
mc.caller == None  # True
property caller

Default caller used to implement Monte Carlo simulation. if set to None, joblib.Parallel will be used.

property most_recent_entropy

The most recently used entropy.

single_iter_caller(option: qdpmc.structures.base.StructureMC, process: qdpmc.model.market_process.BlackScholes, request_greeks=False, fd_steps=None, fd_scheme=None)[source]

This returns a function that takes seed as its sole parameter. Given a random seed, it generates random numbers, computes requested values, and returns them.