Last updated: 2023-03-16.
tf_quant_finance.models.milstein_sampling.sample#
Returns a sample paths from the process using the Milstein method.
tf_quant_finance.models.milstein_sampling.sample(
*, dim, drift_fn, volatility_fn, times, time_step=None, num_time_steps=None,
num_samples=1, initial_state=None, grad_volatility_fn=None, random_type=None,
seed=None, swap_memory=True, skip=0, precompute_normal_draws=True,
watch_params=None, stratonovich_order=5, dtype=None, name=None
)
For an Ito process,
dX = a(t, X_t) dt + b(t, X_t) dW_t
given drift a, volatility b and derivative of volatility b', the
Milstein method generates a
sequence {Y_n} approximating X
Y_{n+1} = Y_n + a(t_n, Y_n) dt + b(t_n, Y_n) dW_n + \frac{1}{2} b(t_n, Y_n)
b'(t_n, Y_n) ((dW_n)^2 - dt)
where dt = t_{n+1} - t_n, dW_n = (N(0, t_{n+1}) - N(0, t_n)) and N is a
sample from the Normal distribution.
In higher dimensions, when a(t, X_t) is a d-dimensional vector valued
function and W_t is a d-dimensional Wiener process, we have for the kth
element of the expansion:
Y_{n+1}[k] = Y_n[k] + a(t_n, Y_n)[k] dt + \sum_{j=1}^d b(t_n, Y_n)[k, j]
dW_n[j] + \sum_{j_1=1}^d \sum_{j_2=1}^d L_{j_1} b(t_n, Y_n)[k, j_2] I(j_1,
j_2)
where L_{j} = \sum_{i=1}^d b(t_n, Y_n)[i, j] \frac{\partial}{\partial x^i}
is an operator and I(j_1, j_2) = \int_{t_n}^{t_{n+1}} \int_{t_n}^{s_1} dW_{s_2}[j_1] dW_{s_1}[j_2] is a multiple Ito integral.
See [1] and [2] for details.
References#
[1]: Wikipedia. Milstein method: https://en.wikipedia.org/wiki/Milstein_method [2]: Peter E. Kloeden, Eckhard Platen. Numerical Solution of Stochastic Differential Equations. Springer. 1992
Args:#
dim: Python int greater than or equal to 1. The dimension of the Ito Process.drift_fn: A Python callable to compute the drift of the process. The callable should accept two realTensorarguments of the same dtype. The first argument is the scalar time t, the second argument is the value of Ito process X - tensor of shapebatch_shape + [dim]. The result is value of drift a(t, X). The return value of the callable is a realTensorof the same dtype as the input arguments and of shapebatch_shape + [dim].volatility_fn: A Python callable to compute the volatility of the process. The callable should accept two realTensorarguments of the same dtype astimes. The first argument is the scalar time t, the second argument is the value of Ito process X - tensor of shapebatch_shape + [dim]. The result is value of volatility b(t, X). The return value of the callable is a realTensorof the same dtype as the input arguments and of shapebatch_shape + [dim, dim].times: Rank 1Tensorof increasing positive real values. The times at which the path points are to be evaluated.time_step: An optional scalar realTensor- maximal distance between points in grid in Milstein schema. Either this ornum_time_stepsshould be supplied. Default value:None.num_time_steps: An optional Scalar integerTensor- a total number of time steps performed by the algorithm. The maximal distance between points in grid is bounded bytimes[-1] / (num_time_steps - times.shape[0]). Either this ortime_stepshould be supplied. Default value:None.num_samples: Positive scalarint. The number of paths to draw. Default value: 1.initial_state:Tensorof shape[dim]. The initial state of the process. Default value: None which maps to a zero initial state.grad_volatility_fn: An optional python callable to compute the gradient ofvolatility_fn. The callable should accept three realTensorarguments of the same dtype astimes. The first argument is the scalar time t. The second argument is the value of Ito process X - tensor of shapebatch_shape + [dim]. The third argument is a tensor of input gradients of shapebatch_shape + [dim]to pass togradient.fwd_gradient. The result is a list of values corresponding to the forward gradient of volatility b(t, X) with respect to X. The return value of the callable is a list of sizedimcontaining realTensors of the same dtype as the input arguments and of shapebatch_shape + [dim, dim]. Each index of the list corresponds to a dimension of the state. IfNone, the gradient is computed fromvolatility_fnusing forward differentiation.random_type: Enum value ofRandomType. The type of (quasi)-random number generator to use to generate the paths. Default value: None which maps to the standard pseudo-random numbers.seed: Seed for the random number generator. The seed is only relevant ifrandom_typeis one of[STATELESS, PSEUDO, HALTON_RANDOMIZED, PSEUDO_ANTITHETIC, STATELESS_ANTITHETIC]. ForPSEUDO,PSEUDO_ANTITHETICandHALTON_RANDOMIZEDthe seed should be a Python integer. ForSTATELESSandSTATELESS_ANTITHETICmust be supplied as an integerTensorof shape[2]. Default value:Nonewhich means no seed is set.swap_memory: A Python bool. Whether GPU-CPU memory swap is enabled for this op. See an equivalent flag intf.while_loopdocumentation for more details. Useful when computing a gradient of the op sincetf.while_loopis used to propagate stochastic process in time. Default value: True.skip:int320-dTensor. The number of initial points of the Sobol or Halton sequence to skip. Used only whenrandom_typeis ‘SOBOL’, ‘HALTON’, or ‘HALTON_RANDOMIZED’, otherwise ignored. Default value:0.precompute_normal_draws: Python bool. Indicates whether the noise incrementsN(0, t_{n+1}) - N(0, t_n)are precomputed. ForHALTONandSOBOLrandom types the increments are always precomputed. While the resulting graph consumes more memory, the performance gains might be significant. Default value:True.watch_params: An optional list of zero-dimensionalTensors of the samedtypeasinitial_state. If provided, specifiesTensors with respect to which the differentiation of the sampling function will happen. A more efficient algorithm is used whenwatch_paramsare specified. Note the the function becomes differentiable only wrt to theseTensors and theinitial_state. The gradient wrt any otherTensoris set to be zero.stratonovich_order: A positive integer. The number of terms to use when calculating the approximate Stratonovich integrals in the multidimensional scheme. Stratonovich integrals are an alternative to Ito integrals, and can be used interchangeably when defining the higher order terms in the update equation. We use Stratonovich integrals here because they have a convenient approximation scheme for calculating cross terms involving different components of the Wiener process. See Eq. 8.10 in Section 5.8 of [2]. Default value:5.dtype:tf.Dtype. If supplied the dtype for the input and outputTensors. Default value: None which means that the dtype implied bytimesis used.name: Python string. The name to give this op. Default value:Nonewhich maps tomilstein_sample.