-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_cov.py
executable file
·93 lines (75 loc) · 2.69 KB
/
graph_cov.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: jairo
# @Date: 2015-12-10 23:57:19
# @Last Modified by: jairo
# @Last Modified time: 2015-12-11 00:13:03
import numpy as np
from scipy.stats import expon, erlang, rv_continuous
import matplotlib.pyplot as plt
from scipy import misc as sc
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import colormaps as cmaps
import numpy.random as mtrand
class hyperexp(rv_continuous):
"""An HyperExponential Random Variable
"""
def __init__(self, alpha=0.5, lambda1=1.0, lambda2=1.0):
self.alpha = alpha
self.lambda1 = lambda1
self.lambda2 = lambda2
def rvs(self, size=1):
vsample = np.vectorize(self._single_sample)
return np.fromfunction(vsample, (size,))
def _single_sample(self, size):
U1 = mtrand.random()
if U1 <= self.alpha:
scale = self.lambda1
else:
scale = self.lambda2
U2 = mtrand.random()
return -np.log(U2)/scale
def pdf(self, x):
a = self.alpha*self.lambda1*np.exp(self.lambda1*-x)
b = (1-self.alpha)*self.lambda2*np.exp(self.lambda2*-x)
return a + b
def mean(self):
return (self.alpha / self.lambda1) + ((1-self.alpha) / self.lambda2)
def standard_dev(self):
a = (self.alpha/(self.lambda1**2)) + ((1-self.alpha)/(self.lambda2**2))
return np.sqrt(2*a + self.mean()**2)
def cdf(self, x):
a = self.alpha*(-np.exp(self.lambda1*-x))
b = (1-self.alpha)*(-np.exp(self.lambda2*-x))
return a + b + 1
def CoV(self):
a = np.sqrt(2*self.alpha/self.lambda1 + 2*(1-self.alpha)/self.lambda2 -
(self.alpha/self.lambda1 + (1-self.alpha)/self.lambda2)**2)
return a/self.mean()
def main():
plt.register_cmap(name='viridis', cmap=cmaps.viridis)
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(0.1, 20, num=50)
Y = np.linspace(0.1, 20, num=50)
Z = np.array([0.]*X.shape[0]*Y.shape[0])
Z.shape = (X.shape[0], Y.shape[0])
for i in range(X.shape[0]):
for j in range(Y.shape[0]):
he = hyperexp(0.5, X[i], Y[j])
Z[i][j] = he.CoV()
X, Y = np.meshgrid(X, Y)
plt.xlabel('lambda1')
plt.ylabel('lambda2')
ax.set_zlabel('CoV')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cmaps.viridis,
linewidth=0, antialiased=False, vmin=0., vmax=6)
ax.set_zlim(0, 6)
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
return
if __name__ == '__main__':
main()