This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLD1.py
73 lines (56 loc) · 1.92 KB
/
LD1.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
import abc
import numpy as np
from scipy.special import gammaln, psi
from scipy.misc import logsumexp
from scipy.spatial.distance import cosine
import scipy as sp
from matplotlib import pyplot as plt
import statsmodels.api as sm
def get_W_1(data,option="euclid",normalise=True):
assert option in ["euclid","cosine"]
dim = len(data)
W_1 = np.zeros((dim,dim))
for i in range(dim):
for j in range(i+1,dim):
if option == "euclid":
W_1[i,j] = np.exp(- np.linalg.norm(data[i] - data[j],ord=2))
else:
W_1[i,j] = cosine(data[i],data[j])
# Since the distances are symetric
W_1 += W_1.T
# Normalize the matrix
if normalise:
row_sums = W.sum(axis=1)
W_1 = W_1 / row_sums[:, np.newaxis]
return W_1
def get_W_real(W,threshold=15):
#get distribution from W
K = 30
N = W_1.shape[0]
# Remove zeros on diagonals from matrix
W_no_diag = np.setdiff1d(np.concatenate(W),[0.])
# Univariate KDE with Gaussian
kde = sm.nonparametric.KDEUnivariate(W_no_diag)
#kde.fit()
# Printing the densitry fit to histogram
#print("Value threshold: %s" % np.percentile(kde.support,q=threshold))
#fig, ax = plt.subplots(figsize=(8, 6))
#ax.hist(W_no_diag, bins=K, density=True, lw=0, alpha=0.5)
#ax.plot(kde.support, kde.density, lw=3, label='Kernel Density Estimation', zorder=10)
#plt.show()
# make binary matrix A from W
A_1= W_1 < np.percentile(kde.support,q=threshold)
A_1 = A_1.astype(int)
W = A*W_1
return W
def A_gph(self, W)
"""
compute the adjacency matrix for the random graph using the Bernulli distribution A_{i,j} ~ Bern(W_{i,j})
"""
A = self.A
W = self.A
for n1 in range(self.N):
for n2 in range(self.N):
p = self.W[n1,n2]
A[n1,n2] = np.random.binomial(1,p,size=none)
return A