diff --git a/doc/Projects/2018/hw1/html/hw1-bs.html b/doc/Projects/2018/hw1/html/hw1-bs.html new file mode 100644 index 000000000..0041f1940 --- /dev/null +++ b/doc/Projects/2018/hw1/html/hw1-bs.html @@ -0,0 +1,287 @@ + + + + + + + +Homework 1 + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +

 

 

 

+ + + + + + +
+

Homework 1

+ +

+ + +

+Data Analysis and Machine Learning FYS-STK3155/FYS4155 +
+ +

+ + +

Department of Physics, University of Oslo, Norway
+
+

+

Aug 27, 2018

+
+

+

+ +

Exercise 1

+ +

+The first exercise here is of a mere technical art. We want you have installed + +

+ +We will make extensive use of Python as programming language and its +myriad of available libraries. You will find +IPython/Jupyter notebooks invaluable in your work. You can run R +codes in the Jupyter/IPython notebooks, with the immediate benefit of +visualizing your data. You can also use compiled languages like C++, +Rust, Fortran etc if you prefer. The focus in these lectures will be +on Python, but we will provide many code examples for those of you who +prefer R or compiled languages. You can integrate C++ codes and R in for example +a Jupyter notebook. + +

+If you have Python installed (we recommend Python3) and you feel +pretty familiar with installing different packages, we recommend that +you install the following Python packages via pip as + +

    +
  1. pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow
  2. +
+ +For Tensorflow, we recommend following the instructions in the text of +Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly + +

+We will come back to tensorflow later. + +

+For Python3, replace pip with pip3. + +

+For OSX users we recommend, after having installed Xcode, to +install brew. Brew allows for a seamless installation of additional +software via for example + +

    +
  1. brew install python3
  2. +
+ +For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, +you can use pip as well and simply install Python as + +
    +
  1. sudo apt-get install python3 (or python for pyhton2.7)
  2. +
+ +If you don't want to perform these operations separately and venture +into the hassle of exploring how to set up dependencies and paths, we +recommend two widely used distrubutions which set up all relevant +dependencies for Python, namely + + + +which is an open source +distribution of the Python and R programming languages for large-scale +data processing, predictive analytics, and scientific computing, that +aims to simplify package management and deployment. Package versions +are managed by the package management system conda. + + + +is a Python +distribution for scientific and analytic computing distribution and +analysis environment, available for free and under a commercial +license. + +

+We recommend using Anaconda. + +

Exercise 2

+ +

+We will generate our own dataset for function \( y(x) \) where \( x \in [0,2] \) and defined by random numbers computed with the uniform distribution. The function \( y \) is a quadratic polynomial in \( x \) with added stochastic noise according to the normal distribution \( \cal {N}(0,1) \). +The following simple Python instructions define our \( x \) and \( y \) values (with 100 data points). +

+ + +

x = np.random.rand(100,1)
+y = 5*x*x+0.1*np.random.randn(100,1)
+
+
    +
  1. Write your own code (following the examples under the regression slides for computing the parametrization of the data set fitting a second-order polynomial.
  2. +
  3. Use thereafter scikit-learn (see again the examples in the regression slides) and compare with your own code.
  4. +
  5. Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
  6. +
+ +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +$$ + +and the \( R^2 \) score function. +If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as +$$ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +$$ + +where we have defined the mean value of \( \hat{y} \) as +$$ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +$$ + +

+Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. + +

Exercise 3, variance of the parameters \( \beta \) in linear regression

+ +

+Show that the variance of the parameters \( \beta \) in the linear regression method (chapter 3, equation (3.8) of Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer) is given as + +$$ +mathrm{Var}(\hat{\beta}) = \left(\hat{X}^T\hat{X}\right)^{-1}\sigma^2, +$$ + +with +$$ +\sigma^2 = \frac{1}{N-p-1}\sum_{i=1}{N} (y_i-\tilde{y}_i)^2, +$$ + +where we have assumed that we fit a function of degree \( p-1 \) (for example a polynomial in \( x \)). + +

+ +

+ + +
+ + + + + + + +
+ © 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/Projects/2018/hw1/html/hw1.html b/doc/Projects/2018/hw1/html/hw1.html new file mode 100644 index 000000000..4111259da --- /dev/null +++ b/doc/Projects/2018/hw1/html/hw1.html @@ -0,0 +1,235 @@ + + + + + + + +Homework 1 + + + + + + + + + + + + + + + + + + + + + + + +

Homework 1

+ +

+ + +

+Data Analysis and Machine Learning FYS-STK3155/FYS4155 +
+ +

+ + +

Department of Physics, University of Oslo, Norway
+
+

+

Aug 27, 2018

+
+ +

Exercise 1

+ +

+The first exercise here is of a mere technical art. We want you have installed + +

+ +We will make extensive use of Python as programming language and its +myriad of available libraries. You will find +IPython/Jupyter notebooks invaluable in your work. You can run R +codes in the Jupyter/IPython notebooks, with the immediate benefit of +visualizing your data. You can also use compiled languages like C++, +Rust, Fortran etc if you prefer. The focus in these lectures will be +on Python, but we will provide many code examples for those of you who +prefer R or compiled languages. You can integrate C++ codes and R in for example +a Jupyter notebook. + +

+If you have Python installed (we recommend Python3) and you feel +pretty familiar with installing different packages, we recommend that +you install the following Python packages via pip as + +

    +
  1. pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow
  2. +
+ +For Tensorflow, we recommend following the instructions in the text of +Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly + +

+We will come back to tensorflow later. + +

+For Python3, replace pip with pip3. + +

+For OSX users we recommend, after having installed Xcode, to +install brew. Brew allows for a seamless installation of additional +software via for example + +

    +
  1. brew install python3
  2. +
+ +For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, +you can use pip as well and simply install Python as + +
    +
  1. sudo apt-get install python3 (or python for pyhton2.7)
  2. +
+ +If you don't want to perform these operations separately and venture +into the hassle of exploring how to set up dependencies and paths, we +recommend two widely used distrubutions which set up all relevant +dependencies for Python, namely + + + +which is an open source +distribution of the Python and R programming languages for large-scale +data processing, predictive analytics, and scientific computing, that +aims to simplify package management and deployment. Package versions +are managed by the package management system conda. + + + +is a Python +distribution for scientific and analytic computing distribution and +analysis environment, available for free and under a commercial +license. + +

+We recommend using Anaconda. + +

Exercise 2

+ +

+We will generate our own dataset for function \( y(x) \) where \( x \in [0,2] \) and defined by random numbers computed with the uniform distribution. The function \( y \) is a quadratic polynomial in \( x \) with added stochastic noise according to the normal distribution \( \cal {N}(0,1) \). +The following simple Python instructions define our \( x \) and \( y \) values (with 100 data points). +

+ + +

x = np.random.rand(100,1)
+y = 5*x*x+0.1*np.random.randn(100,1)
+
+
    +
  1. Write your own code (following the examples under the regression slides for computing the parametrization of the data set fitting a second-order polynomial.
  2. +
  3. Use thereafter scikit-learn (see again the examples in the regression slides) and compare with your own code.
  4. +
  5. Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
  6. +
+ +$$ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +$$ + +and the \( R^2 \) score function. +If \( \tilde{\hat{y}}_i \) is the predicted value of the \( i-th \) sample and \( y_i \) is the corresponding true value, then the score \( R^2 \) is defined as +$$ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +$$ + +where we have defined the mean value of \( \hat{y} \) as +$$ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +$$ + +

+Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. + +

Exercise 3, variance of the parameters \( \beta \) in linear regression

+ +

+Show that the variance of the parameters \( \beta \) in the linear regression method (chapter 3, equation (3.8) of Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer) is given as + +$$ +mathrm{Var}(\hat{\beta}) = \left(\hat{X}^T\hat{X}\right)^{-1}\sigma^2, +$$ + +with +$$ +\sigma^2 = \frac{1}{N-p-1}\sum_{i=1}{N} (y_i-\tilde{y}_i)^2, +$$ + +where we have assumed that we fit a function of degree \( p-1 \) (for example a polynomial in \( x \)). + + + + +

+ © 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license +
+ + + + + + diff --git a/doc/Projects/2018/hw1/ipynb/ipynb-hw1-src.tar.gz b/doc/Projects/2018/hw1/ipynb/ipynb-hw1-src.tar.gz new file mode 100644 index 000000000..a0d469c37 Binary files /dev/null and b/doc/Projects/2018/hw1/ipynb/ipynb-hw1-src.tar.gz differ diff --git a/doc/Projects/2018/hw1/pdf/hw1.p.tex b/doc/Projects/2018/hw1/pdf/hw1.p.tex new file mode 100644 index 000000000..7baad6833 --- /dev/null +++ b/doc/Projects/2018/hw1/pdf/hw1.p.tex @@ -0,0 +1,302 @@ +%% +%% Automatically generated file from DocOnce source +%% (https://github.com/hplgit/doconce/) +%% +%% +% #ifdef PTEX2TEX_EXPLANATION +%% +%% The file follows the ptex2tex extended LaTeX format, see +%% ptex2tex: http://code.google.com/p/ptex2tex/ +%% +%% Run +%% ptex2tex myfile +%% or +%% doconce ptex2tex myfile +%% +%% to turn myfile.p.tex into an ordinary LaTeX file myfile.tex. +%% (The ptex2tex program: http://code.google.com/p/ptex2tex) +%% Many preprocess options can be added to ptex2tex or doconce ptex2tex +%% +%% ptex2tex -DMINTED myfile +%% doconce ptex2tex myfile envir=minted +%% +%% ptex2tex will typeset code environments according to a global or local +%% .ptex2tex.cfg configure file. doconce ptex2tex will typeset code +%% according to options on the command line (just type doconce ptex2tex to +%% see examples). If doconce ptex2tex has envir=minted, it enables the +%% minted style without needing -DMINTED. +% #endif + +% #define PREAMBLE + +% #ifdef PREAMBLE +%-------------------- begin preamble ---------------------- + +\documentclass[% +oneside, % oneside: electronic viewing, twoside: printing +final, % draft: marks overfull hboxes, figures with paths +10pt]{article} + +\listfiles % print all files needed to compile this document + +\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb} +\usepackage[table]{xcolor} +\usepackage{bm,ltablex,microtype} + +\usepackage[pdftex]{graphicx} + +\usepackage{ptex2tex} +% #ifdef MINTED +\usepackage{minted} +\usemintedstyle{default} +% #endif + +\usepackage[T1]{fontenc} +%\usepackage[latin1]{inputenc} +\usepackage{ucs} +\usepackage[utf8x]{inputenc} + +\usepackage{lmodern} % Latin Modern fonts derived from Computer Modern + +% Hyperlinks in PDF: +\definecolor{linkcolor}{rgb}{0,0,0.4} +\usepackage{hyperref} +\hypersetup{ + breaklinks=true, + colorlinks=true, + linkcolor=linkcolor, + urlcolor=linkcolor, + citecolor=black, + filecolor=black, + %filecolor=blue, + pdfmenubar=true, + pdftoolbar=true, + bookmarksdepth=3 % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC + } +%\hyperbaseurl{} % hyperlinks are relative to this root + +\setcounter{tocdepth}{2} % levels in table of contents + +% --- fancyhdr package for fancy headers --- +\usepackage{fancyhdr} +\fancyhf{} % sets both header and footer to nothing +\renewcommand{\headrulewidth}{0pt} +\fancyfoot[LE,RO]{\thepage} +% Ensure copyright on titlepage (article style) and chapter pages (book style) +\fancypagestyle{plain}{ + \fancyhf{} + \fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}} +% \renewcommand{\footrulewidth}{0mm} + \renewcommand{\headrulewidth}{0mm} +} +% Ensure copyright on titlepages with \thispagestyle{empty} +\fancypagestyle{empty}{ + \fancyhf{} + \fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}} + \renewcommand{\footrulewidth}{0mm} + \renewcommand{\headrulewidth}{0mm} +} + +\pagestyle{fancy} + + +% prevent orhpans and widows +\clubpenalty = 10000 +\widowpenalty = 10000 + +% --- end of standard preamble for documents --- + + +% insert custom LaTeX commands... + +\raggedbottom +\makeindex +\usepackage[totoc]{idxlayout} % for index in the toc +\usepackage[nottoc]{tocbibind} % for references/bibliography in the toc + +%-------------------- end preamble ---------------------- + +\begin{document} + +% matching end for #ifdef PREAMBLE +% #endif + +\newcommand{\exercisesection}[1]{\subsection*{#1}} + + +% ------------------- main content ---------------------- + + + +% ----------------- title ------------------------- + +\thispagestyle{empty} + +\begin{center} +{\LARGE\bf +\begin{spacing}{1.25} +Homework 1 +\end{spacing} +} +\end{center} + +% ----------------- author(s) ------------------------- + +\begin{center} +{\bf \href{{http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html}}{Data Analysis and Machine Learning FYS-STK3155/FYS4155}} +\end{center} + + \begin{center} +% List of all institutions: +\centerline{{\small Department of Physics, University of Oslo, Norway}} +\end{center} + +% ----------------- end author(s) ------------------------- + +% --- begin date --- +\begin{center} +Aug 27, 2018 +\end{center} +% --- end date --- + +\vspace{1cm} + + +\subsection{Exercise 1} + +The first exercise here is of a mere technical art. We want you have installed +\begin{itemize} +\item git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo \href{{https://www.uio.no/tjenester/it/maskin/filer/versjonskontroll/github.html}}{GitHub facilities}. + +\item Install various Python packages +\end{itemize} + +\noindent +We will make extensive use of Python as programming language and its +myriad of available libraries. You will find +IPython/Jupyter notebooks invaluable in your work. You can run \textbf{R} +codes in the Jupyter/IPython notebooks, with the immediate benefit of +visualizing your data. You can also use compiled languages like C++, +Rust, Fortran etc if you prefer. The focus in these lectures will be +on Python, but we will provide many code examples for those of you who +prefer R or compiled languages. You can integrate C++ codes and R in for example +a Jupyter notebook. + + +If you have Python installed (we recommend Python3) and you feel +pretty familiar with installing different packages, we recommend that +you install the following Python packages via \textbf{pip} as + +\begin{enumerate} +\item pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow +\end{enumerate} + +\noindent +For \textbf{Tensorflow}, we recommend following the instructions in the text of +\href{{http://shop.oreilly.com/product/0636920052289.do}}{Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly} + +We will come back to \textbf{tensorflow} later. + +For Python3, replace \textbf{pip} with \textbf{pip3}. + +For OSX users we recommend, after having installed Xcode, to +install \textbf{brew}. Brew allows for a seamless installation of additional +software via for example + +\begin{enumerate} +\item brew install python3 +\end{enumerate} + +\noindent +For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, +you can use \textbf{pip} as well and simply install Python as + +\begin{enumerate} +\item sudo apt-get install python3 (or python for pyhton2.7) +\end{enumerate} + +\noindent +If you don't want to perform these operations separately and venture +into the hassle of exploring how to set up dependencies and paths, we +recommend two widely used distrubutions which set up all relevant +dependencies for Python, namely + +\begin{itemize} +\item \href{{https://docs.anaconda.com/}}{Anaconda}, +\end{itemize} + +\noindent +which is an open source +distribution of the Python and R programming languages for large-scale +data processing, predictive analytics, and scientific computing, that +aims to simplify package management and deployment. Package versions +are managed by the package management system \textbf{conda}. + +\begin{itemize} +\item \href{{https://www.enthought.com/product/canopy/}}{Enthought canopy} +\end{itemize} + +\noindent +is a Python +distribution for scientific and analytic computing distribution and +analysis environment, available for free and under a commercial +license. + +We recommend using \textbf{Anaconda}. + +\subsection{Exercise 2} + +We will generate our own dataset for function $y(x)$ where $x \in [0,2]$ and defined by random numbers computed with the uniform distribution. The function $y$ is a quadratic polynomial in $x$ with added stochastic noise according to the normal distribution $\cal {N}(0,1)$. +The following simple Python instructions define our $x$ and $y$ values (with 100 data points). +\bpycod +x = np.random.rand(100,1) +y = 5*x*x+0.1*np.random.randn(100,1) +\epycod + +\begin{enumerate} +\item Write your own code (following the examples under the \href{{https://compphysics.github.io/MachineLearning/doc/pub/Regression/html/Regression-bs.html}}{regression slides} for computing the parametrization of the data set fitting a second-order polynomial. + +\item Use thereafter \textbf{scikit-learn} (see again the examples in the regression slides) and compare with your own code. + +\item Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as +\end{enumerate} + +\noindent +\[ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +\] +and the $R^2$ score function. +If $\tilde{\hat{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as +\[ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +\] +where we have defined the mean value of $\hat{y}$ as +\[ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +\] + +Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. + + + + +\subsection{Exercise 3, variance of the parameters $\beta$ in linear regression} + +Show that the variance of the parameters $\beta$ in the linear regression method (chapter 3, equation (3.8) of \href{{https://www.springer.com/gp/book/9780387848570}}{Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer}) is given as + +\[ +mathrm{Var}(\hat{\beta}) = \left(\hat{X}^T\hat{X}\right)^{-1}\sigma^2, +\] +with +\[ +\sigma^2 = \frac{1}{N-p-1}\sum_{i=1}{N} (y_i-\tilde{y}_i)^2, +\] +where we have assumed that we fit a function of degree $p-1$ (for example a polynomial in $x$). + + +% ------------------- end of main content --------------- + +% #ifdef PREAMBLE +\end{document} +% #endif + diff --git a/doc/Projects/2018/hw1/pdf/hw1.pdf b/doc/Projects/2018/hw1/pdf/hw1.pdf new file mode 100644 index 000000000..aaaed7d36 Binary files /dev/null and b/doc/Projects/2018/hw1/pdf/hw1.pdf differ diff --git a/doc/Projects/2018/hw1/pdf/hw1.tex b/doc/Projects/2018/hw1/pdf/hw1.tex new file mode 100644 index 000000000..4e6872d7d --- /dev/null +++ b/doc/Projects/2018/hw1/pdf/hw1.tex @@ -0,0 +1,270 @@ +%% +%% Automatically generated file from DocOnce source +%% (https://github.com/hplgit/doconce/) +%% +%% + + +%-------------------- begin preamble ---------------------- + +\documentclass[% +oneside, % oneside: electronic viewing, twoside: printing +final, % draft: marks overfull hboxes, figures with paths +10pt]{article} + +\listfiles % print all files needed to compile this document + +\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb} +\usepackage[table]{xcolor} +\usepackage{bm,ltablex,microtype} + +\usepackage[pdftex]{graphicx} + +\usepackage{fancyvrb} % packages needed for verbatim environments + +\usepackage[T1]{fontenc} +%\usepackage[latin1]{inputenc} +\usepackage{ucs} +\usepackage[utf8x]{inputenc} + +\usepackage{lmodern} % Latin Modern fonts derived from Computer Modern + +% Hyperlinks in PDF: +\definecolor{linkcolor}{rgb}{0,0,0.4} +\usepackage{hyperref} +\hypersetup{ + breaklinks=true, + colorlinks=true, + linkcolor=linkcolor, + urlcolor=linkcolor, + citecolor=black, + filecolor=black, + %filecolor=blue, + pdfmenubar=true, + pdftoolbar=true, + bookmarksdepth=3 % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC + } +%\hyperbaseurl{} % hyperlinks are relative to this root + +\setcounter{tocdepth}{2} % levels in table of contents + +% --- fancyhdr package for fancy headers --- +\usepackage{fancyhdr} +\fancyhf{} % sets both header and footer to nothing +\renewcommand{\headrulewidth}{0pt} +\fancyfoot[LE,RO]{\thepage} +% Ensure copyright on titlepage (article style) and chapter pages (book style) +\fancypagestyle{plain}{ + \fancyhf{} + \fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}} +% \renewcommand{\footrulewidth}{0mm} + \renewcommand{\headrulewidth}{0mm} +} +% Ensure copyright on titlepages with \thispagestyle{empty} +\fancypagestyle{empty}{ + \fancyhf{} + \fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}} + \renewcommand{\footrulewidth}{0mm} + \renewcommand{\headrulewidth}{0mm} +} + +\pagestyle{fancy} + + +% prevent orhpans and widows +\clubpenalty = 10000 +\widowpenalty = 10000 + +% --- end of standard preamble for documents --- + + +% insert custom LaTeX commands... + +\raggedbottom +\makeindex +\usepackage[totoc]{idxlayout} % for index in the toc +\usepackage[nottoc]{tocbibind} % for references/bibliography in the toc + +%-------------------- end preamble ---------------------- + +\begin{document} + +% matching end for #ifdef PREAMBLE + +\newcommand{\exercisesection}[1]{\subsection*{#1}} + + +% ------------------- main content ---------------------- + + + +% ----------------- title ------------------------- + +\thispagestyle{empty} + +\begin{center} +{\LARGE\bf +\begin{spacing}{1.25} +Homework 1 +\end{spacing} +} +\end{center} + +% ----------------- author(s) ------------------------- + +\begin{center} +{\bf \href{{http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html}}{Data Analysis and Machine Learning FYS-STK3155/FYS4155}} +\end{center} + + \begin{center} +% List of all institutions: +\centerline{{\small Department of Physics, University of Oslo, Norway}} +\end{center} + +% ----------------- end author(s) ------------------------- + +% --- begin date --- +\begin{center} +Aug 27, 2018 +\end{center} +% --- end date --- + +\vspace{1cm} + + +\subsection*{Exercise 1} + +The first exercise here is of a mere technical art. We want you have installed +\begin{itemize} +\item git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo \href{{https://www.uio.no/tjenester/it/maskin/filer/versjonskontroll/github.html}}{GitHub facilities}. + +\item Install various Python packages +\end{itemize} + +\noindent +We will make extensive use of Python as programming language and its +myriad of available libraries. You will find +IPython/Jupyter notebooks invaluable in your work. You can run \textbf{R} +codes in the Jupyter/IPython notebooks, with the immediate benefit of +visualizing your data. You can also use compiled languages like C++, +Rust, Fortran etc if you prefer. The focus in these lectures will be +on Python, but we will provide many code examples for those of you who +prefer R or compiled languages. You can integrate C++ codes and R in for example +a Jupyter notebook. + + +If you have Python installed (we recommend Python3) and you feel +pretty familiar with installing different packages, we recommend that +you install the following Python packages via \textbf{pip} as + +\begin{enumerate} +\item pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow +\end{enumerate} + +\noindent +For \textbf{Tensorflow}, we recommend following the instructions in the text of +\href{{http://shop.oreilly.com/product/0636920052289.do}}{Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly} + +We will come back to \textbf{tensorflow} later. + +For Python3, replace \textbf{pip} with \textbf{pip3}. + +For OSX users we recommend, after having installed Xcode, to +install \textbf{brew}. Brew allows for a seamless installation of additional +software via for example + +\begin{enumerate} +\item brew install python3 +\end{enumerate} + +\noindent +For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, +you can use \textbf{pip} as well and simply install Python as + +\begin{enumerate} +\item sudo apt-get install python3 (or python for pyhton2.7) +\end{enumerate} + +\noindent +If you don't want to perform these operations separately and venture +into the hassle of exploring how to set up dependencies and paths, we +recommend two widely used distrubutions which set up all relevant +dependencies for Python, namely + +\begin{itemize} +\item \href{{https://docs.anaconda.com/}}{Anaconda}, +\end{itemize} + +\noindent +which is an open source +distribution of the Python and R programming languages for large-scale +data processing, predictive analytics, and scientific computing, that +aims to simplify package management and deployment. Package versions +are managed by the package management system \textbf{conda}. + +\begin{itemize} +\item \href{{https://www.enthought.com/product/canopy/}}{Enthought canopy} +\end{itemize} + +\noindent +is a Python +distribution for scientific and analytic computing distribution and +analysis environment, available for free and under a commercial +license. + +We recommend using \textbf{Anaconda}. + +\subsection*{Exercise 2} + +We will generate our own dataset for function $y(x)$ where $x \in [0,2]$ and defined by random numbers computed with the uniform distribution. The function $y$ is a quadratic polynomial in $x$ with added stochastic noise according to the normal distribution $\cal {N}(0,1)$. +The following simple Python instructions define our $x$ and $y$ values (with 100 data points). +\begin{print} +x = np.random.rand(100,1) +y = 5*x*x+0.1*np.random.randn(100,1) +\end{print} + +\begin{enumerate} +\item Write your own code (following the examples under the \href{{https://compphysics.github.io/MachineLearning/doc/pub/Regression/html/Regression-bs.html}}{regression slides} for computing the parametrization of the data set fitting a second-order polynomial. + +\item Use thereafter \textbf{scikit-learn} (see again the examples in the regression slides) and compare with your own code. + +\item Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as +\end{enumerate} + +\noindent +\[ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +\] +and the $R^2$ score function. +If $\tilde{\hat{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as +\[ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +\] +where we have defined the mean value of $\hat{y}$ as +\[ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +\] + +Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. + + + + +\subsection*{Exercise 3, variance of the parameters $\beta$ in linear regression} + +Show that the variance of the parameters $\beta$ in the linear regression method (chapter 3, equation (3.8) of \href{{https://www.springer.com/gp/book/9780387848570}}{Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer}) is given as + +\[ +mathrm{Var}(\hat{\beta}) = \left(\hat{X}^T\hat{X}\right)^{-1}\sigma^2, +\] +with +\[ +\sigma^2 = \frac{1}{N-p-1}\sum_{i=1}{N} (y_i-\tilde{y}_i)^2, +\] +where we have assumed that we fit a function of degree $p-1$ (for example a polynomial in $x$). + + +% ------------------- end of main content --------------- + +\end{document} + diff --git a/doc/src/Projects/2018/Exercises/hw1.do.txt b/doc/src/Projects/2018/Exercises/hw1.do.txt index bd2355fab..c19d24d2e 100644 --- a/doc/src/Projects/2018/Exercises/hw1.do.txt +++ b/doc/src/Projects/2018/Exercises/hw1.do.txt @@ -5,8 +5,117 @@ DATE:Today ===== Exercise 1 ===== -Make sure you have installed all necessary t +The first exercise here is of a mere technical art. We want you have installed +* git as a version control software and to establish a user account on a provider like GitHub. Other providers like GitLab etc are equally fine. You can also use the University of Oslo "GitHub facilities":"https://www.uio.no/tjenester/it/maskin/filer/versjonskontroll/github.html". +* Install various Python packages + +We will make extensive use of Python as programming language and its +myriad of available libraries. You will find +IPython/Jupyter notebooks invaluable in your work. You can run _R_ +codes in the Jupyter/IPython notebooks, with the immediate benefit of +visualizing your data. You can also use compiled languages like C++, +Rust, Fortran etc if you prefer. The focus in these lectures will be +on Python, but we will provide many code examples for those of you who +prefer R or compiled languages. You can integrate C++ codes and R in for example +a Jupyter notebook. + + +If you have Python installed (we recommend Python3) and you feel +pretty familiar with installing different packages, we recommend that +you install the following Python packages via _pip_ as + +o pip install numpy scipy matplotlib ipython scikit-learn sympy pandas pillow + +For _Tensorflow_, we recommend following the instructions in the text of +"Aurelien Geron, Hands‑On Machine Learning with Scikit‑Learn and TensorFlow, O'Reilly":"http://shop.oreilly.com/product/0636920052289.do" + +We will come back to _tensorflow_ later. + +For Python3, replace _pip_ with _pip3_. + +For OSX users we recommend, after having installed Xcode, to +install _brew_. Brew allows for a seamless installation of additional +software via for example + +o brew install python3 + +For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, +you can use _pip_ as well and simply install Python as + +o sudo apt-get install python3 (or python for pyhton2.7) + +If you don't want to perform these operations separately and venture +into the hassle of exploring how to set up dependencies and paths, we +recommend two widely used distrubutions which set up all relevant +dependencies for Python, namely + +* "Anaconda":"https://docs.anaconda.com/", + +which is an open source +distribution of the Python and R programming languages for large-scale +data processing, predictive analytics, and scientific computing, that +aims to simplify package management and deployment. Package versions +are managed by the package management system _conda_. + +* "Enthought canopy":"https://www.enthought.com/product/canopy/" + +is a Python +distribution for scientific and analytic computing distribution and +analysis environment, available for free and under a commercial +license. + +We recommend using _Anaconda_. ===== Exercise 2 ===== -===== Exercise 3 ===== +We will generate our own dataset for function $y(x)$ where $x \in [0,2]$ and defined by random numbers computed with the uniform distribution. The function $y$ is a quadratic polynomial in $x$ with added stochastic noise according to the normal distribution $\cal {N}(0,1)$. +The following simple Python instructions define our $x$ and $y$ values (with 100 data points). +!bc pycod +x = np.random.rand(100,1) +y = 5*x*x+0.1*np.random.randn(100,1) +!ec + +o Write your own code (following the examples under the "regression slides":"https://compphysics.github.io/MachineLearning/doc/pub/Regression/html/Regression-bs.html" for computing the parametrization of the data set fitting a second-order polynomial. +o Use thereafter _scikit-learn_ (see again the examples in the regression slides) and compare with your own code. +o Using scikit-learn, compute also the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as +!bt +\[ MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +\] +!et +and the $R^2$ score function. +If $\tilde{\hat{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as +!bt +\[ +R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +\] +!et +where we have defined the mean value of $\hat{y}$ as +!bt +\[ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +\] +!et + +Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. + + + + +===== Exercise 3, variance of the parameters $\beta$ in linear regression ===== + +Show that the variance of the parameters $\beta$ in the linear regression method (chapter 3, equation (3.8) of "Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer":"https://www.springer.com/gp/book/9780387848570") is given as + +!bt +\[ +mathrm{Var}(\hat{\beta}) = \left(\hat{X}^T\hat{X}\right)^{-1}\sigma^2, +\] +!et +with +!bt +\[ +\sigma^2 = \frac{1}{N-p-1}\sum_{i=1}{N} (y_i-\tilde{y}_i)^2, +\] +!et +where we have assumed that we fit a function of degree $p-1$ (for example a polynomial in $x$). + diff --git a/doc/src/Projects/2018/Exercises/hw2.do.txt b/doc/src/Projects/2018/Exercises/hw2.do.txt index ac2a84a73..bd2355fab 100644 --- a/doc/src/Projects/2018/Exercises/hw2.do.txt +++ b/doc/src/Projects/2018/Exercises/hw2.do.txt @@ -1,100 +1,12 @@ -TITLE: Project 1 on Machine Learning, deadline October 1 +TITLE: Homework 1 AUTHOR: "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" {copyright, 1999-present|CC BY-NC} at Department of Physics, University of Oslo, Norway -DATE: May 2018 +DATE:Today -===== Regression analysis and classification ===== +===== Exercise 1 ===== -=== Introduction === +Make sure you have installed all necessary t +===== Exercise 2 ===== - -=== Part a): The data === - - -===== Background literature ===== - - - - -===== Introduction to numerical projects ===== - -Here follows a brief recipe and recommendation on how to write a report for each -project. - - * Give a short description of the nature of the problem and the eventual numerical methods you have used. - - * Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself. - - * Include the source code of your program. Comment your program properly. - - * If possible, try to find analytic solutions, or known limits in order to test your program when developing the code. - - * Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes. - - * Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc. - - * Try to give an interpretation of you results in your answers to the problems. - - * Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it. - - * Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning. - - - - - -===== Format for electronic delivery of report and programs ===== - -The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008 or Python. The following prescription should be followed when preparing the report: - - * Use Devilry to hand in your projects, log in at URL:"http://devilry.ifi.uio.no" with your normal UiO username and password and choose either 'fysstk3155' or 'fysstk4155'. There you can load up the files within the deadline. - - * Upload _only_ the report file! For the source code file(s) you have developed please provide us with your link to your github domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them. - - * In your git repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters. - - * In this and all later projects, you should include tests (for example unit tests) of your code(s). - - * Comments from us on your projects, approval or not, corrections to be made etc can be found under your Devilry domain and are only visible to you and the teachers of the course. - - - -Finally, -we encourage you to work two and two together. Optimal working groups consist of -2-3 students. You can then hand in a common report. - - - -===== Software and needed installations ===== - -If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages, -we recommend that you install the following Python packages via _pip_ as -o pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow -For Python3, replace _pip_ with _pip3_. - -See below for a discussion of _tensorflow_ and _scikit-learn_. - -For OSX users we recommend also, after having installed Xcode, to install _brew_. Brew allows -for a seamless installation of additional software via for example -o brew install python3 - -For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution -you can use _pip_ as well and simply install Python as -o sudo apt-get install python3 (or python for python2.7) -etc etc. - -If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely -o "Anaconda":"https://docs.anaconda.com/" Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system _conda_ -o "Enthought canopy":"https://www.enthought.com/product/canopy/" is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license. - -Popular software packages written in Python for ML are - -* "Scikit-learn":"http://scikit-learn.org/stable/", -* "Tensorflow":"https://www.tensorflow.org/", -* "PyTorch":"http://pytorch.org/" and -* "Keras":"https://keras.io/". -These are all freely available at their respective GitHub sites. They -encompass communities of developers in the thousands or more. And the number -of code developers and contributors keeps increasing. - +===== Exercise 3 ===== diff --git a/doc/web/course.do.txt b/doc/web/course.do.txt index 0fa52779d..d98c3a3ac 100644 --- a/doc/web/course.do.txt +++ b/doc/web/course.do.txt @@ -86,7 +86,17 @@ ${text_types(ch)} !split -===== Projects Fall 2018 ===== +===== Projects and Exercises Fall 2018 ===== + +=== First homework set, week 35 === + * LaTeX and PDF: + * "LaTex file":"http://compphysics.github.io/MachineLearning/doc/Projects/2018/hw1/pdf/hw1.tex" + * "PDF file":"http://compphysics.github.io/MachineLearning/doc/Projects/2018/hw1/pdf/hw1.pdf" + * HTML: + * "Plain html":"http://compphysics.github.io/MachineLearning/doc/Projects/2018/hw1/html/hw1.html" + * "Bootstrap slide style, easy for reading on mobile devices": "http://compphysics.github.io/MachineLearning/doc/Projects/2018/hw1/html/hw1-bs.html" + + === Project 1, Deadline October 1 === * LaTeX and PDF: diff --git a/doc/web/course.html b/doc/web/course.html index 8b8e93559..f17cafa09 100644 --- a/doc/web/course.html +++ b/doc/web/course.html @@ -97,26 +97,27 @@ div { text-align: justify; text-justify: inter-word; } '___sec11'), ('Python and Scikit Learn, a short guide', 2, None, '___sec12'), ('Teach yourself C++', 2, None, '___sec13'), - ('Projects Fall 2018', 2, None, '___sec14'), - ('Project 1, Deadline October 1', 3, None, '___sec15'), - ('Project 2, Deadline November 5', 3, None, '___sec16'), - ('Project 3, Deadline November 30', 3, None, '___sec17'), - ('Course content', 3, None, '___sec18'), - ('Learning outcomes', 2, None, '___sec19'), - ('Prerequisites', 2, None, '___sec20'), - ('The course has two central parts', 2, None, '___sec21'), + ('Projects and Exercises Fall 2018', 2, None, '___sec14'), + ('First homework set, week 35', 3, None, '___sec15'), + ('Project 1, Deadline October 1', 3, None, '___sec16'), + ('Project 2, Deadline November 5', 3, None, '___sec17'), + ('Project 3, Deadline November 30', 3, None, '___sec18'), + ('Course content', 3, None, '___sec19'), + ('Learning outcomes', 2, None, '___sec20'), + ('Prerequisites', 2, None, '___sec21'), + ('The course has two central parts', 2, None, '___sec22'), ('Statistical analysis and optimization of data', 3, None, - '___sec22'), - ('Machine learning', 3, None, '___sec23'), - ('Recommended textbooks', 2, None, '___sec24'), + '___sec23'), + ('Machine learning', 3, None, '___sec24'), + ('Recommended textbooks', 2, None, '___sec25'), ('"Other ' 'textbooks":"https://github.com/CompPhysics/MachineLearning/tree/master/doc/Textbooks"', 2, None, - '___sec25'), - ('Teaching schedule Fall 2018', 2, None, '___sec26')]} + '___sec26'), + ('Teaching schedule Fall 2018', 2, None, '___sec27')]} end of tocinfo --> @@ -555,9 +556,28 @@ formulas in HTML or ipython notebook files. -

Projects Fall 2018

+

Projects and Exercises Fall 2018

-

Project 1, Deadline October 1

+

First homework set, week 35

+ + + +

Project 1, Deadline October 1

-

Project 2, Deadline November 5

+

Project 2, Deadline November 5

-

Project 3, Deadline November 30

+

Project 3, Deadline November 30

-

Course content

+

Course content

Probability theory and statistical methods play a central role in science. Nowadays we are @@ -633,7 +653,7 @@ tools of probability theory, the aim of this course is to expose you to central This course covers thus topics like Monte Carlo methods and Markov chains, Bayesian statistics, error estimates, various linear methods, optimization of data and error analysis and central algorithms in machine learning. The course has several numerical projects and numerical exercises that are meant to illustrate the theory. -

Learning outcomes

+

Learning outcomes

The course introduces a variety of central algorithms and methods @@ -650,19 +670,19 @@ essential for studies of data analysis and machine learning. The course is proje

  • Work on numerical projects to illustrate the theory. The projects play a central role and students are expected to know modern programming languages like Python or C++.
  • -

    Prerequisites

    +

    Prerequisites

    Basic knowledge in programming and numerics. Required courses are the equivalents to the University of Oslo mathematics courses MAT1100, MAT1110, MAT1120 and at least one of the corresponding computing and programming courses INF1000/INF1110 or MAT-INF1100/MAT-INF1100L/BIOS1100/KJM-INF1xxx. -

    The course has two central parts

    +

    The course has two central parts

    1. Statistical analysis and optimization of data
    2. Machine learning
    -

    Statistical analysis and optimization of data

    +

    Statistical analysis and optimization of data

    The following topics will be covered @@ -679,7 +699,7 @@ The following topics will be covered

  • Practical optimization using Singular-value decomposition and least squares for parameterizing data.
  • -

    Machine learning

    +

    Machine learning

    The following topics will be covered @@ -695,14 +715,14 @@ The following topics will be covered All the above topics will be supported by examples, hands-on exercises and project work. -

    Recommended textbooks

    +

    Recommended textbooks

    -

    Other textbooks

    +

    Other textbooks

    General learning book on statistical analysis: @@ -724,7 +744,7 @@ All the above topics will be supported by examples, hands-on exercises and proje -

    Teaching schedule Fall 2018

    +

    Teaching schedule Fall 2018

    Acronyms for textbooks and references to chapter