first launch
Browse filesonly covariance plots
app.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from scipy import linalg
|
4 |
+
from sklearn.datasets import make_sparse_spd_matrix
|
5 |
+
from sklearn.covariance import GraphicalLassoCV,
|
6 |
+
_wolf
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
prng = np.random.RandomState(1)
|
11 |
+
|
12 |
+
def get_precision_matrix(alpha = 0.98, smallest_coef = 0.4, largest_coef = 0.7):
|
13 |
+
|
14 |
+
prec = make_sparse_spd_matrix(
|
15 |
+
n_features, alpha=alpha, smallest_coef=smallest_coef, largest_coef=largest_coef, random_state=prng
|
16 |
+
)
|
17 |
+
|
18 |
+
return prec
|
19 |
+
|
20 |
+
def get_covariance_matrix(precision_matrix):
|
21 |
+
|
22 |
+
return linalg.inv(precision_matrix)
|
23 |
+
|
24 |
+
def scaled_covariance_matrix(precision_matrix):
|
25 |
+
|
26 |
+
covariance_matrix = get_covariance_matrix(precision_matrix)
|
27 |
+
d = np.sqrt(np.diag(covariance_matrix))
|
28 |
+
scaled_covariance_matrix = covariance_matrix / d
|
29 |
+
scaled_covariance_matrix /= d[:, np.newaxis]
|
30 |
+
|
31 |
+
return scaled_covariance_matrix
|
32 |
+
|
33 |
+
def scaled_precision_matrix(precision_matrix):
|
34 |
+
|
35 |
+
covariance_matrix = get_covariance_matrix(precision_matrix)
|
36 |
+
d = np.sqrt(np.diag(covariance_matrix))
|
37 |
+
scaled_precision_matrix = precision_matrix * d
|
38 |
+
scaled_precision_matrix *= d[:, np.newaxis]
|
39 |
+
|
40 |
+
return scaled_precision_matrix
|
41 |
+
|
42 |
+
|
43 |
+
def get_samples(n_features, n_samples, scaled_covariance_matrix):
|
44 |
+
|
45 |
+
X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)
|
46 |
+
X -= X.mean(axis=0)
|
47 |
+
X /= X.std(axis=0)
|
48 |
+
|
49 |
+
return X
|
50 |
+
|
51 |
+
def get_empirical_covariance(X, n_samples):
|
52 |
+
|
53 |
+
return np.dot(X.T, X) / n_samples
|
54 |
+
|
55 |
+
def estimate_covariance_lasso(X):
|
56 |
+
|
57 |
+
model = GraphicalLassoCV()
|
58 |
+
model.fit(X)
|
59 |
+
return model.covariance_
|
60 |
+
|
61 |
+
def estimate_precision_lasso(X):
|
62 |
+
|
63 |
+
model = GraphicalLassoCV()
|
64 |
+
model.fit(X)
|
65 |
+
return model.precision_
|
66 |
+
|
67 |
+
def estimate_covariance_leidotwolf(X):
|
68 |
+
|
69 |
+
lw_cov_, _ =
|
70 |
+
_wolf(X)
|
71 |
+
|
72 |
+
return lw_cov_
|
73 |
+
|
74 |
+
def estimate_precision_leidotwolf(leidot_cov):
|
75 |
+
|
76 |
+
return linalg.inv(leidot_cov)
|
77 |
+
|
78 |
+
|
79 |
+
# main function that will be called in the block
|
80 |
+
def compute_and_plot(alpha = 0.98, smallest_coef = 0.4, largest_coef = 0.7,
|
81 |
+
n_features = 20, n_samples = 60, measure = None, model = None):
|
82 |
+
|
83 |
+
prec = get_precision_matrix(alpha = alpha, smallest_coef = smallest_coef, largest_coef = largest_coef)
|
84 |
+
prec = scaled_precision_matrix(prec)
|
85 |
+
cov = scaled_covariance_matrix(prec)
|
86 |
+
X = get_samples(n_features, n_samples, cov)
|
87 |
+
|
88 |
+
if measure == 'covariance':
|
89 |
+
if model == 'empirical':
|
90 |
+
emp_cov = get_empirical_covariance(X, n_samples)
|
91 |
+
fig, ax = plt.subplots()
|
92 |
+
ax.imshow(emp_cov, interpolation="nearest", cmap=plt.cm.RdBu_r)
|
93 |
+
elif model == 'lasso':
|
94 |
+
lasso_cov = estimate_covariance_lasso(X)
|
95 |
+
fig, ax = plt.subplots()
|
96 |
+
ax.imshow(lasso_cov, interpolation="nearest", cmap=plt.cm.RdBu_r)
|
97 |
+
elif model == 'leidot-wolf':
|
98 |
+
lw_cov = estimate_covariance_leidotwolf(X)
|
99 |
+
fig, ax = plt.subplots()
|
100 |
+
ax.imshow(lw_cov, interpolation="nearest", cmap=plt.cm.RdBu_r)
|
101 |
+
else:
|
102 |
+
print('invalid')
|
103 |
+
# elif measure == 'precision':
|
104 |
+
|
105 |
+
# else:
|
106 |
+
# # TO DO: add empty plot
|
107 |
+
# print('invalid')
|
108 |
+
|
109 |
+
|
110 |
+
#lasso_prec = estimate_precision_lasso(X)
|
111 |
+
|
112 |
+
#lw_prec = estimate_precision_leidotwolf(leidot_cov)
|
113 |
+
|
114 |
+
return fig
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
def iter_grid(n_rows, n_cols):
|
119 |
+
# create a grid using gradio Block
|
120 |
+
for _ in range(n_rows):
|
121 |
+
with gr.Row():
|
122 |
+
for _ in range(n_cols):
|
123 |
+
with gr.Column():
|
124 |
+
yield
|
125 |
+
|
126 |
+
title = "Sparse inverse covariance estimation"
|
127 |
+
with gr.Blocks(title=title) as demo:
|
128 |
+
gr.Markdown(f"## {title}")
|
129 |
+
gr.Markdown("Estimating covariance and sparse precision from a small number of samples using GraphicalLasso and
|
130 |
+
-Wolf algorithms.")
|
131 |
+
n_samples = gr.Slider(minimum=20, maximum=100, step=5,
|
132 |
+
label = "Number of Samples")
|
133 |
+
n_features = gr.Slider(minimum=10, maximum=100, step=5,
|
134 |
+
label = "Number of features")
|
135 |
+
alpha = gr.Slider(minimum=0, maximum=1, step=0.1,
|
136 |
+
label = "sparsity coefficient (alpha)")
|
137 |
+
smallest_coef = gr.Slider(minimum=0, maximum=1, step=0.1,
|
138 |
+
label = "minimum correlation value")
|
139 |
+
largest_coef = gr.Slider(minimum=0, maximum=1, step=0.1,
|
140 |
+
label = "maximum correlation value")
|
141 |
+
|
142 |
+
models = ['empirical', 'lasso', 'leidot-wolf']
|
143 |
+
model_counter = 0
|
144 |
+
for _ in iter_grid(1, 3):
|
145 |
+
|
146 |
+
model = models[model_counter]
|
147 |
+
plot = gr.Plot(label=input_model)
|
148 |
+
n_samples.change(fn=compute_and_plot, inputs=[0.98, 0.4, 0.7, 20, 60, 'covariance', model], outputs=plot)
|
149 |
+
n_features.change(fn=compute_and_plot, inputs=[0.98, 0.4, 0.7, 20, 60, 'covariance', model], outputs=plot)
|
150 |
+
alpha.change(fn=compute_and_plot, inputs=[0.98, 0.4, 0.7, 20, 60, 'covariance', model], outputs=plot)
|
151 |
+
smallest_coef.change(fn=compute_and_plot, inputs=[0.98, 0.4, 0.7, 20, 60, 'covariance', model], outputs=plot)
|
152 |
+
largest_coef.change(fn=compute_and_plot, inputs=[0.98, 0.4, 0.7, 20, 60, 'covariance', model], outputs=plot)
|
153 |
+
|
154 |
+
|
155 |
+
demo.launch()
|
156 |
+
|