diff --git a/.gitignore b/.gitignore index 62c8935..936ed51 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -.idea/ \ No newline at end of file +.idea/ +**/tex/** +!**/tex/*.tex +!**/tex/library.bib \ No newline at end of file diff --git a/Task 2/Assignment_2_ru.pdf b/Task 2/Assignment_2_ru.pdf new file mode 100644 index 0000000..9baa109 Binary files /dev/null and b/Task 2/Assignment_2_ru.pdf differ diff --git a/Task 2/tex/Assignment_2_eng.tex b/Task 2/tex/Assignment_2_eng.tex new file mode 100644 index 0000000..81f68e0 --- /dev/null +++ b/Task 2/tex/Assignment_2_eng.tex @@ -0,0 +1,112 @@ +\documentclass[prb, notitlepage, aps, 10pt]{revtex4-2} +\usepackage[utf8]{inputenc} +\usepackage{listings} +\usepackage{amssymb} +\usepackage{graphicx,amsmath} +\usepackage{enumitem} +\usepackage{nicefrac} +\usepackage{amsmath} +\usepackage{graphicx} +\usepackage{amsfonts} +\usepackage{comment} +\usepackage{bm} +\newcommand{\norm}[1]{\left\lVert#1\right\rVert} +\usepackage{hyperref} +\hypersetup{ + colorlinks=true, + linkcolor=blue, + filecolor=magenta, + urlcolor=cyan, + pdfpagemode=FullScreen, +} + +\begin{document} + +\title{\texorpdfstring{Numerical Methods, Fall 2022\\ Assignment 2 [SVD decomposition with applications] \\ Total: 40, Deadline: 21 Oct}{}} + +\maketitle + +\section*{Suggested Reading} + +\begin{itemize} +\item Lectures 4-5 of \cite{trefethen1997numerical} +\item Lecture 2 of \cite{tyrtyshnikov2012brief} +\item \href{https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues}{Making sense of principal component analysis, eigenvectors and eigenvalues} +\end{itemize} + +\section*{Exercises} + +\begin{enumerate} + +\begin{comment} +\item (5) Construct (manually) SVD decomposition of the following matrices: +$$ +(a)\quad\begin{bmatrix} +3 & 0\\ +0 & -2 +\end{bmatrix},\quad +(b)\quad\begin{bmatrix} +0 & 2\\ +0 & 0\\ +0 & 0 +\end{bmatrix},\quad +(c)\quad\begin{bmatrix} +1 & 1\\ +1 & 1 +\end{bmatrix}. +$$ +For the case (b), construct both full and reduced SVD decomposition via \texttt{np.linalg.svd}. +\end{comment} + +\item (10) In this exercise, we will explore three main algorithms available in scientific \lstinline{python} distributions for computation of SVD: \lstinline{numpy.linalg.svd}, \lstinline{scipy.sparse.linalg.svds} and \lstinline{sklearn.utils.extmath.randomized_svd}. To this end: +\begin{itemize} + \item Construct a random $n\times n$ matrix $A$ (with iid elements sampled from standard normal distribution); consider $n=2000$. + \item Using these implementations, construct rank-2 approximations to $A$. You will thus obtain three rank-2 matrices $A_\textrm{svd}$, $A_\textrm{svds}$ and $A_\textrm{rsvd}$. Measure the run--time of these three algorithms for the given task. + \item Compute the error norms: $\Vert A-A_\textrm{svd}\Vert_F$, $\Vert A-A_\textrm{svds}\Vert_F$, $\Vert A-A_\textrm{rsvd}\Vert_F$. Explain the result. +\end{itemize} +\item (5) Let $A$ be $m\times n$ with SVD $A = U\Sigma V^T$. Compute SVDs of the following matrices in terms of $U$, $\Sigma$ and $V$: (i) $\left(A^T A\right)^{-1}$, (ii) $\left(A^T A\right)^{-1}A^T$, (iii) $A\left(A^T A\right)^{-1}$, (iv) $A\left(A^T A\right)^{-1}A^T$. + +\item (10) Consider the matrix: +$$ +\begin{bmatrix} +-2 & 11\\ +-10 & 5 +\end{bmatrix} +$$ +\begin{itemize} + \item List the singular values, left singular vectors and right singular vectors of $A$. The SVD is not unique, so find the one that has the minimal number of minus signs in $U$ and $V$. + \item Draw a labeled picture of the unit ball in $\mathbb{R}^2$ and its image under $A$, together with the singular vectors with the coordinates of their vertices marked. + \item What are $2$-norm and Frobenius norm of $A$? + \item Find $A^{-1}$ not directly, but via SVD. + \item Find the eigenvalues $\lambda_1, \lambda_2$ of $A$. +\end{itemize} +\item (5) The file \path{A.npy} contains the $n\times n$ matrix $A$. Determine the best approximation of $A_{ij}$ in terms of the following anzats, where the variables are separated: $A_{ij}\approx h_i\eta_j$. What is the related relative error of such approximation: +$$ +\delta_{\textrm{err}}=\frac{\sqrt{\sum_{ij}\left(A_{ij}-h_i\eta_j\right)^2}}{\sqrt{\sum_{ij}A_{ij}^2}}? +$$ +How many terms $K$ would an exact representation of the following form: +$$ +A_{ij}=\sum_{\alpha=1}^K h_{\alpha i}\eta_{\alpha j} +$$ +require? +\item (10) In this exercise, you will explore application of SVD to dimensionality reduction. Let us start with loading the dataset: +\lstset{language=Python} +\lstset{frame=lines} +\lstset{label={lst:code_direct}} +\lstset{basicstyle=\ttfamily} +\begin{lstlisting} +from sklearn.datasets import load_digits +digits = load_digits() +A = digits.data +y = digits.target +\end{lstlisting} +\end{enumerate} +so that rows of A contain monochromatic images of digits (64 float values which should be reshaped into $8\times 8$ images) and $y$ contains the digit labels. +\begin{itemize} +\item Inspect the dataset: plot examples of images, corresponding to several digits (say $0, 3, 7$). +\item Normalize the dataset $A$. +\item Use SVD to project the dataset $A$ from $64$ dimensions to $2$ dimensions. Show the colored scatter plot, where colors encode the digits. +\end{itemize} + +\bibliography{library.bib} +\end{document} diff --git a/Task 2/tex/Assignment_2_ru.tex b/Task 2/tex/Assignment_2_ru.tex new file mode 100644 index 0000000..c30e746 --- /dev/null +++ b/Task 2/tex/Assignment_2_ru.tex @@ -0,0 +1,150 @@ +\documentclass[prb, notitlepage, aps, 11pt]{revtex4-2}% +\usepackage[utf8]{inputenc} +\usepackage[T2A]{fontenc} +\usepackage[english, russian]{babel} +\usepackage{listings} +\usepackage{amssymb} +\usepackage{graphicx,amsmath} +\usepackage{enumitem} +\usepackage{nicefrac} +\usepackage{amsmath} +\usepackage{graphicx} +\usepackage{amsfonts} +\usepackage{comment} +\usepackage{bm} +\usepackage{delimset} +\usepackage[pdftitle = a]{hyperref} +\usepackage{datetime} +\hypersetup{ + colorlinks=true, + linkcolor=blue, + filecolor=magenta, + urlcolor=cyan, + pdfpagemode=FullScreen, +} + +\begin{document} + +% \begin{center} +% версия от \today \quad \currenttime +% \end{center} + +\title{\texorpdfstring{ + Численные методы, осень 2022\\ + Задание 2 [SVD-разложение и его применения] \\ + Всего баллов: 40 \ Срок сдачи: 21 октября + }{} +} + +\maketitle + +\section*{Рекомендованная литература} + +\begin{itemize} +\item Лекции 4-5 из \cite{trefethen1997numerical} +\item Лекция 2 из \cite{tyrtyshnikov2012brief} +\item \href{https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues}{StackExchange: в чём смысл собственных векторов, собственных чисел и анализа главных компонент} +\end{itemize} + +\section*{Упражнения} + +\begin{enumerate} + +\begin{comment} +\item (5) Construct (manually) SVD decomposition of the following matrices: +$$ +(a)\quad\begin{bmatrix} +3 & 0\\ +0 & -2 +\end{bmatrix},\quad +(b)\quad\begin{bmatrix} +0 & 2\\ +0 & 0\\ +0 & 0 +\end{bmatrix},\quad +(c)\quad\begin{bmatrix} +1 & 1\\ +1 & 1 +\end{bmatrix}. +$$ +For the case (b), construct both full and reduced SVD decomposition via \path{np.linalg.svd}. +\end{comment} + +\item (10) В этом упражнении мы познакомимся с +тремя основными алгоритмами вычисления сингулярного разложения, доступными в Python: \path{numpy.linalg.svd}, \path{scipy.sparse.linalg.svds} и \path{sklearn.utils.extmath.randomized_svd}. +% +\begin{itemize} + \item Создайте матрицу $A$ размера $n\times n \ (n=2000)$ со случайными элементами из стандартного нормального распределения. + + \item С помощью этих трёх алгоритмов (и теоремы Эккарта-Янга?) аппроксимируйте матрицу $A$ матрицами ранга 2. + Вы получите матрицы $A_\text{svd}$, $A_\text{svds}$ и $A_\text{rsvd}$. Измерьте времена выполнения. + + \item Вычислите нормы отклонений: $\norm{A-A_\text{svd}}_F \quad \norm{A-A_\text{svds}}_F \quad \norm{ A - A_\textrm{rsvd}}_F$\\ Объясните результат. +\end{itemize} +\item (5) Пусть матрица $A$ размером $m\times n$ имеет сингулярное разложение $A = U\Sigma V^T$. +Выразите через $U$, $\Sigma$ и $V$ сингулярные разложения следующих матриц: +\begin{enumerate}[label=(\roman*)] + \item $\left(A^T A\right)^{-1}$ + \item $\left(A^T A\right)^{-1}A^T$ + \item $A\left(A^T A\right)^{-1}$ + \item $A\left(A^T A\right)^{-1}A^T$ +\end{enumerate} + +\item (10) Рассмотрим матрицу: +$$ +\begin{bmatrix} +-2 & 11\\ +-10 & 5 +\end{bmatrix} +$$ +\begin{itemize} + \item Выпишите сингулярные числа, а также левые и правые сингулярные векторы матрицы $A$. Так как SVD-разложение не единственно, найдите то разложение, в котором матрицы $U$ и $V$ имеют наименьшее количество отрицательных элементов. + + \item Нарисуйте единичный круг и его образ под действием оператора $A$. Изобразите сингулярные векторы и отметьте их координаты. + + \item Чему равна спектральная норма и норма Фробениуса матрицы $A$? + + \item Найдите $A^{-1}$ с помощью SVD-разложения. + + \item Найдите собственные числа $\lambda_1$ и $ \lambda_2$ матрицы $A$. +\end{itemize} + +\item (5) В файле \path{A.npy} находится матрица $A$ размера $n\times n$. Определите наилучшее приближение следующего вида, в котором переменные разделяются: $A_{ij} \approx h_i\eta_j$. Чему равна относительная ошибка такой аппроксимации? +$$ +\delta_{\textrm{err}}=\frac{\sqrt{\sum_{ij}\left(A_{ij}-h_i\eta_j\right)^2}}{\sqrt{\sum_{ij}A_{ij}^2}} +$$ +Сколько понадобится слагаемых, чтобы приближение стало точным? +$$ +A_{ij} = \sum_{\alpha=1}^K h_{\alpha i}\eta_{\alpha j} +\qquad +K = {} ? +$$ + +\item (10) В этом упражнении мы применим SVD-разложение к задаче уменьшения размерности. Начнём с загрузки датасета: + +\lstset{language=Python} +\lstset{frame=lines} +\lstset{label={lst:code_direct}} +\lstset{basicstyle=\ttfamily} + +\begin{lstlisting} + from sklearn.datasets import load_digits + digits = load_digits() + A = digits.data + y = digits.target +\end{lstlisting} +% +Каждая строка массива \path{A} состоит из 64 чисел с плавающей точкой, которые задают черно-белое изображение $8 \times 8$. Это изображение цифры. Сама цифра (метка данных) указана в массиве \path{y}. +% +\begin{itemize} + \item Изучите датасет. Постройте изображения нескольких цифр, например, 0, 3, 7 + + \item Отнормируйте датасет \path{A}. + + \item С помощью SVD-разложения спроецируйте датасет, cделайте его из $N \!{\times} 64$--мерного $N \!{\times} 2$--мерным. Постройте двумерный \path{scatter_plot}, окрасьте точки, соответствующие разным цифрам, в разные цвета. +\end{itemize} + +\end{enumerate} + +\bibliography{library.bib} +\end{document} diff --git a/Task 2/tex/library.bib b/Task 2/tex/library.bib new file mode 100644 index 0000000..c3643a3 --- /dev/null +++ b/Task 2/tex/library.bib @@ -0,0 +1,17 @@ +@book{trefethen1997numerical, + title={Numerical Linear Algebra}, + author={Trefethen, L.N. and Bau, D.}, + isbn={9780898719574}, + series={Other Titles in Applied Mathematics}, + year={1997}, + publisher={SIAM} +} + +@book{tyrtyshnikov2012brief, + title={A Brief Introduction to Numerical Analysis}, + author={Tyrtyshnikov, E.E.}, + isbn={9780817681364}, + lccn={97000301}, + year={2012}, + publisher={Birkh{\"a}user Boston} +}