-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (61 loc) · 1.96 KB
/
utils.py
File metadata and controls
73 lines (61 loc) · 1.96 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
70
71
72
73
import numpy as np
import numba as nb
def logit(x, lb=0., ub=1.):
z = (x-lb)/(ub-lb)
return np.log(z) - np.log(1-z)
def sigmoid(x, lb=0., ub=1.):
return lb + (ub-lb)/(1 + np.exp(-x))
def hill_estimate(x, k):
x = np.sort(abs(x))[::-1]
log_x = np.log(x + 1e-10)[:k]
avesumlog = np.cumsum(log_x)/np.arange(1, k+1)
xihat = (avesumlog - log_x)[1:]
alphahat = 1./xihat
return alphahat
# https://stackoverflow.com/a/55060589
@nb.njit(fastmath=True,error_model='numpy')
def gammaln(z):
"""Numerical Recipes 6.1"""
#Don't use global variables.. (They only can be changed if you recompile the function)
coefs = np.array([
57.1562356658629235, -59.5979603554754912,
14.1360979747417471, -0.491913816097620199,
.339946499848118887e-4, .465236289270485756e-4,
-.983744753048795646e-4, .158088703224912494e-3,
-.210264441724104883e-3, .217439618115212643e-3,
-.164318106536763890e-3, .844182239838527433e-4,
-.261908384015814087e-4, .368991826595316234e-5])
y = z
tmp = z + 5.24218750000000000
tmp = (z + 0.5) * np.log(tmp) - tmp
ser = 0.999999999999997092
for coef in coefs:
y = y + 1.
ser = ser + coef/y
out = tmp + np.log(2.5066282746310005 * ser / z)
return out
def VaR(x, p=0.99):
return np.percentile(np.sort(x), (1-p)*100)
def ecdf(samples, x=None):
"""
Compute empirical cdf from samples
Parameters
----------
samples
Array of samples from the distribution of interest
x
Array of points where to evaluate the cdf
Returns
-------
x
Array of points where to evaluate the cdf
cdf_eval
Evaluations of the empirical cdf
"""
sorted_samples = np.sort(samples)
if x is None:
x = sorted_samples
cdf_eval = np.searchsorted(sorted_samples, x, side='left')
cdf_eval = cdf_eval/len(sorted_samples)
cdf_eval[-1] += np.sum(sorted_samples==x[-1])/len(sorted_samples)
return cdf_eval