Spaces:
Sleeping
Sleeping
Jayabalambika
commited on
Commit
·
dbdf7e4
1
Parent(s):
7d7dc8e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
|
6 |
+
from scipy.linalg import toeplitz, cholesky
|
7 |
+
from sklearn.covariance import LedoitWolf, OAS
|
8 |
+
|
9 |
+
np.random.seed(0)
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
def plot_mse(min_slider_samples_range,max_slider_samples_range):
|
15 |
+
# plot MSE
|
16 |
+
print("inside plot_mse")
|
17 |
+
plt.subplot(2, 1, 1)
|
18 |
+
plt.errorbar(
|
19 |
+
slider_samples_range,
|
20 |
+
lw_mse.mean(1),
|
21 |
+
yerr=lw_mse.std(1),
|
22 |
+
label="Ledoit-Wolf",
|
23 |
+
color="navy",
|
24 |
+
lw=2,
|
25 |
+
)
|
26 |
+
plt.errorbar(
|
27 |
+
slider_samples_range,
|
28 |
+
oa_mse.mean(1),
|
29 |
+
yerr=oa_mse.std(1),
|
30 |
+
label="OAS",
|
31 |
+
color="darkorange",
|
32 |
+
lw=2,
|
33 |
+
)
|
34 |
+
plt.ylabel("Squared error")
|
35 |
+
plt.legend(loc="upper right")
|
36 |
+
plt.title("Comparison of covariance estimators")
|
37 |
+
plt.xlim(5, 31)
|
38 |
+
print("outside plot_mse")
|
39 |
+
return plt
|
40 |
+
|
41 |
+
|
42 |
+
def plot_shrinkage(min_slider_samples_range,max_slider_samples_range):
|
43 |
+
# plot shrinkage coefficient
|
44 |
+
print("inside plot_shrink")
|
45 |
+
plt.subplot(2, 1, 2)
|
46 |
+
plt.errorbar(
|
47 |
+
slider_samples_range,
|
48 |
+
lw_shrinkage.mean(1),
|
49 |
+
yerr=lw_shrinkage.std(1),
|
50 |
+
label="Ledoit-Wolf",
|
51 |
+
color="navy",
|
52 |
+
lw=2,
|
53 |
+
)
|
54 |
+
plt.errorbar(
|
55 |
+
slider_samples_range,
|
56 |
+
oa_shrinkage.mean(1),
|
57 |
+
yerr=oa_shrinkage.std(1),
|
58 |
+
label="OAS",
|
59 |
+
color="darkorange",
|
60 |
+
lw=2,
|
61 |
+
)
|
62 |
+
plt.xlabel("n_samples")
|
63 |
+
plt.ylabel("Shrinkage")
|
64 |
+
plt.legend(loc="lower right")
|
65 |
+
plt.ylim(plt.ylim()[0], 1.0 + (plt.ylim()[1] - plt.ylim()[0]) / 10.0)
|
66 |
+
plt.xlim(5, 31)
|
67 |
+
print("outside plot_shrink")
|
68 |
+
# plt.show()
|
69 |
+
return plt
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
title = "Ledoit-Wolf vs OAS estimation"
|
77 |
+
|
78 |
+
|
79 |
+
with gr.Blocks(title=title, theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as demo:
|
80 |
+
gr.Markdown(f"# {title}")
|
81 |
+
|
82 |
+
gr.Markdown(
|
83 |
+
"""
|
84 |
+
The usual covariance maximum likelihood estimate can be regularized using shrinkage. Ledoit and Wolf proposed a close formula to compute the asymptotically optimal shrinkage parameter (minimizing a MSE criterion), yielding the Ledoit-Wolf covariance estimate.
|
85 |
+
|
86 |
+
Chen et al. proposed an improvement of the Ledoit-Wolf shrinkage parameter, the OAS coefficient, whose convergence is significantly better under the assumption that the data are Gaussian.
|
87 |
+
|
88 |
+
This example, inspired from Chen’s publication [1], shows a comparison of the estimated MSE of the LW and OAS methods, using Gaussian distributed data.
|
89 |
+
|
90 |
+
[1] “Shrinkage Algorithms for MMSE Covariance Estimation” Chen et al., IEEE Trans. on Sign. Proc., Volume 58, Issue 10, October 2010.
|
91 |
+
""")
|
92 |
+
|
93 |
+
n_features = 100
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
min_slider_samples_range = gr.Slider(6, 31, value=6, step=1, label="min_samples_range", info="Choose between 6 and 31")
|
102 |
+
max_slider_samples_range = gr.Slider(6, 31, value=31, step=1, label="max_samples_range", info="Choose between 6 and 31")
|
103 |
+
|
104 |
+
print("min_slider_samples_range=",min_slider_samples_range.value)
|
105 |
+
print("max_slider_samples_range=",max_slider_samples_range.value)
|
106 |
+
|
107 |
+
|
108 |
+
low = min_slider_samples_range.value
|
109 |
+
high = max_slider_samples_range.value
|
110 |
+
###### initialisation code
|
111 |
+
slider_samples_range =np.arange(low, high,1)
|
112 |
+
n_features = 100
|
113 |
+
repeat = 100
|
114 |
+
lw_mse = np.zeros((slider_samples_range.size, repeat))
|
115 |
+
|
116 |
+
oa_mse = np.zeros((slider_samples_range.size, repeat))
|
117 |
+
|
118 |
+
lw_shrinkage = np.zeros((slider_samples_range.size, repeat))
|
119 |
+
|
120 |
+
oa_shrinkage = np.zeros((slider_samples_range.size, repeat))
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
r = 0.1
|
126 |
+
|
127 |
+
real_cov = toeplitz(r ** np.arange(n_features))
|
128 |
+
coloring_matrix = cholesky(real_cov)
|
129 |
+
|
130 |
+
for i, n_samples in enumerate(slider_samples_range):
|
131 |
+
for j in range(repeat):
|
132 |
+
X = np.dot(np.random.normal(size=(n_samples, n_features)), coloring_matrix.T)
|
133 |
+
|
134 |
+
lw = LedoitWolf(store_precision=False, assume_centered=True)
|
135 |
+
lw.fit(X)
|
136 |
+
lw_mse[i, j] = lw.error_norm(real_cov, scaling=False)
|
137 |
+
lw_shrinkage[i, j] = lw.shrinkage_
|
138 |
+
|
139 |
+
oa = OAS(store_precision=False, assume_centered=True)
|
140 |
+
oa.fit(X)
|
141 |
+
oa_mse[i, j] = oa.error_norm(real_cov, scaling=False)
|
142 |
+
oa_shrinkage[i, j] = oa.shrinkage_
|
143 |
+
|
144 |
+
|
145 |
+
gr.Markdown(" **[Demo is based on sklearn docs](https://scikit-learn.org/stable/auto_examples/covariance/plot_lw_vs_oas.html)**")
|
146 |
+
|
147 |
+
gr.Label(value="Comparison of Covariance Estimators")
|
148 |
+
|
149 |
+
|
150 |
+
min_slider_samples_range.change(plot_mse, inputs=[min_slider_samples_range,max_slider_samples_range], outputs= gr.Plot() )
|
151 |
+
max_slider_samples_range.change(plot_shrinkage, inputs=[min_slider_samples_range,max_slider_samples_range], outputs= gr.Plot() )
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
demo.launch()
|