API Documentation

Timewave Engine

Engine This class implements Monte Carlo engine
State simulation state
Producer abstract class implementing simple producer for a model between grid dates
Consumer base class for simulation consumers

module containing simulation method related classes incl. multiprocessing support

class timewave.engine.Producer(func=None, initial_state=None)[source]

Bases: object

abstract class implementing simple producer for a model between grid dates

initialize(grid, num_of_paths, seed)[source]

inits producer for a simulation run

initialize_worker(process_num=None)[source]

inits producer for a simulation run on a single process

initialize_path(path_num=None)[source]

inits producer for next path, i.e. sets current state to initial state

evolve(new_date)[source]

evolve to the new process state at the next date, i.e. do one step in the simulation

Parameters:new_date (date) – date of the new state
Return State:
class timewave.engine.State(value=0.0)[source]

Bases: object

simulation state

class timewave.engine.Engine(producer=None, consumer=None)[source]

Bases: object

This class implements Monte Carlo engine

run(grid=None, num_of_paths=2000, seed=0, num_of_workers=4, profiling=False)[source]

implements simulation

Parameters:
  • grid (list(date)) – list of Monte Carlo grid dates
  • num_of_paths (int) – number of Monte Carlo paths
  • seed (hashable) – seed used for rnds initialisation (additional adjustment in place)
  • or None num_of_workers (int) – number of parallel workers (default: cpu_count()), if None no parallel processing is used
  • profiling (bool) – signal whether to use profiling, True means used, else not
Return object:

final consumer state

It returns a list of lists. The list contains per path a list produced by consumer at observation dates

class timewave.engine.Consumer(func=None)[source]

Bases: object

base class for simulation consumers

initiatlizes consumer by providing a function :param func: consumer function with exact 1 argument which will consume the producer state. Default will return state.value

initialize(grid=None, num_of_paths=None, seed=None)[source]

initialize consumer for simulation :param num_of_paths: number of path :type num_of_paths: int :param grid: list of grid point :type grid: list(date) :param seed: simulation seed :type seed: hashable

initialize_worker(process_num=None)[source]

reinitialize consumer for process in multiprocesing

initialize_path(path_num=None)[source]

initialize consumer for next path

consume(state)[source]

consume new producer state

finalize_path(path_num=None)[source]

finalize last path for consumer

finalize_worker(process_num=None)[source]

finalize process for consumer

finalize()[source]

finalize simulation for consumer

put()[source]

to put state into multiprocessing.queue

get(queue_get)[source]

to get states from multiprocessing.queue

Timewave Producer

DeterministicProducer
StringReaderProducer
MultiProducer initializer
Inheritance diagram of timewave.producers

module containing brownian motion model related classes

class timewave.producers.MultiProducer(*producers)[source]

Bases: timewave.engine.Producer

initializer

Parameters:or produces (list(Producer)) – list of producers to be used one after another
producers = None

list of consumers to be used one after another

Type:list(Producer)
initialize(grid, num_of_paths, seed)[source]

inits producer for a simulation run

initialize_worker(process_num=None)[source]

inits producer for a simulation run on a single process

initialize_path(path_num=None)[source]

inits producer for next path, i.e. sets current state to initial state

evolve(new_date)[source]

evolve to the new process state at the next date, i.e. do one step in the simulation

Parameters:new_date (date) – date of the new state
Return State:
class timewave.producers.DeterministicProducer(sample_list, func=None, initial_state=None)[source]

Bases: timewave.engine.Producer

evolve(new_date)[source]

evolve to the new process state at the next date, i.e. do one step in the simulation

Parameters:new_date (date) – date of the new state
Return State:
class timewave.producers.StringReaderProducer(data_str, str_decoder=None)[source]

Bases: timewave.producers.DeterministicProducer

Timewave Consumer

QuietConsumer QuietConsumer returns nothing, since QuietConsumer does simply not populate result in finalize_path()
StringWriterConsumer
StackedConsumer stacked version of consumer, i.e.
ConsumerConsumer class implementing the consumer interface several consumers can be saved and are executed one after another only the result of the last consumer is returned (see finalize_worker)
MultiConsumer initializer
ResetConsumer FunctionConsumer that admits a reset function for each path
TransposedConsumer TransposedConsumer returns sample distribution per grid point not per sample path
Inheritance diagram of timewave.consumers
class timewave.consumers.QuietConsumer(func=None)[source]

Bases: timewave.engine.Consumer

QuietConsumer returns nothing, since QuietConsumer does simply not populate result in finalize_path()

initiatlizes consumer by providing a function :param func: consumer function with exact 1 argument which will consume the producer state. Default will return state.value

finalize_path(path_num=None)[source]

QuietConsumer does simply not populate result in finalize_path()

finalize()[source]

finalize simulation for consumer

class timewave.consumers.StringWriterConsumer(str_decoder=None)[source]

Bases: timewave.engine.Consumer

finalize()[source]

finalize simulation for consumer

class timewave.consumers.ResetConsumer(fixing_func=None, reset_func=None)[source]

Bases: timewave.engine.Consumer

FunctionConsumer that admits a reset function for each path

initialize_path(path_num=None)[source]

initialize consumer for next path

finalize()[source]

finalize simulation for consumer

class timewave.consumers.StackedConsumer(*consumers)[source]

Bases: timewave.engine.Consumer

stacked version of consumer, i.e. a following consumer is populated with out state of the preceding one

initialize(num_of_paths=None, grid=None, seed=None)[source]

initialize StackedConsumer

initialize_path(path_num=None)[source]

make the consumer_state ready for the next MC path

Parameters:path_num (int) –
consume(state)[source]

consume new producer state

finalize_path(path_num=None)[source]

finalize path and populate result for ConsumerConsumer

finalize()[source]

finalize for ConsumerConsumer

put()[source]

to put state into multiprocessing.queue

get(queue_get)[source]

to get states from multiprocessing.queue

class timewave.consumers.ConsumerConsumer(*consumers)[source]

Bases: timewave.engine.Consumer

class implementing the consumer interface several consumers can be saved and are executed one after another only the result of the last consumer is returned (see finalize_worker)

initializer

Parameters:list(Consumer)
initial_state = None

list of consumers to be used one after another

Type:list(Consumer)
initialize(grid=None, num_of_paths=None, seed=None)[source]

initialize ConsumerConsumer

initialize_path(path_num=None)[source]

make the consumer_state ready for the next MC path

Parameters:path_num (int) –
consume(state)[source]

returns pair containing the result of consumption and consumer state the returned state is equal to the state.get_short_rate() the returned consume state is None

Parameters:state (State) – specific process state
Return object:the new consumer state
finalize_path(path_num=None)[source]

finalize path and populate result for ConsumerConsumer

finalize()[source]

finalize for ConsumerConsumer

get(queue_get)[source]

get to given consumer states. This function is used for merging of results of parallelized MC. The first state is used for merging in place. The states must be disjoint.

Parameters:queue_get (object) – second consumer state
class timewave.consumers.MultiConsumer(*consumers)[source]

Bases: timewave.consumers.ConsumerConsumer

initializer

Parameters:list(Consumer)
consume(state)[source]

returns pair containing the result of consumption and consumer state the returned state is equal to the state.get_short_rate() the returned consume state is None

Parameters:state (State) – specific process state
Return object:the new consumer state
class timewave.consumers.TransposedConsumer(func=None)[source]

Bases: timewave.engine.Consumer

TransposedConsumer returns sample distribution per grid point not per sample path

initiatlizes consumer by providing a function :param func: consumer function with exact 1 argument which will consume the producer state. Default will return state.value

finalize()[source]

finalize for PathConsumer

Stochastic Process Simulation

Inheritance diagram of timewave.stochasticproducer
Inheritance diagram of timewave.stochasticconsumer

module containing stochastic process model producer

class timewave.stochasticproducer.GaussEvolutionFunctionProducer(func=None, initial_state=None, length=None)[source]

Bases: timewave.engine.Producer

class implementing general Gauss process between grid dates

Parameters:
  • func (callable) – evolve function, e.g. lambda x, s, e, q: x + sqrt(e - s) * q by default with x current state value, s current point in time, i.e. start point of next evolution step, e next point in time, i.e. end point of evolution step, q standard normal random number to do step
  • initial_state – initial state (value) of evolution,
  • or None length (int) – length of q as a list of Gauss random numbers, if None or 0 the evolution function func will be invoked with q not as a list but a float random number.

class implementing general Gauss process between grid dates and provides state to any evolve style function foo(x, s, e, q) with x last state, s last state time, e current point in time and q current Gauss process state

evolve(new_date)[source]

evolve to the new process state at the next date

Parameters:new_date (date) – date or point in time of the new state
Return State:
class timewave.stochasticproducer.GaussEvolutionProducer(process)[source]

Bases: timewave.stochasticproducer.GaussEvolutionFunctionProducer

producer to bring diffusion process to life

Parameters:process (StochasticProcess) – diffusion process to evolve
class timewave.stochasticproducer.CorrelatedGaussEvolutionProducer(producers, correlation=None, diffusion_driver=None)[source]

Bases: timewave.producers.MultiProducer

class implementing general correlated Gauss process between grid dates

Parameters:
  • producers (list(GaussEvolutionProducer)) – list of producers to evolve
  • or dict((StochasticProcess, StochasticProcess) (list(list(float))) – float) or None correlation: correlation matrix of underlying multivariate Gauss process of diffusion drivers. If dict keys must be pairs of diffusion drivers, diagonal and zero entries can be omitted. If not give, all drivers evolve independently.
  • or None diffusion_driver (list(StochasticProcess)) – list of diffusion drivers indexing the correlation matrix. If not given and correlation is not an IndexMatrix, e.g. comes already with list of drivers, it is assumed that each process producer has different drivers and the correlation is order in the same way.
evolve(new_date)[source]

evolve to the new process state at the next date

Parameters:new_date (date) – date or point in time of the new state
Return State:
class timewave.stochasticproducer.MultiGaussEvolutionProducer(process_list, correlation=None, diffusion_driver=None)[source]

Bases: timewave.stochasticproducer.CorrelatedGaussEvolutionProducer

class implementing multi variant GaussEvolutionProducer

class timewave.stochasticconsumer.StatisticsConsumer(func=None, statistics=None, **kwargs)[source]

Bases: timewave.consumers.TransposedConsumer

run basic statistics on storage consumer result per time slice

finalize()[source]

finalize for StatisticsConsumer

class timewave.stochasticconsumer.StochasticProcessStatisticsConsumer(func=None, statistics=None, **kwargs)[source]

Bases: timewave.stochasticconsumer.StatisticsConsumer

run basic statistics on storage consumer result as a stochastic process

finalize()[source]

finalize for StochasticProcessStatisticsConsumer

class timewave.stochasticconsumer.TimeWaveConsumer(func=None)[source]

Bases: timewave.consumers.TransposedConsumer

initiatlizes consumer by providing a function :param func: consumer function with exact 1 argument which will consume the producer state. Default will return state.value

finalize()[source]

finalize for PathConsumer

Stochastic Process Definition

stochasticprocess.base.StochasticProcess base class for stochastic process \(X\), e.g. Wiener process \(W\) or Markov chain \(M\)
stochasticprocess.base.MultivariateStochasticProcess base class for stochastic process \(X\), e.g. Wiener process \(W\) or Markov chain \(M\)
Inheritance diagram of timewave.stochasticprocess.gauss
Inheritance diagram of timewave.stochasticprocess.markovchain
class timewave.stochasticprocess.base.StochasticProcess(start)[source]

Bases: object

base class for stochastic process \(X\), e.g. Wiener process \(W\) or Markov chain \(M\)
Parameters:start – initial state \(X_0\)
classmethod random()[source]

initializes stochastic process with some randomly generated parameters

Return type:StochasticProcess
diffusion_driver

diffusion driver are the underlying dW of each process X in a SDE like dX = m dt + s dW

Return list(StochasticProcess):
 
evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
median(t)[source]
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment
stdev(t)[source]
skewness(t)[source]
kurtosis(t)[source]
class timewave.stochasticprocess.base.MultivariateStochasticProcess(start)[source]

Bases: timewave.stochasticprocess.base.StochasticProcess

base class for stochastic process \(X\), e.g. Wiener process \(W\) or Markov chain \(M\)
Parameters:start – initial state \(X_0\)
class timewave.stochasticprocess.gauss.WienerProcess(mu=0.0, sigma=1.0, start=0.0)[source]

Bases: timewave.stochasticprocess.base.StochasticProcess

class implementing general Gauss process between grid dates

evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
median(t)[source]
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment
class timewave.stochasticprocess.gauss.OrnsteinUhlenbeckProcess(theta=0.1, mu=0.1, sigma=0.1, start=0.0)[source]

Bases: timewave.stochasticprocess.base.StochasticProcess

class implementing Ornstein Uhlenbeck process

Parameters:
  • theta (flaot) – mean reversion speed
  • mu (float) – drift
  • sigma (float) – diffusion
  • start (float) – initial value
\[dx_t = \theta ( \mu - x_t) dt + \sigma dW_t, x_0 = a\]
evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment
class timewave.stochasticprocess.gauss.GeometricBrownianMotion(mu=0.0, sigma=1.0, start=1.0)[source]

Bases: timewave.stochasticprocess.gauss.WienerProcess

class implementing general Gauss process between grid dates

evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
median(t)[source]
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment
skewness(t)[source]
kurtosis(t)[source]
class timewave.stochasticprocess.gauss.TimeDependentParameter(parameter=0.0, time=1.0)[source]

Bases: object

integrate(s, e)[source]
class timewave.stochasticprocess.gauss.TimeDependentWienerProcess(mu=0.0, sigma=1.0, time=1.0, start=0.0)[source]

Bases: timewave.stochasticprocess.gauss.WienerProcess

class implementing a Gauss process with time depending drift and diffusion

class timewave.stochasticprocess.gauss.TimeDependentGeometricBrownianMotion(mu=0.0, sigma=1.0, time=1.0, start=1.0)[source]

Bases: timewave.stochasticprocess.gauss.GeometricBrownianMotion

class timewave.stochasticprocess.markovchain.FiniteStateMarkovChain(transition=None, r_squared=1.0, start=None)[source]

Bases: timewave.stochasticprocess.base.StochasticProcess

Parameters:
  • transition (list) – stochastic matrix of transition probabilities, i.e. np.ndarray with shape=2 and sum of each line equal to 1 (optional) default: identity matrix
  • r_squared (float) – square of systematic correlation in factor simulation (optional) default: 1.
  • start (list) – initial state distribution, i.e. np.ndarray with shape=1 or list, adding up to 1, (optional) default: unique distribution
transition
r_squared
classmethod random(d=5)[source]

initializes stochastic process with some randomly generated parameters

Return type:StochasticProcess
evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment
covariance(t)[source]
class timewave.stochasticprocess.markovchain.FiniteStateContinuousTimeMarkovChain(transition=None, r_squared=1.0, start=None)[source]

Bases: timewave.stochasticprocess.markovchain.FiniteStateMarkovChain

class timewave.stochasticprocess.markovchain.FiniteStateInhomogeneousMarkovChain(transition=None, r_squared=1.0, start=None)[source]

Bases: timewave.stochasticprocess.markovchain.FiniteStateMarkovChain

classmethod random(d=5, l=3)[source]

initializes stochastic process with some randomly generated parameters

Return type:StochasticProcess
class timewave.stochasticprocess.markovchain.AugmentedFiniteStateMarkovChain(underlying, augmentation=None)[source]

Bases: timewave.stochasticprocess.base.StochasticProcess

Parameters:
  • underlying (FiniteStateMarkovChain) – underlying Markov chain stochastic process
  • augmentation (callable) – function \(f:S \rightarrow \mathbb{R}\) defined on single states to weight augmentation (aggregate) of state distributions (optional) default: \(f=id\) Augmentation argument can be list or tuple, too. In this case __getitem__ is called.
classmethod random(d=5, augmentation=None)[source]

initializes stochastic process with some randomly generated parameters

Return type:StochasticProcess
diffusion_driver

diffusion driver are the underlying dW of each process X in a SDE like dX = m dt + s dW

Return list(StochasticProcess):
 
start
evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

eval(s)[source]
mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment
class timewave.stochasticprocess.multifactor.SABR(alpha=0.1, beta=0.2, nu=0.3, rho=-0.2, start=0.05)[source]

Bases: timewave.stochasticprocess.base.MultivariateStochasticProcess

class implementing the Hagan et al SABR model

evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment
class timewave.stochasticprocess.multifactor.MultiGauss(mu=[0.0], covar=[[1.0]], start=[0.0])[source]

Bases: timewave.stochasticprocess.base.MultivariateStochasticProcess

class implementing multi dimensional brownian motion

evolve(x, s, e, q)[source]

evolves process state x from s to e in time depending of standard normal random variable q

Parameters:
  • x (object) – current state value, i.e. value before evolution step
  • s (float) – current point in time, i.e. start point of next evolution step
  • e (float) – next point in time, i.e. end point of evolution step
  • q (float) – standard normal random number to do step
Returns:

next state value, i.e. value after evolution step

Return type:

object

mean(t)[source]

expected value of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:expected value of time \(t\) increment
variance(t)[source]

second central moment of time \(t\) increment

Parameters:t (float) –
Return type:float
Returns:variance, i.e. second central moment of time \(t\) increment