-
Notifications
You must be signed in to change notification settings - Fork 0
/
Risk.py
86 lines (68 loc) · 1.85 KB
/
Risk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 7 16:35:02 2016
@author: d_floriello
Example Risk calculation
"""
import numpy as np
import sdeint
import matplotlib.pyplot as plt
from sympy.solvers.pde import pdsolve
from sympy import Function, diff, Eq, cos, sin
from sympy.abc import x, t
# Weiner Process Simulation
f = Function('f')
u = f(x, t)
ux = u.diff(x)
ut = u.diff(t)
uxx = ux.diff(x)
eq = Eq(ut + (1 - cos(2*x)*u + (x + 2 - sin(2*x))*ux - 0.5*(sin(x)**2)*uxx))
pdsolve(eq)
# Time steps
n = 1000
T = 1
###############################################################################
def Wiener(mu, sig, n, T):
Delta = T/n
# t = np.arange(1, step=Delta)
S = np.zeros(n, np.dtype(float))
x = range(1, len(S))
for i in x:
dW = np.random.normal(mu, sig)
dt = np.sqrt(Delta)
dS = dW*dt
S[i] = S[i-1] + dS
return S
###############################################################################
plt.figure()
plt.plot(Wiener(0,1,n,T))
def mu(x,t):
return x**2
def SIG(x, t):
return np.sqrt(x)
tspan = np.linspace(0.0, 1, 10001)
res = sdeint.itoint(mu, SIG, 1, tspan)
plt.figure()
plt.plot(tspan,res.ravel())
def mu2(x,t):
return x + 2
def sig2(x,t):
return np.sin(x)
plt.figure()
tspan2 = np.linspace(0.0, 1, 10001)
for i in range(10):
plt.plot(tspan2, sdeint.itoint(mu2, sig2, 0, tspan2))
def ForwardKolmogorovEquation(drift, diffusion):
p = Function('p')
u = p(x, t)
ut = u.diff(t)
tdrift = u * drift
tdiffusion = u * (1/2*diffusion**2)
tdt = tdrift.diff(x)
tdifft = tdiffusion.diff(x)
tdifftt = tdifft.diff(x)
eq = Eq(ut + tdt - tdifftt)
return pdsolve(eq)
drift = Function('x + 2')
diffusion = Function('sin(x)')
ForwardKolmogorovEquation(drift, diffusion)