forked from CamDavidsonPilon/PyProcess
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.py
More file actions
Latest commit
69 lines (51 loc) · 2.4 KB
/
Copy pathexamples.py
File metadata and controls
69 lines (51 loc) · 2.4 KB
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
importmath
importpyprocessasSP
importscipy.statsasstats
#lets create a Wiener process conditioned at an end position (Brownian Bridge)
#Initialize the initial values and parameters
wienerParams= {"mu":0, "sigma":1}
wienerInitial= {"startTime":0, "startPosition":0, "endTime":12, "endPosition": 1 }
Wiener=SP.Wiener_process(wienerParams, wienerInitial)
#Get the mean and variance at time=10
t=10
printWiener.get_mean_at(t)
printWiener.get_variance_at(t)
print
#Generate a sample position at time=10
printWiener.generate_position_at(t)
print
#Generate a sample path at set times in an array:
times=range(10) # = [0,1,2,3,4,5,6,7,8,9]
printWiener.generate_sample_path(times)
# will print an array of points [(t, W_t) for t in times]
# Lets create a custom diffusion. We specify the drift and diffusion functions in the SDE:
# The functions must have time and space parameters, though they don't need to depend on them.
defdrift(x,t):
returnt*x
#the diffusion function must be non-negative in it's domain.
defdiffusion(x,t):
returnmath.sqrt(abs(x))
customParams={"a":drift, "b":diffusion}
customInitial= {"startTime":0, "startPosition":1}
Custom=SP.Custom_diffusion(customParams, customInitial)
#get the mean and poisiton at t=10.
#As this diffusion is untractable, we use MC methods. You can specify the accuracy in the library.
printCustom.generate_position_at(10)
#Lets create a process which can be seperated into the sum of a CEV diffusion and Compound Poisson jump process.
#First the CEV Process
CEVParams= {"r":0.002, "delta":0.5, "beta":-0.3}
CEVInitial= {"startTime":0, "startPosition":100}
CEV=SP.CEV_process(CEVParams, CEVInitial)
#Next the Compound Process. The jumps are Normal random variables, so lets create a "frozen" scipy.stats random variable.
#Frozen just means that the parameters are fixed upon construction of the random variable.
Nor=stats.norm(-0.2,0.05) #The parameters represent the mean and standard deviation.
rate=0.1#This is the jump rate, often denoted lambda in literature.
CPoiParams= {"J":Nor, "rate":rate}
CPoiInitial= {"startTime":0, "startPosition":0}
CPoi=SP.Compound_poisson_process(CPoiParams, CPoiInitial)
#create the custom process:
CustomProcess=SP.Custom_process(GBM, CPoi)
#play around with it?
printCustomProcess.get_mean_at(10)
printCustomProcess.generate_sample_path(range(100))
#Any question please email me at cam.davidson.pilon@gmail.com