-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcopula.py
64 lines (53 loc) · 1.57 KB
/
copula.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
# source: http://firsttimeprogrammer.blogspot.com/2015/02/copulalib-how-to-use-copulas-in-python.html
import numpy as np
import matplotlib.pyplot as plt
from copulalib.copulalib import Copula
plt.style.use('ggplot')
def generateData():
global x,y
x = np.random.normal(size=250)
y = 2.5*x + np.random.normal(size=250)
# Data and histograms
def plotData():
global x,y
fig = plt.figure()
fig.add_subplot(2,2,1)
plt.hist(x,bins=20,color='green',alpha=0.8,align='mid')
plt.title('X variable distribution')
fig.add_subplot(2,2,3)
plt.scatter(x,y,marker="o",alpha=0.8)
fig.add_subplot(2,2,4)
plt.title('Joint X,Y')
plt.hist(y,bins=20,orientation='horizontal',color='red',alpha=0.8,align='mid')
plt.title('Y variable distribution')
plt.show()
def generateCopulas():
global x,y
fig = plt.figure()
frank = Copula(x,y,family='frank')
uf,vf = frank.generate_uv(1000)
fig.add_subplot(2,2,1)
plt.scatter(uf,vf,marker='.',color='blue')
plt.ylim(0,1)
plt.xlim(0,1)
plt.title('Frank copula')
clayton = Copula(x,y,family='clayton')
uc,vc = clayton.generate_uv(1000)
fig.add_subplot(2,2,2)
plt.scatter(uc,vc,marker='.',color='red')
plt.ylim(0,1)
plt.xlim(0,1)
plt.title('Clayton copula')
gumbel = Copula(x,y,family='gumbel')
ug,vg = gumbel.generate_uv(1000)
fig.add_subplot(2,2,3)
plt.scatter(ug,vg,marker='.',color='green')
plt.ylim(0,1)
plt.xlim(0,1)
plt.title('Gumbel copula')
plt.show()
#---
# Run the functions
generateData()
plotData()
generateCopulas()