-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
149 lines (123 loc) · 4.41 KB
/
evaluation.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
# -*- coding: utf-8 -*-
"""
@authors: Amin, Isabel
"""
# %%
import jax.numpy as jnp
from sklearn.covariance import LedoitWolf
from sklearn.covariance import GraphicalLasso
from sklearn.covariance import EmpiricalCovariance
from sklearn.decomposition import FactorAnalysis
from sklearn.metrics.pairwise import cosine_similarity
from scipy.sparse import linalg
# %%
def compare(y,prec=False,params={}):
result = {}
try:
if prec:
result['lw'] = jnp.stack([
LedoitWolf().fit(y[:,i,:]).precision_ for i in range(y.shape[1])
], axis=-1)
else:
result['lw'] = jnp.stack([
LedoitWolf().fit(y[:,i,:]).covariance_ for i in range(y.shape[1])
], axis=-1)
except: pass
try:
if prec:
result['lasso'] = jnp.stack([
GraphicalLasso(alpha=params['alpha']).fit(y[:, i, :]).precision_ for i in range(y.shape[1])
], axis=-1)
else:
result['lasso'] = jnp.stack([
GraphicalLasso(alpha=params['alpha']).fit(y[:, i, :]).covariance_ for i in range(y.shape[1])
], axis=-1)
except: pass
try:
if prec:
result['empirical'] = jnp.stack([
EmpiricalCovariance().fit(y[:, i, :]).precision_ for i in range(y.shape[1])
], axis=-1)
else:
result['empirical'] = jnp.stack([
EmpiricalCovariance().fit(y[:, i, :]).covariance_ for i in range(y.shape[1])
], axis=-1)
except: pass
try:
if prec:
result['fa'] = jnp.stack([
FactorAnalysis(n_components=params['n_components']).fit(y[:, i, :]).get_precision() for i in range(y.shape[1])
], axis=-1)
else:
result['fa'] = jnp.stack([
FactorAnalysis(n_components=params['n_components']).fit(y[:, i, :]).get_covariance() for i in range(y.shape[1])
], axis=-1)
except: pass
return result
def evaluate(methods,sigma,ord=2,prec=False):
N = sigma.shape[2]
result = {}
for key in methods.keys():
if prec:
try:
result[key] = [jnp.linalg.norm(
jnp.linalg.inv(methods[key][:,:,i]) - jnp.linalg.inv(sigma[:,:,i]), ord=ord
) for i in range(N)]
except: continue
else:
result[key] = [jnp.linalg.norm(
methods[key][:,:,i] - sigma[:,:,i], ord=ord
) for i in range(N)]
return result
import numpy as np
from scipy.stats import rankdata
# %%
def evaluate_var_smoothness(x,y,methods):
if x.shape[1] == 2:
idx = rankdata(x, method='dense',axis=0)-1
y_reshaped = {}
for m in range(len(y)):
a = np.nan*np.zeros((idx.max(0)+1).tolist() + [y[0].shape[1]])
for i in range(len(x)):
a[idx[i][0],idx[i][1]] = y[m][i]
y_reshaped[methods[m]] = a.copy()
mse = {}
for m in methods:
mse[m] = np.median(((
y_reshaped[m].flatten()-
y_reshaped['empirical'].flatten()
)**2))
return mse
if len(x.shape) == 1 or x.shape[1]==1:
y_reshaped = {}
for m in range(len(y)):
y_reshaped[methods[m]] = y[m]
mse = {}
for m in methods:
mse[m] = np.median(((
y_reshaped[m].flatten()-
y_reshaped['empirical'].flatten()
)**2))
return mse
# %%
def top_eig_overlap(x,mu_prime,sigma):
'''The cosine similarity between the top eigenvalue of the covariance
and the gradient of the means wrt the input parameter x
'''
overlap = np.array([abs(cosine_similarity(
mu_prime[:,[i]].T,
linalg.eigsh(np.array(sigma[i]),1)[1].T
)) for i in range(len(x))
]).squeeze()
return overlap
# %%
def fisher_information(x,mu_prime,sigma,sigma_prime=None):
'''Computing Fisher Information with and without access to the gradient
of the covariances wrt the input parameter x
'''
tr = lambda x: jnp.diag(x).sum()
sigma_inv = [np.linalg.inv(sigma[i]) for i in range(len(x))]
fi = [mu_prime[:,[i]].T@sigma_inv[i]@mu_prime[:,[i]] for i in range(len(x))]
if sigma_prime is not None:
fi = [fi[i] + .5*tr((sigma_inv[i]@sigma_prime[i])**2) for i in range(len(x))]
return np.array(fi)