-
Notifications
You must be signed in to change notification settings - Fork 0
/
svd.html
236 lines (175 loc) · 10.6 KB
/
svd.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>SVD</title>
<script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script>
<script src="js/algebra-0.2.5.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.js"></script>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<script src="js/numeric-1.2.6.js"></script>
<script src="js/numeric-1.2.6.min.js"></script>
<script src="js/Chart.js"></script>
<script src="js/svd.js"></script>
<script src="js/eigen.js"></script>
</head>
<body>
<!-- --------------------------------Sript starts here ------------------------------------------- -->
<script type="text/javascript">
var lolz;
function onload() {
a1 = document.getElementById('a1');
a2 = document.getElementById('a2');
b1 = document.getElementById('b1');
b2 = document.getElementById('b2');
}
function kk(){
//document.write(lolz.value)
a11= parseInt(a1.value, 10);
a12= parseInt(a2.value, 10);
b11 = parseInt(b1.value, 10);
b12 = parseInt(b2.value, 10);
// document.getElementById("para").innerHTML = a +b+c+d;
/* ----------------------- SVD Spherical--------------------------*/
var arr = [];
for (var i=0, t=100; i<t; i++) {
arr.push(Math.round(Math.random() * t)+100)
}
//document.write(arr);
x = numeric.sin(arr); // sin values
y = numeric.cos(arr) ; // corresponding cos values
test("myChart1",x,y,a11,a12,b11,b12);
/* ----------------------- END--------------------------*/
/* ----------------------- SVD Normal Distribution--------------------------*/
x = [-0.46451278, -0.25046501, -0.51806833 , 0.99894498, 0.73771435 , 0.73099705,
0.09312375, 0.0632059, -1.70159466, -1.24575886, 0.01076841, -1.41489839,
-1.06089061, -1.17768482 , 1.32294074, 0.48184197, -1.09207554, 1.28676039,
-0.62833635, 1.15297009, -0.73849013, 0.14887586, -0.00502372, 0.92186764,
-0.18798167, -1.36313759, 0.41047916, 0.16664193, 0.67311235, -0.38333168,
-2.25291738, 0.1430112, 0.17146572, 0.32349661, -0.19105782, 1.12148534,
0.29017927, 0.07871344, 0.78093253, 0.38855509, -0.49198588, -0.36156125,
0.53079948, -0.1395298, 0.44595019, -1.03865962, -0.30844344, 0.0283737,
-1.96009623, 0.73699817, 0.49701227 , 0.71600812, -0.00434543, -0.1585489,
0.89149159, -0.60997769, -2.02301457 , 0.28367774, -0.7862965 , 1.27749461,
-0.73828382, -0.22775878, -0.11962854, 0.84902884 ,-0.59548639, -0.23509587,
-0.77470115, 1.89765104 , 0.4843028 , 1.03036639 , 1.34952532 , 2.27973736,
-1.1295057, -0.67376216, -0.41321023 , 0.8441677 , 2.21130172, -0.19273507,
1.62930181, -0.69625182 ,-0.39406484 , 0.19161894, -0.00651644 , 0.0250081,
-0.43334037 , 1.04647376, -0.60894646, -0.08299575, 0.07357034, -0.61485319,
0.45730375, 0.72101381, -1.33084269, -1.33580141, 0.23424626 , 1.88964003,
1.06242757, 1.4371286, -0.45041773, -1.63202829];
y = [ 0.37740005, 0.92899475, -0.10084313, -0.21743969 , 1.58688803 , 0.50114471,
-1.7435476, 1.4232702 , -1.3067183 , 0.09576455 , 0.56985116 , 0.16367249,
-1.74555918 ,-0.93211707 , 0.27586248 , 1.20443691, -0.21652742 , 0.52400757,
1.78360078, -0.23155539, -1.74151965, -0.00489169, -0.03998455, 0.82990602,
-0.51121972, -1.17501371 ,-0.65772076, 0.854292 , -2.34445344, -0.07385904,
-1.82904508, 1.08445051, -2.54131129, -0.3191781 , 0.36532003 , 1.134781,
-2.21224824, -0.61539941 , 1.37155311 , 0.74648628, -0.17149801, -0.83560127,
0.50485351, -1.82903826 , 0.3737704 , 0.1743545, -0.62152738 , 0.40097886,
0.85616968 , 1.76785697 ,-0.38389765, 1.10067283, 0.99308216 ,-0.68012738,
-0.72841539 , 1.97116526, 1.63249056, -2.28088668, -1.04031674 ,-0.87615306,
-0.21690281, -0.47855623, -1.58321227 ,-0.16995373 ,-0.06060492 , 0.38217233,
0.15122451, -0.78806209 , 0.21656458 , 0.46268099 , 0.9501108 , -0.41202521,
-0.12908068, 1.12392241, 1.19911314, 1.47574796 , 1.58216604,-1.51087478,
0.94964669, -0.39860465 , 0.03677222, -0.65036716 , 0.42641722 , 0.21811883,
-0.31287446 ,-0.80121004, 1.153026 , 1.29010087, 1.18175946, -0.91379828,
0.5793928 , -0.56344789, -0.54860289 , 0.20759468 ,-1.18822006 ,-1.44778305,
-1.7347841 , 0.0402381 , 0.54378189, -1.82115282];
test("myChart2",x,y,a11,a12,b11,b12);
/*----------------------------END-----------------------------------*/
/*------------------------_Eigen Values-----------------------------*/
plotVector("myChart3",a11,a12,b11,b12);
}
</script>
<nav class="nav" id="navi1">
<div class="padded">
<ul>
<li class="active"><a id="link1" class="nav-section1" href="index.html">Home</a></li>
<li class="scrollTop"><a href="#"><span class="entypo-up-open"></span></a></li>
</ul>
</div>
</nav>
<article>
Enter the matrix whose SVD Visualization you want to see.
<h1 id="section1">SVD</h1>
<input type="text" name="enter" class="enter" value="" id="a1"/>
<input type="text" name="enter" class="enter" value="" id="a2"/>
<br>
<input type="text" name="enter" class="enter" value="" id="b1"/>
<input type="text" name="enter" class="enter" value="" id="b2"/>
<input type="button" value="Submit" onclick="kk();"/>
<br>
<br>
<br>
<h2>Visualization</h2>
<p>To understand what is happening when we do SVD, let us take 100 random points that lie on a circle and see what happens when we apply each decomposition to these set of points.For the given matrix <span id="a"></span> on applying SVD we get <span id="svd"></span> where <span id="U"></span> is mXm matrix , <span id="S"></span> is mXn diagonal matrix and <span id="VT"></span> is nxn matrix.</p>
<p>On taking the 100 generated points as a matrix of x and y coordinates (say matrix M), and then multiplying with U we find that the dataset gets rotated (the shape of the circle remains same) which can be seen in brown, then multiplying with S scales the circle (seen in green) and then finally multiplying with <span id="VT1"></span> rotates the figure again (seen in red). So SVD rotates, scales and then rotates the figure hence changing the circle to look like ellipse. </p>
<canvas id="myChart1" width="100%" height="40%"></canvas>
<br>
<br>
<br>
<script >
var Fraction = algebra.Fraction;
var Expression = algebra.Expression;
var Equation = algebra.Equation;
katex.render("A",a);
katex.render("USV^T",svd);
katex.render("U",U);
katex.render("S",S);
katex.render("V^T",VT);
katex.render("V^T",VT1);
//katex.render(algebra.toTex(b),y2);
</script>
<h2>Normal Distribution</h2>
<p>For the set of data points, we have taken a standard normal distribution with mean as 0 and covariance matrix represented by [1 0 ; 0 1] which gives us points spread out from the origin upto a radius of one.
A standard normal distribution is a normal distribution with zero mean and unit variance , given by the probability density function and distribution function
<img src = "images/Normaldist.jpg"/><br>
When the first matrix in the SVD (V') is applied to the data, this serves to rotate the data in three-space so that the data is represented relative to the V-basis.<br><br>
The second step in the SVD is to multiply our rotated data by the 'singular matrix' S, which is mxn.Generally, multiplying a vector <span id="v"></span> by a diagonal matrix with r nonzero elements on the diagonal <span id="s"></span> simply yields <span id="fo"></span>. That is, it stretches or contracts the components of v by the magnitude of the the singular values and zeroes out those elements of x that correspond to the zeros on the diagonal. <br><br>
The third step,matrix <span id="u"></span> to the transformed data represented in the <span id="u1"></span>-basis. Just like <span id="vt"></span>, <span id="u2"></span> is a rotation matrix: it transforms the data back to the standard basis.<br><br>
Finally finding the covariance matrix, for this new distribution, we obtain the same input matrix used above because matrix multiplication is happening with Identity matrix.<br><br>
</p>
<canvas id="myChart2" width="100%" height="40%"></canvas>
<br>
<br>
<br>
<script >
var Fraction = algebra.Fraction;
var Expression = algebra.Expression;
var Equation = algebra.Equation;
katex.render("v=(v_1,v_2...v_n)^T",v);
katex.render("(s_1,....s_r)",s);
katex.render("b=(s_1v_1, s_2v_2, .... s_rv_r, 0 .. 0)",fo);
katex.render("U",u);
katex.render("U",u1);
katex.render("U",u2);
katex.render("V^T",vt);
</script>
<h2>Eigen Vectors</h2>
<p> Given a Matrice A with eigen vectors v1,v2 corresponding to eigen values λ1 and λ2 repectively.Then we know that</p>
<center><p> Av=λv</p></center>
<p>where v ε { v1, v2} using the above result we can conclude that</p>
<center>
<p>⇒ A<sup>2</sup>v=λ<sup>2</sup>v</p>
<p>⇒ AAv=Aλv</p>
<p>⇒ AAv=λAv</p>
<p>⇒ A<sup>2</sup>v=λλv</p>
<p>⇒ A<sup>2</sup>v=λ<sup>2</sup>v</p>
</center>
From the above we get
<center><p>A<sup>n</sup>v=λ<sup>n</sup>v</p></center>
where n ε{1,2,3,4,5,.............}
<p>The graph plotted represents the various vectors obtained on multiplying the eigen vectors of matrice A with the powers of A. We observe the following cases :</p>
<br>
<p>1) if |λ| < 1 and λ > 0 The eigen vector of the matrice A keeps on shrinking as the value of n increases.</p>
<p>2) if |λ| <1 and λ >0 The eigen vector of the matrice A keeps on shrinking as the value of n increases and also for even value of n it keeps flipping its direction by 180°.</p>
<p>3) if |λ| >1 and λ<0 The eigen vector of the matrice A keeps on elongating as the value of n increases and also for even value of n it keeps flipping its direction by 180°.</p>
<p> 4) if |λ| >1 and λ >0 The eigen vector of the matrice A keeps on elongating as the value of n increases .</p>
<canvas id="myChart3" width="100%" height="40%"></canvas>
</article>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>