Package: coindy
CoinDy - Stochastic Differential EquationsCoinDy is a package allowing the symbolic computation of Stochastic Differential Equations and their simulation for mechanical as well as Itō systems.
This package can help in deriving the complex derivative terms needed to apply explicit algorithms such as the ones described by Kloeden and Platen. It can also be used as a teaching tool, to confirm hand-derived formulas or to obtain quick results.Build Status: PassedInstallationCoinDy can be installed by using:pip install coindyIt is for now supported on Python 3.9 and later.TheoryItō SDE:
$$\mathbf{g} = \mathbf{a}dt + \mathbf{B}\mathrm{d}\mathbf{w}_t$$Itō's Lemma:
$$\mathrm{d}\mathbf{g} = \mathcal{L}^0(\mathbf{g})\mathrm{d}t  + \mathcal{L}^1(\mathbf{g}) \mathrm{d}\mathbf{w}$$First Kolmogorov operator:
$$\mathcal{L}^0(\cdot) = \left(\frac{\partial (\cdot)}{\partial t} + \sum_{i=1}^{p} a_i \frac{\partial (\cdot)}{\partial y_{i}} + \frac{1}{2} \sum_{i=1}^{p} \sum_{j=1}^p \sum_{k=1}^n B_{ij} B_{kj} \frac{\partial^2 (\cdot)}{\partial y_i \partial y_j}\right)$$Second Kolmogorov operator:
$$\mathcal{L}^1(\cdot) = \left[\mathcal{L}^1_1(\cdot), \ldots, \mathcal{L}^1_n(\cdot)\right] \qquad \mathcal{L}^1_j(\cdot) = \sum_{i=1}^p  B_{ij} \frac{\partial (\cdot)}{\partial y_i}$$ClassesSDEModel(n_dof, n_rvs, algorithm='all')- Main class, which performs the computation of derivative terms need for explicit integration schemes and their simulation. It makes use of the utilities listed below to provide an easy to manipulate object. For a more detailed investigation of symbolic terms, the utilities can be used. A use case for usingSDEModelis demonstrated incoindy.sde_demo.py.Utility functionsIncoindy.ito_utils:a, B = ito_sde_form(M, C, K, f, x, B_init)- Translates a system from a mechanical formulation to an Itō SDE formulation. The termsaandBrepresent the drift vector and diffusion matrix respectively where $d\mathbf{x} = \mathbf{a}*dt + \mathbf{B} * d\mathbf{w}_t$. These results would be similar to the ones used by queryingSDEModel.sde_terms['a']andSDEModel.sde_terms['B'].Lo = L0(X, a, B, x, t)- Compute the result of applying the first Kolmogorov operator to vectorXwithaandBresulting fromito_sde_form.Lj = LJ(X, B, x, j)- Compute the result of applying the second Kolmogorov operator toXwhereXcan be a vector or a matrix at indexj. This computes $$\mathcal{L}^1_j(\cdot) = \sum_{i=1}^p B_{ij} \frac{\partial (\cdot)}{\partial y_i} \mathrm{d}w_j$$ where $\mathcal{L}^1_j(\cdot)$ is the jthvector of $\mathcal{L}^1(\cdot)$.Lj = LJ_total(X, B, x)- Compute all the vectors of $\mathcal{L}^1(\cdot)$. Equivalent of runningLJ(X, B, x, j)for allj.l1l1 = L1L1_total(X, x)- Compute the double application of the second Kolmogorov operator to matrixX.Incoindy.simulation_utils:dW = generate_wiener_increment(time_step, time_stop, n_dof)- Utility that generates an n_dof * (time_stop/time_step) matrix of Wiener incrementsterm = constant_substitution(term, x, constant_map)- Utility that substitutes the symbols intermby the constants inconstant_mapand excluding the symbols inx.Scriptssde_demo.py- Script demonstrating the use of SDEModel for derivation and simulation of Itо̄ SDEs.Example:Integrate a one-dimensional mechanical oscillator with mass 1kg, damping 2.5 N s/m, stiffness 5 N/m
and stochastic force amplitude 0.01 N. The initial conditions arex0 = 0.01, x1 = 0.fromcoindyimportSDEModel,display_matrixn_dof=1n_rvs=1equations={'M':[['m']],'C':[['c']],'K':[['k']],'f':[['0','0','s']]}constant_map={'m':1,'c':2.5,'k':5,'s':0.01}initial_values={'x0':0.01,'x1':0}SDEModel.show_progress=Truesde_model=SDEModel(n_dof,n_rvs)sde_model.equations=equationssde_model.compute_ito_sde_terms()# Print drift termdisplay_matrix(sde_model.sde_terms['a'],'a')sde_model.simulate([0.01,10],constant_map,initial_values)ReferencesKloeden, P. E., Platen, E. and Schurz, H. (2003)Numerical Solution of SDE Through Computer Experiments. Berlin, Heidelberg: Springer Berlin Heidelberg (Universitext).Kloeden, P. E. and Platen, E. (1992)Numerical Solution of Stochastic Differential Equations, Springer. Berlin, Heidelberg: Springer Berlin Heidelberg.Roy, D. and Visweswara Rao, G. (2017)Stochastic dynamics, filtering and optimization, Stochastic Dynamics, Filtering and Optimization.Cyganowski, S., Kloeden, P. and Ombach, J. (2002)From Elementary Probability to Stochastic Differential Equations with MAPLE®. Berlin, Heidelberg: Springer Berlin Heidelberg (Universitext).