diff --git a/doc/pub/DimRed/html/._DimRed-bs000.html b/doc/pub/DimRed/html/._DimRed-bs000.html index 4d83066c4..74d5b19a6 100644 --- a/doc/pub/DimRed/html/._DimRed-bs000.html +++ b/doc/pub/DimRed/html/._DimRed-bs000.html @@ -76,17 +76,24 @@ Automatically generated HTML file from DocOnce source 2, None, '___sec9'), - ('Classical PCA Theorem', 2, None, '___sec10'), - ('Prof of the PCA Theorem', 2, None, '___sec11'), - ('Getting started with PCA', 2, None, '___sec12'), - ('Principal Component Analysis', 2, None, '___sec13'), - ('PCA and scikit-learn', 2, None, '___sec14'), - ('More on the PCA', 2, None, '___sec15'), - ('Incremental PCA', 2, None, '___sec16'), - ('Randomized PCA', 2, None, '___sec17'), - ('Kernel PCA', 2, None, '___sec18'), - ('LLE', 2, None, '___sec19'), - ('Other techniques', 2, None, '___sec20')]} + ('Covariance Matrix Examples', 2, None, '___sec10'), + ('Correlation Matrix', 2, None, '___sec11'), + ('Correlation Matrix with Pandas', 2, None, '___sec12'), + ('Correlation Matrix with Pandas and the Franke function', + 2, + None, + '___sec13'), + ('Classical PCA Theorem', 2, None, '___sec14'), + ('Prof of the PCA Theorem', 2, None, '___sec15'), + ('Getting started with PCA', 2, None, '___sec16'), + ('Principal Component Analysis', 2, None, '___sec17'), + ('PCA and scikit-learn', 2, None, '___sec18'), + ('More on the PCA', 2, None, '___sec19'), + ('Incremental PCA', 2, None, '___sec20'), + ('Randomized PCA', 2, None, '___sec21'), + ('Kernel PCA', 2, None, '___sec22'), + ('LLE', 2, None, '___sec23'), + ('Other techniques', 2, None, '___sec24')]} end of tocinfo -->
@@ -134,17 +141,21 @@ MathJax.Hub.Config({Suppose we have defined two vectors -$\hat{x} and \hat{y} with \( n \) elements each. The covariance matrix $\boldsymbol{C}is defined as +\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as $$ \boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} cov[\boldsymbol{x},\boldsymbol{x}] & cov[\boldsymbol{x},\boldsymbol{y}] \\ cov[\boldsymbol{y},\boldsymbol{x}] & cov[\boldsymbol{y},\boldsymbol{y}] \\ @@ -196,7 +207,7 @@ introducing instead the correlation matrix defined via the so-called correlation function $$ -corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var\boldsymbol{y}]}}. +corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var[\boldsymbol{y}]}}. $$
@@ -239,7 +250,7 @@ In the above example this is the function we constructed using pandas.
Suppose we have defined two vectors -$\hat{x} and \hat{y} with \( n \) elements each. The covariance matrix $\boldsymbol{C}is defined as +\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as
$$
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} cov[\boldsymbol{x},\boldsymbol{x}] & cov[\boldsymbol{x},\boldsymbol{y}] \\
@@ -567,7 +567,7 @@ correlation function
$$
-corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var\boldsymbol{y}]}}.
+corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var[\boldsymbol{y}]}}.
$$
@@ -652,13 +652,22 @@ corr[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & corr[\boldsymbol{x}_{p-1},\bolds
\end{bmatrix},
$$
-The Numpy function np.cov calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
-The following simple function uses the np.vstack function which takes each vector of dimension \( 1\times n \) and produces a \( 2\times n \) matrix \( \hat{W} \)
+The Numpy function np.cov calculates the covariance elements using
+the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
+the exact mean values. The following simple function uses the
+np.vstack function which takes each vector of dimension \( 1\times n \)
+and produces a \( 2\times n \) matrix \( \boldsymbol{W} \)
+
-which in turn is converted into into the \( 3\times 3 \) covariance matrix
-\( \hat{\Sigma} \) via the Numpy function np.cov(). We note that we can also calculate
-the mean value of each set of samples \( \hat{x} \) etc using the Numpy
+which in turn is converted into into the \( 2\times 2 \) covariance matrix
+\( \boldsymbol{C} \) via the Numpy function np.cov(). We note that we can also calculate
+the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
function np.mean(x). We can also extract the eigenvalues of the
covariance matrix through the np.linalg.eig() function.
@@ -680,35 +689,165 @@ covariance matrix through the np.linalg.eig() function.
+The previous example can be converted into the correlation matrix by
+simply scaling the matrix elements with the variances. We should also
+subtract the mean values for each column. This leads to the following
+code which sets up the correlations matrix for the previous example in
+a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors).
+
+
+
+
+
+We see that the matrix elements along the diagonal are one as they
+should be and that the matrix is symmetric. Furthermore, diagonalizing
+this matrix we easily see that it is a positive definite matrix.
+
+
+The above procedure with numpy can be made more compact if we use pandas.
+We whow here how we can set up the correlation matrix using pandas, as done in this simple code
+
+
+
+
+We expand this model to the Franke function discussed above.
+
+
+
+We note here that the covariance is zero for the first rows and
+columns since all matrix elements in the design matrix were set to one
+(we are fitting the function in terms of a polynomial of degree \( n \)).
+
+
+This means that the variance for these elements will be zero and will
+cause problems when we set up the correlation matrix. We can simply
+drop these elements as follows and then construct the correlation
+matrix.
+
@@ -724,7 +863,7 @@ X_pca = pca.transform(X_train_scaled)
@@ -761,7 +900,7 @@ X2D = X_centered.dot(W2)
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -792,7 +931,7 @@ More material to come here.
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
@@ -823,7 +962,7 @@ X_reduced = pca.fit_transform(X)
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
@@ -835,7 +974,7 @@ instances arrive).
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
@@ -849,7 +988,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
@@ -875,7 +1014,7 @@ X_reduced = rbf_pca.fit_transform(X)
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
@@ -887,7 +1026,7 @@ these local relationships are best preserved (more details shortly).
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
diff --git a/doc/pub/DimRed/html/DimRed-solarized.html b/doc/pub/DimRed/html/DimRed-solarized.html
index dbbe0c5ef..c215b4a80 100644
--- a/doc/pub/DimRed/html/DimRed-solarized.html
+++ b/doc/pub/DimRed/html/DimRed-solarized.html
@@ -96,17 +96,24 @@ div { text-align: justify; text-justify: inter-word; }
2,
None,
'___sec9'),
- ('Classical PCA Theorem', 2, None, '___sec10'),
- ('Prof of the PCA Theorem', 2, None, '___sec11'),
- ('Getting started with PCA', 2, None, '___sec12'),
- ('Principal Component Analysis', 2, None, '___sec13'),
- ('PCA and scikit-learn', 2, None, '___sec14'),
- ('More on the PCA', 2, None, '___sec15'),
- ('Incremental PCA', 2, None, '___sec16'),
- ('Randomized PCA', 2, None, '___sec17'),
- ('Kernel PCA', 2, None, '___sec18'),
- ('LLE', 2, None, '___sec19'),
- ('Other techniques', 2, None, '___sec20')]}
+ ('Covariance Matrix Examples', 2, None, '___sec10'),
+ ('Correlation Matrix', 2, None, '___sec11'),
+ ('Correlation Matrix with Pandas', 2, None, '___sec12'),
+ ('Correlation Matrix with Pandas and the Franke function',
+ 2,
+ None,
+ '___sec13'),
+ ('Classical PCA Theorem', 2, None, '___sec14'),
+ ('Prof of the PCA Theorem', 2, None, '___sec15'),
+ ('Getting started with PCA', 2, None, '___sec16'),
+ ('Principal Component Analysis', 2, None, '___sec17'),
+ ('PCA and scikit-learn', 2, None, '___sec18'),
+ ('More on the PCA', 2, None, '___sec19'),
+ ('Incremental PCA', 2, None, '___sec20'),
+ ('Randomized PCA', 2, None, '___sec21'),
+ ('Kernel PCA', 2, None, '___sec22'),
+ ('LLE', 2, None, '___sec23'),
+ ('Other techniques', 2, None, '___sec24')]}
end of tocinfo -->
Suppose we have defined two vectors
-$\hat{x} and \hat{y} with \( n \) elements each. The covariance matrix $\boldsymbol{C}is defined as
+\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as
$$
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} cov[\boldsymbol{x},\boldsymbol{x}] & cov[\boldsymbol{x},\boldsymbol{y}] \\
cov[\boldsymbol{y},\boldsymbol{x}] & cov[\boldsymbol{y},\boldsymbol{y}] \\
@@ -554,7 +561,7 @@ introducing instead the correlation matrix defined via the so-called
correlation function
$$
-corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var\boldsymbol{y}]}}.
+corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var[\boldsymbol{y}]}}.
$$
@@ -628,10 +635,19 @@ corr[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & corr[\boldsymbol{x}_{p-1},\bolds
$$
-The Numpy function np.cov calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
-The following simple function uses the np.vstack function which takes each vector of dimension \( 1\times n \) and produces a \( 2\times n \) matrix \( \hat{W} \)
+
+The Numpy function np.cov calculates the covariance elements using
+the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
+the exact mean values. The following simple function uses the
+np.vstack function which takes each vector of dimension \( 1\times n \)
+and produces a \( 2\times n \) matrix \( \boldsymbol{W} \)
+
$$
-\hat{W} = \begin{bmatrix} x_0 & y_0 \\
+\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\
x_1 & y_1 \\
x_2 & y_2\\
\dots & \dots \\
@@ -641,9 +657,9 @@ $$
$$
-which in turn is converted into into the \( 3\times 3 \) covariance matrix
-\( \hat{\Sigma} \) via the Numpy function np.cov(). We note that we can also calculate
-the mean value of each set of samples \( \hat{x} \) etc using the Numpy
+which in turn is converted into into the \( 2\times 2 \) covariance matrix
+\( \boldsymbol{C} \) via the Numpy function np.cov(). We note that we can also calculate
+the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
function np.mean(x). We can also extract the eigenvalues of the
covariance matrix through the np.linalg.eig() function.
@@ -652,34 +668,164 @@ covariance matrix through the np.linalg.eig() function.
+The previous example can be converted into the correlation matrix by
+simply scaling the matrix elements with the variances. We should also
+subtract the mean values for each column. This leads to the following
+code which sets up the correlations matrix for the previous example in
+a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors).
+
+
+
+
+
+We see that the matrix elements along the diagonal are one as they
+should be and that the matrix is symmetric. Furthermore, diagonalizing
+this matrix we easily see that it is a positive definite matrix.
+
+
+The above procedure with numpy can be made more compact if we use pandas.
+We whow here how we can set up the correlation matrix using pandas, as done in this simple code
+
+
+
+
+We expand this model to the Franke function discussed above.
+
+
+
+We note here that the covariance is zero for the first rows and
+columns since all matrix elements in the design matrix were set to one
+(we are fitting the function in terms of a polynomial of degree \( n \)).
+
+
+This means that the variance for these elements will be zero and will
+cause problems when we set up the correlation matrix. We can simply
+drop these elements as follows and then construct the correlation
+matrix.
+
+
+
+
+
@@ -694,7 +840,7 @@ X_pca = pca.transform(X_train_scaled)
@@ -730,7 +876,7 @@ X2D = X_centered.dot(W2)
-
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -761,7 +907,7 @@ More material to come here.
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
@@ -791,7 +937,7 @@ X_reduced = pca.fit_transform(X)
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
@@ -803,7 +949,7 @@ instances arrive).
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
@@ -818,7 +964,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
@@ -847,7 +993,7 @@ X_reduced = rbf_pca.fit_transform(X)
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
@@ -859,7 +1005,7 @@ these local relationships are best preserved (more details shortly).
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
diff --git a/doc/pub/DimRed/html/DimRed.html b/doc/pub/DimRed/html/DimRed.html
index 8478d5b7f..d3244342a 100644
--- a/doc/pub/DimRed/html/DimRed.html
+++ b/doc/pub/DimRed/html/DimRed.html
@@ -101,17 +101,24 @@ div { text-align: justify; text-justify: inter-word; }
2,
None,
'___sec9'),
- ('Classical PCA Theorem', 2, None, '___sec10'),
- ('Prof of the PCA Theorem', 2, None, '___sec11'),
- ('Getting started with PCA', 2, None, '___sec12'),
- ('Principal Component Analysis', 2, None, '___sec13'),
- ('PCA and scikit-learn', 2, None, '___sec14'),
- ('More on the PCA', 2, None, '___sec15'),
- ('Incremental PCA', 2, None, '___sec16'),
- ('Randomized PCA', 2, None, '___sec17'),
- ('Kernel PCA', 2, None, '___sec18'),
- ('LLE', 2, None, '___sec19'),
- ('Other techniques', 2, None, '___sec20')]}
+ ('Covariance Matrix Examples', 2, None, '___sec10'),
+ ('Correlation Matrix', 2, None, '___sec11'),
+ ('Correlation Matrix with Pandas', 2, None, '___sec12'),
+ ('Correlation Matrix with Pandas and the Franke function',
+ 2,
+ None,
+ '___sec13'),
+ ('Classical PCA Theorem', 2, None, '___sec14'),
+ ('Prof of the PCA Theorem', 2, None, '___sec15'),
+ ('Getting started with PCA', 2, None, '___sec16'),
+ ('Principal Component Analysis', 2, None, '___sec17'),
+ ('PCA and scikit-learn', 2, None, '___sec18'),
+ ('More on the PCA', 2, None, '___sec19'),
+ ('Incremental PCA', 2, None, '___sec20'),
+ ('Randomized PCA', 2, None, '___sec21'),
+ ('Kernel PCA', 2, None, '___sec22'),
+ ('LLE', 2, None, '___sec23'),
+ ('Other techniques', 2, None, '___sec24')]}
end of tocinfo -->
Suppose we have defined two vectors
-$\hat{x} and \hat{y} with \( n \) elements each. The covariance matrix $\boldsymbol{C}is defined as
+\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as
$$
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} cov[\boldsymbol{x},\boldsymbol{x}] & cov[\boldsymbol{x},\boldsymbol{y}] \\
cov[\boldsymbol{y},\boldsymbol{x}] & cov[\boldsymbol{y},\boldsymbol{y}] \\
@@ -559,7 +566,7 @@ introducing instead the correlation matrix defined via the so-called
correlation function
$$
-corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var\boldsymbol{y}]}}.
+corr[\boldsymbol{x},\boldsymbol{y}]=\frac{cov[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{var[\boldsymbol{x}]\var[\boldsymbol{y}]}}.
$$
@@ -633,10 +640,19 @@ corr[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & corr[\boldsymbol{x}_{p-1},\bolds
$$
-The Numpy function np.cov calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
-The following simple function uses the np.vstack function which takes each vector of dimension \( 1\times n \) and produces a \( 2\times n \) matrix \( \hat{W} \)
+
+The Numpy function np.cov calculates the covariance elements using
+the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
+the exact mean values. The following simple function uses the
+np.vstack function which takes each vector of dimension \( 1\times n \)
+and produces a \( 2\times n \) matrix \( \boldsymbol{W} \)
+
$$
-\hat{W} = \begin{bmatrix} x_0 & y_0 \\
+\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\
x_1 & y_1 \\
x_2 & y_2\\
\dots & \dots \\
@@ -646,9 +662,9 @@ $$
$$
-which in turn is converted into into the \( 3\times 3 \) covariance matrix
-\( \hat{\Sigma} \) via the Numpy function np.cov(). We note that we can also calculate
-the mean value of each set of samples \( \hat{x} \) etc using the Numpy
+which in turn is converted into into the \( 2\times 2 \) covariance matrix
+\( \boldsymbol{C} \) via the Numpy function np.cov(). We note that we can also calculate
+the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
function np.mean(x). We can also extract the eigenvalues of the
covariance matrix through the np.linalg.eig() function.
@@ -657,34 +673,164 @@ covariance matrix through the np.linalg.eig() function.
+The previous example can be converted into the correlation matrix by
+simply scaling the matrix elements with the variances. We should also
+subtract the mean values for each column. This leads to the following
+code which sets up the correlations matrix for the previous example in
+a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors).
+
+
+
+
+
+We see that the matrix elements along the diagonal are one as they
+should be and that the matrix is symmetric. Furthermore, diagonalizing
+this matrix we easily see that it is a positive definite matrix.
+
+
+The above procedure with numpy can be made more compact if we use pandas.
+We whow here how we can set up the correlation matrix using pandas, as done in this simple code
+
+
+
+
+We expand this model to the Franke function discussed above.
+
+
+
+We note here that the covariance is zero for the first rows and
+columns since all matrix elements in the design matrix were set to one
+(we are fitting the function in terms of a polynomial of degree \( n \)).
+
+
+This means that the variance for these elements will be zero and will
+cause problems when we set up the correlation matrix. We can simply
+drop these elements as follows and then construct the correlation
+matrix.
+
+
+
+
+
@@ -699,7 +845,7 @@ X_pca = pca.
@@ -735,7 +881,7 @@ X2D = X_centered
-
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -766,7 +912,7 @@ More material to come here.
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
@@ -796,7 +942,7 @@ X_reduced = pca
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
@@ -808,7 +954,7 @@ instances arrive).
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
@@ -823,7 +969,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
@@ -852,7 +998,7 @@ X_reduced = rbf_pcaLLE
+
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
@@ -864,7 +1010,7 @@ these local relationships are best preserved (more details shortly).
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
diff --git a/doc/pub/DimRed/ipynb/DimRed.ipynb b/doc/pub/DimRed/ipynb/DimRed.ipynb
index 802dd81f3..0e28b6e62 100644
--- a/doc/pub/DimRed/ipynb/DimRed.ipynb
+++ b/doc/pub/DimRed/ipynb/DimRed.ipynb
@@ -412,7 +412,7 @@
"## Introducing the Covariance and Correlation functions\n",
"\n",
"Suppose we have defined two vectors\n",
- "$\\hat{x} and \\hat{y} with $n$ elements each. The covariance matrix $\\boldsymbol{C}is defined as"
+ "$\\hat{x}$ and $\\hat{y}$ with $n$ elements each. The covariance matrix $\\boldsymbol{C}$ is defined as"
]
},
{
@@ -492,7 +492,7 @@
"metadata": {},
"source": [
"$$\n",
- "corr[\\boldsymbol{x},\\boldsymbol{y}]=\\frac{cov[\\boldsymbol{x},\\boldsymbol{y}]}{\\sqrt{var[\\boldsymbol{x}]\\var\\boldsymbol{y}]}}.\n",
+ "corr[\\boldsymbol{x},\\boldsymbol{y}]=\\frac{cov[\\boldsymbol{x},\\boldsymbol{y}]}{\\sqrt{var[\\boldsymbol{x}]\\var[\\boldsymbol{y}]}}.\n",
"$$"
]
},
@@ -628,8 +628,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "The Numpy function **np.cov** calculates the covariance elements using the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have the exact mean values. \n",
- "The following simple function uses the **np.vstack** function which takes each vector of dimension $1\\times n$ and produces a $2\\times n$ matrix $\\hat{W}$"
+ "## Covariance Matrix Examples\n",
+ "\n",
+ "\n",
+ "The Numpy function **np.cov** calculates the covariance elements using\n",
+ "the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have\n",
+ "the exact mean values. The following simple function uses the\n",
+ "**np.vstack** function which takes each vector of dimension $1\\times n$\n",
+ "and produces a $2\\times n$ matrix $\\boldsymbol{W}$"
]
},
{
@@ -637,7 +643,7 @@
"metadata": {},
"source": [
"$$\n",
- "\\hat{W} = \\begin{bmatrix} x_0 & y_0 \\\\\n",
+ "\\boldsymbol{W} = \\begin{bmatrix} x_0 & y_0 \\\\\n",
" x_1 & y_1 \\\\\n",
" x_2 & y_2\\\\\n",
" \\dots & \\dots \\\\\n",
@@ -651,9 +657,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "which in turn is converted into into the $3\\times 3$ covariance matrix\n",
- "$\\hat{\\Sigma}$ via the Numpy function **np.cov()**. We note that we can also calculate\n",
- "the mean value of each set of samples $\\hat{x}$ etc using the Numpy\n",
+ "which in turn is converted into into the $2\\times 2$ covariance matrix\n",
+ "$\\boldsymbol{C}$ via the Numpy function **np.cov()**. We note that we can also calculate\n",
+ "the mean value of each set of samples $\\boldsymbol{x}$ etc using the Numpy\n",
"function **np.mean(x)**. We can also extract the eigenvalues of the\n",
"covariance matrix through the **np.linalg.eig()** function."
]
@@ -668,25 +674,175 @@
"source": [
"# Importing various packages\n",
"import numpy as np\n",
- "\n",
"n = 100\n",
"x = np.random.normal(size=n)\n",
"print(np.mean(x))\n",
"y = 4+3*x+np.random.normal(size=n)\n",
"print(np.mean(y))\n",
- "z = x**3+np.random.normal(size=n)\n",
- "print(np.mean(z))\n",
- "W = np.vstack((x, y, z))\n",
- "Sigma = np.cov(W)\n",
- "print(Sigma)\n",
- "Eigvals, Eigvecs = np.linalg.eig(Sigma)\n",
- "print(Eigvals)"
+ "W = np.vstack((x, y))\n",
+ "C = np.cov(W)\n",
+ "print(C)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
+ "## Correlation Matrix\n",
+ "\n",
+ "The previous example can be converted into the correlation matrix by\n",
+ "simply scaling the matrix elements with the variances. We should also\n",
+ "subtract the mean values for each column. This leads to the following\n",
+ "code which sets up the correlations matrix for the previous example in\n",
+ "a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the $2\\times 2$ correlation matrix (since we have only two vectors)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "n = 100\n",
+ "# define two vectors \n",
+ "x = np.random.random(size=n)\n",
+ "y = 4+3*x+np.random.normal(size=n)\n",
+ "#scaling the x and y vectors \n",
+ "x = x - np.mean(x)\n",
+ "y = y - np.mean(y)\n",
+ "variance_x = np.sum(x@x)/n\n",
+ "variance_y = np.sum(y@y)/n\n",
+ "print(variance_x)\n",
+ "print(variance_y)\n",
+ "cov_xy = np.sum(x@y)/n\n",
+ "cov_xx = np.sum(x@x)/n\n",
+ "cov_yy = np.sum(y@y)/n\n",
+ "C = np.zeros((2,2))\n",
+ "C[0,0]= cov_xx/variance_x\n",
+ "C[1,1]= cov_yy/variance_y\n",
+ "C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)\n",
+ "C[1,0]= C[0,1]\n",
+ "print(C)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We see that the matrix elements along the diagonal are one as they\n",
+ "should be and that the matrix is symmetric. Furthermore, diagonalizing\n",
+ "this matrix we easily see that it is a positive definite matrix.\n",
+ "\n",
+ "The above procedure with **numpy** can be made more compact if we use **pandas**.\n",
+ "\n",
+ "## Correlation Matrix with Pandas\n",
+ "\n",
+ "We whow here how we can set up the correlation matrix using **pandas**, as done in this simple code"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "import pandas as pd\n",
+ "n = 10\n",
+ "x = np.random.normal(size=n)\n",
+ "x = x - np.mean(x)\n",
+ "y = 4+3*x+np.random.normal(size=n)\n",
+ "y = y - np.mean(y)\n",
+ "X = (np.vstack((x, y))).T\n",
+ "print(X)\n",
+ "Xpd = pd.DataFrame(X)\n",
+ "print(Xpd)\n",
+ "correlation_matrix = Xpd.corr()\n",
+ "print(correlation_matrix)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We expand this model to the Franke function discussed above.\n",
+ "\n",
+ "## Correlation Matrix with Pandas and the Franke function"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "# Common imports\n",
+ "import numpy as np\n",
+ "import pandas as pd\n",
+ "\n",
+ "\n",
+ "def FrankeFunction(x,y):\n",
+ "\tterm1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))\n",
+ "\tterm2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))\n",
+ "\tterm3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))\n",
+ "\tterm4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)\n",
+ "\treturn term1 + term2 + term3 + term4\n",
+ "\n",
+ "\n",
+ "def create_X(x, y, n ):\n",
+ "\tif len(x.shape) > 1:\n",
+ "\t\tx = np.ravel(x)\n",
+ "\t\ty = np.ravel(y)\n",
+ "\n",
+ "\tN = len(x)\n",
+ "\tl = int((n+1)*(n+2)/2)\t\t# Number of elements in beta\n",
+ "\tX = np.ones((N,l))\n",
+ "\n",
+ "\tfor i in range(1,n+1):\n",
+ "\t\tq = int((i)*(i+1)/2)\n",
+ "\t\tfor k in range(i+1):\n",
+ "\t\t\tX[:,q+k] = (x**(i-k))*(y**k)\n",
+ "\n",
+ "\treturn X\n",
+ "\n",
+ "\n",
+ "# Making meshgrid of datapoints and compute Franke's function\n",
+ "n = 4\n",
+ "N = 100\n",
+ "x = np.sort(np.random.uniform(0, 1, N))\n",
+ "y = np.sort(np.random.uniform(0, 1, N))\n",
+ "z = FrankeFunction(x, y)\n",
+ "X = create_X(x, y, n=n) \n",
+ "\n",
+ "Xpd = pd.DataFrame(X)\n",
+ "# subtract the mean values and set up the covariance matrix\n",
+ "Xpd = Xpd - Xpd.mean()\n",
+ "covariance_matrix = Xpd.cov()\n",
+ "print(covariance_matrix)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We note here that the covariance is zero for the first rows and\n",
+ "columns since all matrix elements in the design matrix were set to one\n",
+ "(we are fitting the function in terms of a polynomial of degree $n$).\n",
+ "\n",
+ "This means that the variance for these elements will be zero and will\n",
+ "cause problems when we set up the correlation matrix. We can simply\n",
+ "drop these elements as follows and then construct the correlation\n",
+ "matrix. \n",
+ "\n",
+ "\n",
+ "\n",
"## Classical PCA Theorem\n",
"\n",
"\n",
@@ -702,7 +858,7 @@
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 11,
"metadata": {
"collapsed": false
},
@@ -730,7 +886,7 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 12,
"metadata": {
"collapsed": false
},
@@ -757,7 +913,7 @@
},
{
"cell_type": "code",
- "execution_count": 10,
+ "execution_count": 13,
"metadata": {
"collapsed": false
},
@@ -781,7 +937,7 @@
},
{
"cell_type": "code",
- "execution_count": 11,
+ "execution_count": 14,
"metadata": {
"collapsed": false
},
@@ -803,7 +959,7 @@
},
{
"cell_type": "code",
- "execution_count": 12,
+ "execution_count": 15,
"metadata": {
"collapsed": false
},
@@ -833,7 +989,7 @@
},
{
"cell_type": "code",
- "execution_count": 13,
+ "execution_count": 16,
"metadata": {
"collapsed": false
},
@@ -856,7 +1012,7 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 17,
"metadata": {
"collapsed": false
},
@@ -903,7 +1059,7 @@
},
{
"cell_type": "code",
- "execution_count": 15,
+ "execution_count": 18,
"metadata": {
"collapsed": false
},
@@ -948,7 +1104,7 @@
},
{
"cell_type": "code",
- "execution_count": 16,
+ "execution_count": 19,
"metadata": {
"collapsed": false
},
@@ -978,7 +1134,7 @@
},
{
"cell_type": "code",
- "execution_count": 17,
+ "execution_count": 20,
"metadata": {
"collapsed": false
},
@@ -1013,7 +1169,7 @@
},
{
"cell_type": "code",
- "execution_count": 18,
+ "execution_count": 21,
"metadata": {
"collapsed": false
},
diff --git a/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz b/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz
index 990230aad..359365f9f 100644
Binary files a/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz and b/doc/pub/DimRed/ipynb/ipynb-DimRed-src.tar.gz differ
diff --git a/doc/pub/DimRed/pdf/DimRed-minted.pdf b/doc/pub/DimRed/pdf/DimRed-minted.pdf
index 50d765d8b..965c1920a 100644
Binary files a/doc/pub/DimRed/pdf/DimRed-minted.pdf and b/doc/pub/DimRed/pdf/DimRed-minted.pdf differ
diff --git a/doc/src/DimRed/DimRed.do.txt b/doc/src/DimRed/DimRed.do.txt
index 8d74941d7..cdeab6903 100644
--- a/doc/src/DimRed/DimRed.do.txt
+++ b/doc/src/DimRed/DimRed.do.txt
@@ -341,7 +341,7 @@ We have a data set defined by a design/feature matrix $\bm{X}$ (see below for it
===== Introducing the Covariance and Correlation functions =====
Suppose we have defined two vectors
-$\hat{x} and \hat{y} with $n$ elements each. The covariance matrix $\bm{C}is defined as
+$\hat{x}$ and $\hat{y}$ with $n$ elements each. The covariance matrix $\bm{C}$ is defined as
!bt
\[
\bm{C}[\bm{x},\bm{y}] = \begin{bmatrix} cov[\bm{x},\bm{x}] & cov[\bm{x},\bm{y}] \\
@@ -378,7 +378,7 @@ correlation function
!bt
\[
-corr[\bm{x},\bm{y}]=\frac{cov[\bm{x},\bm{y}]}{\sqrt{var[\bm{x}]\var\bm{y}]}}.
+corr[\bm{x},\bm{y}]=\frac{cov[\bm{x},\bm{y}]}{\sqrt{var[\bm{x}]\var[\bm{y}]}}.
\]
!et
@@ -456,14 +456,20 @@ corr[\bm{x}_{p-1},\bm{x}_0] & corr[\bm{x}_{p-1},\bm{x}_1] & corr[\bm{x}_{p-1},
!et
+!split
+===== Covariance Matrix Examples =====
+The Numpy function _np.cov_ calculates the covariance elements using
+the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have
+the exact mean values. The following simple function uses the
+_np.vstack_ function which takes each vector of dimension $1\times n$
+and produces a $2\times n$ matrix $\bm{W}$
+
-The Numpy function _np.cov_ calculates the covariance elements using the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have the exact mean values.
-The following simple function uses the _np.vstack_ function which takes each vector of dimension $1\times n$ and produces a $2\times n$ matrix $\hat{W}$
!bt
\[
-\hat{W} = \begin{bmatrix} x_0 & y_0 \\
+\bm{W} = \begin{bmatrix} x_0 & y_0 \\
x_1 & y_1 \\
x_2 & y_2\\
\dots & \dots \\
@@ -473,30 +479,144 @@ The following simple function uses the _np.vstack_ function which takes each vec
\]
!et
-which in turn is converted into into the $3\times 3$ covariance matrix
-$\hat{\Sigma}$ via the Numpy function _np.cov()_. We note that we can also calculate
-the mean value of each set of samples $\hat{x}$ etc using the Numpy
+which in turn is converted into into the $2\times 2$ covariance matrix
+$\bm{C}$ via the Numpy function _np.cov()_. We note that we can also calculate
+the mean value of each set of samples $\bm{x}$ etc using the Numpy
function _np.mean(x)_. We can also extract the eigenvalues of the
covariance matrix through the _np.linalg.eig()_ function.
!bc pycod
# Importing various packages
import numpy as np
-
n = 100
x = np.random.normal(size=n)
print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
print(np.mean(y))
-z = x**3+np.random.normal(size=n)
-print(np.mean(z))
-W = np.vstack((x, y, z))
-Sigma = np.cov(W)
-print(Sigma)
-Eigvals, Eigvecs = np.linalg.eig(Sigma)
-print(Eigvals)
+W = np.vstack((x, y))
+C = np.cov(W)
+print(C)
!ec
+!split
+===== Correlation Matrix =====
+
+The previous example can be converted into the correlation matrix by
+simply scaling the matrix elements with the variances. We should also
+subtract the mean values for each column. This leads to the following
+code which sets up the correlations matrix for the previous example in
+a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the $2\times 2$ correlation matrix (since we have only two vectors).
+
+!bc pycod
+import numpy as np
+n = 100
+# define two vectors
+x = np.random.random(size=n)
+y = 4+3*x+np.random.normal(size=n)
+#scaling the x and y vectors
+x = x - np.mean(x)
+y = y - np.mean(y)
+variance_x = np.sum(x@x)/n
+variance_y = np.sum(y@y)/n
+print(variance_x)
+print(variance_y)
+cov_xy = np.sum(x@y)/n
+cov_xx = np.sum(x@x)/n
+cov_yy = np.sum(y@y)/n
+C = np.zeros((2,2))
+C[0,0]= cov_xx/variance_x
+C[1,1]= cov_yy/variance_y
+C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
+C[1,0]= C[0,1]
+print(C)
+!ec
+
+We see that the matrix elements along the diagonal are one as they
+should be and that the matrix is symmetric. Furthermore, diagonalizing
+this matrix we easily see that it is a positive definite matrix.
+
+The above procedure with _numpy_ can be made more compact if we use _pandas_.
+
+!split
+===== Correlation Matrix with Pandas =====
+
+We whow here how we can set up the correlation matrix using _pandas_, as done in this simple code
+!bc pycod
+import numpy as np
+import pandas as pd
+n = 10
+x = np.random.normal(size=n)
+x = x - np.mean(x)
+y = 4+3*x+np.random.normal(size=n)
+y = y - np.mean(y)
+X = (np.vstack((x, y))).T
+print(X)
+Xpd = pd.DataFrame(X)
+print(Xpd)
+correlation_matrix = Xpd.corr()
+print(correlation_matrix)
+!ec
+
+
+We expand this model to the Franke function discussed above.
+
+!split
+===== Correlation Matrix with Pandas and the Franke function =====
+
+!bc pycod
+# Common imports
+import numpy as np
+import pandas as pd
+
+
+def FrankeFunction(x,y):
+ term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
+ term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
+ term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
+ term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
+ return term1 + term2 + term3 + term4
+
+
+def create_X(x, y, n ):
+ if len(x.shape) > 1:
+ x = np.ravel(x)
+ y = np.ravel(y)
+
+ N = len(x)
+ l = int((n+1)*(n+2)/2) # Number of elements in beta
+ X = np.ones((N,l))
+
+ for i in range(1,n+1):
+ q = int((i)*(i+1)/2)
+ for k in range(i+1):
+ X[:,q+k] = (x**(i-k))*(y**k)
+
+ return X
+
+
+# Making meshgrid of datapoints and compute Franke's function
+n = 4
+N = 100
+x = np.sort(np.random.uniform(0, 1, N))
+y = np.sort(np.random.uniform(0, 1, N))
+z = FrankeFunction(x, y)
+X = create_X(x, y, n=n)
+
+Xpd = pd.DataFrame(X)
+# subtract the mean values and set up the covariance matrix
+Xpd = Xpd - Xpd.mean()
+covariance_matrix = Xpd.cov()
+print(covariance_matrix)
+!ec
+
+We note here that the covariance is zero for the first rows and
+columns since all matrix elements in the design matrix were set to one
+(we are fitting the function in terms of a polynomial of degree $n$).
+
+This means that the variance for these elements will be zero and will
+cause problems when we set up the correlation matrix. We can simply
+drop these elements as follows and then construct the correlation
+matrix.
diff --git a/doc/src/DimRed/covariance.py b/doc/src/DimRed/covariance.py
new file mode 100644
index 000000000..5f47aeac5
--- /dev/null
+++ b/doc/src/DimRed/covariance.py
@@ -0,0 +1,24 @@
+# Importing various packages
+import numpy as np
+n = 100
+# define two vectors
+x = np.random.random(size=n)
+y = 4+3*x+np.random.normal(size=n)
+#scaling the x and y vectors
+x = x - np.mean(x)
+y = y - np.mean(y)
+variance_x = np.sum(x@x)/n
+variance_y = np.sum(y@y)/n
+print(variance_x)
+print(variance_y)
+cov_xy = np.sum(x@y)/n
+cov_xx = np.sum(x@x)/n
+cov_yy = np.sum(y@y)/n
+C = np.zeros((2,2))
+C[0,0]= cov_xx/variance_x
+C[1,1]= cov_yy/variance_y
+C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
+C[1,0]= C[0,1]
+print(C)
+Eigvals, Eigvecs = np.linalg.eig(C)
+print(Eigvals)
diff --git a/doc/src/DimRed/covfrance.py b/doc/src/DimRed/covfrance.py
new file mode 100644
index 000000000..ab57bf2bd
--- /dev/null
+++ b/doc/src/DimRed/covfrance.py
@@ -0,0 +1,43 @@
+# Common imports
+import numpy as np
+import pandas as pd
+
+
+def FrankeFunction(x,y):
+ term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
+ term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
+ term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
+ term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
+ return term1 + term2 + term3 + term4
+
+
+def create_X(x, y, n ):
+ if len(x.shape) > 1:
+ x = np.ravel(x)
+ y = np.ravel(y)
+
+ N = len(x)
+ l = int((n+1)*(n+2)/2) # Number of elements in beta
+ X = np.ones((N,l))
+
+ for i in range(1,n+1):
+ q = int((i)*(i+1)/2)
+ for k in range(i+1):
+ X[:,q+k] = (x**(i-k))*(y**k)
+
+ return X
+
+
+# Making meshgrid of datapoints and compute Franke's function
+n = 4
+N = 1000
+x = np.sort(np.random.uniform(0, 1, N))
+y = np.sort(np.random.uniform(0, 1, N))
+z = FrankeFunction(x, y)
+X = create_X(x, y, n=n)
+
+Xpd = pd.DataFrame(X)
+Xpd = Xpd - Xpd.mean()
+correlation_matrix = Xpd.cov()
+print(correlation_matrix)
+
diff --git a/doc/src/DimRed/newcov.py b/doc/src/DimRed/newcov.py
new file mode 100644
index 000000000..8e6fab13e
--- /dev/null
+++ b/doc/src/DimRed/newcov.py
@@ -0,0 +1,28 @@
+# Importing various packages
+import numpy as np
+n = 10
+x = np.random.normal(size=n)
+x = x - np.mean(x)
+y = 4+3*x+np.random.normal(size=n)
+y = y - np.mean(y)
+X = (np.vstack((x, y))).T
+print(X)
+import pandas as pd
+Xpd = pd.DataFrame(X)
+print(Xpd)
+correlation_matrix = Xpd.corr()
+print(correlation_matrix)
+
+
+
+variance_x = np.sum(x@x)/n
+variance_y = np.sum(y@y)/n
+cov_xy = np.sum(x@y)/n
+cov_xx = np.sum(x@x)/n
+cov_yy = np.sum(y@y)/n
+C = np.zeros((2,2))
+C[0,0]= cov_xx/variance_x
+C[1,1]= cov_yy/variance_y
+C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
+C[1,0]= C[0,1]
+print(C)
+
+
+
+Covariance Matrix Examples
$$
-\hat{W} = \begin{bmatrix} x_0 & y_0 \\
+\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\
x_1 & y_1 \\
x_2 & y_2\\
\dots & \dots \\
@@ -669,9 +678,9 @@ $$
# Importing various packages
import numpy as np
-
n = 100
x = np.random.normal(size=n)
print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
print(np.mean(y))
-z = x**3+np.random.normal(size=n)
-print(np.mean(z))
-W = np.vstack((x, y, z))
-Sigma = np.cov(W)
-print(Sigma)
-Eigvals, Eigvecs = np.linalg.eig(Sigma)
-print(Eigvals)
+W = np.vstack((x, y))
+C = np.cov(W)
+print(C)
Classical PCA Theorem
+Correlation Matrix
+
+import numpy as np
+n = 100
+# define two vectors
+x = np.random.random(size=n)
+y = 4+3*x+np.random.normal(size=n)
+#scaling the x and y vectors
+x = x - np.mean(x)
+y = y - np.mean(y)
+variance_x = np.sum(x@x)/n
+variance_y = np.sum(y@y)/n
+print(variance_x)
+print(variance_y)
+cov_xy = np.sum(x@y)/n
+cov_xx = np.sum(x@x)/n
+cov_yy = np.sum(y@y)/n
+C = np.zeros((2,2))
+C[0,0]= cov_xx/variance_x
+C[1,1]= cov_yy/variance_y
+C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
+C[1,0]= C[0,1]
+print(C)
+
Prof of the PCA Theorem
+Correlation Matrix with Pandas
+
+import numpy as np
+import pandas as pd
+n = 10
+x = np.random.normal(size=n)
+x = x - np.mean(x)
+y = 4+3*x+np.random.normal(size=n)
+y = y - np.mean(y)
+X = (np.vstack((x, y))).T
+print(X)
+Xpd = pd.DataFrame(X)
+print(Xpd)
+correlation_matrix = Xpd.corr()
+print(correlation_matrix)
+
Getting started with PCA
+Correlation Matrix with Pandas and the Franke function
+
+# Common imports
+import numpy as np
+import pandas as pd
+
+
+def FrankeFunction(x,y):
+ term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
+ term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
+ term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
+ term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
+ return term1 + term2 + term3 + term4
+
+
+def create_X(x, y, n ):
+ if len(x.shape) > 1:
+ x = np.ravel(x)
+ y = np.ravel(y)
+
+ N = len(x)
+ l = int((n+1)*(n+2)/2) # Number of elements in beta
+ X = np.ones((N,l))
+
+ for i in range(1,n+1):
+ q = int((i)*(i+1)/2)
+ for k in range(i+1):
+ X[:,q+k] = (x**(i-k))*(y**k)
+
+ return X
+
+
+# Making meshgrid of datapoints and compute Franke's function
+n = 4
+N = 100
+x = np.sort(np.random.uniform(0, 1, N))
+y = np.sort(np.random.uniform(0, 1, N))
+z = FrankeFunction(x, y)
+X = create_X(x, y, n=n)
+
+Xpd = pd.DataFrame(X)
+# subtract the mean values and set up the covariance matrix
+Xpd = Xpd - Xpd.mean()
+covariance_matrix = Xpd.cov()
+print(covariance_matrix)
+
Classical PCA Theorem
+Prof of the PCA Theorem
+Getting started with PCA
Principal Component Analysis
+Principal Component Analysis
PCA and scikit-learn
+PCA and scikit-learn
More on the PCA
+More on the PCA
Incremental PCA
+Incremental PCA
Randomized PCA
+Randomized PCA
Kernel PCA
+Kernel PCA
LLE
+LLE
Other techniques
+Other techniques
+
+Covariance Matrix Examples
+
+# Importing various packages
import numpy as np
-
n = 100
x = np.random.normal(size=n)
print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
print(np.mean(y))
-z = x**3+np.random.normal(size=n)
-print(np.mean(z))
-W = np.vstack((x, y, z))
-Sigma = np.cov(W)
-print(Sigma)
-Eigvals, Eigvecs = np.linalg.eig(Sigma)
-print(Eigvals)
+W = np.vstack((x, y))
+C = np.cov(W)
+print(C)
-Classical PCA Theorem
+Correlation Matrix
+
+import numpy as np
+n = 100
+# define two vectors
+x = np.random.random(size=n)
+y = 4+3*x+np.random.normal(size=n)
+#scaling the x and y vectors
+x = x - np.mean(x)
+y = y - np.mean(y)
+variance_x = np.sum(x@x)/n
+variance_y = np.sum(y@y)/n
+print(variance_x)
+print(variance_y)
+cov_xy = np.sum(x@y)/n
+cov_xx = np.sum(x@x)/n
+cov_yy = np.sum(y@y)/n
+C = np.zeros((2,2))
+C[0,0]= cov_xx/variance_x
+C[1,1]= cov_yy/variance_y
+C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
+C[1,0]= C[0,1]
+print(C)
+
-Prof of the PCA Theorem
+Correlation Matrix with Pandas
+
+import numpy as np
+import pandas as pd
+n = 10
+x = np.random.normal(size=n)
+x = x - np.mean(x)
+y = 4+3*x+np.random.normal(size=n)
+y = y - np.mean(y)
+X = (np.vstack((x, y))).T
+print(X)
+Xpd = pd.DataFrame(X)
+print(Xpd)
+correlation_matrix = Xpd.corr()
+print(correlation_matrix)
+
-Getting started with PCA
+Correlation Matrix with Pandas and the Franke function
+
+# Common imports
+import numpy as np
+import pandas as pd
+
+
+def FrankeFunction(x,y):
+ term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
+ term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
+ term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
+ term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
+ return term1 + term2 + term3 + term4
+
+
+def create_X(x, y, n ):
+ if len(x.shape) > 1:
+ x = np.ravel(x)
+ y = np.ravel(y)
+
+ N = len(x)
+ l = int((n+1)*(n+2)/2) # Number of elements in beta
+ X = np.ones((N,l))
+
+ for i in range(1,n+1):
+ q = int((i)*(i+1)/2)
+ for k in range(i+1):
+ X[:,q+k] = (x**(i-k))*(y**k)
+
+ return X
+
+
+# Making meshgrid of datapoints and compute Franke's function
+n = 4
+N = 100
+x = np.sort(np.random.uniform(0, 1, N))
+y = np.sort(np.random.uniform(0, 1, N))
+z = FrankeFunction(x, y)
+X = create_X(x, y, n=n)
+
+Xpd = pd.DataFrame(X)
+# subtract the mean values and set up the covariance matrix
+Xpd = Xpd - Xpd.mean()
+covariance_matrix = Xpd.cov()
+print(covariance_matrix)
+
+
+Classical PCA Theorem
+
+
+
+Prof of the PCA Theorem
+
+
+
+Getting started with PCA
-Principal Component Analysis
+Principal Component Analysis
PCA and scikit-learn
+PCA and scikit-learn
-More on the PCA
+More on the PCA
-Incremental PCA
+Incremental PCA
-Randomized PCA
+Randomized PCA
-Kernel PCA
+Kernel PCA
-LLE
+LLE
-Other techniques
+Other techniques
+
+Covariance Matrix Examples
+
+# Importing various packages
import numpy as np
-
n = 100
x = np.random.normal(size=n)
print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
print(np.mean(y))
-z = x**3+np.random.normal(size=n)
-print(np.mean(z))
-W = np.vstack((x, y, z))
-Sigma = np.cov(W)
-print(Sigma)
-Eigvals, Eigvecs = np.linalg.eig(Sigma)
-print(Eigvals)
+W = np.vstack((x, y))
+C = np.cov(W)
+print(C)
-Classical PCA Theorem
+Correlation Matrix
+
+import numpy as np
+n = 100
+# define two vectors
+x = np.random.random(size=n)
+y = 4+3*x+np.random.normal(size=n)
+#scaling the x and y vectors
+x = x - np.mean(x)
+y = y - np.mean(y)
+variance_x = np.sum(x@x)/n
+variance_y = np.sum(y@y)/n
+print(variance_x)
+print(variance_y)
+cov_xy = np.sum(x@y)/n
+cov_xx = np.sum(x@x)/n
+cov_yy = np.sum(y@y)/n
+C = np.zeros((2,2))
+C[0,0]= cov_xx/variance_x
+C[1,1]= cov_yy/variance_y
+C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
+C[1,0]= C[0,1]
+print(C)
+
-Prof of the PCA Theorem
+Correlation Matrix with Pandas
+
+import numpy as np
+import pandas as pd
+n = 10
+x = np.random.normal(size=n)
+x = x - np.mean(x)
+y = 4+3*x+np.random.normal(size=n)
+y = y - np.mean(y)
+X = (np.vstack((x, y))).T
+print(X)
+Xpd = pd.DataFrame(X)
+print(Xpd)
+correlation_matrix = Xpd.corr()
+print(correlation_matrix)
+
-Getting started with PCA
+Correlation Matrix with Pandas and the Franke function
+
+# Common imports
+import numpy as np
+import pandas as pd
+
+
+def FrankeFunction(x,y):
+ term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
+ term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
+ term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
+ term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
+ return term1 + term2 + term3 + term4
+
+
+def create_X(x, y, n ):
+ if len(x.shape) > 1:
+ x = np.ravel(x)
+ y = np.ravel(y)
+
+ N = len(x)
+ l = int((n+1)*(n+2)/2) # Number of elements in beta
+ X = np.ones((N,l))
+
+ for i in range(1,n+1):
+ q = int((i)*(i+1)/2)
+ for k in range(i+1):
+ X[:,q+k] = (x**(i-k))*(y**k)
+
+ return X
+
+
+# Making meshgrid of datapoints and compute Franke's function
+n = 4
+N = 100
+x = np.sort(np.random.uniform(0, 1, N))
+y = np.sort(np.random.uniform(0, 1, N))
+z = FrankeFunction(x, y)
+X = create_X(x, y, n=n)
+
+Xpd = pd.DataFrame(X)
+# subtract the mean values and set up the covariance matrix
+Xpd = Xpd - Xpd.mean()
+covariance_matrix = Xpd.cov()
+print(covariance_matrix)
+
+
+Classical PCA Theorem
+
+
+
+Prof of the PCA Theorem
+
+
+
+Getting started with PCA
-Principal Component Analysis
+Principal Component Analysis
PCA and scikit-learn
+PCA and scikit-learn
-More on the PCA
+More on the PCA
-Incremental PCA
+Incremental PCA
-Randomized PCA
+Randomized PCA
-Kernel PCA
+Kernel PCA
LLE
-Other techniques
+Other techniques