-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguageCompute.py
212 lines (165 loc) · 5.18 KB
/
languageCompute.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
### Language Computations
## Alex John Quijano
## Created: 2/18/2018
import pandas as pd
import numpy as np
import os
from sklearn.metrics.pairwise import cosine_similarity
from scipy.stats import norm
#### General Utilities ####
## zipf expected rank/value - empirical
def zipf_E_data(er,df):
return np.sum(np.multiply(er,df))
## returns (output is word-pairs) the top n cosine similar ngrams for a given list of ngrams
def cc_top(grams,grams_cosines,vocabulary,n=5,k=0):
pairs = []
for j, i in enumerate(grams_cosines):
i_sorted_index = np.squeeze(np.array(np.argsort(-1*i)))[0:n-1]
for c in i_sorted_index:
pairs.append((grams[j],vocabulary['reverse'][c]))
return np.array(pairs)
## construct cscore (word classes - needs existing text files)
def readClasses(n,g,l,vocabulary):
list_class = []
file = open(n+'gram-list/'+g+'/'+n+'gram-class-'+l)
for f in file:
list_class.append(f.replace('\n',''))
list_gram_all = {}
list_gram_exist = {}
cscore = np.zeros((len(vocabulary['forward']),len(list_class)),dtype=int)
for lc_i, lc in enumerate(list_class):
list_gram_all[lc] = []
list_gram_exist[lc] = []
file_1 = open(n+'gram-list/'+g+'/'+l+'/'+n+'gram-list-'+lc+'-'+l,'r')
for f in file_1:
gram = f.replace('\n','')
list_gram_all[lc].append(gram)
try:
gram_indexInVocabulary = vocabulary['forward'][gram]
list_gram_exist[lc].append(gram)
cscore[gram_indexInVocabulary,lc_i] = 1
except KeyError:
pass
file_1.close()
return cscore, np.array(list_class), list_gram_exist, list_gram_all
# generate pscore matrix
def generate_pscore(rscore_matrix):
return np.divide(rscore_matrix,np.sum(rscore_matrix,axis=0))
# generate zscore matrix
def generate_zscore(pscore_matrix):
means = np.mean(pscore_matrix,axis=1)
stds = np.std(pscore_matrix,axis=1)
out = pd.DataFrame(np.zeros(pscore_matrix.shape),index=pscore_matrix.index,columns=pscore_matrix.columns)
for i in out.columns:
out[i] = pscore_matrix[i] - means
for j in out.columns:
out[j] = np.divide(out[j],stds)
return out
# generate delta matrix
def generate_delta(data_matrix):
t_vect = data_matrix.columns
delta = pd.DataFrame(np.zeros(data_matrix.shape,dtype=int),index=data_matrix.index,columns=data_matrix.columns)
for t in t_vect[:-1]:
diff = data_matrix[t+1]-data_matrix[t]
delta[t+1] = diff
delta = delta.T.drop([t_vect[0]]).T
return delta
#### Ranking computations ####
## assign word rank using data (probabilty scores or raw scores as inputs)
def return_ranks(df):
df_sort = df.sort_index().sort_values(ascending=False) #np.argsort(-1*df.values)
ranks = {}
for i, j in enumerate(df_sort.keys()):
ranks[j] = i+1
return ranks
#### Similarity Computations ####
## compute pairwise cosine similarity matrix
def pairwise_cosine_similarity(df):
return pd.DataFrame(cosine_similarity(df),index=df.index,columns=df.index)
## rth sample moment function
def sample_moment(df,r):
return (1/df.shape[0])*np.sum(np.power(df-np.mean(df),r))
## sample skewness function
def sample_skewness(df):
return np.divide(sample_moment(df,3),np.power(np.var(df,ddof=1),3/2))
## sample kurtosis function
def sample_kurtosis(df):
return np.divide(sample_moment(df,4),np.power(np.var(df),2))-3
## sample bc function
def sample_bimodality_coefficient(m4,m3):
n = m3.shape[0]
b = float(np.power(n-1,2))/float((n-2)*(n-3))
bc = (np.power(m3,2) + 1)/(m4+3*b)
return bc
## Compute CC matrix and BC values of a given data matrix
def bimodality_coefficient(df,pcs=True):
M = df
if pcs == True:
M = pd.DataFrame(np.ma.masked_values(np.matrix(pairwise_cosine_similarity(df)),1),index=df.index,columns=df.index)
elif pcs == False:
M = df
m3 = sample_skewness(M)
m4 = sample_kurtosis(M)
bc = sample_bimodality_coefficient(m4,m3)
out = {'m3':m3,'m4':m4,'bc':bc}
del M, m3, m4, bc
return out
## BC - Critical line m4 <- f(m3)
def bc_curve_m3(m3,c,n=1000):
if isinstance(m3,int):
N = n
else:
N = m3.shape[0]
b = float(np.power(N-1,2))/float((N-2)*(N-3))
a = ((1/c)*(np.power(m3,2)+1))-3*b
return a
## BC - Critical line m3 <- f(m4)
def bc_curve_m4(m4,c,n=1000):
if isinstance(m4,int):
N = n
else:
N = m4.shape[0]
b = float(np.power(N-1,2))/float((N-2)*(N-3))
m3 = np.sqrt(c*(m4+3*b)-1)
return m3
#### Statistical Tests ####
## Mann-Kendall test
def mk_test(x, alpha=0.05):
"""
Credit: (see LICENCE)
Created on Wed Jul 29 09:16:06 2015
@author: Michael Schramm (Github)
"""
n = len(x)
# calculate S
s = 0
for k in range(n-1):
for j in range(k+1, n):
s += np.sign(x[j] - x[k])
# calculate the unique data
unique_x = np.unique(x)
g = len(unique_x)
# calculate the var(s)
if n == g: # there is no tie
var_s = (n*(n-1)*(2*n+5))/18
else: # there are some ties in data
tp = np.zeros(unique_x.shape)
for i in range(len(unique_x)):
tp[i] = sum(x == unique_x[i])
var_s = (n*(n-1)*(2*n+5) - np.sum(tp*(tp-1)*(2*tp+5)))/18
if s > 0:
z = (s - 1)/np.sqrt(var_s)
elif s == 0:
z = 0
elif s < 0:
z = (s + 1)/np.sqrt(var_s)
# calculate the p_value
p = 2*(1-norm.cdf(abs(z))) # two tail test
h = abs(z) > norm.ppf(1-alpha/2)
if (z < 0) and h:
trend = 'decreasing'
elif (z > 0) and h:
trend = 'increasing'
else:
trend = 'neither'
return trend, h, p, z