-
Notifications
You must be signed in to change notification settings - Fork 0
/
joe.py
152 lines (119 loc) · 4.14 KB
/
joe.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ========== module document ==========
__pyVersion__ = '3.5.2'
__author__ = 'Yilin Zhang'
__date__ = '2017-8-18'
__moduleVersion__ = '1.0'
# ========== import packages ==========
# import third-party packages
import numpy as np
import sympy as sp
from scipy import integrate
# ========== class definition ==========
## Archimedean Copula(distributed with theta)
class ArcmCopula(object):
"""
Note to reader: Each bivariate copula should be its own subclass that implements the following methods.
"""
def __init__(self):
pass
def pdf(self):
"""
Evaluate the probability distribution function (pdf) at a point (u,v) for a parameter theta
"""
pass
def cdf(self):
"""
Evaluate the cumulative distribution function (cdf) at a point (u,v) for a parameter theta
"""
pass
def rvs(self):
"""
Generate a simulation for the copula given a specified parameter theta.
"""
pass
def rho(self):
"""
Calculate Spearman's rho for the copula given a specified parameter theta.
"""
pass
## Joe Copula (distributed with theta)
class JoeCopula(object): # object waiting to change
"""
An joe_copula with one parameter theta.
Get pdf, cdf, logpdf, sf, logsf given 2D-ndarray,
seeing each row as a point in distribution.
Parameters
----------
x: 2D-ndarray;
number in x should be between (0,1);
theta: float;
number should larger than one
Examples
-----------
x = np.array([[0.1,0.2,0.3],[0.2,0.3,0.4]])
myjoe = JoeCopula()
myjoe.pdf(x,1.2)
"""
def __init__(self):
pass
def cdf(self, x, theta):
"""
genarator function : f(x)
inverse genarator function : g(x)
X = (x1, x2, ... ,xn)
cdf(X) = g(f(x1)+f(x2)+...+f(xn))
"""
term = np.sum(-np.log(1.0-(1.0-x)**theta),axis=-1)
return 1.0-(1.0-np.exp(-term))**(1.0/theta)
def pdf(self, x, theta):
"""
genarator function : f(x)
inverse genarator function : g(x)
X = (x1, x2, ... ,xn)
pdf(X) = g^{(-n)}(f(x1)+f(x2)+...+f(xn))f'(x1)f'(x2)...f'(xn)
"""
dim = x.shape[-1]
term = np.sum(-np.log(1.0-(1.0-x)**theta),axis=-1)
# get the differentiation of the genarator function and inverse genarator function
t = sp.Symbol('t')
f1 = 1.0-(1.0-sp.exp(-t))**(1.0/theta)
for i in range(dim):
f1 = sp.diff(f1,t)
f2 = -sp.log(1.0-(1.0-t)**theta)
f2 = sp.diff(f2,t)
# get ufunc of the differentiation equation
u1 = lambda z: f1.subs(t,z)
u1 = np.frompyfunc(u1,1,1)
u2 = lambda z: f2.subs(t,z)
u2 = np.frompyfunc(u2,1,1)
return u1(term)*np.prod(u2(x),axis=-1)
def rvs(self, n, dim, theta):
"""
Monte Carlo simulation using JoeCopula.pdf(self, k, theta)
"""
i = 0
while(i<n):
k = np.random.random_sample(dim)
if theta*np.random.random_sample(1)< JoeCopula.pdf(self, k, theta):
if i == 0:
sample = k
else:
sample = np.vstack((sample,k))
i = i+1
return sample
def tau(self, x, theta):
"""
genarator function : f(t)
$$tau(x)=1+\int^1_0 \frac{f(t)}{f'(t)}dt$$
"""
f = lambda t: np.log(1.0-(1.0-t)**theta)/(theta*(1.0-t)**(theta-1.0)/(1.0-(1.0-t)**theta))# f = (-log(1-(1-t)^theta)) / (theta*(1-t)^theta/(1-(1-t)^theta))
return 1.0+4.0*integrate.quad(f, 0, 1.0)[0]
#----------class definition----------
#----------main function----------
if __name__ == '__main__':
x = np.array([[0.1,0.2],[0.8,0.9]])
myjoe = JoeCopula()
print(myjoe.rvs(n=5,dim=7,theta=1.2))
#----------main function----------