-
Notifications
You must be signed in to change notification settings - Fork 0
/
tail_functions.py
174 lines (149 loc) · 4.74 KB
/
tail_functions.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from pl_packages import *
#Finds emperical distribution of data
def ecdf_points(data):
L=len(data)
data_array=[]
for i in range(L):
data_array.append(data[i])
np.asarray(data_array,float)
x=np.sort(data_array)
y=np.arange(0,1,1/L)
X=[x[0]]
Y=[y[0]]
for i in range(0,L-1):
if(x[i]!=x[i+1]):
X.append(x[i+1])
Y.append(y[i+1])
X=np.asarray(X,float)
Y=np.asarray(Y,float)
return X,Y
#Finds tail of data
def tail(data):
x,y=ecdf_points(data)
return x, 1-y
#######################################################################
#Finds index of array with value of that index closest to the value imputted
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
#Power law function
def power_law(x,a,b):
return a*x**b
###########################################################################
'''Functions for Clauset et al paper'''
#Get's the MLE power beta parameter
def mle_pl_beta(x,x_min):
n=len(x)
return n/np.sum(np.log(x/x_min))
#Fit's the power law with MLE outputting parameters, predictions and index for inputted
#minimum x value for sample fit
def get_power_law_fit(sample,x_m):
pl_sample=sample[sample>=x_m]
y_n=len(pl_sample)/len(sample)
x,y=tail(pl_sample)
beta=mle_pl_beta(pl_sample,x_m)
alpha=y_n*x_m**beta
y_pred=power_law(x,alpha,-beta)
return x,y_pred,alpha,beta
#This slightly adjusted function fits the unbiased MLE
def get_power_law_fit1(sample,x_m):
pl_sample=sample[sample>=x_m]
ns=len(pl_sample)
y_n=ns/len(sample)
x,y=tail(pl_sample)
beta=(ns-1)/ns*mle_pl_beta(pl_sample,x_m)
alpha=y_n*x_m**beta
y_pred=power_law(x,alpha,-beta)
return x,y_pred,alpha,beta
#KS statistic
def KS_stat(y,y_pred):
return np.max(np.abs(y-y_pred))
#Get's estimate for x_min. Outputs this estimate which is the minimum of the
#KS-statistics also outputted
def x_min_pred(data):
es1=[]
x,y=tail(data)
for i in range(len(x)-2):
xs,ys,a,b=get_power_law_fit(data,x[i])
y_norm=y[i]
yn=y[i:]/y_norm
ysn=ys/y_norm
e1=KS_stat(yn,ysn)
es1.append(e1)
ind1=np.argmin(es1)
x_min_pred=x[ind1]
return es1,x_min_pred
#As above function but chooses x_min only within pre-chosen interval [x_l,x_u]
def x_min_pred_interval(data,x_l,x_u):
data=np.sort(data)
if (x_l>=data[0] )& (x_u<data[-2]):
es1=[]
x,y=tail(data)
ind=np.where((x_l<=x) & (x<=x_u))[0]
for i in ind:
xs,ys,a,b=get_power_law_fit(data,x[i])
y_norm=y[i]
yn=y[i:]/y_norm
ysn=ys/y_norm
e1=KS_stat(yn,ysn)
es1.append(e1)
ind1=np.argmin(es1)+ind[0]
x_min_pred=x[ind1]
return es1,x_min_pred
else:
return 'Interval chosen out of range'
#Goodness of fit test for power law
def get_p_val(x_m,b,K,N,n):
Ks=[]
for i in range(N):
sample=pareto.rvs(pareto_fit_vec[3], scale=x_m, size=n)
x,y=tail(sample)
vec=get_power_law_fit(sample,x_m)
Ks.append(KS_stat(y,vec[1]))
p=len(np.where(Ks>K)[0])/N
return p
############################################################################
#Linear regression fit - zero intercept
#https://math.stackexchange.com/questions/3297060/linear-regression-without-intercept-formula-for-slope
def pl_reg_fit(sample,x_m):
pl_sample=sample[sample>=x_m]
y_n=len(pl_sample)/len(sample)
x,y=tail(pl_sample)
x_lin_reg=np.log10(x/x_m)
y_lin_reg=np.log10(y)
b=np.sum(x_lin_reg*y_lin_reg)/np.sum(x_lin_reg*x_lin_reg)
beta=-b
alpha=y_n*x_m**(beta)
y_reg_pred=alpha*x**(-beta)
return [x,y_reg_pred,alpha,beta]
#Expected mean function linear regression
def lr_mean_est(n,g):
return np.log((np.exp(1)-(np.log(n))**(g)/n))
#Linear regression fit, approximately unbiased
def pl_reg_fit1(sample,x_m,g):
pl_sample=sample[sample>=x_m]
n=len(pl_sample)
y_n=n/len(sample)
x,y=tail(pl_sample)
x_lin_reg=np.log10(x/x_m)
y_lin_reg=np.log10(y)
b=np.sum(x_lin_reg*y_lin_reg)/np.sum(x_lin_reg*x_lin_reg)
beta=-b/lr_mean_est(n,g)
alpha=y_n*x_m**(beta)
y_reg_pred=alpha*x**(-beta)
return [x,y_reg_pred,alpha,beta]
##############################################################################
#Fit power law with non-linear regression
#b free param, a = P(X>x_m)*x_m^b
def min_non_lin_pl(b,x_m,x,y):
return y-power_law(x,x_m**b,-b)
def pl_nlr_fit(sample,b0,x_m):
pl_sample=sample[sample>=x_m]
y_n=len(pl_sample)/len(sample)
x,y=tail(pl_sample)
res=least_squares(min_non_lin_pl,b0,args=(x_m,x,y))
b=res.x[0]
a=y_n*x_m**b
y_pred=power_law(x,a,-b)
return x,y_pred,a,b