%% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[letterpaper,10pt,english]{sphinxmanual}
\ifdefined\pdfpxdimen
   \let\sphinxpxdimen\pdfpxdimen\else\newdimen\sphinxpxdimen
\fi \sphinxpxdimen=.75bp\relax

\PassOptionsToPackage{warn}{textcomp}
\usepackage[utf8]{inputenc}
\ifdefined\DeclareUnicodeCharacter
% support both utf8 and utf8x syntaxes
  \ifdefined\DeclareUnicodeCharacterAsOptional
    \def\sphinxDUC#1{\DeclareUnicodeCharacter{"#1}}
  \else
    \let\sphinxDUC\DeclareUnicodeCharacter
  \fi
  \sphinxDUC{00A0}{\nobreakspace}
  \sphinxDUC{2500}{\sphinxunichar{2500}}
  \sphinxDUC{2502}{\sphinxunichar{2502}}
  \sphinxDUC{2514}{\sphinxunichar{2514}}
  \sphinxDUC{251C}{\sphinxunichar{251C}}
  \sphinxDUC{2572}{\textbackslash}
\fi
\usepackage{cmap}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,amstext}
\usepackage{babel}



\usepackage{times}
\expandafter\ifx\csname T@LGR\endcsname\relax
\else
% LGR was declared as font encoding
  \substitutefont{LGR}{\rmdefault}{cmr}
  \substitutefont{LGR}{\sfdefault}{cmss}
  \substitutefont{LGR}{\ttdefault}{cmtt}
\fi
\expandafter\ifx\csname T@X2\endcsname\relax
  \expandafter\ifx\csname T@T2A\endcsname\relax
  \else
  % T2A was declared as font encoding
    \substitutefont{T2A}{\rmdefault}{cmr}
    \substitutefont{T2A}{\sfdefault}{cmss}
    \substitutefont{T2A}{\ttdefault}{cmtt}
  \fi
\else
% X2 was declared as font encoding
  \substitutefont{X2}{\rmdefault}{cmr}
  \substitutefont{X2}{\sfdefault}{cmss}
  \substitutefont{X2}{\ttdefault}{cmtt}
\fi


\usepackage[Bjarne]{fncychap}
\usepackage[,numfigreset=1,mathnumfig]{sphinx}

\fvset{fontsize=\small}
\usepackage{geometry}


% Include hyperref last.
\usepackage{hyperref}
% Fix anchor placement for figures with captions.
\usepackage{hypcap}% it must be loaded after hyperref.
% Set up styles of URL: it should be placed after hyperref.
\urlstyle{same}


\usepackage{sphinxmessages}




\title{Applied Machine Learning and Data Analysis}
\date{Sep 20, 2020}
\release{}
\author{Morten Hjorth\sphinxhyphen{}Jensen}
\newcommand{\sphinxlogo}{\vbox{}}
\renewcommand{\releasename}{}
\makeindex
\begin{document}

\pagestyle{empty}
\sphinxmaketitle
\pagestyle{plain}
\sphinxtableofcontents
\pagestyle{normal}
\phantomsection\label{\detokenize{chapter1::doc}}








\sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

Date: \sphinxstylestrong{Sep 19, 2020}

Copyright 1999\sphinxhyphen{}2020, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license


\chapter{Introduction}
\label{\detokenize{chapter1:introduction}}
During the last two decades there has been a swift and amazing
development of Machine Learning techniques and algorithms that impact
many areas in not only Science and Technology but also the Humanities,
Social Sciences, Medicine, Law, indeed, almost all possible
disciplines. The applications are incredibly many, from self\sphinxhyphen{}driving
cars to solving high\sphinxhyphen{}dimensional differential equations or complicated
quantum mechanical many\sphinxhyphen{}body problems. Machine Learning is perceived
by many as one of the main disruptive techniques nowadays.

Statistics, Data science and Machine Learning form important
fields of research in modern science.  They describe how to learn and
make predictions from data, as well as allowing us to extract
important correlations about physical process and the underlying laws
of motion in large data sets. The latter, big data sets, appear
frequently in essentially all disciplines, from the traditional
Science, Technology, Mathematics and Engineering fields to Life
Science, Law, education research, the Humanities and the Social
Sciences.

It has become more
and more common to see research projects on big data in for example
the Social Sciences where extracting patterns from complicated survey
data is one of many research directions.  Having a solid grasp of data
analysis and machine learning is thus becoming central to scientific
computing in many fields, and competences and skills within the fields
of machine learning and scientific computing are nowadays strongly
requested by many potential employers. The latter cannot be
overstated, familiarity with machine learning has almost become a
prerequisite for many of the most exciting employment opportunities,
whether they are in bioinformatics, life science, physics or finance,
in the private or the public sector. This author has had several
students or met students who have been hired recently based on their
skills and competences in scientific computing and data science, often
with marginal knowledge of machine learning.

Machine learning is a subfield of computer science, and is closely
related to computational statistics.  It evolved from the study of
pattern recognition in artificial intelligence (AI) research, and has
made contributions to AI tasks like computer vision, natural language
processing and speech recognition. Many of the methods we will study are also
strongly rooted in basic mathematics and physics research.

Ideally, machine learning represents the science of giving computers
the ability to learn without being explicitly programmed.  The idea is
that there exist generic algorithms which can be used to find patterns
in a broad class of data sets without having to write code
specifically for each problem. The algorithm will build its own logic
based on the data.  You should however always keep in mind that
machines and algorithms are to a large extent developed by humans. The
insights and knowledge we have about a specific system, play a central
role when we develop a specific machine learning algorithm.

Machine learning is an extremely rich field, in spite of its young
age. The increases we have seen during the last three decades in
computational capabilities have been followed by developments of
methods and techniques for analyzing and handling large date sets,
relying heavily on statistics, computer science and mathematics.  The
field is rather new and developing rapidly. Popular software packages
written in Python for machine learning like
\sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}learn},
\sphinxhref{https://www.tensorflow.org/}{Tensorflow},
\sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, all
freely available at their respective GitHub sites, encompass
communities of developers in the thousands or more. And the number of
code developers and contributors keeps increasing. Not all the
algorithms and methods can be given a rigorous mathematical
justification, opening up thereby large rooms for experimenting and
trial and error and thereby exciting new developments.  However, a
solid command of linear algebra, multivariate theory, probability
theory, statistical data analysis, understanding errors and Monte
Carlo methods are central elements in a proper understanding of many
of algorithms and methods we will discuss.


\chapter{Learning outcomes}
\label{\detokenize{chapter1:learning-outcomes}}
These sets of lectures aim at giving you an overview of central aspects of
statistical data analysis as well as some of the central algorithms
used in machine learning.  We will introduce a variety of central
algorithms and methods essential for studies of data analysis and
machine learning.

Hands\sphinxhyphen{}on projects and experimenting with data and algorithms plays a central role in
these lectures, and our hope is, through the various
projects and exercises, to  expose you to fundamental
research problems in these fields, with the aim to reproduce state of
the art scientific results. You  will learn to develop and
structure codes for studying these systems, get acquainted with
computing facilities and learn to handle large scientific projects. A
good scientific and ethical conduct is emphasized throughout the
course. More specifically, you will
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
Learn about basic data analysis, Bayesian statistics, Monte Carlo methods, data optimization and machine learning;

\item {} 
Be capable of extending the acquired knowledge to other systems and cases;

\item {} 
Have an understanding of central algorithms used in data analysis and machine learning;

\item {} 
Gain knowledge of central aspects of Monte Carlo methods, Markov chains, Gibbs samplers and their possible applications, from numerical integration to simulation of stock markets;

\item {} 
Understand methods for regression and classification;

\item {} 
Learn about neural network, genetic algorithms and Boltzmann machines;

\item {} 
Work on numerical projects to illustrate the theory. The projects play a central role and you are expected to know modern programming languages like Python or C++, in addition to a basic knowledge of linear algebra (typically taught during the first one or two years of undergraduate studies).

\end{enumerate}

There are several topics we will cover here, spanning from
statistical data analysis and its basic concepts such as expectation
values, variance, covariance, correlation functions and errors, via
well\sphinxhyphen{}known probability distribution functions like the uniform
distribution, the binomial distribution, the Poisson distribution and
simple and multivariate normal distributions to central elements of
Bayesian statistics and modeling. We will also remind the reader about
central elements from linear algebra and standard methods based on
linear algebra used to optimize (minimize) functions (the family of gradient descent methods)
and the Singular\sphinxhyphen{}value decomposition and
least square methods for parameterizing data.

We will also cover Monte Carlo methods, Markov chains, well\sphinxhyphen{}known
algorithms for sampling stochastic events like the Metropolis\sphinxhyphen{}Hastings
and Gibbs sampling methods. An important aspect of all our
calculations is a proper estimation of errors. Here we will also
discuss famous resampling techniques like the blocking, the bootstrapping
and the jackknife methods and the infamous bias\sphinxhyphen{}variance tradeoff.

The second part of the material covers several algorithms used in
machine learning.


\chapter{Machine Learning, a small (and probably biased) introduction}
\label{\detokenize{chapter1:machine-learning-a-small-and-probably-biased-introduction}}
Ideally, machine learning represents the science of giving computers
the ability to learn without being explicitly programmed.  The idea is
that there exist generic algorithms which can be used to find patterns
in a broad class of data sets without having to write code
specifically for each problem. The algorithm will build its own logic
based on the data.  You should however always keep in mind that
machines and algorithms are to a large extent developed by humans. The
insights and knowledge we have about a specific system, play a central
role when we develop a specific machine learning algorithm.


\chapter{Machine Learning, an extremely rich field}
\label{\detokenize{chapter1:machine-learning-an-extremely-rich-field}}
Machine learning is an extremely rich field, in spite of its young
age. The increases we have seen during the last  decades in
computational capabilities have been followed by developments of
methods and techniques for analyzing and handling large date sets,
relying heavily on statistics, computer science and mathematics.  The
field is rather new and developing rapidly. Popular software libraries
written in Python for machine learning like
\sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}learn},
\sphinxhref{https://www.tensorflow.org/}{Tensorflow},
\sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, all
freely available at their respective GitHub sites, encompass
communities of developers in the thousands or more. And the number of
code developers and contributors keeps increasing.


\chapter{A multidisciplinary approach}
\label{\detokenize{chapter1:a-multidisciplinary-approach}}
Not all the
algorithms and methods can be given a rigorous mathematical
justification (for example decision trees and random forests), opening up thereby large rooms for experimenting and
trial and error and thereby exciting new developments.  However, a
solid command of linear algebra, multivariate theory, probability
theory, statistical data analysis, understanding errors and Monte
Carlo methods are central elements in a proper understanding of many
of the algorithms and methods we will discuss.


\chapter{Types of Machine Learning}
\label{\detokenize{chapter1:types-of-machine-learning}}
The approaches to machine learning are many, but are often split into
two main categories.  In \sphinxstyleemphasis{supervised learning} we know the answer to a
problem, and let the computer deduce the logic behind it. On the other
hand, \sphinxstyleemphasis{unsupervised learning} is a method for finding patterns and
relationship in data sets without any prior knowledge of the system.
Some authours also operate with a third category, namely
\sphinxstyleemphasis{reinforcement learning}. This is a paradigm of learning inspired by
behavioral psychology, where learning is achieved by trial\sphinxhyphen{}and\sphinxhyphen{}error,
solely from rewards and punishment.

Another way to categorize machine learning tasks is to consider the
desired output of a system.  Some of the most common tasks are:
\begin{itemize}
\item {} 
Classification: Outputs are divided into two or more classes. The goal is to   produce a model that assigns inputs into one of these classes. An example is to identify  digits based on pictures of hand\sphinxhyphen{}written ones. Classification is often supervised learning.

\item {} 
Regression: Finding a functional relationship between an input data set and a reference data set.   The goal is to construct a function that maps input data to continuous output values.

\item {} 
Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand.  It is thus a form of unsupervised learning.

\end{itemize}


\chapter{Essential elements of ML}
\label{\detokenize{chapter1:essential-elements-of-ml}}
The methods we cover have three main topics in common, irrespective of
whether we deal with supervised or unsupervised learning.
\begin{itemize}
\item {} 
The first ingredient is normally our data set (which can be subdivided into training, validation  and test data). Many find the most difficult part of using Machine Learning to be the set up of your data in a meaningful way.

\item {} 
The second item is a model which is normally a function of some parameters.  The model reflects our knowledge of the system (or lack thereof). As an example, if we know that our data show a behavior similar to what would be predicted by a polynomial, fitting our data to a polynomial of some degree would then determin our model.

\item {} 
The last ingredient is a so\sphinxhyphen{}called \sphinxstylestrong{cost/loss} function (or error function) which allows us to present an estimate on how good our model is in reproducing the data it is supposed to train.

\end{itemize}


\chapter{An optimization/minimization problem}
\label{\detokenize{chapter1:an-optimization-minimization-problem}}
At the heart of basically all Machine Learning algorithms we will encounter so\sphinxhyphen{}called minimization or optimization algorithms. A large family of such methods are so\sphinxhyphen{}called \sphinxstylestrong{gradient methods}.


\chapter{A Frequentist approach to data analysis}
\label{\detokenize{chapter1:a-frequentist-approach-to-data-analysis}}
When you hear phrases like \sphinxstylestrong{predictions and estimations} and
\sphinxstylestrong{correlations and causations}, what do you think of?  May be you think
of the difference between classifying new data points and generating
new data points.
Or perhaps you consider that correlations represent some kind of symmetric statements like
if \(A\) is correlated with \(B\), then \(B\) is correlated with
\(A\). Causation on the other hand is directional, that is if \(A\) causes \(B\), \(B\) does not
necessarily cause \(A\).

These concepts are in some sense the difference between machine
learning and statistics. In machine learning and prediction based
tasks, we are often interested in developing algorithms that are
capable of learning patterns from given data in an automated fashion,
and then using these learned patterns to make predictions or
assessments of newly given data. In many cases, our primary concern
is the quality of the predictions or assessments, and we are less
concerned about the underlying patterns that were learned in order
to make these predictions.

In machine learning we normally use \sphinxhref{https://en.wikipedia.org/wiki/Frequentist\_inference}{a so\sphinxhyphen{}called frequentist approach},
where the aim is to make predictions and find correlations. We focus
less on for example extracting a probability distribution function (PDF). The PDF can be
used in turn to make estimations and find causations such as given \(A\)
what is the likelihood of finding \(B\).


\chapter{What is a good model?}
\label{\detokenize{chapter1:what-is-a-good-model}}
In science and engineering we often end up in situations where we want to infer (or learn) a
quantitative model \(M\) for a given set of sample points \(\boldsymbol{X} \in [x_1, x_2,\dots x_N]\).

As we will see repeatedely in these lectures, we could try to fit these data points to a model given by a
straight line, or if we wish to be more sophisticated to a more complex
function.

The reason for inferring such a model is that it
serves many useful purposes. On the one hand, the model can reveal information
encoded in the data or underlying mechanisms from which the data were generated. For instance, we could discover important
corelations that relate interesting physics interpretations.

In addition, it can simplify the representation of the given data set and help
us in making predictions about  future data samples.

A first important consideration to keep in mind is that inferring the \sphinxstyleemphasis{correct} model
for a given data set is an elusive, if not impossible, task. The fundamental difficulty
is that if we are not specific about what we mean by a \sphinxstyleemphasis{correct} model, there
could easily be many different models that fit the given data set \sphinxstyleemphasis{equally well}.


\chapter{What is a good model? Can we define it?}
\label{\detokenize{chapter1:what-is-a-good-model-can-we-define-it}}
The central question is this: what leads us to say that a model is correct or
optimal for a given data set? To make the model inference problem well posed, i.e.,
to guarantee that there is a unique optimal model for the given data, we need to
impose additional assumptions or restrictions on the class of models considered. To
this end, we should not be looking for just any model that can describe the data.
Instead, we should look for a \sphinxstylestrong{model} \(M\) that is the best among a restricted class
of models. In addition, to make the model inference problem computationally
tractable, we need to specify how restricted the class of models needs to be. A
common strategy is to start
with the simplest possible class of models that is just necessary to describe the data
or solve the problem at hand. More precisely, the model class should be rich enough
to contain at least one model that can fit the data to a desired accuracy and yet be
restricted enough that it is relatively simple to find the best model for the given data.

Thus, the most popular strategy is to start from the
simplest class of models and increase the complexity of the models only when the
simpler models become inadequate. For instance, if we work with a regression problem to fit a set of sample points, one
may first try the simplest class of models, namely linear models, followed obviously by more complex models.

How to evaluate which model fits best the data is something we will come back to over and over again in these set of lectures.


\chapter{Choice of Programming Language}
\label{\detokenize{chapter1:choice-of-programming-language}}
Python plays nowadays a central role in the development of machine
learning techniques and tools for data analysis. In particular, seen
the wealth of machine learning and data analysis libraries written in
Python, easy to use libraries with immediate visualization(and not the
least impressive galleries of existing examples), the popularity of the
Jupyter notebook framework with the possibility to run \sphinxstylestrong{R} codes or
compiled programs written in C++, and much more made our choice of
programming language for this series of lectures easy. However,
since the focus here is not only on using existing Python libraries such
as \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}, \sphinxstylestrong{Tensorflow} and \sphinxstylestrong{Pytorch}, but also on developing your own
algorithms and codes, we will as far as possible present many of these
algorithms either as a Python codes or C++ or Fortran (or other languages) codes.


\chapter{Data handling, machine learning  and ethical aspects}
\label{\detokenize{chapter1:data-handling-machine-learning-and-ethical-aspects}}
In most of the cases we will study, we will either generate the data
to analyze ourselves (both for supervised learning and unsupervised
learning) or we will recur again and again to data present in say
\sphinxstylestrong{Scikit\sphinxhyphen{}Learn} or \sphinxstylestrong{Tensorflow}.  Many of the examples we end up
dealing with are from a privacy and data protection point of view,
rather inoccuous and boring results of numerical
calculations. However, this does not hinder us from developing a sound
ethical attitude to the data we use, how we analyze the data and how
we handle the data.

The most immediate and simplest possible ethical aspects deal with our
approach to the scientific process. Nowadays, with version control
software like \sphinxhref{https://git-scm.com/}{Git} and various online
repositories like \sphinxhref{https://github.com/}{Github},
\sphinxhref{https://about.gitlab.com/}{Gitlab} etc, we can easily make our codes
and data sets we have used, freely and easily accessible to a wider
community. This helps us almost automagically in making our science
reproducible. The large open\sphinxhyphen{}source development communities involved
in say \sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}Learn},
\sphinxhref{https://www.tensorflow.org/}{Tensorflow},
\sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, are
all excellent examples of this. The codes can be tested and improved
upon continuosly, helping thereby our scientific community at large in
developing data analysis and machine learning tools.  It is much
easier today to gain traction and acceptance for making your science
reproducible. From a societal stand, this is an important element
since many of the developers are employees of large public institutions like
universities and research labs. Our fellow taxpayers do deserve to get
something back for their bucks.

However, this more mechanical aspect of the ethics of science (in
particular the reproducibility of scientific results) is something
which is obvious and everybody should do so as part of the dialectics of
science.  The fact that many scientists are not willing to share their codes or
data is detrimental to the scientific discourse.

Before we proceed, we should add a disclaimer. Even though
we may dream of computers developing some kind of higher learning
capabilities, at the end (even if the artificial intelligence
community keeps touting our ears full of fancy futuristic avenues), it is we, yes you reading these lines,
who end up constructing and instructing, via various algorithms, the
machine learning approaches. Self\sphinxhyphen{}driving cars for example, rely on sofisticated
programs which take into account all possible situations a car can
encounter. In addition, extensive usage of training data from GPS
information, maps etc, are typically fed into the software for
self\sphinxhyphen{}driving cars. Adding to this various sensors and cameras that
feed information to the programs, there are zillions of ethical issues
which arise from this.

For self\sphinxhyphen{}driving cars, where basically many of the standard machine
learning algorithms discussed here enter into the codes, at a certain
stage we have to make choices. Yes, we , the lads and lasses who wrote
a program for a specific brand of a self\sphinxhyphen{}driving car.  As an example,
all carmakers have as their utmost priority the security of the
driver and the accompanying passengers. A famous European carmaker, which is
one of the leaders in the market of self\sphinxhyphen{}driving cars, had \sphinxstylestrong{if}
statements of the following type: suppose there are two obstacles in
front of you and you cannot avoid to collide with one of them. One of
the obstacles is a monstertruck while the other one is a kindergarten
class trying to cross the road. The self\sphinxhyphen{}driving car algo would then
opt for the hitting the small folks instead of the monstertruck, since
the likelihood of surving a collision with our future citizens, is
much higher.

This leads to serious ethical aspects. Why should we opt for such an
option? Who decides and who is entitled to make such choices? Keep in
mind that many of the algorithms you will encounter in this series of
lectures or hear about later, are indeed based on simple programming
instructions. And you are very likely to be one of the people who may
end up writing such a code. Thus, developing a sound ethical attitude
to what we do, an approach well beyond the simple mechanistic one of
making our science available and reproducible, is much needed. The
example of the self\sphinxhyphen{}driving cars is just one of infinitely many cases
where we have to make choices. When you analyze data on economic
inequalities, who guarantees that you are not weighting some data in a
particular way, perhaps because you dearly want a specific conclusion
which may support your political views? Or what about the recent
claims that a famous IT company like Apple has a sexist bias on the
their recently \sphinxhref{https://qz.com/1748321/the-role-of-goldman-sachs-algorithms-in-the-apple-credit-card-scandal/}{launched credit card}?

We do not have the answers here, nor will we venture into a deeper
discussions of these aspects, but we want you think over these topics
in a more overarching way.  A statistical data analysis with its dry
numbers and graphs meant to guide the eye, does not necessarily
reflect the truth, whatever that is.  As a scientist, and after a
university education, you are supposedly a better citizen, with an
improved critical view and understanding of the scientific method, and
perhaps some deeper understanding of the ethics of science at
large. Use these insights. Be a critical citizen. You owe it to our
society.




\section{Data Analysis and Machine Learning: Getting started, our first data and Machine Learning encounters}
\label{\detokenize{chapter2:data-analysis-and-machine-learning-getting-started-our-first-data-and-machine-learning-encounters}}\label{\detokenize{chapter2::doc}}




\sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

Date: \sphinxstylestrong{Dec 25, 2019}

Copyright 1999\sphinxhyphen{}2019, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license


\subsection{Introduction}
\label{\detokenize{chapter2:introduction}}
Our emphasis throughout this series of lecturesis on understanding the mathematical aspects of
different algorithms used in the fields of data analysis and machine learning.

However, where possible we will emphasize the
importance of using available software. We start thus with a hands\sphinxhyphen{}on
and top\sphinxhyphen{}down approach to machine learning. The aim is thus to start with
relevant data or data we have produced
and use these to introduce statistical data analysis
concepts and machine learning algorithms before we delve into the
algorithms themselves. The examples we will use in the beginning, start with simple
polynomials with random noise added. We will use the Python
software package \sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}Learn} and
introduce various machine learning algorithms to make fits of
the data and predictions. We move thereafter to more interesting
cases such as data from say experiments (below we will look at experimental nuclear binding energies as an example).
These are examples where we can easily set up the data and
then use machine learning algorithms included in for example
\sphinxstylestrong{Scikit\sphinxhyphen{}Learn}.

These examples will serve us the purpose of getting
started. Furthermore, they allow us to catch more than two birds with
a stone. They will allow us to bring in some programming specific
topics and tools as well as showing the power of various Python
libraries for machine learning and statistical data analysis.

Here, we will mainly focus on two
specific Python packages for Machine Learning, Scikit\sphinxhyphen{}Learn and
Tensorflow (see below for links etc).  Moreover, the examples we
introduce will serve as inputs to many of our discussions later, as
well as allowing you to set up models and produce your own data and
get started with programming.


\subsection{What is Machine Learning?}
\label{\detokenize{chapter2:what-is-machine-learning}}
Statistics, data science and machine learning form important fields of
research in modern science.  They describe how to learn and make
predictions from data, as well as allowing us to extract important
correlations about physical process and the underlying laws of motion
in large data sets. The latter, big data sets, appear frequently in
essentially all disciplines, from the traditional Science, Technology,
Mathematics and Engineering fields to Life Science, Law, education
research, the Humanities and the Social Sciences.

It has become more
and more common to see research projects on big data in for example
the Social Sciences where extracting patterns from complicated survey
data is one of many research directions.  Having a solid grasp of data
analysis and machine learning is thus becoming central to scientific
computing in many fields, and competences and skills within the fields
of machine learning and scientific computing are nowadays strongly
requested by many potential employers. The latter cannot be
overstated, familiarity with machine learning has almost become a
prerequisite for many of the most exciting employment opportunities,
whether they are in bioinformatics, life science, physics or finance,
in the private or the public sector. This author has had several
students or met students who have been hired recently based on their
skills and competences in scientific computing and data science, often
with marginal knowledge of machine learning.

Machine learning is a subfield of computer science, and is closely
related to computational statistics.  It evolved from the study of
pattern recognition in artificial intelligence (AI) research, and has
made contributions to AI tasks like computer vision, natural language
processing and speech recognition. Many of the methods we will study are also
strongly rooted in basic mathematics and physics research.

Ideally, machine learning represents the science of giving computers
the ability to learn without being explicitly programmed.  The idea is
that there exist generic algorithms which can be used to find patterns
in a broad class of data sets without having to write code
specifically for each problem. The algorithm will build its own logic
based on the data.  You should however always keep in mind that
machines and algorithms are to a large extent developed by humans. The
insights and knowledge we have about a specific system, play a central
role when we develop a specific machine learning algorithm.

Machine learning is an extremely rich field, in spite of its young
age. The increases we have seen during the last three decades in
computational capabilities have been followed by developments of
methods and techniques for analyzing and handling large date sets,
relying heavily on statistics, computer science and mathematics.  The
field is rather new and developing rapidly. Popular software packages
written in Python for machine learning like
\sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}learn},
\sphinxhref{https://www.tensorflow.org/}{Tensorflow},
\sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, all
freely available at their respective GitHub sites, encompass
communities of developers in the thousands or more. And the number of
code developers and contributors keeps increasing. Not all the
algorithms and methods can be given a rigorous mathematical
justification, opening up thereby large rooms for experimenting and
trial and error and thereby exciting new developments.  However, a
solid command of linear algebra, multivariate theory, probability
theory, statistical data analysis, understanding errors and Monte
Carlo methods are central elements in a proper understanding of many
of algorithms and methods we will discuss.


\subsection{Types of Machine Learning}
\label{\detokenize{chapter2:types-of-machine-learning}}
The approaches to machine learning are many, but are often split into
two main categories.  In \sphinxstyleemphasis{supervised learning} we know the answer to a
problem, and let the computer deduce the logic behind it. On the other
hand, \sphinxstyleemphasis{unsupervised learning} is a method for finding patterns and
relationship in data sets without any prior knowledge of the system.
Some authours also operate with a third category, namely
\sphinxstyleemphasis{reinforcement learning}. This is a paradigm of learning inspired by
behavioral psychology, where learning is achieved by trial\sphinxhyphen{}and\sphinxhyphen{}error,
solely from rewards and punishment.

Another way to categorize machine learning tasks is to consider the
desired output of a system.  Some of the most common tasks are:
\begin{itemize}
\item {} 
Classification: Outputs are divided into two or more classes. The goal is to   produce a model that assigns inputs into one of these classes. An example is to identify  digits based on pictures of hand\sphinxhyphen{}written ones. Classification is typically supervised learning.

\item {} 
Regression: Finding a functional relationship between an input data set and a reference data set.   The goal is to construct a function that maps input data to continuous output values.

\item {} 
Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand.  It is thus a form of unsupervised learning.

\end{itemize}

The methods we cover have three main topics in common, irrespective of
whether we deal with supervised or unsupervised learning. The first
ingredient is normally our data set (which can be subdivided into
training and test data), the second item is a model which is normally a
function of some parameters.  The model reflects our knowledge of the system (or lack thereof). As an example, if we know that our data show a behavior similar to what would be predicted by a polynomial, fitting our data to a polynomial of some degree would then determin our model.

The last ingredient is a so\sphinxhyphen{}called \sphinxstylestrong{cost}
function which allows us to present an estimate on how good our model
is in reproducing the data it is supposed to train.At the heart of basically all ML algorithms there are so\sphinxhyphen{}called minimization algorithms, often we end up with various variants of \sphinxstylestrong{gradient} methods.


\subsection{Software and needed installations}
\label{\detokenize{chapter2:software-and-needed-installations}}
We will make extensive use of Python as programming language and its
myriad of available libraries.  You will find
Jupyter notebooks invaluable in your work.  You can run \sphinxstylestrong{R}
codes in the Jupyter/IPython notebooks, with the immediate benefit of
visualizing your data. You can also use compiled languages like C++,
Rust, Julia, Fortran etc if you prefer. The focus in these lectures will be
on Python.

If you have Python installed (we strongly recommend Python3) and you feel
pretty familiar with installing different packages, we recommend that
you install the following Python packages via \sphinxstylestrong{pip} as
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
pip install numpy scipy matplotlib ipython scikit\sphinxhyphen{}learn mglearn sympy pandas pillow

\end{enumerate}

For Python3, replace \sphinxstylestrong{pip} with \sphinxstylestrong{pip3}.

For OSX users we recommend, after having installed Xcode, to
install \sphinxstylestrong{brew}. Brew allows for a seamless installation of additional
software via for example
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
brew install python3

\end{enumerate}

For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution,
you can use \sphinxstylestrong{pip} as well and simply install Python as
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
sudo apt\sphinxhyphen{}get install python3  (or python for pyhton2.7)

\end{enumerate}

etc etc.


\subsection{Python installers}
\label{\detokenize{chapter2:python-installers}}
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 {} 
\sphinxhref{https://docs.anaconda.com/}{Anaconda},

\end{itemize}

which is an open source
distribution of the Python and R programming languages for large\sphinxhyphen{}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 \sphinxstylestrong{conda}.
\begin{itemize}
\item {} 
\sphinxhref{https://www.enthought.com/product/canopy/}{Enthought canopy}

\end{itemize}

is a Python
distribution for scientific and analytic computing distribution and
analysis environment, available for free and under a commercial
license.

Furthermore, \sphinxhref{https://colab.research.google.com/notebooks/welcome.ipynb}{Google’s Colab} is a free Jupyter notebook environment that requires
no setup and runs entirely in the cloud. Try it out!


\subsection{Useful Python libraries}
\label{\detokenize{chapter2:useful-python-libraries}}
Here we list several useful Python libraries we strongly recommend (if you use anaconda many of these are already there)
\begin{itemize}
\item {} 
\sphinxhref{https://www.numpy.org/}{NumPy} is a highly popular library for large, multi\sphinxhyphen{}dimensional arrays and matrices, along with a large collection of high\sphinxhyphen{}level mathematical functions to operate on these arrays

\item {} 
\sphinxhref{https://pandas.pydata.org/}{The pandas} library provides high\sphinxhyphen{}performance, easy\sphinxhyphen{}to\sphinxhyphen{}use data structures and data analysis tools

\item {} 
\sphinxhref{http://xarray.pydata.org/en/stable/}{Xarray} is a Python package that makes working with labelled multi\sphinxhyphen{}dimensional arrays simple, efficient, and fun!

\item {} 
\sphinxhref{https://www.scipy.org/}{Scipy} (pronounced “Sigh Pie”) is a Python\sphinxhyphen{}based ecosystem of open\sphinxhyphen{}source software for mathematics, science, and engineering.

\item {} 
\sphinxhref{https://matplotlib.org/}{Matplotlib} is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.

\item {} 
\sphinxhref{https://github.com/HIPS/autograd}{Autograd} can automatically differentiate native Python and Numpy code. It can handle a large subset of Python’s features, including loops, ifs, recursion and closures, and it can even take derivatives of derivatives of derivatives

\item {} 
\sphinxhref{https://www.sympy.org/en/index.html}{SymPy} is a Python library for symbolic mathematics.

\item {} 
\sphinxhref{https://scikit-learn.org/stable/}{scikit\sphinxhyphen{}learn} has simple and efficient tools for machine learning, data mining and data analysis

\item {} 
\sphinxhref{https://www.tensorflow.org/}{TensorFlow} is a Python library for fast numerical computing created and released by Google

\item {} 
\sphinxhref{https://keras.io/}{Keras} is a high\sphinxhyphen{}level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano

\item {} 
And many more such as \sphinxhref{https://pytorch.org/}{pytorch},  \sphinxhref{https://pypi.org/project/Theano/}{Theano} etc

\end{itemize}


\subsection{Installing R, C++, cython or Julia}
\label{\detokenize{chapter2:installing-r-c-cython-or-julia}}
You will also find it convenient to utilize \sphinxstylestrong{R}. We will mainly
use Python during our lectures and in various projects and exercises.
Those of you
already familiar with \sphinxstylestrong{R} should feel free to continue using \sphinxstylestrong{R}, keeping
however an eye on the parallel Python set ups. Similarly, if you are a
Python afecionado, feel free to explore \sphinxstylestrong{R} as well.  Jupyter/Ipython
notebook allows you to run \sphinxstylestrong{R} codes interactively in your
browser. The software library \sphinxstylestrong{R} is really tailored  for statistical data analysis
and allows for an easy usage of the tools and algorithms we will discuss in these
lectures.

To install \sphinxstylestrong{R} with Jupyter notebook
\sphinxhref{https://mpacer.org/maths/r-kernel-for-ipython-notebook}{follow the link here}


\subsection{Installing R, C++, cython, Numba etc}
\label{\detokenize{chapter2:installing-r-c-cython-numba-etc}}
For the C++ aficionados, Jupyter/IPython notebook allows you also to
install C++ and run codes written in this language interactively in
the browser. Since we will emphasize writing many of the algorithms
yourself, you can thus opt for either Python or C++ (or Fortran or other compiled languages) as programming
languages.

To add more entropy, \sphinxstylestrong{cython} can also be used when running your
notebooks. It means that Python with the jupyter notebook
setup allows you to integrate widely popular softwares and tools for
scientific computing. Similarly, the
\sphinxhref{https://numba.pydata.org/}{Numba Python package} delivers increased performance
capabilities with minimal rewrites of your codes.  With its
versatility, including symbolic operations, Python offers a unique
computational environment. Your jupyter notebook can easily be
converted into a nicely rendered \sphinxstylestrong{PDF} file or a Latex file for
further processing. For example, convert to latex as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
    pycod jupyter nbconvert filename.ipynb \PYGZhy{}\PYGZhy{}to latex 
\end{sphinxVerbatim}

And to add more versatility, the Python package \sphinxhref{http://www.sympy.org/en/index.html}{SymPy} is a Python library for symbolic mathematics. It aims to become a full\sphinxhyphen{}featured computer algebra system (CAS)  and is entirely written in Python.

Finally, if you wish to use the light mark\sphinxhyphen{}up language
\sphinxhref{https://github.com/hplgit/doconce}{doconce} you can convert a standard ascii text file into various HTML
formats, ipython notebooks, latex files, pdf files etc with minimal edits. These lectures were generated using \sphinxstylestrong{doconce}.


\subsection{Numpy examples and Important Matrix and vector handling packages}
\label{\detokenize{chapter2:numpy-examples-and-important-matrix-and-vector-handling-packages}}
There are several central software libraries for linear algebra and eigenvalue problems. Several of the more
popular ones have been wrapped into ofter software packages like those from the widely used text \sphinxstylestrong{Numerical Recipes}. The original source codes in many of the available packages are often taken from the widely used
software package LAPACK, which follows two other popular packages
developed in the 1970s, namely EISPACK and LINPACK.  We describe them shortly here.
\begin{itemize}
\item {} 
LINPACK: package for linear equations and least square problems.

\item {} 
LAPACK:package for solving symmetric, unsymmetric and generalized eigenvalue problems. From LAPACK’s website \sphinxurl{http://www.netlib.org} it is possible to download for free all source codes from this library. Both C/C++ and Fortran versions are available.

\item {} 
BLAS (I, II and III): (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations. Blas I is vector operations, II vector\sphinxhyphen{}matrix operations and III matrix\sphinxhyphen{}matrix operations. Highly parallelized and efficient codes, all available for download from \sphinxurl{http://www.netlib.org}.

\end{itemize}


\subsection{Basic Matrix Features}
\label{\detokenize{chapter2:basic-matrix-features}}
\sphinxstylestrong{Matrix properties reminder.}
\begin{equation*}
\begin{split}
\mathbf{A} =
      \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\
                                 a_{21} & a_{22} & a_{23} & a_{24} \\
                                   a_{31} & a_{32} & a_{33} & a_{34} \\
                                  a_{41} & a_{42} & a_{43} & a_{44}
             \end{bmatrix}\qquad
\mathbf{I} =
      \begin{bmatrix} 1 & 0 & 0 & 0 \\
                                 0 & 1 & 0 & 0 \\
                                 0 & 0 & 1 & 0 \\
                                 0 & 0 & 0 & 1
             \end{bmatrix}
\end{split}
\end{equation*}
The inverse of a matrix is defined by
\begin{equation*}
\begin{split}
\mathbf{A}^{-1} \cdot \mathbf{A} = I
\end{split}
\end{equation*}



\subsubsection{Some famous Matrices}
\label{\detokenize{chapter2:some-famous-matrices}}\begin{itemize}
\item {} 
Diagonal if \(a_{ij}=0\) for \(i\ne j\)

\item {} 
Upper triangular if \(a_{ij}=0\) for \(i > j\)

\item {} 
Lower triangular if \(a_{ij}=0\) for \(i < j\)

\item {} 
Upper Hessenberg if \(a_{ij}=0\) for \(i > j+1\)

\item {} 
Lower Hessenberg if \(a_{ij}=0\) for \(i < j+1\)

\item {} 
Tridiagonal if \(a_{ij}=0\) for \(|i -j| > 1\)

\item {} 
Lower banded with bandwidth \(p\): \(a_{ij}=0\) for \(i > j+p\)

\item {} 
Upper banded with bandwidth \(p\): \(a_{ij}=0\) for \(i < j+p\)

\item {} 
Banded, block upper triangular, block lower triangular….

\end{itemize}


\subsubsection{More Basic Matrix Features}
\label{\detokenize{chapter2:more-basic-matrix-features}}
\sphinxstylestrong{Some Equivalent Statements.}

For an \(N\times N\) matrix  \(\mathbf{A}\) the following properties are all equivalent
\begin{itemize}
\item {} 
If the inverse of \(\mathbf{A}\) exists, \(\mathbf{A}\) is nonsingular.

\item {} 
The equation \(\mathbf{Ax}=0\) implies \(\mathbf{x}=0\).

\item {} 
The rows of \(\mathbf{A}\) form a basis of \(R^N\).

\item {} 
The columns of \(\mathbf{A}\) form a basis of \(R^N\).

\item {} 
\(\mathbf{A}\) is a product of elementary matrices.

\item {} 
\(0\) is not eigenvalue of \(\mathbf{A}\).

\end{itemize}


\subsection{Numpy and arrays}
\label{\detokenize{chapter2:numpy-and-arrays}}
\sphinxhref{http://www.numpy.org/}{Numpy} provides an easy way to handle arrays in Python. The standard way to import this library is as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\end{sphinxVerbatim}

Here follows a simple example where we set up an array of ten elements, all determined by random numbers drawn according to the normal distribution,

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[ 0.66109153  0.97132954 \PYGZhy{}0.91470448  1.81588452  0.49665195  0.77698678
  1.04576423  1.65832096 \PYGZhy{}0.25613699 \PYGZhy{}0.7554783 ]
0.971329541323762
\end{sphinxVerbatim}

We defined a vector \(x\) with \(n=10\) elements with its values given by the Normal distribution \(N(0,1)\).
Another alternative is to declare a vector as follows

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[1 2 3]
\end{sphinxVerbatim}

Here we have defined a vector with three elements, with \(x_0=1\), \(x_1=2\) and \(x_2=3\). Note that both Python and C++
start numbering array elements from \(0\) and on. This means that a vector with \(n\) elements has a sequence of entities \(x_0, x_1, x_2, \dots, x_{n-1}\). We could also let (recommended) Numpy to compute the logarithms of a specific array as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{8}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[1.38629436 1.94591015 2.07944154]
\end{sphinxVerbatim}

In the last example we used Numpy’s unary function \(np.log\). This function is
highly tuned to compute array elements since the code is vectorized
and does not require looping. We normaly recommend that you use the
Numpy intrinsic functions instead of the corresponding \sphinxstylestrong{log} function
from Python’s \sphinxstylestrong{math} module. The looping is done explicitely by the
\sphinxstylestrong{np.log} function. The alternative, and slower way to compute the
logarithms of a vector would be to write

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{log}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{8}\PYG{p}{]}\PYG{p}{)}
\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{x}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[1 1 2]
\end{sphinxVerbatim}

We note that our code is much longer already and we need to import the \sphinxstylestrong{log} function from the \sphinxstylestrong{math} module.
The attentive reader will also notice that the output is \([1, 1, 2]\). Python interprets automagically our numbers as integers (like the \sphinxstylestrong{automatic} keyword in C++). To change this we could define our array elements to be double precision numbers as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{8}\PYG{p}{]}\PYG{p}{,} \PYG{n}{dtype} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{float64}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[1.38629436 1.94591015 2.07944154]
\end{sphinxVerbatim}

or simply write them as double precision numbers (Python uses 64 bits as default for floating point type variables), that is

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[1.38629436 1.94591015 2.07944154]
\end{sphinxVerbatim}

To check the number of bytes (remember that one byte contains eight bits for double precision variables), you can use simple use the \sphinxstylestrong{itemsize} functionality (the array \(x\) is actually an object which inherits the functionalities defined in Numpy) as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{o}{.}\PYG{n}{itemsize}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
8
\end{sphinxVerbatim}


\subsection{Matrices in Python}
\label{\detokenize{chapter2:matrices-in-python}}
Having defined vectors, we are now ready to try out matrices. We can
define a \(3 \times 3 \) real matrix \(\hat{A}\) as (recall that we user
lowercase letters for vectors and uppercase letters for matrices)

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{3.0}\PYG{p}{,} \PYG{l+m+mf}{10.0}\PYG{p}{,} \PYG{l+m+mf}{11.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]} \PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[1.38629436 1.94591015 2.07944154]
 [1.09861229 2.30258509 2.39789527]
 [1.38629436 1.60943791 1.94591015]]
\end{sphinxVerbatim}

If we use the \sphinxstylestrong{shape} function we would get \((3, 3)\) as output, that is verifying that our matrix is a \(3\times 3\) matrix. We can slice the matrix and print for example the first column (Python organized matrix elements in a row\sphinxhyphen{}major order, see below) as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{3.0}\PYG{p}{,} \PYG{l+m+mf}{10.0}\PYG{p}{,} \PYG{l+m+mf}{11.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]} \PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} print the first column, row\PYGZhy{}major order and elements start with 0}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[1.38629436 1.09861229 1.38629436]
\end{sphinxVerbatim}

We can continue this was by printing out other columns or rows. The example here prints out the second column

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{3.0}\PYG{p}{,} \PYG{l+m+mf}{10.0}\PYG{p}{,} \PYG{l+m+mf}{11.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]} \PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} print the first column, row\PYGZhy{}major order and elements start with 0}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{p}{:}\PYG{p}{]}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[1.09861229 2.30258509 2.39789527]
\end{sphinxVerbatim}

Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the \sphinxhref{http://www.numpy.org/}{Numpy website for more details}. Useful functions when defining a matrix are the \sphinxstylestrong{np.zeros} function which declares a matrix of a given dimension and sets all elements to zero

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10}
\PYG{c+c1}{\PYGZsh{} define a matrix of dimension 10 x 10 and set all elements to zero}
\PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(} \PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)} \PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
\end{sphinxVerbatim}

or initializing all elements to

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10}
\PYG{c+c1}{\PYGZsh{} define a matrix of dimension 10 x 10 and set all elements to one}
\PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{eye}\PYG{p}{(} \PYG{n}{n} \PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
\end{sphinxVerbatim}

or as unitarily distributed random numbers (see the material on random number generators in the statistics part)

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10}
\PYG{c+c1}{\PYGZsh{} define a matrix of dimension 10 x 10 and set all elements to random numbers with x \PYGZbs{}in [0, 1]}
\PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[0.88645063 0.21176899 0.23897722 0.06720011 0.38272048 0.88809424
  0.0878077  0.73425375 0.72093518 0.19764225]
 [0.74839206 0.31545538 0.14076925 0.59046117 0.89437867 0.81232389
  0.20782531 0.52852411 0.10079113 0.07130568]
 [0.64573187 0.71051155 0.65626927 0.92475512 0.58540859 0.52327428
  0.28610035 0.69580848 0.94590985 0.98668663]
 [0.01101253 0.38937429 0.60430671 0.81789875 0.2280742  0.17786312
  0.34053316 0.19374203 0.01520576 0.2466458 ]
 [0.60613717 0.19314313 0.959787   0.6376673  0.73798553 0.63486051
  0.15742577 0.39450088 0.07204545 0.46013967]
 [0.23509059 0.79672774 0.81910307 0.53487394 0.50992632 0.112086
  0.55751711 0.10662789 0.70911163 0.59072491]
 [0.0259775  0.26815865 0.5090044  0.41809535 0.32232515 0.72743704
  0.32110496 0.04202295 0.18265714 0.70084503]
 [0.07736581 0.57300172 0.97099592 0.62357997 0.36579921 0.37062279
  0.74683978 0.59433006 0.35540585 0.97384662]
 [0.25205634 0.7601454  0.40639052 0.24504215 0.21745329 0.95788719
  0.7490677  0.29789413 0.81839129 0.93822723]
 [0.6302074  0.47072129 0.16215217 0.53540566 0.91760002 0.14146861
  0.37860338 0.8099108  0.07897476 0.69254202]]
\end{sphinxVerbatim}

As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors
\(\hat{x}, \hat{y}, \hat{z}\) with \(n\) elements each. The covariance matrix is defined as
\begin{equation*}
\begin{split}
\hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
                              \sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\
                              \sigma_{zx} & \sigma_{zy} & \sigma_{zz} 
             \end{bmatrix},
\end{split}
\end{equation*}
where for example
\begin{equation*}
\begin{split}
\sigma_{xy} =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}).
\end{split}
\end{equation*}
The Numpy function \sphinxstylestrong{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 \sphinxstylestrong{np.vstack} function which takes each vector of dimension \(1\times n\) and produces a \(3\times n\) matrix \(\hat{W}\)
\begin{equation*}
\begin{split}
\hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
                          x_1 & y_1 & z_1 \\
                          x_2 & y_2 & z_2 \\
                          \dots & \dots & \dots \\
                          x_{n-2} & y_{n-2} & z_{n-2} \\
                          x_{n-1} & y_{n-1} & z_{n-1}
             \end{bmatrix},
\end{split}
\end{equation*}
which in turn is converted into into the \(3\times 3\) covariance matrix
\(\hat{\Sigma}\) via the Numpy function \sphinxstylestrong{np.cov()}. We note that we can also calculate
the mean value of each set of samples \(\hat{x}\) etc using the Numpy
function \sphinxstylestrong{np.mean(x)}. We can also extract the eigenvalues of the
covariance matrix through the \sphinxstylestrong{np.linalg.eig()} function.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Importing various packages}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}

\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{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\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYGZhy{}0.13489611340279922
3.7349937337703607
\PYGZhy{}0.6150223388222948
[[ 1.15822308  3.44928776  3.99889831]
 [ 3.44928776 11.11151113 12.25819175]
 [ 3.99889831 12.25819175 21.09855273]]
[30.28557034  0.07629455  3.00642205]
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{o}{\PYGZpc{}}\PYG{k}{matplotlib} inline

\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{scipy} \PYG{k+kn}{import} \PYG{n}{sparse}
\PYG{n}{eye} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{eye}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{eye}\PYG{p}{)}
\PYG{n}{sparse\PYGZus{}mtx} \PYG{o}{=} \PYG{n}{sparse}\PYG{o}{.}\PYG{n}{csr\PYGZus{}matrix}\PYG{p}{(}\PYG{n}{eye}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{sparse\PYGZus{}mtx}\PYG{p}{)}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{100}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,}\PYG{n}{marker}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{x}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
  (0, 0)	1.0
  (1, 1)	1.0
  (2, 2)	1.0
  (3, 3)	1.0
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_42_1}.png}


\subsection{Meet the Pandas}
\label{\detokenize{chapter2:meet-the-pandas}}








Another useful Python package is
\sphinxhref{https://pandas.pydata.org/}{pandas}, which is an open source library
providing high\sphinxhyphen{}performance, easy\sphinxhyphen{}to\sphinxhyphen{}use data structures and data
analysis tools for Python. \sphinxstylestrong{pandas} stands for panel data, a term borrowed from econometrics and is an efficient library for data analysis with an emphasis on tabular data.
\sphinxstylestrong{pandas} has two major classes, the \sphinxstylestrong{DataFrame} class with two\sphinxhyphen{}dimensional data objects and tabular data organized in columns and the class \sphinxstylestrong{Series} with a focus on one\sphinxhyphen{}dimensional data objects. Both classes allow you to index data easily as we will see in the examples below.
\sphinxstylestrong{pandas} allows you also to perform mathematical operations on the data, spanning from simple reshapings of vectors and matrices to statistical operations.

The following simple example shows how we can, in an easy way make tables of our data. Here we define a data set which includes names, place of birth and date of birth, and displays the data in an easy to read way. We will see repeated use of \sphinxstylestrong{pandas}, in particular in connection with classification of data.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{from} \PYG{n+nn}{IPython}\PYG{n+nn}{.}\PYG{n+nn}{display} \PYG{k+kn}{import} \PYG{n}{display}
\PYG{n}{data} \PY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YGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Eriador}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Shire}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,}
        \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Date of Birth T.A.}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+m+mi}{2968}\PYG{p}{,} \PYG{l+m+mi}{2890}\PYG{p}{,} \PYG{l+m+mi}{2931}\PYG{p}{,} \PYG{l+m+mi}{2980}\PYG{p}{]}
        \PYG{p}{\PYGZcb{}}
\PYG{n}{data\PYGZus{}pandas} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}
\PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
   First Name Last Name Place of birth  Date of Birth T.A.
0       Frodo   Baggins          Shire                2968
1       Bilbo   Baggins          Shire                2890
2  Aragorn II   Elessar        Eriador                2931
3     Samwise    Gamgee          Shire                2980
\end{sphinxVerbatim}

In the above we have imported \sphinxstylestrong{pandas} with the shorthand \sphinxstylestrong{pd}, the latter has become the standard way we import \sphinxstylestrong{pandas}. We make then a list of various variables
and reorganize the aboves lists into a \sphinxstylestrong{DataFrame} and then print out  a neat table with specific column labels as \sphinxstyleemphasis{Name}, \sphinxstyleemphasis{place of birth} and \sphinxstyleemphasis{date of birth}.
Displaying these results, we see that the indices are given by the default numbers from zero to three.
\sphinxstylestrong{pandas} is extremely flexible and we can easily change the above indices by defining a new type of indexing as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{data\PYGZus{}pandas} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{n}{index}\PYG{o}{=}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Frodo}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Bilbo}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Aragorn}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Sam}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
         First Name Last Name Place of birth  Date of Birth T.A.
Frodo         Frodo   Baggins          Shire                2968
Bilbo         Bilbo   Baggins          Shire                2890
Aragorn  Aragorn II   Elessar        Eriador                2931
Sam         Samwise    Gamgee          Shire                2980
\end{sphinxVerbatim}

Thereafter we display the content of the row which begins with the index \sphinxstylestrong{Aragorn}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{o}{.}\PYG{n}{loc}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Aragorn}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
First Name            Aragorn II
Last Name                Elessar
Place of birth           Eriador
Date of Birth T.A.          2931
Name: Aragorn, dtype: object
\end{sphinxVerbatim}

We can easily append data to this, for example

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{new\PYGZus{}hobbit} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{First Name}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Peregrin}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,}
              \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Last Name}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Took}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,}
              \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Place of birth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Shire}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,}
              \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Date of Birth T.A.}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+m+mi}{2990}\PYG{p}{]}
              \PYG{p}{\PYGZcb{}}
\PYG{n}{data\PYGZus{}pandas}\PYG{o}{=}\PYG{n}{data\PYGZus{}pandas}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{new\PYGZus{}hobbit}\PYG{p}{,} \PYG{n}{index}\PYG{o}{=}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Pippin}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
         First Name Last Name Place of birth  Date of Birth T.A.
Frodo         Frodo   Baggins          Shire                2968
Bilbo         Bilbo   Baggins          Shire                2890
Aragorn  Aragorn II   Elessar        Eriador                2931
Sam         Samwise    Gamgee          Shire                2980
Pippin     Peregrin      Took          Shire                2990
\end{sphinxVerbatim}

Here are other examples where we use the \sphinxstylestrong{DataFrame} functionality to handle arrays, now with more interesting features for us, namely numbers. We set up a matrix
of dimensionality \(10\times 5\) and compute the mean value and standard deviation of each column. Similarly, we can perform mathematial operations like squaring the matrix elements and many other operations.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{from} \PYG{n+nn}{IPython}\PYG{n+nn}{.}\PYG{n+nn}{display} \PYG{k+kn}{import} \PYG{n}{display}
\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} setting up a 10 x 5 matrix}
\PYG{n}{rows} \PYG{o}{=} \PYG{l+m+mi}{10}
\PYG{n}{cols} \PYG{o}{=} \PYG{l+m+mi}{5}
\PYG{n}{a} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{rows}\PYG{p}{,}\PYG{n}{cols}\PYG{p}{)}
\PYG{n}{df} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}
\PYG{n}{display}\PYG{p}{(}\PYG{n}{df}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{std}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{display}\PYG{p}{(}\PYG{n}{df}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{\PYGZhy{}}\PYG{n}{df}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
          0         1         2         3         4
0 \PYGZhy{}1.749765  0.342680  1.153036 \PYGZhy{}0.252436  0.981321
1  0.514219  0.221180 \PYGZhy{}1.070043 \PYGZhy{}0.189496  0.255001
2 \PYGZhy{}0.458027  0.435163 \PYGZhy{}0.583595  0.816847  0.672721
3 \PYGZhy{}0.104411 \PYGZhy{}0.531280  1.029733 \PYGZhy{}0.438136 \PYGZhy{}1.118318
4  1.618982  1.541605 \PYGZhy{}0.251879 \PYGZhy{}0.842436  0.184519
5  0.937082  0.731000  1.361556 \PYGZhy{}0.326238  0.055676
6  0.222400 \PYGZhy{}1.443217 \PYGZhy{}0.756352  0.816454  0.750445
7 \PYGZhy{}0.455947  1.189622 \PYGZhy{}1.690617 \PYGZhy{}1.356399 \PYGZhy{}1.232435
8 \PYGZhy{}0.544439 \PYGZhy{}0.668172  0.007315 \PYGZhy{}0.612939  1.299748
9 \PYGZhy{}1.733096 \PYGZhy{}0.983310  0.357508 \PYGZhy{}1.613579  1.470714
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0   \PYGZhy{}0.175300
1    0.083527
2   \PYGZhy{}0.044334
3   \PYGZhy{}0.399836
4    0.331939
dtype: float64
0    1.069584
1    0.965548
2    1.018232
3    0.793167
4    0.918992
dtype: float64
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
          0         1         2         3         4
0  3.061679  0.117430  1.329492  0.063724  0.962990
1  0.264421  0.048920  1.144993  0.035909  0.065026
2  0.209789  0.189367  0.340583  0.667239  0.452553
3  0.010902  0.282259  1.060349  0.191963  1.250636
4  2.621102  2.376547  0.063443  0.709698  0.034047
5  0.878123  0.534362  1.853835  0.106431  0.003100
6  0.049462  2.082875  0.572069  0.666597  0.563167
7  0.207888  1.415201  2.858185  1.839818  1.518895
8  0.296414  0.446453  0.000054  0.375694  1.689345
9  3.003620  0.966899  0.127812  2.603636  2.162999
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
          0         1         2         3         4
0 \PYGZhy{}1.574465  0.259153  1.197370  0.147400  0.649382
1  0.689519  0.137652 \PYGZhy{}1.025709  0.210340 \PYGZhy{}0.076938
2 \PYGZhy{}0.282727  0.351636 \PYGZhy{}0.539261  1.216683  0.340782
3  0.070889 \PYGZhy{}0.614808  1.074067 \PYGZhy{}0.038300 \PYGZhy{}1.450257
4  1.794282  1.458078 \PYGZhy{}0.207545 \PYGZhy{}0.442600 \PYGZhy{}0.147420
5  1.112383  0.647473  1.405890  0.073598 \PYGZhy{}0.276263
6  0.397700 \PYGZhy{}1.526744 \PYGZhy{}0.712018  1.216290  0.418506
7 \PYGZhy{}0.280647  1.106095 \PYGZhy{}1.646283 \PYGZhy{}0.956563 \PYGZhy{}1.564374
8 \PYGZhy{}0.369139 \PYGZhy{}0.751699  0.051649 \PYGZhy{}0.213103  0.967809
9 \PYGZhy{}1.557795 \PYGZhy{}1.066837  0.401842 \PYGZhy{}1.213743  1.138775
\end{sphinxVerbatim}

Thereafter we can select specific columns only and plot final results

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{df}\PYG{o}{.}\PYG{n}{columns} \PYG{o}{=} \PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{First}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{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}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{font.family}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{serif}\PYG{l+s+s1}{\PYGZsq{}}

\PYG{n}{df}\PYG{o}{.}\PYG{n}{cumsum}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{lw}\PYG{o}{=}\PYG{l+m+mf}{2.0}\PYG{p}{,} \PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}


\PYG{n}{df}\PYG{o}{.}\PYG{n}{plot}\PYG{o}{.}\PYG{n}{bar}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{)}\PYG{p}{,} \PYG{n}{rot}\PYG{o}{=}\PYG{l+m+mi}{15}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
      First    Second     Third    Fourth     Fifth
0 \PYGZhy{}1.749765  0.342680  1.153036 \PYGZhy{}0.252436  0.981321
1  0.514219  0.221180 \PYGZhy{}1.070043 \PYGZhy{}0.189496  0.255001
2 \PYGZhy{}0.458027  0.435163 \PYGZhy{}0.583595  0.816847  0.672721
3 \PYGZhy{}0.104411 \PYGZhy{}0.531280  1.029733 \PYGZhy{}0.438136 \PYGZhy{}1.118318
4  1.618982  1.541605 \PYGZhy{}0.251879 \PYGZhy{}0.842436  0.184519
5  0.937082  0.731000  1.361556 \PYGZhy{}0.326238  0.055676
6  0.222400 \PYGZhy{}1.443217 \PYGZhy{}0.756352  0.816454  0.750445
7 \PYGZhy{}0.455947  1.189622 \PYGZhy{}1.690617 \PYGZhy{}1.356399 \PYGZhy{}1.232435
8 \PYGZhy{}0.544439 \PYGZhy{}0.668172  0.007315 \PYGZhy{}0.612939  1.299748
9 \PYGZhy{}1.733096 \PYGZhy{}0.983310  0.357508 \PYGZhy{}1.613579  1.470714
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.08352721390288316
\PYGZlt{}class \PYGZsq{}pandas.core.frame.DataFrame\PYGZsq{}\PYGZgt{}
Int64Index: 10 entries, 0 to 9
Data columns (total 5 columns):
 \PYGZsh{}   Column  Non\PYGZhy{}Null Count  Dtype  
\PYGZhy{}\PYGZhy{}\PYGZhy{}  \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}  \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}  \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}  
 0   First   10 non\PYGZhy{}null     float64
 1   Second  10 non\PYGZhy{}null     float64
 2   Third   10 non\PYGZhy{}null     float64
 3   Fourth  10 non\PYGZhy{}null     float64
 4   Fifth   10 non\PYGZhy{}null     float64
dtypes: float64(5)
memory usage: 480.0 bytes
None
           First     Second      Third     Fourth      Fifth
count  10.000000  10.000000  10.000000  10.000000  10.000000
mean   \PYGZhy{}0.175300   0.083527  \PYGZhy{}0.044334  \PYGZhy{}0.399836   0.331939
std     1.069584   0.965548   1.018232   0.793167   0.918992
min    \PYGZhy{}1.749765  \PYGZhy{}1.443217  \PYGZhy{}1.690617  \PYGZhy{}1.613579  \PYGZhy{}1.232435
25\PYGZpc{}    \PYGZhy{}0.522836  \PYGZhy{}0.633949  \PYGZhy{}0.713163  \PYGZhy{}0.785061   0.087887
50\PYGZpc{}    \PYGZhy{}0.280179   0.281930  \PYGZhy{}0.122282  \PYGZhy{}0.382187   0.463861
75\PYGZpc{}     0.441264   0.657041   0.861676  \PYGZhy{}0.205231   0.923602
max     1.618982   1.541605   1.361556   0.816847   1.470714
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_54_2}.png}

\noindent\sphinxincludegraphics{{chapter2_54_3}.png}

We can produce a \(4\times 4\) matrix

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{b} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{l+m+mi}{16}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)}
\PYG{n}{df1} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df1}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
    0   1   2   3
0   0   1   2   3
1   4   5   6   7
2   8   9  10  11
3  12  13  14  15
\end{sphinxVerbatim}

and many other operations.

The \sphinxstylestrong{Series} class is another important class included in
\sphinxstylestrong{pandas}. You can view it as a specialization of \sphinxstylestrong{DataFrame} but where
we have just a single column of data. It shares many of the same features as \_DataFrame. As with \sphinxstylestrong{DataFrame},
most operations are vectorized, achieving thereby a high performance when dealing with computations of arrays, in particular labeled arrays.
As we will see below it leads also to a very concice code close to the mathematical operations we may be interested in.
For multidimensional arrays, we recommend strongly \sphinxhref{http://xarray.pydata.org/en/stable/}{xarray}. \sphinxstylestrong{xarray} has much of the same flexibility as \sphinxstylestrong{pandas}, but allows for the extension to higher dimensions than two. We will see examples later of the usage of both \sphinxstylestrong{pandas} and \sphinxstylestrong{xarray}.


\subsection{Reading Data and fitting}
\label{\detokenize{chapter2:reading-data-and-fitting}}
In order to study various Machine Learning algorithms, we need to
access data. Acccessing data is an essential step in all machine
learning algorithms. In particular, setting up the so\sphinxhyphen{}called \sphinxstylestrong{design
matrix} (to be defined below) is often the first element we need in
order to perform our calculations. To set up the design matrix means
reading (and later, when the calculations are done, writing) data
in various formats, The formats span from reading files from disk,
loading data from databases and interacting with online sources
like web application programming interfaces (APIs).

In handling various input formats, as discussed above, we will mainly stay with \sphinxstylestrong{pandas},
a Python package which allows us, in a seamless and painless way, to
deal with a multitude of formats, from standard \sphinxstylestrong{csv} (comma separated
values) files, via \sphinxstylestrong{excel}, \sphinxstylestrong{html} to \sphinxstylestrong{hdf5} formats.  With \sphinxstylestrong{pandas}
and the \sphinxstylestrong{DataFrame}  and \sphinxstylestrong{Series} functionalities we are able to convert text data
into the calculational formats we need for a specific algorithm. And our code is going to be
pretty close the basic mathematical expressions.

Our first data set is going to be a classic from nuclear physics, namely all
available data on binding energies. Don’t be intimidated if you are not familiar with nuclear physics. It serves simply as an example here of a data set.

We will show some of the
strengths of packages like \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} in fitting nuclear binding energies to
specific functions using linear regression first. Then, as a teaser, we will show you how
you can easily implement other algorithms like decision trees and random forests and neural networks.

But before we really start with nuclear physics data, let’s just look at some simpler polynomial fitting cases, such as,
(don’t be offended) fitting straight lines!


\subsubsection{Simple linear regression model using \sphinxstylestrong{scikit\sphinxhyphen{}learn}}
\label{\detokenize{chapter2:simple-linear-regression-model-using-scikit-learn}}
We start with perhaps our simplest possible example, using \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} to perform linear regression analysis on a data set produced by us.

What follows is a simple Python code where we have defined a function
\(y\) in terms of the variable \(x\). Both are defined as vectors with  \(100\) entries.
The numbers in the vector \(\hat{x}\) are given
by random numbers generated with a uniform distribution with entries
\(x_i \in [0,1]\) (more about probability distribution functions
later). These values are then used to define a function \(y(x)\)
(tabulated again as a vector) with a linear dependence on \(x\) plus a
random noise added via the normal distribution.

The Numpy functions are imported used the \sphinxstylestrong{import numpy as np}
statement and the random number generator for the uniform distribution
is called using the function \sphinxstylestrong{np.random.rand()}, where we specificy
that we want \(100\) random variables.  Using Numpy we define
automatically an array with the specified number of elements, \(100\) in
our case.  With the Numpy function \sphinxstylestrong{randn()} we can compute random
numbers with the normal distribution (mean value \(\mu\) equal to zero and
variance \(\sigma^2\) set to one) and produce the values of \(y\) assuming a linear
dependence as function of \(x\)
\begin{equation*}
\begin{split}
y = 2x+N(0,1),
\end{split}
\end{equation*}
where \(N(0,1)\) represents random numbers generated by the normal
distribution.  From \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} we import then the
\sphinxstylestrong{LinearRegression} functionality and make a prediction \(\tilde{y} =
\alpha + \beta x\) using the function \sphinxstylestrong{fit(x,y)}. We call the set of
data \((\hat{x},\hat{y})\) for our training data. The Python package
\sphinxstylestrong{scikit\sphinxhyphen{}learn} has also a functionality which extracts the above
fitting parameters \(\alpha\) and \(\beta\) (see below). Later we will
distinguish between training data and test data.

For plotting we use the Python package
\sphinxhref{https://matplotlib.org/}{matplotlib} which produces publication
quality figures. Feel free to explore the extensive
\sphinxhref{https://matplotlib.org/gallery/index.html}{gallery} of examples. In
this example we plot our original values of \(x\) and \(y\) as well as the
prediction \sphinxstylestrong{ypredict} (\(\tilde{y}\)), which attempts at fitting our
data with a straight line.

The Python code follows here.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Importing various packages}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}

\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mf}{0.01}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{linreg} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{linreg}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{}ynew = linreg.predict(x)}
\PYG{c+c1}{\PYGZsh{}xnew = np.array([[0],[1]])}
\PYG{n}{ypredict} \PYG{o}{=} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{r\PYGZhy{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{ro}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mf}{1.0}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}y\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Simple Linear Regression}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_60_0}.png}

This example serves several aims. It allows us to demonstrate several
aspects of data analysis and later machine learning algorithms. The
immediate visualization shows that our linear fit is not
impressive. It goes through the data points, but there are many
outliers which are not reproduced by our linear regression.  We could
now play around with this small program and change for example the
factor in front of \(x\) and the normal distribution.  Try to change the
function \(y\) to
\begin{equation*}
\begin{split}
y = 10x+0.01 \times N(0,1),
\end{split}
\end{equation*}
where \(x\) is defined as before.  Does the fit look better? Indeed, by
reducing the role of the noise given by the normal distribution we see immediately that
our linear prediction seemingly reproduces better the training
set. However, this testing ‘by the eye’ is obviouly not satisfactory in the
long run. Here we have only defined the training data and our model, and
have not discussed a more rigorous approach to the \sphinxstylestrong{cost} function.

We need more rigorous criteria in defining whether we have succeeded or
not in modeling our training data.  You will be surprised to see that
many scientists seldomly venture beyond this ‘by the eye’ approach. A
standard approach for the \sphinxstyleemphasis{cost} function is the so\sphinxhyphen{}called \(\chi^2\)
function (a variant of the mean\sphinxhyphen{}squared error (MSE))
\begin{equation*}
\begin{split}
\chi^2 = \frac{1}{n}
\sum_{i=0}^{n-1}\frac{(y_i-\tilde{y}_i)^2}{\sigma_i^2},
\end{split}
\end{equation*}
where \(\sigma_i^2\) is the variance (to be defined later) of the entry
\(y_i\).  We may not know the explicit value of \(\sigma_i^2\), it serves
however the aim of scaling the equations and make the cost function
dimensionless.

Minimizing the cost function is a central aspect of
our discussions to come. Finding its minima as function of the model
parameters (\(\alpha\) and \(\beta\) in our case) will be a recurring
theme in these series of lectures. Essentially all machine learning
algorithms we will discuss center around the minimization of the
chosen cost function. This depends in turn on our specific
model for describing the data, a typical situation in supervised
learning. Automatizing the search for the minima of the cost function is a
central ingredient in all algorithms. Typical methods which are
employed are various variants of \sphinxstylestrong{gradient} methods. These will be
discussed in more detail later. Again, you’ll be surprised to hear that
many practitioners minimize the above function ‘’by the eye’, popularly dubbed as
‘chi by the eye’. That is, change a parameter and see (visually and numerically) that
the  \(\chi^2\) function becomes smaller.

There are many ways to define the cost function. A simpler approach is to look at the relative difference between the training data and the predicted data, that is we define
the relative error (why would we prefer the MSE instead of the relative error?) as
\begin{equation*}
\begin{split}
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
\end{split}
\end{equation*}
The squared cost function results in an arithmetic mean\sphinxhyphen{}unbiased
estimator, and the absolute\sphinxhyphen{}value cost function results in a
median\sphinxhyphen{}unbiased estimator (in the one\sphinxhyphen{}dimensional case, and a
geometric median\sphinxhyphen{}unbiased estimator for the multi\sphinxhyphen{}dimensional
case). The squared cost function has the disadvantage that it has the tendency
to be dominated by outliers.

We can modify easily the above Python code and plot the relative error instead

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}

\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mf}{0.01}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{linreg} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{linreg}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)}
\PYG{n}{ypredict} \PYG{o}{=} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{abs}\PYG{p}{(}\PYG{n}{ypredict}\PYG{o}{\PYGZhy{}}\PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{n+nb}{abs}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ro}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mf}{1.0}\PYG{p}{,}\PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{0.5}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{epsilon\PYGZus{}}\PYG{l+s+s1}{\PYGZob{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}relative\PYGZcb{}}\PYG{l+s+s1}{\PYGZcb{}\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Relative error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_68_0}.png}

Depending on the parameter in front of the normal distribution, we may
have a small or larger relative error. Try to play around with
different training data sets and study (graphically) the value of the
relative error.

As mentioned above, \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} has an impressive functionality.
We can for example extract the values of \(\alpha\) and \(\beta\) and
their error estimates, or the variance and standard deviation and many
other properties from the statistical data analysis.

Here we show an
example of the functionality of \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} 
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} 
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression} 
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{,} \PYG{n}{r2\PYGZus{}score}\PYG{p}{,} \PYG{n}{mean\PYGZus{}squared\PYGZus{}log\PYGZus{}error}\PYG{p}{,} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}

\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mf}{2.0}\PYG{o}{+} \PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mf}{0.5}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{linreg} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{linreg}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)}
\PYG{n}{ypredict} \PYG{o}{=} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{The intercept alpha: }\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{intercept\PYGZus{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Coefficient beta : }\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} The mean squared error                               }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Explained variance score: 1 is perfect prediction                                 }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance score: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Mean squared log error                                                        }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean squared log error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}log\PYGZus{}error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)} \PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Mean absolute error                                                           }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean absolute error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{r\PYGZhy{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{ro}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{0.0}\PYG{p}{,}\PYG{l+m+mf}{1.0}\PYG{p}{,}\PYG{l+m+mf}{1.5}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}y\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Linear Regression fit }\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
The intercept alpha: 
 [2.18780801]
Coefficient beta : 
 [[4.72228205]]
Mean squared error: 0.37
Variance score: 0.83
Mean squared log error: 0.01
Mean absolute error: 0.47
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_70_1}.png}

The function \sphinxstylestrong{coef} gives us the parameter \(\beta\) of our fit while \sphinxstylestrong{intercept} yields
\(\alpha\). Depending on the constant in front of the normal distribution, we get values near or far from \(alpha =2\) and \(\beta =5\). Try to play around with different parameters in front of the normal distribution. The function \sphinxstylestrong{meansquarederror} gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as
\begin{equation*}
\begin{split}
MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
\end{split}
\end{equation*}
The smaller the value, the better the fit. Ideally we would like to
have an MSE equal zero.  The attentive reader has probably recognized
this function as being similar to the \(\chi^2\) function defined above.

The \sphinxstylestrong{r2score} function computes \(R^2\), the coefficient of
determination. It provides a measure of how well future samples are
likely to be predicted by the model. Best possible score is 1.0 and it
can be negative (because the model can be arbitrarily worse). A
constant model that always predicts the expected value of \(\hat{y}\),
disregarding the input features, would get a \(R^2\) score of \(0.0\).

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
\begin{equation*}
\begin{split}
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},
\end{split}
\end{equation*}
where we have defined the mean value  of \(\hat{y}\) as
\begin{equation*}
\begin{split}
\bar{y} =  \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
\end{split}
\end{equation*}
Another quantity taht we will meet again in our discussions of regression analysis is
the mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \(l1\)\sphinxhyphen{}norm loss. In our discussion above we presented the relative error.
The MAE is defined as follows
\begin{equation*}
\begin{split}
\text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|.
\end{split}
\end{equation*}
We present the
squared logarithmic (quadratic) error
\begin{equation*}
\begin{split}
\text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2,
\end{split}
\end{equation*}
where \(\log_e (x)\) stands for the natural logarithm of \(x\). This error
estimate is best to use when targets having exponential growth, such
as population counts, average sales of a commodity over a span of
years etc.

Finally, another cost function is the Huber cost function used in robust regression.

The rationale behind this possible cost function is its reduced
sensitivity to outliers in the data set. In our discussions on
dimensionality reduction and normalization of data we will meet other
ways of dealing with outliers.

The Huber cost function is defined as
\begin{equation*}
\begin{split}
H_{\delta}(a)={\begin{cases}{\frac {1}{2}}{a^{2}}&{\text{for }}|a|\leq \delta ,\\\delta (|a|-{\frac {1}{2}}\delta ),&{\text{otherwise.}}\end{cases}}}.
\end{split}
\end{equation*}
Here \(a=\boldsymbol{y} - \boldsymbol{\tilde{y}}\).
We will discuss in more
detail these and other functions in the various lectures.  We conclude this part with another example. Instead of
a linear \(x\)\sphinxhyphen{}dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit\sphinxhyphen{}learn.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{random}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{Ridge}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}pipeline}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}

\PYG{n}{x}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{l+m+mf}{0.02}\PYG{p}{,}\PYG{l+m+mf}{0.98}\PYG{p}{,}\PYG{l+m+mi}{200}\PYG{p}{)}
\PYG{n}{noise} \PYG{o}{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=}\PYG{p}{(}\PYG{n}{y}\PYG{o}{\PYGZhy{}}\PYG{n}{yn}\PYG{p}{)}\PYG{o}{/}\PYG{n}{yn}
    \PYG{k}{return} \PYG{n+nb}{abs}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{n}{err}\PYG{p}{)}\PYG{p}{)}\PYG{o}{/}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{err}\PYG{p}{)}

\PYG{n+nb}{print} \PYG{p}{(}\PYG{n}{error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_84_0}.png}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.005000000000000007
\end{sphinxVerbatim}


\subsubsection{To our real data: nuclear binding energies. Brief reminder on masses and binding energies}
\label{\detokenize{chapter2:to-our-real-data-nuclear-binding-energies-brief-reminder-on-masses-and-binding-energies}}
Let us now dive into  nuclear physics and remind ourselves briefly about some basic features about binding
energies.  A basic quantity which can be measured for the ground
states of nuclei is the atomic mass \(M(N, Z)\) of the neutral atom with
atomic mass number \(A\) and charge \(Z\). The number of neutrons is \(N\). There are indeed several sophisticated experiments worldwide which allow us to measure this quantity to high precision (parts per million even).

Atomic masses are usually tabulated in terms of the mass excess defined by
\begin{equation*}
\begin{split}
\Delta M(N, Z) =  M(N, Z) - uA,
\end{split}
\end{equation*}
where \(u\) is the Atomic Mass Unit
\begin{equation*}
\begin{split}
u = M(^{12}\mathrm{C})/12 = 931.4940954(57) \hspace{0.1cm} \mathrm{MeV}/c^2.
\end{split}
\end{equation*}
The nucleon masses are
\begin{equation*}
\begin{split}
m_p =  1.00727646693(9)u,
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
m_n = 939.56536(8)\hspace{0.1cm} \mathrm{MeV}/c^2 = 1.0086649156(6)u.
\end{split}
\end{equation*}
In the \sphinxhref{http://nuclearmasses.org/resources\_folder/Wang\_2017\_Chinese\_Phys\_C\_41\_030003.pdf}{2016 mass evaluation of by W.J.Huang, G.Audi, M.Wang, F.G.Kondev, S.Naimi and X.Xu}
there are data on masses and decays of 3437 nuclei.

The nuclear binding energy is defined as the energy required to break
up a given nucleus into its constituent parts of \(N\) neutrons and \(Z\)
protons. In terms of the atomic masses \(M(N, Z)\) the binding energy is
defined by
\begin{equation*}
\begin{split}
BE(N, Z) = ZM_H c^2 + Nm_n c^2 - M(N, Z)c^2 ,
\end{split}
\end{equation*}
where \(M_H\) is the mass of the hydrogen atom and \(m_n\) is the mass of the neutron.
In terms of the mass excess the binding energy is given by
\begin{equation*}
\begin{split}
BE(N, Z) = Z\Delta_H c^2 + N\Delta_n c^2 -\Delta(N, Z)c^2 ,
\end{split}
\end{equation*}
where \(\Delta_H c^2 = 7.2890\) MeV and \(\Delta_n c^2 = 8.0713\) MeV.

A popular and physically intuitive model which can be used to parametrize
the experimental binding energies as function of \(A\), is the so\sphinxhyphen{}called
\sphinxstylestrong{liquid drop model}. The ansatz is based on the following expression
\begin{equation*}
\begin{split}
BE(N,Z) = a_1A-a_2A^{2/3}-a_3\frac{Z^2}{A^{1/3}}-a_4\frac{(N-Z)^2}{A},
\end{split}
\end{equation*}
where \(A\) stands for the number of nucleons and the \(a_i\)s are parameters which are determined by a fit
to the experimental data.

To arrive at the above expression we have assumed that we can make the following assumptions:
\begin{itemize}
\item {} 
There is a volume term \(a_1A\) proportional with the number of nucleons (the energy is also an extensive quantity). When an assembly of nucleons of the same size is packed together into the smallest volume, each interior nucleon has a certain number of other nucleons in contact with it. This contribution is proportional to the volume.

\item {} 
There is a surface energy term \(a_2A^{2/3}\). The assumption here is that a nucleon at the surface of a nucleus interacts with fewer other nucleons than one in the interior of the nucleus and hence its binding energy is less. This surface energy term takes that into account and is therefore negative and is proportional to the surface area.

\item {} 
There is a Coulomb energy term \(a_3\frac{Z^2}{A^{1/3}}\). The electric repulsion between each pair of protons in a nucleus yields less binding.

\item {} 
There is an asymmetry term \(a_4\frac{(N-Z)^2}{A}\). This term is associated with the Pauli exclusion principle and reflects the fact that the proton\sphinxhyphen{}neutron interaction is more attractive on the average than the neutron\sphinxhyphen{}neutron and proton\sphinxhyphen{}proton interactions.

\end{itemize}

We could also add a so\sphinxhyphen{}called pairing term, which is a correction term that
arises from the tendency of proton pairs and neutron pairs to
occur. An even number of particles is more stable than an odd number.


\subsubsection{Organizing our data}
\label{\detokenize{chapter2:organizing-our-data}}
Let us start with reading and organizing our data.
We start with the compilation of masses and binding energies from 2016.
After having downloaded this file to our own computer, we are now ready to read the file and start structuring our data.

We start with preparing folders for storing our calculations and the data file over masses and binding energies. We import also various modules that we will find useful in order to present various Machine Learning methods. Here we focus mainly on the functionality of \sphinxstylestrong{scikit\sphinxhyphen{}learn}.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Common imports}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k}{as} \PYG{n+nn}{skl}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{,} \PYG{n}{r2\PYGZus{}score}\PYG{p}{,} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}
\PYG{k+kn}{import} \PYG{n+nn}{os}

\PYG{c+c1}{\PYGZsh{} Where to save the figures and data files}
\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{MassEval2016.dat}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\end{sphinxVerbatim}

Before we proceed, we define also a function for making our plots. You can obviously avoid this and simply set up various \sphinxstylestrong{matplotlib} commands every time you need them. You may however find it convenient to collect all such commands in one function and simply call this function.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{pylab} \PYG{k+kn}{import} \PYG{n}{plt}\PYG{p}{,} \PYG{n}{mpl}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{style}\PYG{o}{.}\PYG{n}{use}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{seaborn}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{mpl}\PYG{o}{.}\PYG{n}{rcParams}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{font.family}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{serif}\PYG{l+s+s1}{\PYGZsq{}}

\PYG{k}{def} \PYG{n+nf}{MakePlot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,} \PYG{n}{styles}\PYG{p}{,} \PYG{n}{labels}\PYG{p}{,} \PYG{n}{axlabels}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{)}\PYG{p}{)}
    \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{y}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{styles}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{n}{labels}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)}
        \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{n}{axlabels}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}
        \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{n}{axlabels}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{n}{loc}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{)}
\end{sphinxVerbatim}

Our next step is to read the data on experimental binding energies and
reorganize them as functions of the mass number \(A\), the number of
protons \(Z\) and neutrons \(N\) using \sphinxstylestrong{pandas}.  Before we do this it is
always useful (unless you have a binary file or other types of compressed
data) to actually open the file and simply take a look at it!

In particular, the program that outputs the final nuclear masses is written in Fortran with a specific format. It means that we need to figure out the format and which columns contain the data we are interested in. Pandas comes with a function that reads formatted output. After having admired the file, we are now ready to start massaging it with \sphinxstylestrong{pandas}. The file begins with some basic format information.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}                                                                                                                         }
\PYG{l+s+sd}{This is taken from the data file of the mass 2016 evaluation.                                                               }
\PYG{l+s+sd}{All files are 3436 lines long with 124 character per line.                                                                  }
\PYG{l+s+sd}{       Headers are 39 lines long.                                                                                           }
\PYG{l+s+sd}{   col 1     :  Fortran character control: 1 = page feed  0 = line feed                                                     }
\PYG{l+s+sd}{   format    :  a1,i3,i5,i5,i5,1x,a3,a4,1x,f13.5,f11.5,f11.3,f9.3,1x,a2,f11.3,f9.3,1x,i3,1x,f12.5,f11.5                     }
\PYG{l+s+sd}{   These formats are reflected in the pandas widths variable below, see the statement                                       }
\PYG{l+s+sd}{   widths=(1,3,5,5,5,1,3,4,1,13,11,11,9,1,2,11,9,1,3,1,12,11,1),                                                            }
\PYG{l+s+sd}{   Pandas has also a variable header, with length 39 in this case.                                                          }
\PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYGZsq{}                                                                                                                         \PYGZbs{}nThis is taken from the data file of the mass 2016 evaluation.                                                               \PYGZbs{}nAll files are 3436 lines long with 124 character per line.                                                                  \PYGZbs{}n       Headers are 39 lines long.                                                                                           \PYGZbs{}n   col 1     :  Fortran character control: 1 = page feed  0 = line feed                                                     \PYGZbs{}n   format    :  a1,i3,i5,i5,i5,1x,a3,a4,1x,f13.5,f11.5,f11.3,f9.3,1x,a2,f11.3,f9.3,1x,i3,1x,f12.5,f11.5                     \PYGZbs{}n   These formats are reflected in the pandas widths variable below, see the statement                                       \PYGZbs{}n   widths=(1,3,5,5,5,1,3,4,1,13,11,11,9,1,2,11,9,1,3,1,12,11,1),                                                            \PYGZbs{}n   Pandas has also a variable header, with length 39 in this case.                                                          \PYGZbs{}n\PYGZsq{}
\end{sphinxVerbatim}

The data we are interested in are in columns 2, 3, 4 and 11, giving us
the number of neutrons, protons, mass numbers and binding energies,
respectively. We add also for the sake of completeness the element name. The data are in fixed\sphinxhyphen{}width formatted lines and we will
covert them into the \sphinxstylestrong{pandas} DataFrame structure.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Read the experimental data with Pandas}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}fwf}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{usecols}\PYG{o}{=}\PYG{p}{(}\PYG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m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{12}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,}
              \PYG{n}{header}\PYG{o}{=}\PYG{l+m+mi}{39}\PYG{p}{,}
              \PYG{n}{index\PYGZus{}col}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Extrapolated values are indicated by \PYGZsq{}\PYGZsh{}\PYGZsq{} in place of the decimal place, so}
\PYG{c+c1}{\PYGZsh{} the Ebinding column won\PYGZsq{}t be numeric. Coerce to float and drop these entries.}
\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Convert from keV to MeV.}
\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{/}\PYG{o}{=} \PYG{l+m+mi}{1000}

\PYG{c+c1}{\PYGZsh{} Group the DataFrame by nucleon number, A.}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{groupby}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Find the rows of the grouped DataFrame with the maximum binding energy.}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{apply}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{t}\PYG{p}{:} \PYG{n}{t}\PYG{p}{[}\PYG{n}{t}\PYG{o}{.}\PYG{n}{Ebinding}\PYG{o}{==}\PYG{n}{t}\PYG{o}{.}\PYG{n}{Ebinding}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)}
\end{sphinxVerbatim}

We have now read in the data, grouped them according to the variables we are interested in.
We see how easy it is to reorganize the data using \sphinxstylestrong{pandas}. If we
were to do these operations in C/C++ or Fortran, we would have had to
write various functions/subroutines which perform the above
reorganizations for us.  Having reorganized the data, we can now start
to make some simple fits using both the functionalities in \sphinxstylestrong{numpy} and
\sphinxstylestrong{Scikit\sphinxhyphen{}Learn} afterwards.

Now we define five variables which contain
the number of nucleons \(A\), the number of protons \(Z\) and the number of neutrons \(N\), the element name and finally the energies themselves.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{A} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Z} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{N} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{N}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Element} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Element}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Energies} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
            N    Z    A Element  Ebinding
A                                        
1   0       0    1    1       H  0.000000
2   1       1    1    2       H  1.112283
3   2       2    1    3       H  2.827265
4   6       2    2    4      He  7.073915
5   9       3    2    5      He  5.512132
...       ...  ...  ...     ...       ...
264 3304  156  108  264      Hs  7.298375
265 3310  157  108  265      Hs  7.296247
266 3317  158  108  266      Hs  7.298273
269 3338  159  110  269      Ds  7.250154
270 3344  160  110  270      Ds  7.253775

[267 rows x 5 columns]
\end{sphinxVerbatim}

The next step, and we will define this mathematically later, is to set up the so\sphinxhyphen{}called \sphinxstylestrong{design matrix}. We will throughout call this matrix \(\boldsymbol{X}\).
It has dimensionality \(p\times n\), where \(n\) is the number of data points and \(p\) are the so\sphinxhyphen{}called predictors. In our case here they are given by the number of polynomials in \(A\) we wish to include in the fit.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Now we set up the design matrix X}
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mi}{1}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mf}{2.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{p}{)}
\end{sphinxVerbatim}

With \sphinxstylestrong{scikitlearn} we are now ready to use linear regression and fit our data.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{clf} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)}
\PYG{n}{fity} \PYG{o}{=} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}
\end{sphinxVerbatim}

Pretty simple!Now we can print measures of how our fit is doing, the coefficients from the fits and plot the final fit together with our data.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} The mean squared error                               }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{fity}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Explained variance score: 1 is perfect prediction                                 }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance score: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{fity}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Mean absolute error                                                           }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean absolute error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{fity}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{clf}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{p}{,} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{intercept\PYGZus{}}\PYG{p}{)}

\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eapprox}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}  \PYG{o}{=} \PYG{n}{fity}
\PYG{c+c1}{\PYGZsh{} Generate a plot comparing the experimental with the fitted values values.}
\PYG{n}{fig}\PYG{p}{,} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplots}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}A = N + Z\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}E\PYGZus{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}bind\PYGZcb{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{,/}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}MeV\PYGZcb{}}\PYG{l+s+s1}{\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.7}\PYG{p}{,} \PYG{n}{lw}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{,}
            \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ame2016}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eapprox}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.7}\PYG{p}{,} \PYG{n}{lw}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{c}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{m}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}
            \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Fit}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{save\PYGZus{}fig}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Masses2016}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
Mean squared error: 0.04
Variance score: 0.95
Mean absolute error: 0.05
[ 0.00000000e+00  7.06492086e\PYGZhy{}03 \PYGZhy{}1.73091052e\PYGZhy{}01 \PYGZhy{}1.66020213e+01
  1.17385778e+00] 15.212327334149508
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_114_1}.png}


\subsubsection{Seeing the wood for the trees}
\label{\detokenize{chapter2:seeing-the-wood-for-the-trees}}
As a teaser, let us now see how we can do this with decision trees using \sphinxstylestrong{scikit\sphinxhyphen{}learn}. Later we will switch to so\sphinxhyphen{}called \sphinxstylestrong{random forests}!

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{}Decision Tree Regression}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{tree} \PYG{k+kn}{import} \PYG{n}{DecisionTreeRegressor}
\PYG{n}{regr\PYGZus{}1}\PYG{o}{=}\PYG{n}{DecisionTreeRegressor}\PYG{p}{(}\PYG{n}{max\PYGZus{}depth}\PYG{o}{=}\PYG{l+m+mi}{5}\PYG{p}{)}
\PYG{n}{regr\PYGZus{}2}\PYG{o}{=}\PYG{n}{DecisionTreeRegressor}\PYG{p}{(}\PYG{n}{max\PYGZus{}depth}\PYG{o}{=}\PYG{l+m+mi}{7}\PYG{p}{)}
\PYG{n}{regr\PYGZus{}3}\PYG{o}{=}\PYG{n}{DecisionTreeRegressor}\PYG{p}{(}\PYG{n}{max\PYGZus{}depth}\PYG{o}{=}\PYG{l+m+mi}{9}\PYG{p}{)}
\PYG{n}{regr\PYGZus{}1}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)}
\PYG{n}{regr\PYGZus{}2}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)}
\PYG{n}{regr\PYGZus{}3}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)}


\PYG{n}{y\PYGZus{}1} \PYG{o}{=} \PYG{n}{regr\PYGZus{}1}\P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)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Decision Tree Regression}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{save\PYGZus{}fig}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Masses2016Trees}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{p}{(}\PYG{n}{Energies}\PYG{o}{\PYGZhy{}}\PYG{n}{y\PYGZus{}1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_116_0}.png}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
            N    Z    A Element  Ebinding   Eapprox
A                                                  
1   0       0    1    1       H  0.000000  0.000000
2   1       1    1    2       H  1.112283  1.112283
3   2       2    1    3       H  2.827265  2.827265
4   6       2    2    4      He  7.073915  7.073915
5   9       3    2    5      He  5.512132  5.512132
...       ...  ...  ...     ...       ...       ...
264 3304  156  108  264      Hs  7.298375  7.298375
265 3310  157  108  265      Hs  7.296247  7.297260
266 3317  158  108  266      Hs  7.298273  7.297260
269 3338  159  110  269      Ds  7.250154  7.250154
270 3344  160  110  270      Ds  7.253775  7.253775

[267 rows x 6 columns]
0.00988361564671618
\end{sphinxVerbatim}


\subsubsection{And what about using neural networks?}
\label{\detokenize{chapter2:and-what-about-using-neural-networks}}
The \sphinxstylestrong{seaborn} package allows us to visualize data in an efficient way. Note that we use \sphinxstylestrong{scikit\sphinxhyphen{}learn}’s multi\sphinxhyphen{}layer perceptron (or feed forward neural network)
functionality.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{neural\PYGZus{}network} \PYG{k+kn}{import} \PYG{n}{MLPRegressor}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{accuracy\PYGZus{}score}
\PYG{k+kn}{import} \PYG{n+nn}{seaborn} \PYG{k}{as} \PYG{n+nn}{sns}

\PYG{n}{X\PYGZus{}train} \PYG{o}{=} \PYG{n}{X}
\PYG{n}{Y\PYGZus{}train} \PYG{o}{=} \PYG{n}{Energies}
\PYG{n}{n\PYGZus{}hidden\PYGZus{}neurons} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{epochs} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{c+c1}{\PYGZsh{} store models for later use}
\PYG{n}{eta\PYGZus{}vals} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{)}
\PYG{n}{lmbd\PYGZus{}vals} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} store the models for later use}
\PYG{n}{DNN\PYGZus{}scikit} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{eta\PYGZus{}vals}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{lmbd\PYGZus{}vals}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{dtype}\PYG{o}{=}\PYG{n+nb}{object}\PYG{p}{)}
\PYG{n}{train\PYGZus{}accuracy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{eta\PYGZus{}vals}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{lmbd\PYGZus{}vals}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{sns}\PYG{o}{.}\PYG{n}{set}\PYG{p}{(}\PYG{p}{)}
\PYG{k}{for} \PYG{n}{i}\PYG{p}{,} \PYG{n}{eta} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{eta\PYGZus{}vals}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{for} \PYG{n}{j}\PYG{p}{,} \PYG{n}{lmbd} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{lmbd\PYGZus{}vals}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{dnn} \PYG{o}{=} \PYG{n}{MLPRegressor}\PYG{p}{(}\PYG{n}{hidden\PYGZus{}layer\PYGZus{}sizes}\PYG{o}{=}\PYG{p}{(}\PYG{n}{n\PYGZus{}hidden\PYGZus{}neurons}\PYG{p}{)}\PYG{p}{,} \PYG{n}{activation}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{logistic}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}
                            \PYG{n}{alpha}\PYG{o}{=}\PYG{n}{lmbd}\PYG{p}{,} \PYG{n}{learning\PYGZus{}rate\PYGZus{}init}\PYG{o}{=}\PYG{n}{eta}\PYG{p}{,} \PYG{n}{max\PYGZus{}iter}\PYG{o}{=}\PYG{n}{epochs}\PYG{p}{)}
        \PYG{n}{dnn}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{Y\PYGZus{}train}\PYG{p}{)}
        \PYG{n}{DNN\PYGZus{}scikit}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]} \PYG{o}{=} \PYG{n}{dnn}
        \PYG{n}{train\PYGZus{}accuracy}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]} \PYG{o}{=} \PYG{n}{dnn}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{Y\PYGZus{}train}\PYG{p}{)}

\PYG{n}{fig}\PYG{p}{,} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplots}\PYG{p}{(}\PYG{n}{figsize} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{sns}\PYG{o}{.}\PYG{n}{heatmap}\PYG{p}{(}\PYG{n}{train\PYGZus{}accuracy}\PYG{p}{,} \PYG{n}{annot}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{,} \PYG{n}{ax}\PYG{o}{=}\PYG{n}{ax}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{viridis}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Training Accuracy}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{eta\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{lambda\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
/Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet.
  warnings.warn(
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter2_118_16}.png}


\subsection{A first summary}
\label{\detokenize{chapter2:a-first-summary}}
The aim behind these introductory words was to present to you various
Python libraries and their functionalities, in particular libraries like
\sphinxstylestrong{numpy}, \sphinxstylestrong{pandas}, \sphinxstylestrong{xarray} and \sphinxstylestrong{matplotlib} and other that make our life much easier
in handling various data sets and visualizing data.

Furthermore,
\sphinxstylestrong{Scikit\sphinxhyphen{}Learn} allows us with few lines of code to implement popular
Machine Learning algorithms for supervised learning. Later we will meet \sphinxstylestrong{Tensorflow}, a powerful library for deep learning.
Now it is time to dive more into the details of various methods. We will start with linear regression and try to take a deeper look at what it entails.




\section{Data Analysis and Machine Learning: Elements of Probability Theory and Statistical Data Analysis}
\label{\detokenize{chapter3:data-analysis-and-machine-learning-elements-of-probability-theory-and-statistical-data-analysis}}\label{\detokenize{chapter3::doc}}




\sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

Date: \sphinxstylestrong{Sep 20, 2020}

Copyright 1999\sphinxhyphen{}2020, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license


\subsection{To do list}
\label{\detokenize{chapter3:to-do-list}}\begin{itemize}
\item {} 
add math about MVN and define MLE and other quantities

\item {} 
rewrite about covariance matrix

\item {} 
add KL theorem

\end{itemize}


\subsection{Domains and probabilities}
\label{\detokenize{chapter3:domains-and-probabilities}}
Consider the following simple example, namely the tossing of two dice, resulting in  the following possible values
\begin{equation*}
\begin{split}
\{2,3,4,5,6,7,8,9,10,11,12\}.
\end{split}
\end{equation*}
These values are called the \sphinxstyleemphasis{domain}.
To this domain we have the corresponding \sphinxstyleemphasis{probabilities}
\begin{equation*}
\begin{split}
\{1/36,2/36/,3/36,4/36,5/36,6/36,5/36,4/36,3/36,2/36,1/36\}.
\end{split}
\end{equation*}

\subsection{Tossing  the dice}
\label{\detokenize{chapter3:tossing-the-dice}}
The numbers in the domain are the outcomes of the physical process of tossing say two dice.
We cannot tell beforehand whether the outcome is 3 or 5 or any other number in this domain.
This defines the randomness of the outcome, or unexpectedness or any other synonimous word which
encompasses the uncertitude of the final outcome.

The only thing we can tell beforehand
is that say the outcome 2 has a certain probability.If our favorite hobby is to  spend an hour every evening throwing dice and
registering the sequence of outcomes, we will note that the numbers in the above domain
\begin{equation*}
\begin{split}
\{2,3,4,5,6,7,8,9,10,11,12\},
\end{split}
\end{equation*}
appear in a random order. After 11 throws the results may look like
\begin{equation*}
\begin{split}
\{10,8,6,3,6,9,11,8,12,4,5\}.
\end{split}
\end{equation*}

\subsection{Stochastic variables}
\label{\detokenize{chapter3:stochastic-variables}}
\sphinxstylestrong{Random variables are characterized by a domain which contains all possible values that the random value may take. This domain has a corresponding probability distribution function(PDF)}.


\subsection{Stochastic variables and the main concepts, the discrete case}
\label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-the-discrete-case}}
There are two main concepts associated with a stochastic variable. The
\sphinxstyleemphasis{domain} is the set \(\mathbb D = \{x\}\) of all accessible values
the variable can assume, so that \(X \in \mathbb D\). An example of a
discrete domain is the set of six different numbers that we may get by
throwing of a dice, \(x\in\{1,\,2,\,3,\,4,\,5,\,6\}\).

The \sphinxstyleemphasis{probability distribution function (PDF)} is a function
\(p(x)\) on the domain which, in the discrete case, gives us the
probability or relative frequency with which these values of \(X\)
occur
\begin{equation*}
\begin{split}
p(x) = \mathrm{Prob}(X=x).
\end{split}
\end{equation*}

\subsection{Stochastic variables and the main concepts, the continuous case}
\label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-the-continuous-case}}
In the continuous case, the PDF does not directly depict the
actual probability. Instead we define the probability for the
stochastic variable to assume any value on an infinitesimal interval
around \(x\) to be \(p(x)dx\). The continuous function \(p(x)\) then gives us
the \sphinxstyleemphasis{density} of the probability rather than the probability
itself. The probability for a stochastic variable to assume any value
on a non\sphinxhyphen{}infinitesimal interval \([a,\,b]\) is then just the integral
\begin{equation*}
\begin{split}
\mathrm{Prob}(a\leq X\leq b) = \int_a^b p(x)dx.
\end{split}
\end{equation*}
Qualitatively speaking, a stochastic variable represents the values of
numbers chosen as if by chance from some specified PDF so that the
selection of a large set of these numbers reproduces this PDF.


\subsection{The cumulative probability}
\label{\detokenize{chapter3:the-cumulative-probability}}
Of interest to us is the \sphinxstyleemphasis{cumulative probability
distribution function} (\sphinxstylestrong{CDF}), \(P(x)\), which is just the probability
for a stochastic variable \(X\) to assume any value less than \(x\)
\begin{equation*}
\begin{split}
P(x)=\mathrm{Prob(}X\leq x\mathrm{)} =
\int_{-\infty}^x p(x^{\prime})dx^{\prime}.
\end{split}
\end{equation*}
The relation between a CDF and its corresponding PDF is then
\begin{equation*}
\begin{split}
p(x) = \frac{d}{dx}P(x).
\end{split}
\end{equation*}

\subsection{Properties of PDFs}
\label{\detokenize{chapter3:properties-of-pdfs}}
There are two properties that all PDFs must satisfy. The first one is
positivity (assuming that the PDF is normalized)
\begin{equation*}
\begin{split}
0 \leq p(x) \leq 1.
\end{split}
\end{equation*}
Naturally, it would be nonsensical for any of the values of the domain
to occur with a probability greater than \(1\) or less than \(0\). Also,
the PDF must be normalized. That is, all the probabilities must add up
to unity.  The probability of “anything” to happen is always unity. For
both discrete and continuous PDFs, this condition is
\begin{equation*}
\begin{split}
\begin{align*}
\sum_{x_i\in\mathbb D} p(x_i) & =  1,\\
\int_{x\in\mathbb D} p(x)\,dx & =  1.
\end{align*}
\end{split}
\end{equation*}

\subsection{Important distributions, the uniform distribution}
\label{\detokenize{chapter3:important-distributions-the-uniform-distribution}}
The first one
is the most basic PDF; namely the uniform distribution




\begin{equation*}
\begin{split}
\begin{equation}
p(x) = \frac{1}{b-a}\theta(x-a)\theta(b-x).
\label{eq:unifromPDF} \tag{1}
\end{equation}
\end{split}
\end{equation*}
For \(a=0\) and \(b=1\) we have
\begin{equation*}
\begin{split}
\begin{array}{ll}
p(x)dx = dx & \in [0,1].
\end{array}
\end{split}
\end{equation*}
The latter distribution is used to generate random numbers. For other PDFs, one needs normally a mapping from this distribution to say for example the exponential distribution.


\subsection{Gaussian distribution}
\label{\detokenize{chapter3:gaussian-distribution}}
The second one is the Gaussian Distribution
\begin{equation*}
\begin{split}
p(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp{(-\frac{(x-\mu)^2}{2\sigma^2})},
\end{split}
\end{equation*}
with mean value \(\mu\) and standard deviation \(\sigma\). If \(\mu=0\) and \(\sigma=1\), it is normally called the \sphinxstylestrong{standard normal distribution}
\begin{equation*}
\begin{split}
p(x) = \frac{1}{\sqrt{2\pi}} \exp{(-\frac{x^2}{2})},
\end{split}
\end{equation*}
The following simple Python code plots the above distribution for different values of \(\mu\) and \(\sigma\).

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{o}{\PYGZpc{}}\PYG{k}{matplotlib} inline

\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{acos}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sqrt}
\PYG{k+kn}{from}  \PYG{n+nn}{matplotlib} \PYG{k+kn}{import} \PYG{n}{pyplot} \PYG{k}{as} \PYG{n}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{matplotlib} \PYG{k+kn}{import} \PYG{n}{rc}\PYG{p}{,} \PYG{n}{rcParams}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{units} \PYG{k}{as} \PYG{n+nn}{units}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{ticker} \PYG{k}{as} \PYG{n+nn}{ticker}
\PYG{n}{rc}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{text}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{n}{usetex}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}
\PYG{n}{rc}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{font}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{o}{*}\PYG{o}{*}\PYG{p}{\PYGZob{}}\PYG{l+s+s1}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= 2\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{fontdict}\PYG{o}{=}\PYG{n}{font}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{text}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{19}\PYG{p}{,} \PYG{l+m+mf}{0.08}\PYG{p}{,} \PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Parameters: \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mu = 2\PYGZdl{}, \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{sigma = 4\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{fontdict}\PYG{o}{=}\PYG{n}{font}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}p(x)\PYGZdl{} [MeV]}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Tweak spacing to prevent clipping of ylabel                                                                       }
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplots\PYGZus{}adjust}\PYG{p}{(}\PYG{n}{left}\PYG{o}{=}\PYG{l+m+mf}{0.15}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{gaussian.pdf}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{pdf}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter3_29_0}.png}


\subsection{Exponential distribution}
\label{\detokenize{chapter3:exponential-distribution}}
Another important distribution in science is the exponential distribution
\begin{equation*}
\begin{split}
p(x) = \alpha\exp{-(\alpha x)}.
\end{split}
\end{equation*}

\subsection{Expectation values}
\label{\detokenize{chapter3:expectation-values}}
Let \(h(x)\) be an arbitrary continuous function on the domain of the stochastic
variable \(X\) whose PDF is \(p(x)\). We define the \sphinxstyleemphasis{expectation value}
of \(h\) with respect to \(p\) as follows




\begin{equation*}
\begin{split}
\begin{equation}
\langle h \rangle_X \equiv \int\! h(x)p(x)\,dx
\label{eq:expectation_value_of_h_wrt_p} \tag{2}
\end{equation}
\end{split}
\end{equation*}
Whenever the PDF is known implicitly, like in this case, we will drop
the index \(X\) for clarity.A particularly useful class of special expectation values are the
\sphinxstyleemphasis{moments}. The \(n\)\sphinxhyphen{}th moment of the PDF \(p\) is defined as
follows
\begin{equation*}
\begin{split}
\langle x^n \rangle \equiv \int\! x^n p(x)\,dx
\end{split}
\end{equation*}

\subsection{Stochastic variables and the main concepts, mean values}
\label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-mean-values}}
The zero\sphinxhyphen{}th moment \(\langle 1\rangle\) is just the normalization condition of
\(p\). The first moment, \(\langle x\rangle\), is called the \sphinxstyleemphasis{mean} of \(p\)
and often denoted by the letter \(\mu\)
\begin{equation*}
\begin{split}
\langle x\rangle  = \mu \equiv \int x p(x)dx,
\end{split}
\end{equation*}
for a continuous distribution and
\begin{equation*}
\begin{split}
\langle x\rangle  = \mu \equiv \sum_{i=1}^N x_i p(x_i),
\end{split}
\end{equation*}
for a discrete distribution.
Qualitatively it represents the centroid or the average value of the
PDF and is therefore simply called the expectation value of \(p(x)\).


\subsection{Stochastic variables and the main concepts, central moments, the variance}
\label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-central-moments-the-variance}}
A special version of the moments is the set of \sphinxstyleemphasis{central moments}, the n\sphinxhyphen{}th central moment defined as
\begin{equation*}
\begin{split}
\langle (x-\langle x\rangle )^n\rangle  \equiv \int\! (x-\langle x\rangle)^n p(x)\,dx
\end{split}
\end{equation*}
The zero\sphinxhyphen{}th and first central moments are both trivial, equal \(1\) and
\(0\), respectively. But the second central moment, known as the
\sphinxstyleemphasis{variance} of \(p\), is of particular interest. For the stochastic
variable \(X\), the variance is denoted as \(\sigma^2_X\) or \(\mathrm{Var}(X)\)
\begin{equation*}
\begin{split}
\begin{align*}
\sigma^2_X &=\mathrm{Var}(X) =  \langle (x-\langle x\rangle)^2\rangle  =
\int (x-\langle x\rangle)^2 p(x)dx\\
& =  \int\left(x^2 - 2 x \langle x\rangle^{2} +\langle x\rangle^2\right)p(x)dx\\
& =  \langle x^2\rangle - 2 \langle x\rangle\langle x\rangle + \langle x\rangle^2\\
& =  \langle x^2 \rangle - \langle x\rangle^2
\end{align*}
\end{split}
\end{equation*}
The square root of the variance, \(\sigma =\sqrt{\langle (x-\langle x\rangle)^2\rangle}\) is called the
\sphinxstylestrong{standard deviation} of \(p\). It is the RMS (root\sphinxhyphen{}mean\sphinxhyphen{}square)
value of the deviation of the PDF from its mean value, interpreted
qualitatively as the “spread” of \(p\) around its mean.


\subsection{Probability Distribution Functions}
\label{\detokenize{chapter3:probability-distribution-functions}}
The following table collects properties of probability distribution functions.
In our notation we reserve the label \(p(x)\) for the probability of a certain event,
while \(P(x)\) is the cumulative probability.




\subsection{Probability Distribution Functions}
\label{\detokenize{chapter3:id1}}
With a PDF we can compute expectation values of selected quantities such as
\begin{equation*}
\begin{split}
\langle x^k\rangle=\sum_{i=1}^{N}x_i^kp(x_i),
\end{split}
\end{equation*}
if we have a discrete PDF or
\begin{equation*}
\begin{split}
\langle x^k\rangle=\int_a^b x^kp(x)dx,
\end{split}
\end{equation*}
in the case of a continuous PDF. We have already defined the mean value \(\mu\)
and the variance \(\sigma^2\).


\subsection{The three famous Probability Distribution Functions}
\label{\detokenize{chapter3:the-three-famous-probability-distribution-functions}}
There are at least three PDFs which one may encounter. These are the

\sphinxstylestrong{Uniform distribution}
\begin{equation*}
\begin{split}
p(x)=\frac{1}{b-a}\Theta(x-a)\Theta(b-x),
\end{split}
\end{equation*}
yielding probabilities different from zero in the interval \([a,b]\).

\sphinxstylestrong{The exponential distribution}
\begin{equation*}
\begin{split}
p(x)=\alpha \exp{(-\alpha x)},
\end{split}
\end{equation*}
yielding probabilities different from zero in the interval \([0,\infty)\) and with mean value
\begin{equation*}
\begin{split}
\mu = \int_0^{\infty}xp(x)dx=\int_0^{\infty}x\alpha \exp{(-\alpha x)}dx=\frac{1}{\alpha},
\end{split}
\end{equation*}
with variance
\begin{equation*}
\begin{split}
\sigma^2=\int_0^{\infty}x^2p(x)dx-\mu^2 = \frac{1}{\alpha^2}.
\end{split}
\end{equation*}

\subsection{Probability Distribution Functions, the normal distribution}
\label{\detokenize{chapter3:probability-distribution-functions-the-normal-distribution}}
Finally, we have the so\sphinxhyphen{}called univariate normal  distribution, or just the \sphinxstylestrong{normal distribution}
\begin{equation*}
\begin{split}
p(x)=\frac{1}{b\sqrt{2\pi}}\exp{\left(-\frac{(x-a)^2}{2b^2}\right)}
\end{split}
\end{equation*}
with probabilities different from zero in the interval \((-\infty,\infty)\).
The integral \(\int_{-\infty}^{\infty}\exp{\left(-(x^2\right)}dx\) appears in many calculations, its value
is \(\sqrt{\pi}\),  a result we will need when we compute the mean value and the variance.
The mean value is
\begin{equation*}
\begin{split}
\mu = \int_0^{\infty}xp(x)dx=\frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}x \exp{\left(-\frac{(x-a)^2}{2b^2}\right)}dx,
\end{split}
\end{equation*}
which becomes with a suitable change of variables
\begin{equation*}
\begin{split}
\mu =\frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}b\sqrt{2}(a+b\sqrt{2}y)\exp{-y^2}dy=a.
\end{split}
\end{equation*}

\subsection{Probability Distribution Functions, the normal distribution}
\label{\detokenize{chapter3:id2}}
Similarly, the variance becomes
\begin{equation*}
\begin{split}
\sigma^2 = \frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}(x-\mu)^2 \exp{\left(-\frac{(x-a)^2}{2b^2}\right)}dx,
\end{split}
\end{equation*}
and inserting the mean value and performing a variable change we obtain
\begin{equation*}
\begin{split}
\sigma^2 = \frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}b\sqrt{2}(b\sqrt{2}y)^2\exp{\left(-y^2\right)}dy=
\frac{2b^2}{\sqrt{\pi}}\int_{-\infty}^{\infty}y^2\exp{\left(-y^2\right)}dy,
\end{split}
\end{equation*}
and performing a final integration by parts we obtain the well\sphinxhyphen{}known result \(\sigma^2=b^2\).
It is useful to introduce the standard normal distribution as well, defined by \(\mu=a=0\), viz. a distribution
centered around zero and with a variance \(\sigma^2=1\), leading to




\begin{equation*}
\begin{split}
\begin{equation}
   p(x)=\frac{1}{\sqrt{2\pi}}\exp{\left(-\frac{x^2}{2}\right)}.
\label{_auto1} \tag{3}
\end{equation}
\end{split}
\end{equation*}

\subsection{Probability Distribution Functions, the cumulative distribution}
\label{\detokenize{chapter3:probability-distribution-functions-the-cumulative-distribution}}
The exponential and uniform distributions have simple cumulative functions,
whereas the normal distribution does not, being proportional to the so\sphinxhyphen{}called
error function \(erf(x)\), given by
\begin{equation*}
\begin{split}
P(x) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^x\exp{\left(-\frac{t^2}{2}\right)}dt,
\end{split}
\end{equation*}
which is difficult to evaluate in a quick way.


\subsection{Probability Distribution Functions, other important distribution}
\label{\detokenize{chapter3:probability-distribution-functions-other-important-distribution}}
Some other PDFs which one encounters often in the natural sciences are the binomial distribution
\begin{equation*}
\begin{split}
p(x) = \left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} \hspace{0.5cm}x=0,1,\dots,n,
\end{split}
\end{equation*}
where \(y\) is the probability for a specific event, such as the tossing of a coin or moving left or right
in case of a random walker. Note that \(x\) is a discrete stochastic variable.

The sequence of binomial trials is characterized by the following definitions
\begin{itemize}
\item {} 
Every experiment is thought to consist of \(N\) independent trials.

\item {} 
In every independent trial one registers if a specific situation happens or not, such as the  jump to the left or right of a random walker.

\item {} 
The probability for every outcome in a single trial has the same value, for example the outcome of tossing (either heads or tails) a coin is always \(1/2\).

\end{itemize}


\subsection{Probability Distribution Functions, the binomial distribution}
\label{\detokenize{chapter3:probability-distribution-functions-the-binomial-distribution}}
In order to compute the mean and variance we need to recall Newton’s binomial
formula
\begin{equation*}
\begin{split}
(a+b)^m=\sum_{n=0}^m \left(\begin{array}{c} m \\ n\end{array}\right)a^nb^{m-n},
\end{split}
\end{equation*}
which can be used to show that
\begin{equation*}
\begin{split}
\sum_{x=0}^n\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} = (y+1-y)^n = 1,
\end{split}
\end{equation*}
the PDF is normalized to one.
The mean value is
\begin{equation*}
\begin{split}
\mu = \sum_{x=0}^n x\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} =
\sum_{x=0}^n x\frac{n!}{x!(n-x)!}y^x(1-y)^{n-x},
\end{split}
\end{equation*}
resulting in
\begin{equation*}
\begin{split}
\mu = 
\sum_{x=0}^n x\frac{(n-1)!}{(x-1)!(n-1-(x-1))!}y^{x-1}(1-y)^{n-1-(x-1)},
\end{split}
\end{equation*}
which we rewrite as
\begin{equation*}
\begin{split}
\mu=ny\sum_{\nu=0}^n\left(\begin{array}{c} n-1 \\ \nu\end{array}\right)y^{\nu}(1-y)^{n-1-\nu} =ny(y+1-y)^{n-1}=ny.
\end{split}
\end{equation*}
The variance is slightly trickier to get. It reads \(\sigma^2=ny(1-y)\).


\subsection{Probability Distribution Functions, Poisson’s  distribution}
\label{\detokenize{chapter3:probability-distribution-functions-poisson-s-distribution}}
Another important distribution with discrete stochastic variables \(x\) isthe Poisson model, which resembles the exponential distribution and reads
\begin{equation*}
\begin{split}
p(x) = \frac{\lambda^x}{x!} e^{-\lambda} \hspace{0.5cm}x=0,1,\dots,;\lambda > 0.
\end{split}
\end{equation*}
In this case both the mean value and the variance are easier to calculate,
\begin{equation*}
\begin{split}
\mu = \sum_{x=0}^{\infty} x \frac{\lambda^x}{x!} e^{-\lambda} = \lambda e^{-\lambda}\sum_{x=1}^{\infty}
\frac{\lambda^{x-1}}{(x-1)!}=\lambda,
\end{split}
\end{equation*}
and the variance is \(\sigma^2=\lambda\).


\subsection{Probability Distribution Functions, Poisson’s  distribution}
\label{\detokenize{chapter3:id3}}
An example of applications of the Poisson distribution could be the counting
of the number of \(\alpha\)\sphinxhyphen{}particles emitted from a radioactive source in a given time interval.
In the limit of \(n\rightarrow \infty\) and for small probabilities \(y\), the binomial distribution
approaches the Poisson distribution. Setting \(\lambda = ny\), with \(y\) the probability for an event in
the binomial distribution we can show that
\begin{equation*}
\begin{split}
\lim_{n\rightarrow \infty}\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} e^{-\lambda}=\sum_{x=1}^{\infty}\frac{\lambda^x}{x!} e^{-\lambda}.
\end{split}
\end{equation*}

\subsection{Meet the  covariance!}
\label{\detokenize{chapter3:meet-the-covariance}}
An important quantity in a statistical analysis is the so\sphinxhyphen{}called covariance.

Consider the set \(\{X_i\}\) of \(n\)
stochastic variables (not necessarily uncorrelated) with the
multivariate PDF \(P(x_1,\dots,x_n)\). The \sphinxstyleemphasis{covariance} of two
of the stochastic variables, \(X_i\) and \(X_j\), is defined as follows




\begin{equation*}
\begin{split}
\begin{equation}
\mathrm{Cov}(X_i,\,X_j)  = \langle (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)\rangle 
\label{_auto2} \tag{4}
\end{equation}
\end{split}
\end{equation*}



\begin{equation*}
\begin{split}
\begin{equation} 
=\int\cdots\int (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)P(x_1,\dots,x_n)\,dx_1\dots dx_n,
\label{eq:def_covariance} \tag{5}
\end{equation}
\end{split}
\end{equation*}
with
\begin{equation*}
\begin{split}
\langle x_i\rangle =
\int\cdots\int x_i P(x_1,\dots,x_n)\,dx_1\dots dx_n.
\end{split}
\end{equation*}

\subsection{Meet the  covariance in matrix disguise}
\label{\detokenize{chapter3:meet-the-covariance-in-matrix-disguise}}
If we consider the above covariance as a matrix
\begin{equation*}
\begin{split}
C_{ij} =\mathrm{Cov}(X_i,\,X_j),
\end{split}
\end{equation*}
then the diagonal elements are just the familiar
variances, \(C_{ii} = \mathrm{Cov}(X_i,\,X_i) = \mathrm{Var}(X_i)\). It turns out that
all the off\sphinxhyphen{}diagonal elements are zero if the stochastic variables are
uncorrelated.


\subsection{Covariance}
\label{\detokenize{chapter3:covariance}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Importing various packages}
\PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sqrt}
\PYG{k+kn}{from} \PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{random}\PYG{p}{,} \PYG{n}{seed}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}

\PYG{k}{def} \PYG{n+nf}{covariance}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
    \PYG{n+nb}{sum} \PYG{o}{=} \PYG{l+m+mf}{0.0}
    \PYG{n}{mean\PYGZus{}x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
    \PYG{n}{mean\PYGZus{}y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}
    \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
        \PYG{n+nb}{sum} \PYG{o}{+}\PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{]}\PYG{o}{\PYGZhy{}}\PYG{n}{mean\PYGZus{}x}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{o}{\PYGZhy{}}\PYG{n}{mean\PYGZus{}y}\PYG{p}{)}
    \PYG{k}{return}  \PYG{n+nb}{sum}\PYG{o}{/}\PYG{n}{n}

\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10}

\PYG{n}{x}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n}{covxy} \PYG{o}{=} \PYG{n}{covariance}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,}\PYG{n}{n}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{covxy}\PYG{p}{)}
\PYG{n}{z} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{vstack}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{c} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{n}{z}\PYG{o}{.}\PYG{n}{T}\PYG{p}{)}

\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
1.3311287509199639
[[14.31659816 13.99268172 19.21873233 14.42933266 18.41189432 16.6094975
  11.00141571  4.4034975  14.34205163  7.63509233]
 [13.99268172 13.67609397 18.78390394 14.10286557 17.99532083 16.23370367
  10.75250606  4.30386732 14.0175593   7.46234655]
 [19.21873233 18.78390394 25.79940208 19.37006815 24.71629535 22.29674138
  14.76840108  5.91129532 19.2529013  10.24941779]
 [14.42933266 14.10286557 19.37006815 14.54295487 18.55687678 16.74028719
  11.08804516  4.43817236 14.45498656  7.69521404]
 [18.41189432 17.99532083 24.71629535 18.55687678 23.67865945 21.36068284
  14.14839622  5.66312819 18.44462881  9.81912823]
 [16.6094975  16.23370367 22.29674138 16.74028719 21.36068284 19.26962007
  12.76336631  5.1087472  16.63902752  8.85790364]
 [11.00141571 10.75250606 14.76840108 11.08804516 14.14839622 12.76336631
   8.45390408  3.38381409 11.02097512  5.86709383]
 [ 4.4034975   4.30386732  5.91129532  4.43817236  5.66312819  5.1087472
   3.38381409  1.35442722  4.41132648  2.34840076]
 [14.34205163 14.0175593  19.2529013  14.45498656 18.44462881 16.63902752
  11.02097512  4.41132648 14.36755036  7.64866676]
 [ 7.63509233  7.46234655 10.24941779  7.69521404  9.81912823  8.85790364
   5.86709383  2.34840076  7.64866676  4.07182169]]
\end{sphinxVerbatim}


\subsection{Meet the  covariance, uncorrelated events}
\label{\detokenize{chapter3:meet-the-covariance-uncorrelated-events}}
Consider the stochastic variables \(X_i\) and \(X_j\), (\(i\neq j\)). We have
\begin{equation*}
\begin{split}
\begin{align*}
Cov(X_i,\,X_j) &= \langle (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)\rangle\\
&=\langle x_i x_j - x_i\langle x_j\rangle - \langle x_i\rangle x_j + \langle x_i\rangle\langle x_j\rangle\rangle\\
&=\langle x_i x_j\rangle - \langle x_i\langle x_j\rangle\rangle - \langle \langle x_i\rangle x_j \rangle +
\langle \langle x_i\rangle\langle x_j\rangle\rangle \\
&=\langle x_i x_j\rangle - \langle x_i\rangle\langle x_j\rangle - \langle x_i\rangle\langle x_j\rangle +
\langle x_i\rangle\langle x_j\rangle \\
&=\langle x_i x_j\rangle - \langle x_i\rangle\langle x_j\rangle
\end{align*}
\end{split}
\end{equation*}
If \(X_i\) and \(X_j\) are independent (assuming \(i \neq j\)), we have that
\begin{equation*}
\begin{split}
\langle x_i x_j\rangle = \langle x_i\rangle\langle x_j\rangle,
\end{split}
\end{equation*}
leading to
\begin{equation*}
\begin{split}
Cov(X_i, X_j) = 0 \hspace{0.1cm} (i\neq j).
\end{split}
\end{equation*}

\subsection{Numerical experiments and the covariance}
\label{\detokenize{chapter3:numerical-experiments-and-the-covariance}}
Now that we have constructed an idealized mathematical framework, let
us try to apply it to empirical observations. Examples of relevant
physical phenomena may be spontaneous decays of nuclei, or a purely
mathematical set of numbers produced by some deterministic
mechanism. It is the latter we will deal with, using so\sphinxhyphen{}called pseudo\sphinxhyphen{}random
number generators.  In general our observations will contain only a limited set of
observables. We remind the reader that
a \sphinxstyleemphasis{stochastic process} is a process that produces sequentially a
chain of values
\begin{equation*}
\begin{split}
\{x_1, x_2,\dots\,x_k,\dots\}.
\end{split}
\end{equation*}

\subsection{Numerical experiments and the covariance}
\label{\detokenize{chapter3:id4}}
We will call these
values our \sphinxstyleemphasis{measurements} and the entire set as our measured
\sphinxstyleemphasis{sample}.  The action of measuring all the elements of a sample
we will call a stochastic \sphinxstyleemphasis{experiment} (since, operationally,
they are often associated with results of empirical observation of
some physical or mathematical phenomena; precisely an experiment). We
assume that these values are distributed according to some
PDF \(p_X^{\phantom X}(x)\), where \(X\) is just the formal symbol for the
stochastic variable whose PDF is \(p_X^{\phantom X}(x)\). Instead of
trying to determine the full distribution \(p\) we are often only
interested in finding the few lowest moments, like the mean
\(\mu_X^{\phantom X}\) and the variance \(\sigma_X^{\phantom X}\).


\subsection{Numerical experiments and the covariance, actual situations}
\label{\detokenize{chapter3:numerical-experiments-and-the-covariance-actual-situations}}
In practical situations however, a sample is always of finite size. Let that
size be \(n\). The expectation value of a sample \(\alpha\), the \sphinxstylestrong{sample mean}, is then defined as follows
\begin{equation*}
\begin{split}
\langle x_{\alpha} \rangle \equiv \frac{1}{n}\sum_{k=1}^n x_{\alpha,k}.
\end{split}
\end{equation*}
The \sphinxstyleemphasis{sample variance} is:
\begin{equation*}
\begin{split}
\mathrm{Var}(x) \equiv \frac{1}{n}\sum_{k=1}^n (x_{\alpha,k} - \langle x_{\alpha} \rangle)^2,
\end{split}
\end{equation*}
with its square root being the \sphinxstyleemphasis{standard deviation of the sample}.


\subsection{Numerical experiments and the covariance, our observables}
\label{\detokenize{chapter3:numerical-experiments-and-the-covariance-our-observables}}
You can think of the above observables as a set of quantities which define
a given experiment. This experiment is then repeated several times, say \(m\) times.
The total average is then




\begin{equation*}
\begin{split}
\begin{equation}
\langle X_m \rangle= \frac{1}{m}\sum_{\alpha=1}^mx_{\alpha}=\frac{1}{mn}\sum_{\alpha, k} x_{\alpha,k},
\label{eq:exptmean} \tag{6}
\end{equation}
\end{split}
\end{equation*}
where the last sums end at \(m\) and \(n\).
The total variance is
\begin{equation*}
\begin{split}
\sigma^2_m= \frac{1}{mn^2}\sum_{\alpha=1}^m(\langle x_{\alpha} \rangle-\langle X_m \rangle)^2,
\end{split}
\end{equation*}
which we rewrite as




\begin{equation*}
\begin{split}
\begin{equation}
\sigma^2_m=\frac{1}{m}\sum_{\alpha=1}^m\sum_{kl=1}^n (x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,l}-\langle X_m \rangle).
\label{eq:exptvariance} \tag{7}
\end{equation}
\end{split}
\end{equation*}

\subsection{Numerical experiments and the covariance, the sample variance}
\label{\detokenize{chapter3:numerical-experiments-and-the-covariance-the-sample-variance}}
We define also the sample variance \(\sigma^2\) of all \(mn\) individual experiments as




\begin{equation*}
\begin{split}
\begin{equation}
\sigma^2=\frac{1}{mn}\sum_{\alpha=1}^m\sum_{k=1}^n (x_{\alpha,k}-\langle X_m \rangle)^2.
\label{eq:sampleexptvariance} \tag{8}
\end{equation}
\end{split}
\end{equation*}
These quantities, being known experimental values or the results from our calculations,
may differ, in some cases
significantly,  from the similarly named
exact values for the mean value \(\mu_X\), the variance \(\mathrm{Var}(X)\)
and the covariance \(\mathrm{Cov}(X,Y)\).


\subsection{Numerical experiments and the covariance, central limit theorem}
\label{\detokenize{chapter3:numerical-experiments-and-the-covariance-central-limit-theorem}}
The central limit theorem states that the PDF \(\tilde{p}(z)\) of
the average of \(m\) random values corresponding to a PDF \(p(x)\)
is a normal distribution whose mean is the
mean value of the PDF \(p(x)\) and whose variance is the variance
of the PDF \(p(x)\) divided by \(m\), the number of values used to compute \(z\).

The central limit theorem leads then to the well\sphinxhyphen{}known expression for the
standard deviation, given by
\begin{equation*}
\begin{split}
\sigma_m=
\frac{\sigma}{\sqrt{m}}.
\end{split}
\end{equation*}
In many cases the above estimate for the standard deviation, in particular if correlations are strong, may be too simplistic.  We need therefore a more precise defintion of the error and the variance in our results.


\subsection{Definition of Correlation Functions and Standard Deviation}
\label{\detokenize{chapter3:definition-of-correlation-functions-and-standard-deviation}}
Our estimate of the true average \(\mu_{X}\) is the sample mean \(\langle X_m \rangle\)
\begin{equation*}
\begin{split}
\mu_{X}^{\phantom X} \approx X_m=\frac{1}{mn}\sum_{\alpha=1}^m\sum_{k=1}^n x_{\alpha,k}.
\end{split}
\end{equation*}
We can then use Eq. ({\hyperref[\detokenize{chapter3:eq:exptvariance}]{\emph{7}}})
\begin{equation*}
\begin{split}
\sigma^2_m=\frac{1}{mn^2}\sum_{\alpha=1}^m\sum_{kl=1}^n (x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,l}-\langle X_m \rangle),
\end{split}
\end{equation*}
and rewrite it as
\begin{equation*}
\begin{split}
\sigma^2_m=\frac{\sigma^2}{n}+\frac{2}{mn^2}\sum_{\alpha=1}^m\sum_{k<l}^n (x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,l}-\langle X_m \rangle),
\end{split}
\end{equation*}
where the first term is the sample variance of all \(mn\) experiments divided by \(n\)
and the last term is nothing but the covariance which arises when \(k\ne l\).


\subsection{Definition of Correlation Functions and Standard Deviation}
\label{\detokenize{chapter3:id5}}
Our estimate of the true average \(\mu_{X}\) is the sample mean \(\langle X_m \rangle\)

If the
observables are uncorrelated, then the covariance is zero and we obtain a total variance
which agrees with the central limit theorem. Correlations may often be present in our data set, resulting in a non\sphinxhyphen{}zero covariance.  The first term is normally called the uncorrelated
contribution.
Computationally the uncorrelated first term is much easier to treat
efficiently than the second.
We just accumulate separately the values \(x^2\) and \(x\) for every
measurement \(x\) we receive. The correlation term, though, has to be
calculated at the end of the experiment since we need all the
measurements to calculate the cross terms. Therefore, all measurements
have to be stored throughout the experiment.


\subsection{Definition of Correlation Functions and Standard Deviation}
\label{\detokenize{chapter3:id6}}
Let us analyze the problem by splitting up the correlation term into
partial sums of the form
\begin{equation*}
\begin{split}
f_d = \frac{1}{nm}\sum_{\alpha=1}^m\sum_{k=1}^{n-d}(x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,k+d}-\langle X_m \rangle),
\end{split}
\end{equation*}
The correlation term of the total variance can now be rewritten in terms of
\(f_d\)
\begin{equation*}
\begin{split}
\frac{2}{mn^2}\sum_{\alpha=1}^m\sum_{k<l}^n (x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,l}-\langle X_m \rangle)=
\frac{2}{n}\sum_{d=1}^{n-1} f_d
\end{split}
\end{equation*}

\subsection{Definition of Correlation Functions and Standard Deviation}
\label{\detokenize{chapter3:id7}}
The value of \(f_d\) reflects the correlation between measurements
separated by the distance \(d\) in the samples.  Notice that for
\(d=0\), \(f\) is just the sample variance, \(\sigma^2\). If we divide \(f_d\)
by \(\sigma^2\), we arrive at the so called \sphinxstylestrong{autocorrelation function}




\begin{equation*}
\begin{split}
\begin{equation}
\kappa_d = \frac{f_d}{\sigma^2}
\label{eq:autocorrelformal} \tag{9}
\end{equation}
\end{split}
\end{equation*}
which gives us a useful measure of the correlation pair correlation
starting always at \(1\) for \(d=0\).


\subsection{Definition of Correlation Functions and Standard Deviation, sample variance}
\label{\detokenize{chapter3:definition-of-correlation-functions-and-standard-deviation-sample-variance}}
The sample variance of the \(mn\) experiments can now be
written in terms of the autocorrelation function




\begin{equation*}
\begin{split}
\begin{equation}
\sigma_m^2=\frac{\sigma^2}{n}+\frac{2}{n}\cdot\sigma^2\sum_{d=1}^{n-1}
\frac{f_d}{\sigma^2}=\left(1+2\sum_{d=1}^{n-1}\kappa_d\right)\frac{1}{n}\sigma^2=\frac{\tau}{n}\cdot\sigma^2
\label{eq:error_estimate_corr_time} \tag{10}
\end{equation}
\end{split}
\end{equation*}
and we see that \(\sigma_m\) can be expressed in terms of the
uncorrelated sample variance times a correction factor \(\tau\) which
accounts for the correlation between measurements. We call this
correction factor the \sphinxstyleemphasis{autocorrelation time}




\begin{equation*}
\begin{split}
\begin{equation}
\tau = 1+2\sum_{d=1}^{n-1}\kappa_d
\label{eq:autocorrelation_time} \tag{11}
\end{equation}
\end{split}
\end{equation*}




For a correlation free experiment, \(\tau\)
equals 1.


\subsection{Definition of Correlation Functions and Standard Deviation}
\label{\detokenize{chapter3:id8}}
From the point of view of
Eq. ({\hyperref[\detokenize{chapter3:eq:error_estimate_corr_time}]{\emph{10}}}) we can interpret a sequential
correlation as an effective reduction of the number of measurements by
a factor \(\tau\). The effective number of measurements becomes
\begin{equation*}
\begin{split}
n_\mathrm{eff} = \frac{n}{\tau}
\end{split}
\end{equation*}
To neglect the autocorrelation time \(\tau\) will always cause our
simple uncorrelated estimate of \(\sigma_m^2\approx \sigma^2/n\) to
be less than the true sample error. The estimate of the error will be
too “good”. On the other hand, the calculation of the full
autocorrelation time poses an efficiency problem if the set of
measurements is very large.  The solution to this problem is given by
more practically oriented methods like the blocking technique.






\subsection{Code to compute the Covariance matrix and the Covariance}
\label{\detokenize{chapter3:code-to-compute-the-covariance-matrix-and-the-covariance}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Importing various packages}
\PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sqrt}
\PYG{k+kn}{from} \PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{random}\PYG{p}{,} \PYG{n}{seed}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}

\PYG{c+c1}{\PYGZsh{} Sample covariance, note the factor 1/(n\PYGZhy{}1)}
\PYG{k}{def} \PYG{n+nf}{covariance}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
    \PYG{n+nb}{sum} \PYG{o}{=} \PYG{l+m+mf}{0.0}
    \PYG{n}{mean\PYGZus{}x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}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PYG{p}{,}\PYG{n}{covyy}\PYG{p}{,} \PYG{n}{covzz}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{covxy}\PYG{p}{,}\PYG{n}{covxz}\PYG{p}{,} \PYG{n}{covyz}\PYG{p}{)}
\PYG{n}{w} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{vstack}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{}print(w)}
\PYG{n}{c} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{n}{w}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{}eigen = np.zeros(n)}
\PYG{n}{Eigvals}\PYG{p}{,} \PYG{n}{Eigvecs} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{eig}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Eigvals}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.2642716177294466
4.940580327634602
1.1829871582902483
1.015877605436128 9.696167748586095 17.72713727297909
2.9616975181296192 3.4095763130714993 9.375087003275128
[[ 1.01587761  2.96169752  3.40957631]
 [ 2.96169752  9.69616775  9.375087  ]
 [ 3.40957631  9.375087   17.72713727]]
[24.75536438  0.07216528  3.61165297]
\end{sphinxVerbatim}


\section{Random Numbers}
\label{\detokenize{chapter3:random-numbers}}
Uniform deviates are just random numbers that lie within a specified range
(typically 0 to 1), with any one number in the range just as likely as any other. They
are, in other words, what you probably think random numbers are. However,
we want to distinguish uniform deviates from other sorts of random numbers, for
example numbers drawn from a normal (Gaussian) distribution of specified mean
and standard deviation. These other sorts of deviates are almost always generated by
performing appropriate operations on one or more uniform deviates, as we will see
in subsequent sections. So, a reliable source of random uniform deviates, the subject
of this section, is an essential building block for any sort of stochastic modeling
or Monte Carlo computer work.


\section{Random Numbers, better name: pseudo random numbers}
\label{\detokenize{chapter3:random-numbers-better-name-pseudo-random-numbers}}
A disclaimer is however appropriate. It should be fairly obvious that
something as deterministic as a computer cannot generate purely random numbers.

Numbers generated by any of the standard algorithms are in reality pseudo random
numbers, hopefully abiding to the following criteria:
\begin{itemize}
\item {} 
they produce a uniform distribution in the interval {[}0,1{]}.

\item {} 
correlations between random numbers are negligible

\item {} 
the period before the same sequence of random numbers is repeated   is as large as possible and finally

\item {} 
the algorithm should be fast.

\end{itemize}


\section{Random number generator RNG}
\label{\detokenize{chapter3:random-number-generator-rng}}
The most common random number generators are based on so\sphinxhyphen{}called
Linear congruential relations of the type
\begin{equation*}
\begin{split}
N_i=(aN_{i-1}+c) \mathrm{MOD} (M),
\end{split}
\end{equation*}
which yield a number in the interval {[}0,1{]} through
\begin{equation*}
\begin{split}
x_i=N_i/M
\end{split}
\end{equation*}
The number
\(M\) is called the period and it should be as large as possible
and
\(N_0\) is the starting value, or seed. The function \(\mathrm{MOD}\) means the remainder,
that is if we were to evaluate \((13)\mathrm{MOD}(9)\), the outcome is the remainder
of the division \(13/9\), namely \(4\).


\section{Random number generator RNG and periodic outputs}
\label{\detokenize{chapter3:random-number-generator-rng-and-periodic-outputs}}
The problem with such generators is that their outputs are periodic;
they
will start to repeat themselves with a period that is at most \(M\). If however
the parameters \(a\) and \(c\) are badly chosen, the period may be even shorter.

Consider the following example
\begin{equation*}
\begin{split}
N_i=(6N_{i-1}+7) \mathrm{MOD} (5),
\end{split}
\end{equation*}
with a seed \(N_0=2\). This generator produces the sequence
\(4,1,3,0,2,4,1,3,0,2,...\dots\), i.e., a sequence with period \(5\).
However, increasing \(M\) may not guarantee a larger period as the following
example shows
\begin{equation*}
\begin{split}
N_i=(27N_{i-1}+11) \mathrm{MOD} (54),
\end{split}
\end{equation*}
which still, with \(N_0=2\), results in \(11,38,11,38,11,38,\dots\), a period of
just \(2\).


\section{Random number generator RNG and its period}
\label{\detokenize{chapter3:random-number-generator-rng-and-its-period}}
Typical periods for the random generators provided in the program library
are of the order of \(\sim 10^9\) or larger. Other random number generators which have
become increasingly popular are so\sphinxhyphen{}called shift\sphinxhyphen{}register generators.
In these generators each successive number depends on many preceding
values (rather than the last values as in the linear congruential
generator).
For example, you could make a shift register generator whose \(l\)th
number is the sum of the \(l-i\)th and \(l-j\)th values with modulo \(M\),
\begin{equation*}
\begin{split}
N_l=(aN_{l-i}+cN_{l-j})\mathrm{MOD}(M).
\end{split}
\end{equation*}

\section{Random number generator RNG, other examples}
\label{\detokenize{chapter3:random-number-generator-rng-other-examples}}
Such a generator again produces a sequence of pseudorandom numbers
but this time with a period much larger than \(M\).
It is also possible to construct more elaborate algorithms by including
more than two past terms in the sum of each iteration.
One example is the generator of \sphinxhref{http://dl.acm.org/citation.cfm?id=187154}{Marsaglia and Zaman}
which consists of two congruential relations




\begin{equation*}
\begin{split}
\begin{equation}
   N_l=(N_{l-3}-N_{l-1})\mathrm{MOD}(2^{31}-69),
\label{eq:mz1} \tag{12}
\end{equation}
\end{split}
\end{equation*}
followed by




\begin{equation*}
\begin{split}
\begin{equation}
   N_l=(69069N_{l-1}+1013904243)\mathrm{MOD}(2^{32}),
\label{eq:mz2} \tag{13}
\end{equation}
\end{split}
\end{equation*}
which according to the authors has a period larger than \(2^{94}\).


\section{Random number generator RNG, other examples}
\label{\detokenize{chapter3:id9}}
Instead of  using modular addition, we could use the bitwise
exclusive\sphinxhyphen{}OR (\(\oplus\)) operation so that
\begin{equation*}
\begin{split}
N_l=(N_{l-i})\oplus (N_{l-j})
\end{split}
\end{equation*}
where the bitwise action of \(\oplus\) means that if \(N_{l-i}=N_{l-j}\) the result is
\(0\) whereas if \(N_{l-i}\ne N_{l-j}\) the result is
\(1\). As an example, consider the case where  \(N_{l-i}=6\) and \(N_{l-j}=11\). The first
one has a bit representation (using 4 bits only) which reads \(0110\) whereas the
second number is \(1011\). Employing the \(\oplus\) operator yields
\(1101\), or \(2^3+2^2+2^0=13\).

In Fortran90, the bitwise \(\oplus\) operation is coded through the intrinsic
function \(\mathrm{IEOR}(m,n)\) where \(m\) and \(n\) are the input numbers, while in \(C\)
it is given by \(m\wedge n\).


\section{Random number generator RNG, RAN0}
\label{\detokenize{chapter3:random-number-generator-rng-ran0}}
We show here how the linear congruential algorithm can be implemented, namely
\begin{equation*}
\begin{split}
N_i=(aN_{i-1}) \mathrm{MOD} (M).
\end{split}
\end{equation*}
However, since \(a\) and \(N_{i-1}\) are integers and their multiplication
could become greater than the standard 32 bit integer, there is a trick via
Schrage’s algorithm which approximates the multiplication
of large integers through the factorization
\begin{equation*}
\begin{split}
M=aq+r,
\end{split}
\end{equation*}
where we have defined
\begin{equation*}
\begin{split}
q=[M/a],
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
r = M\hspace{0.1cm}\mathrm{MOD} \hspace{0.1cm}a.
\end{split}
\end{equation*}
where the brackets denote integer division. In the code below the numbers
\(q\) and \(r\) are chosen so that \(r < q\).


\section{Random number generator RNG, RAN0}
\label{\detokenize{chapter3:id10}}
To see how this works we note first that




\begin{equation*}
\begin{split}
\begin{equation}
(aN_{i-1}) \mathrm{MOD} (M)= (aN_{i-1}-[N_{i-1}/q]M)\mathrm{MOD} (M),
\label{eq:rntrick1} \tag{14}
\end{equation}
\end{split}
\end{equation*}
since we can add or subtract any integer multiple of \(M\) from \(aN_{i-1}\).
The last term \([N_{i-1}/q]M\mathrm{MOD}(M)\) is zero since the integer division
\([N_{i-1}/q]\) just yields a constant which is multiplied with \(M\).


\section{Random number generator RNG, RAN0}
\label{\detokenize{chapter3:id11}}
We can now rewrite Eq. ({\hyperref[\detokenize{chapter3:eq:rntrick1}]{\emph{14}}}) as




\begin{equation*}
\begin{split}
\begin{equation}
(aN_{i-1}) \mathrm{MOD} (M)= (aN_{i-1}-[N_{i-1}/q](aq+r))\mathrm{MOD} (M),
\label{eq:rntrick2} \tag{15}
\end{equation}
\end{split}
\end{equation*}
which results
in




\begin{equation*}
\begin{split}
\begin{equation}
(aN_{i-1}) \mathrm{MOD} (M)= \left(a(N_{i-1}-[N_{i-1}/q]q)-[N_{i-1}/q]r)\right)\mathrm{MOD} (M),
\label{eq:rntrick3} \tag{16}
\end{equation}
\end{split}
\end{equation*}
yielding




\begin{equation*}
\begin{split}
\begin{equation}
(aN_{i-1}) \mathrm{MOD} (M)= \left(a(N_{i-1}\mathrm{MOD} (q)) -[N_{i-1}/q]r)\right)\mathrm{MOD} (M).
\label{eq:rntrick4} \tag{17}
\end{equation}
\end{split}
\end{equation*}

\section{Random number generator RNG, RAN0}
\label{\detokenize{chapter3:id12}}
The term \([N_{i-1}/q]r\) is always smaller or equal \(N_{i-1}(r/q)\) and with \(r < q\) we obtain always a
number smaller than \(N_{i-1}\), which is smaller than \(M\).
And since the number \(N_{i-1}\mathrm{MOD} (q)\) is between zero and \(q-1\) then
\(a(N_{i-1}\mathrm{MOD} (q))< aq\). Combined with our definition of \(q=[M/a]\) ensures that
this term is also smaller than \(M\) meaning that both terms fit into a
32\sphinxhyphen{}bit signed integer. None of these two terms can be negative, but their difference could.
The algorithm below adds \(M\) if their difference is negative.
Note that the program uses the bitwise \(\oplus\) operator to generate
the starting point for each generation of a random number. The period
of \(ran0\) is \(\sim 2.1\times 10^{9}\). A special feature of this
algorithm is that is should never be called with the initial seed
set to \(0\).


\section{Random number generator RNG, RAN0 code}
\label{\detokenize{chapter3:random-number-generator-rng-ran0-code}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
        /*
         ** The function
         **           ran0()
         ** is an \PYGZdq{}Minimal\PYGZdq{} random number generator of Park and Miller
         ** Set or reset the input value
         ** idum to any integer value (except the unlikely value MASK)
         ** to initialize the sequence; idum must not be altered between
         ** calls for sucessive deviates in a sequence.
         ** The function returns a uniform deviate between 0.0 and 1.0.
         */
    double ran0(long \PYGZam{}idum)
    \PYGZob{}
       const int a = 16807, m = 2147483647, q = 127773;
       const int r = 2836, MASK = 123459876;
       const double am = 1./m;
       long     k;
       double   ans;
       idum \PYGZca{}= MASK;
       k = (*idum)/q;
       idum = a*(idum \PYGZhy{} k*q) \PYGZhy{} r*k;
       // add m if negative difference
       if(idum \PYGZlt{} 0) idum += m;
       ans=am*(idum);
       idum \PYGZca{}= MASK;
       return ans;
    \PYGZcb{} // End: function ran0() 
\end{sphinxVerbatim}


\subsection{Properties of Selected Random Number Generators}
\label{\detokenize{chapter3:properties-of-selected-random-number-generators}}
As mentioned previously, the underlying PDF for the generation of
random numbers is the uniform distribution, meaning that the
probability for finding a number \(x\) in the interval {[}0,1{]} is \(p(x)=1\).

A random number generator should produce numbers which are uniformly distributed
in this interval. The table  shows the distribution of \(N=10000\) random
numbers generated by the functions in the program library.
We note in this table that the number of points in the various
intervals \(0.0-0.1\), \(0.1-0.2\) etc are fairly close to \(1000\), with some minor
deviations.

Two additional measures are the standard deviation \(\sigma\) and the mean
\(\mu=\langle x\rangle\).


\subsection{Properties of Selected Random Number Generators}
\label{\detokenize{chapter3:id13}}
For the uniform distribution, the mean value \(\mu\) is then
\begin{equation*}
\begin{split}
\mu=\langle x\rangle=\frac{1}{2}
\end{split}
\end{equation*}
while the standard deviation is
\begin{equation*}
\begin{split}
\sigma=\sqrt{\langle x^2\rangle-\mu^2}=\frac{1}{\sqrt{12}}=0.2886.
\end{split}
\end{equation*}

\subsection{Properties of Selected Random Number Generators}
\label{\detokenize{chapter3:id14}}
The various random number generators produce results which agree rather well with
these limiting values.




\subsection{Simple demonstration of RNGs using python}
\label{\detokenize{chapter3:simple-demonstration-of-rngs-using-python}}
The following simple Python code plots the distribution of the produced random numbers using the linear congruential RNG employed by Python. The trend displayed in the previous table is seen rather clearly.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+ch}{\PYGZsh{}!/usr/bin/env python}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{mlab} \PYG{k}{as} \PYG{n+nn}{mlab}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{random}

\PYG{c+c1}{\PYGZsh{} initialize the rng with a seed}
\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{p}{)} 
\PYG{n}{counts} \PYG{o}{=} \PYG{l+m+mi}{10000}
\PYG{n}{values} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{counts}\PYG{p}{)}   
\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range} \PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{counts}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{values}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{random}\PYG{o}{.}\PYG{n}{random}\PYG{p}{(}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} the histogram of the data}
\PYG{n}{n}\PYG{p}{,} \PYG{n}{bins}\PYG{p}{,} \PYG{n}{patches} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{hist}\PYG{p}{(}\PYG{n}{values}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{,} \PYG{n}{facecolor}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{green}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Number of counts}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Test of uniform distribution}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1100}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{grid}\PYG{p}{(}\PYG{k+kc}{True}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter3_178_0}.png}


\subsection{Properties of Selected Random Number Generators}
\label{\detokenize{chapter3:id15}}
Since our random numbers, which are typically generated via a linear congruential algorithm,
are never fully independent, we can then define
an important test which measures the degree of correlation, namely the  so\sphinxhyphen{}calledauto\sphinxhyphen{}correlation function defined previously, see again Eq. ({\hyperref[\detokenize{chapter3:eq:autocorrelformal}]{\emph{9}}}).
We rewrite it here as
\begin{equation*}
\begin{split}
C_k=\frac{f_d}
             {\sigma^2},
\end{split}
\end{equation*}
with \(C_0=1\). Recall that
\(\sigma^2=\langle x_i^2\rangle-\langle x_i\rangle^2\) and that
\begin{equation*}
\begin{split}
f_d = \frac{1}{nm}\sum_{\alpha=1}^m\sum_{k=1}^{n-d}(x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,k+d}-\langle X_m \rangle),
\end{split}
\end{equation*}
The non\sphinxhyphen{}vanishing of \(C_k\) for \(k\ne 0\) means that the random
numbers are not independent. The independence of the random numbers is crucial
in the evaluation of other expectation values. If they are not independent, our
assumption for approximating \(\sigma_N\) is no longer valid.


\subsection{Autocorrelation function}
\label{\detokenize{chapter3:autocorrelation-function}}
This program computes the autocorrelation function as discussed in the equation on the previous slide for random numbers generated with the normal distribution \(N(0,1)\).

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Importing various packages}
\PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sqrt}
\PYG{k+kn}{from} \PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{random}\PYG{p}{,} \PYG{n}{seed}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}

\PYG{k}{def} \PYG{n+nf}{autocovariance}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{n}\PYG{p}{,} \PYG{n}{k}\PYG{p}{,} \PYG{n}{mean\PYGZus{}x}\PYG{p}{)}\PYG{p}{:}
    \PYG{n+nb}{sum} \PYG{o}{=} \PYG{l+m+mf}{0.0}
    \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{n}\PYG{o}{\PYGZhy{}}\PYG{n}{k}\PYG{p}{)}\PYG{p}{:}
        \PYG{n+nb}{sum} \PYG{o}{+}\PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{(}\PYG{n}{i}\PYG{o}{+}\PYG{n}{k}\PYG{p}{)}\PYG{p}{]}\PYG{o}{\PYGZhy{}}\PYG{n}{mean\PYGZus{}x}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{o}{\PYGZhy{}}\PYG{n}{mean\PYGZus{}x}\PYG{p}{)}
    \PYG{k}{return}  \PYG{n+nb}{sum}\PYG{o}{/}\PYG{n}{n}

\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{1000}
\PYG{n}{x}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n}{autocor} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}
\PYG{n}{figaxis} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}
\PYG{n}{mean\PYGZus{}x}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\PYG{n}{var\PYGZus{}x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{var}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{mean\PYGZus{}x}\PYG{p}{,} \PYG{n}{var\PYGZus{}x}\PYG{p}{)}
\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range} \PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{figaxis}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{i}
    \PYG{n}{autocor}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\P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PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.04229032140441689 0.9337417995235178
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter3_184_1}.png}

As can be seen from the plot, the first point gives back the variance and a value of one.
For the remaining values we notice that there are still non\sphinxhyphen{}zero values for the auto\sphinxhyphen{}correlation function.


\subsection{Correlation function and which random number generators should I use}
\label{\detokenize{chapter3:correlation-function-and-which-random-number-generators-should-i-use}}
The program here computes the correlation function for one of the standard functions included with the c++ compiler.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
    //  This function computes the autocorrelation function for 
    //  the standard c++ random number generator
    
    \PYGZsh{}include \PYGZlt{}fstream\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}iomanip\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}iostream\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}cmath\PYGZgt{}
    using namespace std;
    // output file as global variable
    ofstream ofile;  
    
    //     Main function begins here     
    int main(int argc, char* argv[])
    \PYGZob{}
         int n;
         char *outfilename;
    
         cin \PYGZgt{}\PYGZgt{} n;
         double MCint = 0.;      double MCintsqr2=0.;
         double invers\PYGZus{}period = 1./RAND\PYGZus{}MAX; // initialise the random number generator
         srand(time(NULL));  // This produces the so\PYGZhy{}called seed in MC jargon
         // Compute the variance and the mean value of the uniform distribution
         // Compute also the specific values x for each cycle in order to be able to
         // the covariance and the correlation function  
         // Read in output file, abort if there are too few command\PYGZhy{}line arguments
         if( argc \PYGZlt{}= 2 )\PYGZob{}
           cout \PYGZlt{}\PYGZlt{} \PYGZdq{}Bad Usage: \PYGZdq{} \PYGZlt{}\PYGZlt{} argv[0] \PYGZlt{}\PYGZlt{} 
    	 \PYGZdq{} read also output file and number of cycles on same line\PYGZdq{} \PYGZlt{}\PYGZlt{} endl;
           exit(1);
         \PYGZcb{}
         else\PYGZob{}
           outfilename=argv[1];
         \PYGZcb{}
         ofile.open(outfilename); 
         // Get  the number of Monte\PYGZhy{}Carlo samples
         n = atoi(argv[2]);
         double *X;  
         X = new double[n];
         for (int i = 0;  i \PYGZlt{} n; i++)\PYGZob{}
               double x = double(rand())*invers\PYGZus{}period; 
               X[i] = x;
               MCint += x;
               MCintsqr2 += x*x;
         \PYGZcb{}
         double Mean = MCint/((double) n );
         MCintsqr2 = MCintsqr2/((double) n );
         double STDev = sqrt(MCintsqr2\PYGZhy{}Mean*Mean);
         double Variance = MCintsqr2\PYGZhy{}Mean*Mean;
    //   Write mean value and standard deviation 
         cout \PYGZlt{}\PYGZlt{} \PYGZdq{} Standard deviation= \PYGZdq{} \PYGZlt{}\PYGZlt{} STDev \PYGZlt{}\PYGZlt{} \PYGZdq{} Integral = \PYGZdq{} \PYGZlt{}\PYGZlt{} Mean \PYGZlt{}\PYGZlt{} endl;
    
         // Now we compute the autocorrelation function
         double *autocor;  autocor = new double[n];
         for (int j = 0; j \PYGZlt{} n; j++)\PYGZob{}
           double sum = 0.0;
           for (int k = 0; k \PYGZlt{} (n\PYGZhy{}j); k++)\PYGZob{}
    	 sum  += (X[k]\PYGZhy{}Mean)*(X[k+j]\PYGZhy{}Mean); 
           \PYGZcb{}
           autocor[j] = sum/Variance/((double) n );
           ofile \PYGZlt{}\PYGZlt{} setiosflags(ios::showpoint | ios::uppercase);
           ofile \PYGZlt{}\PYGZlt{} setw(15) \PYGZlt{}\PYGZlt{} setprecision(8) \PYGZlt{}\PYGZlt{} j;
           ofile \PYGZlt{}\PYGZlt{} setw(15) \PYGZlt{}\PYGZlt{} setprecision(8) \PYGZlt{}\PYGZlt{} autocor[j] \PYGZlt{}\PYGZlt{} endl;
         \PYGZcb{}
         ofile.close();  // close output file
         return 0;
    \PYGZcb{}  // end of main program 
\end{sphinxVerbatim}


\section{Which RNG should I use?}
\label{\detokenize{chapter3:which-rng-should-i-use}}\begin{itemize}
\item {} 
C++ has a class called \sphinxstylestrong{random}. The \sphinxhref{http://www.cplusplus.com/reference/random/}{random class} contains a large selection of RNGs and is highly recommended. Some of these RNGs have very large periods making it thereby very safe to use these RNGs in case one is performing large calculations. In particular, the \sphinxhref{http://www.cplusplus.com/reference/random/mersenne\_twister\_engine/}{Mersenne twister random number engine} has a period of \(2^{19937}\).

\item {} 
Add RNGs in Python

\end{itemize}


\subsection{How to use the Mersenne generator}
\label{\detokenize{chapter3:how-to-use-the-mersenne-generator}}
The following part of a c++ code (from project 4) sets up the uniform distribution for \(x\in [0,1]\).

\begin{sphinxVerbatim}[commandchars=\\\{\}]
    /*
    
    //  You need this 
    \PYGZsh{}include \PYGZlt{}random\PYGZgt{}
    
    // Initialize the seed and call the Mersienne algo
    std::random\PYGZus{}device rd;
    std::mt19937\PYGZus{}64 gen(rd());
    // Set up the uniform distribution for x \PYGZbs{}in [[0, 1]
    std::uniform\PYGZus{}real\PYGZus{}distribution\PYGZlt{}double\PYGZgt{} RandomNumberGenerator(0.0,1.0);
    
    // Now use the RNG
    int ix = (int) (RandomNumberGenerator(gen)*NSpins);
\end{sphinxVerbatim}


\subsection{Why blocking?}
\label{\detokenize{chapter3:why-blocking}}
\sphinxstylestrong{Statistical analysis.}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
* Monte Carlo simulations can be treated as *computer experiments*

* The results can be analysed with the same statistical tools as we would use analysing experimental data.

* As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.
\end{sphinxVerbatim}

A very good article which explains blocking is H. Flyvbjerg and H. G. Petersen, \sphinxstyleemphasis{Error estimates on averages of correlated data},  \sphinxhref{http://scitation.aip.org/content/aip/journal/jcp/91/1/10.1063/1.457480}{Journal of Chemical Physics 91, 461\sphinxhyphen{}466 (1989)}.


\subsection{Why blocking?}
\label{\detokenize{chapter3:id16}}
\sphinxstylestrong{Statistical analysis.}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
* As in other experiments, Monte Carlo experiments have two classes of errors:

  * Statistical errors

  * Systematical errors


* Statistical errors can be estimated using standard tools from statistics

* Systematical errors are method specific and must be treated differently from case to case. (In VMC a common source is the step length or time step in importance sampling)
\end{sphinxVerbatim}


\subsection{Code to demonstrate the calculation of the autocorrelation function}
\label{\detokenize{chapter3:code-to-demonstrate-the-calculation-of-the-autocorrelation-function}}
The following code computes the autocorrelation function, the covariance and the standard deviation
for standard RNG.
The \sphinxhref{https://github.com/CompPhysics/ComputationalPhysics2/tree/gh-pages/doc/Programs/LecturePrograms/programs/Blocking/autocorrelation.cpp}{following  file} gives the code.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
    //  This function computes the autocorrelation function for 
    //  the Mersenne random number generator with a uniform distribution
    \PYGZsh{}include \PYGZlt{}iostream\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}fstream\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}iomanip\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}cstdlib\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}random\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}armadillo\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}string\PYGZgt{}
    \PYGZsh{}include \PYGZlt{}cmath\PYGZgt{}
    using namespace  std;
    using namespace arma;
    // output file
    ofstream ofile;
    
    //     Main function begins here     
    int main(int argc, char* argv[])
    \PYGZob{}
      int MonteCarloCycles;
      string filename;
      if (argc \PYGZgt{} 1) \PYGZob{}
        filename=argv[1];
        MonteCarloCycles = atoi(argv[2]);
        string fileout = filename;
        string argument = to\PYGZus{}string(MonteCarloCycles);
        fileout.append(argument);
        ofile.open(fileout);
      \PYGZcb{}
    
      // Compute the variance and the mean value of the uniform distribution
      // Compute also the specific values x for each cycle in order to be able to
      // compute the covariance and the correlation function  
    
      vec X  = zeros\PYGZlt{}vec\PYGZgt{}(MonteCarloCycles);
      double MCint = 0.;      double MCintsqr2=0.;
      std::random\PYGZus{}device rd;
      std::mt19937\PYGZus{}64 gen(rd());
      // Set up the uniform distribution for x \PYGZbs{}in [[0, 1]
      std::uniform\PYGZus{}real\PYGZus{}distribution\PYGZlt{}double\PYGZgt{} RandomNumberGenerator(0.0,1.0);
      for (int i = 0;  i \PYGZlt{} MonteCarloCycles; i++)\PYGZob{}
        double x =   RandomNumberGenerator(gen); 
        X(i) = x;
        MCint += x;
        MCintsqr2 += x*x;
      \PYGZcb{}
      double Mean = MCint/((double) MonteCarloCycles );
      MCintsqr2 = MCintsqr2/((double) MonteCarloCycles );
      double STDev = sqrt(MCintsqr2\PYGZhy{}Mean*Mean);
      double Variance = MCintsqr2\PYGZhy{}Mean*Mean;
      //   Write mean value and variance
      cout \PYGZlt{}\PYGZlt{} \PYGZdq{} Sample variance= \PYGZdq{} \PYGZlt{}\PYGZlt{} Variance  \PYGZlt{}\PYGZlt{} \PYGZdq{} Mean value = \PYGZdq{} \PYGZlt{}\PYGZlt{} Mean \PYGZlt{}\PYGZlt{} endl;
      // Now we compute the autocorrelation function
      vec autocorrelation = zeros\PYGZlt{}vec\PYGZgt{}(MonteCarloCycles);
      for (int j = 0; j \PYGZlt{} MonteCarloCycles; j++)\PYGZob{}
        double sum = 0.0;
        for (int k = 0; k \PYGZlt{} (MonteCarloCycles\PYGZhy{}j); k++)\PYGZob{}
          sum  += (X(k)\PYGZhy{}Mean)*(X(k+j)\PYGZhy{}Mean); 
        \PYGZcb{}
        autocorrelation(j) = sum/Variance/((double) MonteCarloCycles );
        ofile \PYGZlt{}\PYGZlt{} setiosflags(ios::showpoint | ios::uppercase);
        ofile \PYGZlt{}\PYGZlt{} setw(15) \PYGZlt{}\PYGZlt{} setprecision(8) \PYGZlt{}\PYGZlt{} j;
        ofile \PYGZlt{}\PYGZlt{} setw(15) \PYGZlt{}\PYGZlt{} setprecision(8) \PYGZlt{}\PYGZlt{} autocorrelation(j) \PYGZlt{}\PYGZlt{} endl;
      \PYGZcb{}
      // Now compute the exact covariance using the autocorrelation function
      double Covariance = 0.0;
      for (int j = 0; j \PYGZlt{} MonteCarloCycles; j++)\PYGZob{}
        Covariance  += autocorrelation(j);
      \PYGZcb{}
      Covariance *=  2.0/((double) MonteCarloCycles);
      // Compute now the total variance, including the covariance, and obtain the standard deviation
      double TotalVariance = (Variance/((double) MonteCarloCycles ))+Covariance;
      cout \PYGZlt{}\PYGZlt{} \PYGZdq{}Covariance =\PYGZdq{} \PYGZlt{}\PYGZlt{} Covariance \PYGZlt{}\PYGZlt{} \PYGZdq{}Totalvariance= \PYGZdq{} \PYGZlt{}\PYGZlt{} TotalVariance \PYGZlt{}\PYGZlt{} \PYGZdq{}Sample Variance/n= \PYGZdq{} \PYGZlt{}\PYGZlt{} (Variance/((double) MonteCarloCycles )) \PYGZlt{}\PYGZlt{} endl;
      cout \PYGZlt{}\PYGZlt{} \PYGZdq{} STD from sample variance= \PYGZdq{} \PYGZlt{}\PYGZlt{} sqrt(Variance/((double) MonteCarloCycles )) \PYGZlt{}\PYGZlt{} \PYGZdq{} STD with covariance = \PYGZdq{} \PYGZlt{}\PYGZlt{} sqrt(TotalVariance) \PYGZlt{}\PYGZlt{} endl;
    
      ofile.close();  // close output file
      return 0;
    \PYGZcb{}  // end of main program 
\end{sphinxVerbatim}


\subsection{What is blocking?}
\label{\detokenize{chapter3:what-is-blocking}}
\sphinxstylestrong{Blocking.}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
* Say that we have a set of samples from a Monte Carlo experiment

* Assuming (wrongly) that our samples are uncorrelated our best estimate of the standard deviation of the mean \PYGZdl{}\PYGZbs{}langle \PYGZbs{}mathbf\PYGZob{}M\PYGZcb{}\PYGZbs{}rangle\PYGZdl{} is given by
\end{sphinxVerbatim}
\begin{equation*}
\begin{split}
\sigma=\sqrt{\frac{1}{n}\left(\langle \mathbf{M}^2\rangle-\langle \mathbf{M}\rangle^2\right)}
\end{split}
\end{equation*}\begin{itemize}
\item {} 
If the samples are correlated we can rewrite our results to show  that

\end{itemize}
\begin{equation*}
\begin{split}
\sigma=\sqrt{\frac{1+2\tau/\Delta t}{n}\left(\langle \mathbf{M}^2\rangle-\langle \mathbf{M}\rangle^2\right)}
\end{split}
\end{equation*}
where \(\tau\) is the correlation time (the time between a sample and the next uncorrelated sample) and \(\Delta t\) is time between each sample


\subsection{What is blocking?}
\label{\detokenize{chapter3:id17}}
\sphinxstylestrong{Blocking.}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
* If \PYGZdl{}\PYGZbs{}Delta t\PYGZbs{}gg\PYGZbs{}tau\PYGZdl{} our first estimate of \PYGZdl{}\PYGZbs{}sigma\PYGZdl{} still holds

* Much more common that \PYGZdl{}\PYGZbs{}Delta t\PYGZlt{}\PYGZbs{}tau\PYGZdl{}

* In the method of data blocking we divide the sequence of samples into blocks

* We then take the mean \PYGZdl{}\PYGZbs{}langle \PYGZbs{}mathbf\PYGZob{}M\PYGZcb{}\PYGZus{}i\PYGZbs{}rangle\PYGZdl{} of block \PYGZdl{}i=1\PYGZbs{}ldots n\PYGZus{}\PYGZob{}blocks\PYGZcb{}\PYGZdl{} to calculate the total mean and variance

* The size of each block must be so large that sample \PYGZdl{}j\PYGZdl{} of block \PYGZdl{}i\PYGZdl{} is not correlated with sample \PYGZdl{}j\PYGZdl{} of block \PYGZdl{}i+1\PYGZdl{}

* The correlation time \PYGZdl{}\PYGZbs{}tau\PYGZdl{} would be a good choice
\end{sphinxVerbatim}


\subsection{What is blocking?}
\label{\detokenize{chapter3:id18}}
\sphinxstylestrong{Blocking.}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
* Problem: We don\PYGZsq{}t know \PYGZdl{}\PYGZbs{}tau\PYGZdl{} or it is too expensive to compute

* Solution: Make a plot of std. dev. as a function of blocksize

* The estimate of std. dev. of correlated data is too low \PYGZdl{}\PYGZbs{}to\PYGZdl{} the error will increase with increasing block size until the blocks are uncorrelated, where we reach a plateau

* When the std. dev. stops increasing the blocks are uncorrelated
\end{sphinxVerbatim}


\subsection{Implementation}
\label{\detokenize{chapter3:implementation}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
* Do a Monte Carlo simulation, storing all samples to file

* Do the statistical analysis on this file, independently of your Monte Carlo program

* Read the file into an array

* Loop over various block sizes

* For each block size \PYGZdl{}n\PYGZus{}b\PYGZdl{}, loop over the array in steps of \PYGZdl{}n\PYGZus{}b\PYGZdl{} taking the mean of elements \PYGZdl{}i n\PYGZus{}b,\PYGZbs{}ldots,(i+1) n\PYGZus{}b\PYGZdl{}

* Take the mean and variance of the resulting array

* Write the results for each block size to file for later
  analysis
\end{sphinxVerbatim}


\subsection{Actual implementation with code, main function}
\label{\detokenize{chapter3:actual-implementation-with-code-main-function}}
When the file gets large, it can be useful to write your data in binary mode instead of ascii characters.
The \sphinxhref{https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py}{following python file}   reads data from file with the output from every Monte Carlo cycle.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Blocking}
    \PYG{n+nd}{@timeFunction}
    \PYG{k}{def} \PYG{n+nf}{blocking}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{blockSizeMax} \PYG{o}{=} \PYG{l+m+mi}{500}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{blockSizeMin} \PYG{o}{=} \PYG{l+m+mi}{1}

        \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{blockSizes} \PYG{o}{=} \PYG{p}{[}\PYG{p}{]}
        \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{meanVec} \PYG{o}{=} \PYG{p}{[}\PYG{p}{]}
        \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{varVec} \PYG{o}{=} \PYG{p}{[}\PYG{p}{]}

        \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{blockSizeMin}\PYG{p}{,} \PYG{n}{blockSizeMax}\PYG{p}{)}\PYG{p}{:}
            \PYG{k}{if}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{data}\PYG{p}{)} \PYG{o}{\PYGZpc{}} \PYG{n}{i} \PYG{o}{!=} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{:}
                \PYG{k}{pass}\PYG{c+c1}{\PYGZsh{}continue}
            \PYG{n}{blockSize} \PYG{o}{=} \PYG{n}{i}
            \PYG{n}{meanTempVec} \PYG{o}{=} \PYG{p}{[}\PYG{p}{]}
            \PYG{n}{varTempVec} \PYG{o}{=} \PYG{p}{[}\PYG{p}{]}
            \PYG{n}{startPoint} \PYG{o}{=} \PYG{l+m+mi}{0}
            \PYG{n}{endPoint} \PYG{o}{=} \PYG{n}{blockSize}

            \PYG{k}{while} \PYG{n}{endPoint} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{data}\PYG{p}{)}\PYG{p}{:}
                \PYG{n}{meanTempVec}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{average}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{data}\PYG{p}{[}\PYG{n}{startPoint}\PYG{p}{:}\PYG{n}{endPoint}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
                \PYG{n}{startPoint} \PYG{o}{=} \PYG{n}{endPoint}
                \PYG{n}{endPoint} \PYG{o}{+}\PYG{o}{=} \PYG{n}{blockSize}
            \PYG{n}{mean}\PYG{p}{,} \PYG{n}{var} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{average}\PYG{p}{(}\PYG{n}{meanTempVec}\PYG{p}{)}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{var}\PYG{p}{(}\PYG{n}{meanTempVec}\PYG{p}{)}\PYG{o}{/}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{meanTempVec}\PYG{p}{)}
            \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{meanVec}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{mean}\PYG{p}{)}
            \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{varVec}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{var}\PYG{p}{)}
            \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{blockSizes}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{blockSize}\PYG{p}{)}

        \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{blockingAvg} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{average}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{meanVec}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{200}\PYG{p}{:}\PYG{p}{]}\PYG{p}{)}
        \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{blockingVar} \PYG{o}{=} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{average}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{varVec}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{200}\PYG{p}{:}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)}
        \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{blockingStd} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{blockingVar}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{g+gt}{  File}\PYG{n+nn}{ \PYGZdq{}\PYGZlt{}ipython\PYGZhy{}input\PYGZhy{}6\PYGZhy{}2ff97f4bf03b\PYGZgt{}\PYGZdq{}}\PYG{g+gt}{, line }\PYG{l+m+mi}{2}
    \PYG{n+nd}{@timeFunction}
    \PYG{o}{\PYGZca{}}
\PYG{n+ne}{IndentationError}: unexpected indent
\end{sphinxVerbatim}


\subsection{The Bootstrap method}
\label{\detokenize{chapter3:the-bootstrap-method}}
The Bootstrap  resampling method is also very popular. It is very simple:
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
Start with your sample of measurements and compute the sample variance and the mean values

\item {} 
Then start again but pick in a random way the numbers in the sample and recalculate the mean and the sample variance.

\item {} 
Repeat this \(K\) times.

\end{enumerate}

It can be shown, see the article by \sphinxhref{https://projecteuclid.org/download/pdf\_1/euclid.aos/1176344552}{Efron}
that it produces the correct standard deviation.

This method is very useful for small ensembles of data points.




\subsection{Bootstrapping}
\label{\detokenize{chapter3:bootstrapping}}
Given a set of \(N\) data, assume that we are interested in some
observable \(\theta\) which may be estimated from that set. This observable can also be for example the result of a fit based on all \(N\) raw data.
Let us call the value of the observable obtained from the original
data set \(\hat{\theta}\). One recreates from the sample repeatedly
other samples by choosing randomly \(N\) data out of the original set.
This costs essentially nothing, since we just recycle the original data set for the building of new sets.




\subsection{Bootstrapping, recipe}
\label{\detokenize{chapter3:bootstrapping-recipe}}
Let us assume we have done this \(K\) times and thus have \(K\) sets of \(N\)
data values each.
Of course some values will enter more than once in the new sets. For each of these sets one computes the observable \(\theta\) resulting in values \(\theta_k\) with \(k = 1,...,K\). Then one determines
\begin{equation*}
\begin{split}
\tilde{\theta} = \frac{1}{K} \sum_{k=1}^K \theta_k,
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
sigma^2_{\tilde{\theta}} = \frac{1}{K} \sum_{k=1}^K \left(\theta_k-\tilde{\theta}\right)^2.
\end{split}
\end{equation*}
These are estimators for \(\angle\theta\rangle\) and its variance. They are not unbiased and therefore
\(\tilde{\theta}\neq\hat{\theta}\)  for finite K.

The difference is called bias and gives an idea on how far away the result may be from
the true \(\angle\theta\rangle\). As final result for the observable one quotes \(\angle\theta\rangle = \tilde{\theta} \pm \sigma_{\tilde{\theta}}\) .




\subsection{Bootstrapping, code}
\label{\detokenize{chapter3:bootstrapping-code}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
    \PYGZsh{} Bootstrap
        @timeFunction
        def bootstrap(self, nBoots = 1000):
            bootVec = np.zeros(nBoots)
            for k in range(0,nBoots):
                bootVec[k] = np.average(np.random.choice(self.data, len(self.data)))
            self.bootAvg = np.average(bootVec)
            self.bootVar = np.var(bootVec)
            self.bootStd = np.std(bootVec)
\end{sphinxVerbatim}




\subsection{Jackknife, code}
\label{\detokenize{chapter3:jackknife-code}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
    \PYGZsh{} Jackknife
        @timeFunction
        def jackknife(self):
            jackknVec = np.zeros(len(self.data))
            for k in range(0,len(self.data)):
                jackknVec[k] = np.average(np.delete(self.data, k))
            self.jackknAvg = self.avg \PYGZhy{} (len(self.data) \PYGZhy{} 1) * (np.average(jackknVec) \PYGZhy{} self.avg)
            self.jackknVar = float(len(self.data) \PYGZhy{} 1) * np.var(jackknVec)
            self.jackknStd = np.sqrt(self.jackknVar)
\end{sphinxVerbatim}




\section{Data Analysis and Machine Learning: Linear Regression and more Advanced Regression Analysis}
\label{\detokenize{chapter4:data-analysis-and-machine-learning-linear-regression-and-more-advanced-regression-analysis}}\label{\detokenize{chapter4::doc}}




\sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

Date: \sphinxstylestrong{Sep 11, 2020}

Copyright 1999\sphinxhyphen{}2020, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license


\subsection{Why Linear Regression (aka Ordinary Least Squares and family)}
\label{\detokenize{chapter4:why-linear-regression-aka-ordinary-least-squares-and-family}}
Fitting a continuous function with linear parameterization in terms of the parameters  \(\boldsymbol{\beta}\).
\begin{itemize}
\item {} 
Method of choice for fitting a continuous function!

\item {} 
Gives an excellent introduction to central Machine Learning features with \sphinxstylestrong{understandable pedagogical} links to other methods like \sphinxstylestrong{Neural Networks}, \sphinxstylestrong{Support Vector Machines} etc

\item {} 
Analytical expression for the fitting parameters \(\boldsymbol{\beta}\)

\item {} 
Analytical expressions for statistical propertiers like mean values, variances, confidence intervals and more

\item {} 
Analytical relation with probabilistic interpretations

\item {} 
Easy to introduce basic concepts like bias\sphinxhyphen{}variance tradeoff, cross\sphinxhyphen{}validation, resampling and regularization techniques and many other ML topics

\item {} 
Easy to code! And links well with classification problems and logistic regression and neural networks

\item {} 
Allows for \sphinxstylestrong{easy} hands\sphinxhyphen{}on understanding of gradient descent methods

\item {} 
and many more features

\end{itemize}

For more discussions of Ridge and Lasso regression, \sphinxhref{https://arxiv.org/abs/1509.09169}{Wessel van Wieringen’s} article is highly recommended.
Similarly, \sphinxhref{https://arxiv.org/abs/1803.08823}{Mehta et al’s article} is also recommended.


\subsection{Regression analysis, overarching aims}
\label{\detokenize{chapter4:regression-analysis-overarching-aims}}
Regression modeling deals with the description of  the sampling distribution of a given random variable \(y\) and how it varies as function of another variable or a set of such variables \(\boldsymbol{x} =[x_0, x_1,\dots, x_{n-1}]^T\).
The first variable is called the \sphinxstylestrong{dependent}, the \sphinxstylestrong{outcome} or the \sphinxstylestrong{response} variable while the set of variables \(\boldsymbol{x}\) is called the independent variable, or the predictor variable or the explanatory variable.

A regression model aims at finding a likelihood function \(p(\boldsymbol{y}\vert \boldsymbol{x})\), that is the conditional distribution for \(\boldsymbol{y}\) with a given \(\boldsymbol{x}\). The estimation of  \(p(\boldsymbol{y}\vert \boldsymbol{x})\) is made using a data set with
\begin{itemize}
\item {} 
\(n\) cases \(i = 0, 1, 2, \dots, n-1\)

\item {} 
Response (target, dependent or outcome) variable \(y_i\) with \(i = 0, 1, 2, \dots, n-1\)

\item {} 
\(p\) so\sphinxhyphen{}called explanatory (independent or predictor) variables \(\boldsymbol{x}_i=[x_{i0}, x_{i1}, \dots, x_{ip-1}]\) with \(i = 0, 1, 2, \dots, n-1\) and explanatory variables running from \(0\) to \(p-1\). See below for more explicit examples.

\end{itemize}

The goal of the regression analysis is to extract/exploit relationship between \(\boldsymbol{y}\) and \(\boldsymbol{x}\) in or to infer causal dependencies, approximations to the likelihood functions, functional relationships and to make predictions, making fits and many other things.


\subsection{Regression analysis, overarching aims II}
\label{\detokenize{chapter4:regression-analysis-overarching-aims-ii}}
Consider an experiment in which \(p\) characteristics of \(n\) samples are
measured. The data from this experiment, for various explanatory variables \(p\) are normally represented by a matrix\(\mathbf{X}\).

The matrix \(\mathbf{X}\) is called the \sphinxstyleemphasis{design
matrix}. Additional information of the samples is available in the
form of \(\boldsymbol{y}\) (also as above). The variable \(\boldsymbol{y}\) is
generally referred to as the \sphinxstyleemphasis{response variable}. The aim of
regression analysis is to explain \(\boldsymbol{y}\) in terms of
\(\boldsymbol{X}\) through a functional relationship like \(y_i =
f(\mathbf{X}_{i,\ast})\). When no prior knowledge on the form of
\(f(\cdot)\) is available, it is common to assume a linear relationship
between \(\boldsymbol{X}\) and \(\boldsymbol{y}\). This assumption gives rise to
the \sphinxstyleemphasis{linear regression model} where \(\boldsymbol{\beta} = [\beta_0, \ldots,
\beta_{p-1}]^{T}\) are the \sphinxstyleemphasis{regression parameters}.

Linear regression gives us a set of analytical equations for the parameters \(\beta_j\).


\subsection{Examples}
\label{\detokenize{chapter4:examples}}
In order to understand the relation among the predictors \(p\), the set of data \(n\) and the target (outcome, output etc) \(\boldsymbol{y}\),
consider the model we discussed for describing nuclear binding energies.

There we assumed that we could parametrize the data using a polynomial approximation based on the liquid drop model.
Assuming
\begin{equation*}
\begin{split}
BE(A) = a_0+a_1A+a_2A^{2/3}+a_3A^{-1/3}+a_4A^{-1},
\end{split}
\end{equation*}
we have five predictors, that is the intercept, the \(A\) dependent term, the \(A^{2/3}\) term and the \(A^{-1/3}\) and \(A^{-1}\) terms.
This gives \(p=0,1,2,3,4\). Furthermore we have \(n\) entries for each predictor. It means that our design matrix is a
\(p\times n\) matrix \(\boldsymbol{X}\).

Here the predictors are based on a model we have made. A popular data set which is widely encountered in ML applications is the
so\sphinxhyphen{}called \sphinxhref{https://www.sciencedirect.com/science/article/pii/S0957417407006719?via\%3Dihub}{credit card default data from Taiwan}. The data set contains data on \(n=30000\) credit card holders with predictors like gender, marital status, age, profession, education, etc. In total there are \(24\) such predictors or attributes leading to a design matrix of dimensionality \(24 \times 30000\). This is however a classification problem and we will come back to it when we discuss Logistic Regression.


\subsection{General linear models}
\label{\detokenize{chapter4:general-linear-models}}
Before we proceed let us study a case from linear algebra where we aim at fitting a set of data \(\boldsymbol{y}=[y_0,y_1,\dots,y_{n-1}]\). We could think of these data as a result of an experiment or a complicated numerical experiment. These data are functions of a series of variables \(\boldsymbol{x}=[x_0,x_1,\dots,x_{n-1}]\), that is \(y_i = y(x_i)\) with \(i=0,1,2,\dots,n-1\). The variables \(x_i\) could represent physical quantities like time, temperature, position etc. We assume that \(y(x)\) is a smooth function.

Since obtaining these data points may not be trivial, we want to use these data to fit a function which can allow us to make predictions for values of \(y\) which are not in the present set. The perhaps simplest approach is to assume we can parametrize our function in terms of a polynomial of degree \(n-1\) with \(n\) points, that is
\begin{equation*}
\begin{split}
y=y(x) \rightarrow y(x_i)=\tilde{y}_i+\epsilon_i=\sum_{j=0}^{n-1} \beta_j x_i^j+\epsilon_i,
\end{split}
\end{equation*}
where \(\epsilon_i\) is the error in our approximation.


\subsection{Rewriting the fitting procedure as a linear algebra problem}
\label{\detokenize{chapter4:rewriting-the-fitting-procedure-as-a-linear-algebra-problem}}
For every set of values \(y_i,x_i\) we have thus the corresponding set of equations
\begin{equation*}
\begin{split}
\begin{align*}
y_0&=\beta_0+\beta_1x_0^1+\beta_2x_0^2+\dots+\beta_{n-1}x_0^{n-1}+\epsilon_0\\
y_1&=\beta_0+\beta_1x_1^1+\beta_2x_1^2+\dots+\beta_{n-1}x_1^{n-1}+\epsilon_1\\
y_2&=\beta_0+\beta_1x_2^1+\beta_2x_2^2+\dots+\beta_{n-1}x_2^{n-1}+\epsilon_2\\
\dots & \dots \\
y_{n-1}&=\beta_0+\beta_1x_{n-1}^1+\beta_2x_{n-1}^2+\dots+\beta_{n-1}x_{n-1}^{n-1}+\epsilon_{n-1}.\\
\end{align*}
\end{split}
\end{equation*}

\subsection{Rewriting the fitting procedure as a linear algebra problem, more details}
\label{\detokenize{chapter4:rewriting-the-fitting-procedure-as-a-linear-algebra-problem-more-details}}
Defining the vectors
\begin{equation*}
\begin{split}
\boldsymbol{y} = [y_0,y_1, y_2,\dots, y_{n-1}]^T,
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
\boldsymbol{\beta} = [\beta_0,\beta_1, \beta_2,\dots, \beta_{n-1}]^T,
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
\boldsymbol{\epsilon} = [\epsilon_0,\epsilon_1, \epsilon_2,\dots, \epsilon_{n-1}]^T,
\end{split}
\end{equation*}
and the design matrix
\begin{equation*}
\begin{split}
\boldsymbol{X}=
\begin{bmatrix} 
1& x_{0}^1 &x_{0}^2& \dots & \dots &x_{0}^{n-1}\\
1& x_{1}^1 &x_{1}^2& \dots & \dots &x_{1}^{n-1}\\
1& x_{2}^1 &x_{2}^2& \dots & \dots &x_{2}^{n-1}\\                      
\dots& \dots &\dots& \dots & \dots &\dots\\
1& x_{n-1}^1 &x_{n-1}^2& \dots & \dots &x_{n-1}^{n-1}\\
\end{bmatrix}
\end{split}
\end{equation*}
we can rewrite our equations as
\begin{equation*}
\begin{split}
\boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta}+\boldsymbol{\epsilon}.
\end{split}
\end{equation*}
The above design matrix is called a \sphinxhref{https://en.wikipedia.org/wiki/Vandermonde\_matrix}{Vandermonde matrix}.


\subsection{Generalizing the fitting procedure as a linear algebra problem}
\label{\detokenize{chapter4:generalizing-the-fitting-procedure-as-a-linear-algebra-problem}}
We are obviously not limited to the above polynomial expansions.  We
could replace the various powers of \(x\) with elements of Fourier
series or instead of \(x_i^j\) we could have \(\cos{(j x_i)}\) or \(\sin{(j
x_i)}\), or time series or other orthogonal functions.  For every set
of values \(y_i,x_i\) we can then generalize the equations to
\begin{equation*}
\begin{split}
\begin{align*}
y_0&=\beta_0x_{00}+\beta_1x_{01}+\beta_2x_{02}+\dots+\beta_{n-1}x_{0n-1}+\epsilon_0\\
y_1&=\beta_0x_{10}+\beta_1x_{11}+\beta_2x_{12}+\dots+\beta_{n-1}x_{1n-1}+\epsilon_1\\
y_2&=\beta_0x_{20}+\beta_1x_{21}+\beta_2x_{22}+\dots+\beta_{n-1}x_{2n-1}+\epsilon_2\\
\dots & \dots \\
y_{i}&=\beta_0x_{i0}+\beta_1x_{i1}+\beta_2x_{i2}+\dots+\beta_{n-1}x_{in-1}+\epsilon_i\\
\dots & \dots \\
y_{n-1}&=\beta_0x_{n-1,0}+\beta_1x_{n-1,2}+\beta_2x_{n-1,2}+\dots+\beta_{n-1}x_{n-1,n-1}+\epsilon_{n-1}.\\
\end{align*}
\end{split}
\end{equation*}
\sphinxstylestrong{Note that we have \(p=n\) here. The matrix is symmetric. This is generally not the case!}


\subsection{Generalizing the fitting procedure as a linear algebra problem}
\label{\detokenize{chapter4:id1}}
We redefine in turn the matrix \(\boldsymbol{X}\) as
\begin{equation*}
\begin{split}
\boldsymbol{X}=
\begin{bmatrix} 
x_{00}& x_{01} &x_{02}& \dots & \dots &x_{0,n-1}\\
x_{10}& x_{11} &x_{12}& \dots & \dots &x_{1,n-1}\\
x_{20}& x_{21} &x_{22}& \dots & \dots &x_{2,n-1}\\                      
\dots& \dots &\dots& \dots & \dots &\dots\\
x_{n-1,0}& x_{n-1,1} &x_{n-1,2}& \dots & \dots &x_{n-1,n-1}\\
\end{bmatrix}
\end{split}
\end{equation*}
and without loss of generality we rewrite again  our equations as
\begin{equation*}
\begin{split}
\boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta}+\boldsymbol{\epsilon}.
\end{split}
\end{equation*}
The left\sphinxhyphen{}hand side of this equation is kwown. Our error vector \(\boldsymbol{\epsilon}\) and the parameter vector \(\boldsymbol{\beta}\) are our unknow quantities. How can we obtain the optimal set of \(\beta_i\) values?


\subsection{Optimizing our parameters}
\label{\detokenize{chapter4:optimizing-our-parameters}}
We have defined the matrix \(\boldsymbol{X}\) via the equations
\begin{equation*}
\begin{split}
\begin{align*}
y_0&=\beta_0x_{00}+\beta_1x_{01}+\beta_2x_{02}+\dots+\beta_{n-1}x_{0n-1}+\epsilon_0\\
y_1&=\beta_0x_{10}+\beta_1x_{11}+\beta_2x_{12}+\dots+\beta_{n-1}x_{1n-1}+\epsilon_1\\
y_2&=\beta_0x_{20}+\beta_1x_{21}+\beta_2x_{22}+\dots+\beta_{n-1}x_{2n-1}+\epsilon_1\\
\dots & \dots \\
y_{i}&=\beta_0x_{i0}+\beta_1x_{i1}+\beta_2x_{i2}+\dots+\beta_{n-1}x_{in-1}+\epsilon_1\\
\dots & \dots \\
y_{n-1}&=\beta_0x_{n-1,0}+\beta_1x_{n-1,2}+\beta_2x_{n-1,2}+\dots+\beta_{n-1}x_{n-1,n-1}+\epsilon_{n-1}.\\
\end{align*}
\end{split}
\end{equation*}
As we noted above, we stayed with a system with the design matrix
\(\boldsymbol{X}\in {\mathbb{R}}^{n\times n}\), that is we have \(p=n\). For reasons to come later (algorithmic arguments) we will hereafter define
our matrix as \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\), with the predictors refering to the column numbers and the entries \(n\) being the row elements.


\subsection{Our model for the nuclear binding energies}
\label{\detokenize{chapter4:our-model-for-the-nuclear-binding-energies}}
In our \sphinxhref{https://compphysics.github.io/MachineLearning/doc/pub/How2ReadData/html/How2ReadData.html}{introductory notes} we looked at the so\sphinxhyphen{}called \sphinxhref{https://en.wikipedia.org/wiki/Semi-empirical\_mass\_formula}{liquid drop model}. Let us remind ourselves about what we did by looking at the code.

We restate the parts of the code we are most interested in.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{o}{\PYGZpc{}}\PYG{k}{matplotlib} inline

\PYG{c+c1}{\PYGZsh{} Common imports}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{IPython}\PYG{n+nn}{.}\PYG{n+nn}{display} \PYG{k+kn}{import} \PYG{n}{display}
\PYG{k+kn}{import} \PYG{n+nn}{os}

\PYG{c+c1}{\PYGZsh{} Where to save the figures and data files}
\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{MassEval2016.dat}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}


\PYG{c+c1}{\PYGZsh{} Read the experimental data with Pandas}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}fwf}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{usecols}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{)}\PYG{p}{,}
              \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{N}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Element}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,}
              \PYG{n}{widths}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{13}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{9}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{9}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{12}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,}
              \PYG{n}{header}\PYG{o}{=}\PYG{l+m+mi}{39}\PYG{p}{,}
              \PYG{n}{index\PYGZus{}col}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Extrapolated values are indicated by \PYGZsq{}\PYGZsh{}\PYGZsq{} in place of the decimal place, so}
\PYG{c+c1}{\PYGZsh{} the Ebinding column won\PYGZsq{}t be numeric. Coerce to float and drop these entries.}
\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Convert from keV to MeV.}
\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{/}\PYG{o}{=} \PYG{l+m+mi}{1000}

\PYG{c+c1}{\PYGZsh{} Group the DataFrame by nucleon number, A.}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{groupby}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Find the rows of the grouped DataFrame with the maximum binding energy.}
\PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{apply}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{t}\PYG{p}{:} \PYG{n}{t}\PYG{p}{[}\PYG{n}{t}\PYG{o}{.}\PYG{n}{Ebinding}\PYG{o}{==}\PYG{n}{t}\PYG{o}{.}\PYG{n}{Ebinding}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{A} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Z} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[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{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Then nice printout using pandas}
\PYG{n}{DesignMatrix} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}
\PYG{n}{DesignMatrix}\PYG{o}{.}\PYG{n}{index} \PYG{o}{=} \PYG{n}{A}
\PYG{n}{DesignMatrix}\PYG{o}{.}\PYG{n}{columns} \PYG{o}{=} \PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{1}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A\PYGZca{}(2/3)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A\PYGZca{}(\PYGZhy{}1/3)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{1/A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{display}\PYG{p}{(}\PYG{n}{DesignMatrix}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
       1      A    A\PYGZca{}(2/3)  A\PYGZca{}(\PYGZhy{}1/3)       1/A
A                                             
1    1.0    1.0   1.000000  1.000000  1.000000
2    1.0    2.0   1.587401  0.793701  0.500000
3    1.0    3.0   2.080084  0.693361  0.333333
4    1.0    4.0   2.519842  0.629961  0.250000
5    1.0    5.0   2.924018  0.584804  0.200000
..   ...    ...        ...       ...       ...
264  1.0  264.0  41.153106  0.155883  0.003788
265  1.0  265.0  41.256962  0.155687  0.003774
266  1.0  266.0  41.360688  0.155491  0.003759
269  1.0  269.0  41.671089  0.154911  0.003717
270  1.0  270.0  41.774300  0.154720  0.003704

[267 rows x 5 columns]
\end{sphinxVerbatim}

With \(\boldsymbol{\beta}\in {\mathbb{R}}^{p\times 1}\), it means that we will hereafter write our equations for the approximation as
\begin{equation*}
\begin{split}
\boldsymbol{\tilde{y}}= \boldsymbol{X}\boldsymbol{\beta},
\end{split}
\end{equation*}
throughout these lectures.


\subsection{Optimizing our parameters, more details}
\label{\detokenize{chapter4:optimizing-our-parameters-more-details}}
With the above we use the design matrix to define the approximation \(\boldsymbol{\tilde{y}}\) via the unknown quantity \(\boldsymbol{\beta}\) as
\begin{equation*}
\begin{split}
\boldsymbol{\tilde{y}}= \boldsymbol{X}\boldsymbol{\beta},
\end{split}
\end{equation*}
and in order to find the optimal parameters \(\beta_i\) instead of solving the above linear algebra problem, we define a function which gives a measure of the spread between the values \(y_i\) (which represent hopefully the exact values) and the parameterized values \(\tilde{y}_i\), namely
\begin{equation*}
\begin{split}
C(\boldsymbol{\beta})=\frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2=\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{\tilde{y}}\right)^T\left(\boldsymbol{y}-\boldsymbol{\tilde{y}}\right)\right\},
\end{split}
\end{equation*}
or using the matrix \(\boldsymbol{X}\) and in a more compact matrix\sphinxhyphen{}vector notation as
\begin{equation*}
\begin{split}
C(\boldsymbol{\beta})=\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}.
\end{split}
\end{equation*}
This function is one possible way to define the so\sphinxhyphen{}called cost function.

It is also common to define
the function \(C\) as
\begin{equation*}
\begin{split}
C(\boldsymbol{\beta})=\frac{1}{2n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2,
\end{split}
\end{equation*}
since when taking the first derivative with respect to the unknown parameters \(\beta\), the factor of \(2\) cancels out.


\subsection{Interpretations and optimizing our parameters}
\label{\detokenize{chapter4:interpretations-and-optimizing-our-parameters}}
The function
\begin{equation*}
\begin{split}
C(\boldsymbol{\beta})=\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\},
\end{split}
\end{equation*}
can be linked to the variance of the quantity \(y_i\) if we interpret the latter as the mean value.
When linking (see the discussion below) with the maximum likelihood approach below, we will indeed interpret \(y_i\) as a mean value
\begin{equation*}
\begin{split}
y_{i}=\langle y_i \rangle = \beta_0x_{i,0}+\beta_1x_{i,1}+\beta_2x_{i,2}+\dots+\beta_{n-1}x_{i,n-1}+\epsilon_i,
\end{split}
\end{equation*}
where \(\langle y_i \rangle\) is the mean value. Keep in mind also that
till now we have treated \(y_i\) as the exact value. Normally, the
response (dependent or outcome) variable \(y_i\) the outcome of a
numerical experiment or another type of experiment and is thus only an
approximation to the true value. It is then always accompanied by an
error estimate, often limited to a statistical error estimate given by
the standard deviation discussed earlier. In the discussion here we
will treat \(y_i\) as our exact value for the response variable.

In order to find the parameters \(\beta_i\) we will then minimize the spread of \(C(\boldsymbol{\beta})\), that is we are going to solve the problem
\begin{equation*}
\begin{split}
{\displaystyle \min_{\boldsymbol{\beta}\in
{\mathbb{R}}^{p}}}\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}.
\end{split}
\end{equation*}
In practical terms it means we will require
\begin{equation*}
\begin{split}
\frac{\partial C(\boldsymbol{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}\right)^2\right]=0,
\end{split}
\end{equation*}
which results in
\begin{equation*}
\begin{split}
\frac{\partial C(\boldsymbol{\beta})}{\partial \beta_j} = -\frac{2}{n}\left[ \sum_{i=0}^{n-1}x_{ij}\left(y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}\right)\right]=0,
\end{split}
\end{equation*}
or in a matrix\sphinxhyphen{}vector form as
\begin{equation*}
\begin{split}
\frac{\partial C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} = 0 = \boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right).
\end{split}
\end{equation*}

\subsection{Interpretations and optimizing our parameters}
\label{\detokenize{chapter4:id2}}
We can rewrite
\begin{equation*}
\begin{split}
\frac{\partial C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} = 0 = \boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right),
\end{split}
\end{equation*}
as
\begin{equation*}
\begin{split}
\boldsymbol{X}^T\boldsymbol{y} = \boldsymbol{X}^T\boldsymbol{X}\boldsymbol{\beta},
\end{split}
\end{equation*}
and if the matrix \(\boldsymbol{X}^T\boldsymbol{X}\) is invertible we have the solution
\begin{equation*}
\begin{split}
\boldsymbol{\beta} =\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}.
\end{split}
\end{equation*}
We note also that since our design matrix is defined as \(\boldsymbol{X}\in
{\mathbb{R}}^{n\times p}\), the product \(\boldsymbol{X}^T\boldsymbol{X} \in
{\mathbb{R}}^{p\times p}\).  In the above case we have that \(p \ll n\),
in our case \(p=5\) meaning that we end up with inverting a small
\(5\times 5\) matrix. This is a rather common situation, in many cases we end up with low\sphinxhyphen{}dimensional
matrices to invert. The methods discussed here and for many other
supervised learning algorithms like classification with logistic
regression or support vector machines, exhibit dimensionalities which
allow for the usage of direct linear algebra methods such as \sphinxstylestrong{LU} decomposition or \sphinxstylestrong{Singular Value Decomposition} (SVD) for finding the inverse of the matrix
\(\boldsymbol{X}^T\boldsymbol{X}\).

\sphinxstylestrong{Small question}: Do you think the example we have at hand here (the nuclear binding energies) can lead to problems in inverting the matrix  \(\boldsymbol{X}^T\boldsymbol{X}\)? What kind of problems can we expect?


\subsection{Some useful matrix and vector expressions}
\label{\detokenize{chapter4:some-useful-matrix-and-vector-expressions}}
The following matrix and vector relation will be useful here and for the rest of the course. Vectors are always written as boldfaced lower case letters and
matrices as upper case boldfaced letters.

2
6

\textless{}
\textless{}
\textless{}
!
!
M
A
T
H
\_
B
L
O
C
K

2
7

\textless{}
\textless{}
\textless{}
!
!
M
A
T
H
\_
B
L
O
C
K

2
8

\textless{}
\textless{}
\textless{}
!
!
M
A
T
H
\_
B
L
O
C
K
\begin{equation*}
\begin{split}
\frac{\partial \log{\vert\boldsymbol{A}\vert}}{\partial \boldsymbol{A}} = (\boldsymbol{A}^{-1})^T.
\end{split}
\end{equation*}

\subsection{Interpretations and optimizing our parameters}
\label{\detokenize{chapter4:id3}}
The residuals \(\boldsymbol{\epsilon}\) are in turn given by
\begin{equation*}
\begin{split}
\boldsymbol{\epsilon} = \boldsymbol{y}-\boldsymbol{\tilde{y}} = \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta},
\end{split}
\end{equation*}
and with
\begin{equation*}
\begin{split}
\boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)= 0,
\end{split}
\end{equation*}
we have
\begin{equation*}
\begin{split}
\boldsymbol{X}^T\boldsymbol{\epsilon}=\boldsymbol{X}^T\left( \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)= 0,
\end{split}
\end{equation*}
meaning that the solution for \(\boldsymbol{\beta}\) is the one which minimizes the residuals.  Later we will link this with the maximum likelihood approach.

Let us now return to our nuclear binding energies and simply code the above equations.


\subsection{Own code for Ordinary Least Squares}
\label{\detokenize{chapter4:own-code-for-ordinary-least-squares}}
It is rather straightforward to implement the matrix inversion and obtain the parameters \(\boldsymbol{\beta}\). After having defined the matrix \(\boldsymbol{X}\) we simply need to
write

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} matrix inversion to find beta}
\PYG{n}{beta} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{inv}\PYG{p}{(}\PYG{n}{X}\PYG{o}{.}\PYG{n}{T}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{X}\PYG{o}{.}\PYG{n}{T}\PYG{p}{)}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} and then make the prediction}
\PYG{n}{ytilde} \PYG{o}{=} \PYG{n}{X} \PYG{o}{@} \PYG{n}{beta}
\end{sphinxVerbatim}

Alternatively, you can use the least squares functionality in \sphinxstylestrong{Numpy} as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{fit} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{lstsq}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{,} \PYG{n}{rcond} \PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{)}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}
\PYG{n}{ytildenp} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{fit}\PYG{p}{,}\PYG{n}{X}\PYG{o}{.}\PYG{n}{T}\PYG{p}{)}
\end{sphinxVerbatim}

And finally we plot our fit with and compare with data

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eapprox}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}  \PYG{o}{=} \PYG{n}{ytilde}
\PYG{c+c1}{\PYGZsh{} Generate a plot comparing the experimental with the fitted values values.}
\PYG{n}{fig}\PYG{p}{,} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplots}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}A = N + Z\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}E\PYGZus{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}bind\PYGZcb{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{,/}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}MeV\PYGZcb{}}\PYG{l+s+s1}{\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n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}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Masses2016OLS}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_70_0}.png}


\subsection{Adding error analysis and training set up}
\label{\detokenize{chapter4:adding-error-analysis-and-training-set-up}}
We can easily test our fit by computing the \(R2\) score that we discussed in connection with the functionality of \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} in the introductory slides.
Since we are not using \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} here we can define our own \(R2\) function as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k}{def} \PYG{n+nf}{R2}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{p}{,} \PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}data} \PYG{o}{\PYGZhy{}} \PYG{n}{y\PYGZus{}model}\PYG{p}{)} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{/} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}data} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{p}{)}\PYG{p}{)} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)}
\end{sphinxVerbatim}

and we would be using it as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{R2}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,}\PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.9547578478889096
\end{sphinxVerbatim}

We can easily add our \sphinxstylestrong{MSE} score as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k}{def} \PYG{n+nf}{MSE}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{p}{,}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{n} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{size}\PYG{p}{(}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}
    \PYG{k}{return} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{o}{\PYGZhy{}}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n}

\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{MSE}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,}\PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.03787596148305239
\end{sphinxVerbatim}

and finally the relative error as

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k}{def} \PYG{n+nf}{RelativeError}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{p}{,}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n+nb}{abs}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{o}{\PYGZhy{}}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{o}{/}\PYG{n}{y\PYGZus{}data}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{RelativeError}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
A        
1    0            inf
2    1       1.123190
3    2       0.327631
4    6       0.344172
5    9       0.044402
               ...   
264  3304    0.009911
265  3310    0.009154
266  3317    0.007824
269  3338    0.011347
270  3344    0.009790
Name: Ebinding, Length: 267, dtype: float64
\end{sphinxVerbatim}


\subsection{The \protect\(\chi^2\protect\) function}
\label{\detokenize{chapter4:the-chi-2-function}}
Normally, the response (dependent or outcome) variable \(y_i\) is the
outcome of a numerical experiment or another type of experiment and is
thus only an approximation to the true value. It is then always
accompanied by an error estimate, often limited to a statistical error
estimate given by the standard deviation discussed earlier. In the
discussion here we will treat \(y_i\) as our exact value for the
response variable.

Introducing the standard deviation \(\sigma_i\) for each measurement
\(y_i\), we define now the \(\chi^2\) function (omitting the \(1/n\) term)
as
\begin{equation*}
\begin{split}
\chi^2(\boldsymbol{\beta})=\frac{1}{n}\sum_{i=0}^{n-1}\frac{\left(y_i-\tilde{y}_i\right)^2}{\sigma_i^2}=\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{\tilde{y}}\right)^T\frac{1}{\boldsymbol{\Sigma^2}}\left(\boldsymbol{y}-\boldsymbol{\tilde{y}}\right)\right\},
\end{split}
\end{equation*}
where the matrix \(\boldsymbol{\Sigma}\) is a diagonal matrix with \(\sigma_i\) as matrix elements.


\subsection{The \protect\(\chi^2\protect\) function}
\label{\detokenize{chapter4:id4}}
In order to find the parameters \(\beta_i\) we will then minimize the spread of \(\chi^2(\boldsymbol{\beta})\) by requiring
\begin{equation*}
\begin{split}
\frac{\partial \chi^2(\boldsymbol{\beta})}{\partial \beta_j} = \frac{\partial }{\partial \beta_j}\left[ \frac{1}{n}\sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)^2\right]=0,
\end{split}
\end{equation*}
which results in
\begin{equation*}
\begin{split}
\frac{\partial \chi^2(\boldsymbol{\beta})}{\partial \beta_j} = -\frac{2}{n}\left[ \sum_{i=0}^{n-1}\frac{x_{ij}}{\sigma_i}\left(\frac{y_i-\beta_0x_{i,0}-\beta_1x_{i,1}-\beta_2x_{i,2}-\dots-\beta_{n-1}x_{i,n-1}}{\sigma_i}\right)\right]=0,
\end{split}
\end{equation*}
or in a matrix\sphinxhyphen{}vector form as
\begin{equation*}
\begin{split}
\frac{\partial \chi^2(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} = 0 = \boldsymbol{A}^T\left( \boldsymbol{b}-\boldsymbol{A}\boldsymbol{\beta}\right).
\end{split}
\end{equation*}
where we have defined the matrix \(\boldsymbol{A} =\boldsymbol{X}/\boldsymbol{\Sigma}\) with matrix elements \(a_{ij} = x_{ij}/\sigma_i\) and the vector \(\boldsymbol{b}\) with elements \(b_i = y_i/\sigma_i\).


\subsection{The \protect\(\chi^2\protect\) function}
\label{\detokenize{chapter4:id5}}
We can rewrite
\begin{equation*}
\begin{split}
\frac{\partial \chi^2(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}} = 0 = \boldsymbol{A}^T\left( \boldsymbol{b}-\boldsymbol{A}\boldsymbol{\beta}\right),
\end{split}
\end{equation*}
as
\begin{equation*}
\begin{split}
\boldsymbol{A}^T\boldsymbol{b} = \boldsymbol{A}^T\boldsymbol{A}\boldsymbol{\beta},
\end{split}
\end{equation*}
and if the matrix \(\boldsymbol{A}^T\boldsymbol{A}\) is invertible we have the solution
\begin{equation*}
\begin{split}
\boldsymbol{\beta} =\left(\boldsymbol{A}^T\boldsymbol{A}\right)^{-1}\boldsymbol{A}^T\boldsymbol{b}.
\end{split}
\end{equation*}

\subsection{The \protect\(\chi^2\protect\) function}
\label{\detokenize{chapter4:id6}}
If we then introduce the matrix
\begin{equation*}
\begin{split}
\boldsymbol{H} =  \left(\boldsymbol{A}^T\boldsymbol{A}\right)^{-1},
\end{split}
\end{equation*}
we have then the following expression for the parameters \(\beta_j\) (the matrix elements of \(\boldsymbol{H}\) are \(h_{ij}\))
\begin{equation*}
\begin{split}
\beta_j = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}\frac{y_i}{\sigma_i}\frac{x_{ik}}{\sigma_i} = \sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}b_ia_{ik}
\end{split}
\end{equation*}
We state without proof the expression for the uncertainty  in the parameters \(\beta_j\) as (we leave this as an exercise)
\begin{equation*}
\begin{split}
\sigma^2(\beta_j) = \sum_{i=0}^{n-1}\sigma_i^2\left( \frac{\partial \beta_j}{\partial y_i}\right)^2,
\end{split}
\end{equation*}
resulting in
\begin{equation*}
\begin{split}
\sigma^2(\beta_j) = \left(\sum_{k=0}^{p-1}h_{jk}\sum_{i=0}^{n-1}a_{ik}\right)\left(\sum_{l=0}^{p-1}h_{jl}\sum_{m=0}^{n-1}a_{ml}\right) = h_{jj}!
\end{split}
\end{equation*}

\subsection{The \protect\(\chi^2\protect\) function}
\label{\detokenize{chapter4:id7}}
The first step here is to approximate the function \(y\) with a first\sphinxhyphen{}order polynomial, that is we write
\begin{equation*}
\begin{split}
y=y(x) \rightarrow y(x_i) \approx \beta_0+\beta_1 x_i.
\end{split}
\end{equation*}
By computing the derivatives of \(\chi^2\) with respect to \(\beta_0\) and \(\beta_1\) show that these are given by
\begin{equation*}
\begin{split}
\frac{\partial \chi^2(\boldsymbol{\beta})}{\partial \beta_0} = -2\left[ \frac{1}{n}\sum_{i=0}^{n-1}\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0,
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
\frac{\partial \chi^2(\boldsymbol{\beta})}{\partial \beta_1} = -\frac{2}{n}\left[ \sum_{i=0}^{n-1}x_i\left(\frac{y_i-\beta_0-\beta_1x_{i}}{\sigma_i^2}\right)\right]=0.
\end{split}
\end{equation*}

\subsection{The \protect\(\chi^2\protect\) function}
\label{\detokenize{chapter4:id8}}
For a linear fit (a first\sphinxhyphen{}order polynomial) we don’t need to invert a matrix!!Defining
\begin{equation*}
\begin{split}
\gamma =  \sum_{i=0}^{n-1}\frac{1}{\sigma_i^2},
\end{split}
\end{equation*}\begin{equation*}
\begin{split}
\gamma_x =  \sum_{i=0}^{n-1}\frac{x_{i}}{\sigma_i^2},
\end{split}
\end{equation*}\begin{equation*}
\begin{split}
\gamma_y = \sum_{i=0}^{n-1}\left(\frac{y_i}{\sigma_i^2}\right),
\end{split}
\end{equation*}\begin{equation*}
\begin{split}
\gamma_{xx} =  \sum_{i=0}^{n-1}\frac{x_ix_{i}}{\sigma_i^2},
\end{split}
\end{equation*}\begin{equation*}
\begin{split}
\gamma_{xy} = \sum_{i=0}^{n-1}\frac{y_ix_{i}}{\sigma_i^2},
\end{split}
\end{equation*}
we obtain
\begin{equation*}
\begin{split}
\beta_0 = \frac{\gamma_{xx}\gamma_y-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2},
\end{split}
\end{equation*}\begin{equation*}
\begin{split}
\beta_1 = \frac{\gamma_{xy}\gamma-\gamma_x\gamma_y}{\gamma\gamma_{xx}-\gamma_x^2}.
\end{split}
\end{equation*}
This approach (different linear and non\sphinxhyphen{}linear regression) suffers
often from both being underdetermined and overdetermined in the
unknown coefficients \(\beta_i\).  A better approach is to use the
Singular Value Decomposition (SVD) method discussed below. Or using
Lasso and Ridge regression. See below.


\subsection{Fitting an Equation of State for Dense Nuclear Matter}
\label{\detokenize{chapter4:fitting-an-equation-of-state-for-dense-nuclear-matter}}
Before we continue, let us introduce yet another example. We are going to fit the
nuclear equation of state using results from many\sphinxhyphen{}body calculations.
The equation of state we have made available here, as function of
density, has been derived using modern nucleon\sphinxhyphen{}nucleon potentials with
\sphinxhref{https://www.sciencedirect.com/science/article/pii/S0370157399001106}{the addition of three\sphinxhyphen{}body
forces}. This
time the file is presented as a standard \sphinxstylestrong{csv} file.

The beginning of the Python code here is similar to what you have seen
before, with the same initializations and declarations. We use also
\sphinxstylestrong{pandas} again, rather extensively in order to organize our data.

The difference now is that we use \sphinxstylestrong{Scikit\sphinxhyphen{}Learn’s} regression tools
instead of our own matrix inversion implementation. Furthermore, we
sneak in \sphinxstylestrong{Ridge} regression (to be discussed below) which includes a
hyperparameter \(\lambda\), also to be explained below.


\subsection{The code}
\label{\detokenize{chapter4:the-code}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Common imports}
\PYG{k+kn}{import} \PYG{n+nn}{os}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k}{as} \PYG{n+nn}{skl}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{,} \PYG{n}{r2\PYGZus{}score}\PYG{p}{,} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}

\PYG{c+c1}{\PYGZsh{} Where to save the figures and data files}
\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{EoS.csv}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Read the EoS data as  csv file and organize the data into two arrays with density and energies}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}csv}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{EoS}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{Energies} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Density} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{c+c1}{\PYGZsh{}  The design matrix now as function of various polytrops}
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{Density}\PYG{p}{)}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mf}{4.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mf}{2.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mi}{1}

\PYG{c+c1}{\PYGZsh{} We use now Scikit\PYGZhy{}Learn\PYGZsq{}s linear regressor and ridge regressor}
\PYG{c+c1}{\PYGZsh{} OLS part}
\PYG{n}{clf} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)}
\PYG{n}{ytilde} \PYG{o}{=} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}
\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eols}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}  \PYG{o}{=} \PYG{n}{ytilde}
\PYG{c+c1}{\PYGZsh{} The mean squared error                               }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Explained variance score: 1 is perfect prediction                                 }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance score: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Mean absolute error                                                           }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean absolute error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{clf}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{p}{,} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{intercept\PYGZus{}}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} The Ridge regression with a hyperparameter lambda = 0.1}
\PYG{n}{\PYGZus{}lambda} \PYG{o}{=} \PYG{l+m+mf}{0.1}
\PYG{n}{clf\PYGZus{}ridge} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)}
\PYG{n}{yridge} \PYG{o}{=} \PYG{n}{clf\PYGZus{}ridge}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}
\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eridge}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}  \PYG{o}{=} \PYG{n}{yridge}
\PYG{c+c1}{\PYGZsh{} The mean squared error                               }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{yridge}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Explained variance score: 1 is perfect prediction                                 }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance score: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{yridge}\PYG{p}{)}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Mean absolute error                                                           }
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean absolute error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}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p}{]}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.7}\PYG{p}{,} \PYG{n}{lw}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{c}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{g}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}
            \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ridge \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{lambda = 0.1\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{save\PYGZus{}fig}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{EoSfitting}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
Mean squared error: 12.36
Variance score: 1.00
Mean absolute error: 2.83
[   0.          618.32047562 \PYGZhy{}861.13519106 1404.91549644] \PYGZhy{}11.057088709963637
Mean squared error: 197.93
Variance score: 1.00
Mean absolute error: 11.69
[  0.          28.18220995 282.79902342 842.30879705] 12.946893955206974
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_117_1}.png}

The above simple polynomial in density \(\rho\) gives an excellent fit
to the data.

We note also that there is a small deviation between the
standard OLS and the Ridge regression at higher densities. We discuss this in more detail
below.


\subsection{Splitting our Data in Training and Test data}
\label{\detokenize{chapter4:splitting-our-data-in-training-and-test-data}}
It is normal in essentially all Machine Learning studies to split the
data in a training set and a test set (sometimes also an additional
validation set).  \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} has an own function for this. There
is no explicit recipe for how much data should be included as training
data and say test data.  An accepted rule of thumb is to use
approximately \(2/3\) to \(4/5\) of the data as training data. We will
postpone a discussion of this splitting to the end of these notes and
our discussion of the so\sphinxhyphen{}called \sphinxstylestrong{bias\sphinxhyphen{}variance} tradeoff. Here we
limit ourselves to repeat the above equation of state fitting example
but now splitting the data into a training set and a test set.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{os}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{c+c1}{\PYGZsh{} Where to save the figures and data files}
\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{R2}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{p}{,} \PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}data} \PYG{o}{\PYGZhy{}} \PYG{n}{y\PYGZus{}model}\PYG{p}{)} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{/} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}data} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{p}{)}\PYG{p}{)} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)}
\PYG{k}{def} \PYG{n+nf}{MSE}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{p}{,}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{n} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{size}\PYG{p}{(}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}
    \PYG{k}{return} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}data}\PYG{o}{\PYGZhy{}}\PYG{n}{y\PYGZus{}model}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n}

\PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{EoS.csv}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Read the EoS data as  csv file and organized into two arrays with density and energies}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}csv}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{EoS}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{Energies} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Density} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{c+c1}{\PYGZsh{}  The design matrix now as function of various polytrops}
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{Density}\PYG{p}{)}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mi}{1}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mf}{2.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mf}{4.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mf}{5.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} We split the data in test and training data}
\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} matrix inversion to find beta}
\PYG{n}{beta} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{inv}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{o}{.}\PYG{n}{T}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{o}{.}\PYG{n}{T}\PYG{p}{)}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{y\PYGZus{}train}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} and then make the prediction}
\PYG{n}{ytilde} \PYG{o}{=} \PYG{n}{X\PYGZus{}train} \PYG{o}{@} \PYG{n}{beta}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Training R2}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{R2}\PYG{p}{(}\PYG{n}{y\PYGZus{}train}\PYG{p}{,}\PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Training MSE}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{MSE}\PYG{p}{(}\PYG{n}{y\PYGZus{}train}\PYG{p}{,}\PYG{n}{ytilde}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{ypredict} \PYG{o}{=} \PYG{n}{X\PYGZus{}test} \PYG{o}{@} \PYG{n}{beta}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Test R2}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{R2}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{p}{,}\PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Test MSE}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{MSE}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{p}{,}\PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
Training R2
0.9999941778221182
Training MSE
2.1663059697316176
Test R2
0.9999188193958993
Test MSE
57.32209754560856
\end{sphinxVerbatim}




\subsection{The Boston housing data example}
\label{\detokenize{chapter4:the-boston-housing-data-example}}
The Boston housingdata set was originally a part of UCI Machine Learning Repository
and has been removed now. The data set is now included in \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}’s
library.  There are 506 samples and 13 feature (predictor) variables
in this data set. The objective is to predict the value of prices of
the house using the features (predictors) listed here.

The features/predictors are
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
CRIM: Per capita crime rate by town

\item {} 
ZN: Proportion of residential land zoned for lots over 25000 square feet

\item {} 
INDUS: Proportion of non\sphinxhyphen{}retail business acres per town

\item {} 
CHAS: Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)

\item {} 
NOX: Nitric oxide concentration (parts per 10 million)

\item {} 
RM: Average number of rooms per dwelling

\item {} 
AGE: Proportion of owner\sphinxhyphen{}occupied units built prior to 1940

\item {} 
DIS: Weighted distances to five Boston employment centers

\item {} 
RAD: Index of accessibility to radial highways

\item {} 
TAX: Full\sphinxhyphen{}value property tax rate per USD10000

\item {} 
B: \(1000(Bk - 0.63)^2\), where \(Bk\) is the proportion of {[}people of African American descent{]} by town

\item {} 
LSTAT: Percentage of lower status of the population

\item {} 
MEDV: Median value of owner\sphinxhyphen{}occupied homes in USD 1000s

\end{enumerate}


\subsection{Housing data, the code}
\label{\detokenize{chapter4:housing-data-the-code}}
We start by importing the libraries

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} 

\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}  
\PYG{k+kn}{import} \PYG{n+nn}{seaborn} \PYG{k}{as} \PYG{n+nn}{sns}
\end{sphinxVerbatim}

and load the Boston Housing DataSet from \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{datasets} \PYG{k+kn}{import} \PYG{n}{load\PYGZus{}boston}

\PYG{n}{boston\PYGZus{}dataset} \PYG{o}{=} \PYG{n}{load\PYGZus{}boston}\PYG{p}{(}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} boston\PYGZus{}dataset is a dictionary}
\PYG{c+c1}{\PYGZsh{} let\PYGZsq{}s check what it contains}
\PYG{n}{boston\PYGZus{}dataset}\PYG{o}{.}\PYG{n}{keys}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
dict\PYGZus{}keys([\PYGZsq{}data\PYGZsq{}, \PYGZsq{}target\PYGZsq{}, \PYGZsq{}feature\PYGZus{}names\PYGZsq{}, \PYGZsq{}DESCR\PYGZsq{}, \PYGZsq{}filename\PYGZsq{}])
\end{sphinxVerbatim}

Then we invoke Pandas

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{boston} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{boston\PYGZus{}dataset}\PYG{o}{.}\PYG{n}{data}\PYG{p}{,} \PYG{n}{columns}\PYG{o}{=}\PYG{n}{boston\PYGZus{}dataset}\PYG{o}{.}\PYG{n}{feature\PYGZus{}names}\PYG{p}{)}
\PYG{n}{boston}\PYG{o}{.}\PYG{n}{head}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{boston}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{MEDV}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{boston\PYGZus{}dataset}\PYG{o}{.}\PYG{n}{target}
\end{sphinxVerbatim}

and preprocess the data

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} check for missing values in all the columns}
\PYG{n}{boston}\PYG{o}{.}\PYG{n}{isnull}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
CRIM       0
ZN         0
INDUS      0
CHAS       0
NOX        0
RM         0
AGE        0
DIS        0
RAD        0
TAX        0
PTRATIO    0
B          0
LSTAT      0
MEDV       0
dtype: int64
\end{sphinxVerbatim}

We can then visualize the data

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} set the size of the figure}
\PYG{n}{sns}\PYG{o}{.}\PYG{n}{set}\PYG{p}{(}\PYG{n}{rc}\PYG{o}{=}\PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{figure.figsize}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:}\PYG{p}{(}\PYG{l+m+mf}{11.7}\PYG{p}{,}\PYG{l+m+mf}{8.27}\PYG{p}{)}\PYG{p}{\PYGZcb{}}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} plot a histogram showing the distribution of the target values}
\PYG{n}{sns}\PYG{o}{.}\PYG{n}{distplot}\PYG{p}{(}\PYG{n}{boston}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{MEDV}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{bins}\PYG{o}{=}\PYG{l+m+mi}{30}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_129_0}.png}

It is now useful to look at the correlation matrix

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} compute the pair wise correlation for all columns  }
\PYG{n}{correlation\PYGZus{}matrix} \PYG{o}{=} \PYG{n}{boston}\PYG{o}{.}\PYG{n}{corr}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} use the heatmap function from seaborn to plot the correlation matrix}
\PYG{c+c1}{\PYGZsh{} annot = True to print the values inside the square}
\PYG{n}{sns}\PYG{o}{.}\PYG{n}{heatmap}\PYG{p}{(}\PYG{n}{data}\PYG{o}{=}\PYG{n}{correlation\PYGZus{}matrix}\PYG{p}{,} \PYG{n}{annot}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYGZlt{}matplotlib.axes.\PYGZus{}subplots.AxesSubplot at 0x7fa71a4449d0\PYGZgt{}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_131_1}.png}

From the above coorelation plot we can see that \sphinxstylestrong{MEDV} is strongly correlated to \sphinxstylestrong{LSTAT} and  \sphinxstylestrong{RM}. We see also that \sphinxstylestrong{RAD} and \sphinxstylestrong{TAX} are stronly correlated, but we don’t include this in our features together to avoid multi\sphinxhyphen{}colinearity

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)}

\PYG{n}{features} \PYG{o}{=} \PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{LSTAT}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{RM}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{target} \PYG{o}{=} \PYG{n}{boston}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{MEDV}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}

\PYG{k}{for} \PYG{n}{i}\PYG{p}{,} \PYG{n}{col} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{features}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplot}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{features}\PYG{p}{)} \PYG{p}{,} \PYG{n}{i}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}
    \PYG{n}{x} \PYG{o}{=} \PYG{n}{boston}\PYG{p}{[}\PYG{n}{col}\PYG{p}{]}
    \PYG{n}{y} \PYG{o}{=} \PYG{n}{target}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{marker}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{o}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{n}{col}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{n}{col}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{MEDV}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_133_0}.png}

Now we start training our model

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{X} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{c\PYGZus{}}\PYG{p}{[}\PYG{n}{boston}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{LSTAT}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{boston}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{RM}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{]}\PYG{p}{,} \PYG{n}{columns} \PYG{o}{=} \PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{LSTAT}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{RM}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{Y} \PYG{o}{=} \PYG{n}{boston}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{MEDV}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\end{sphinxVerbatim}

We split the data into training and test sets

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}

\PYG{c+c1}{\PYGZsh{} splits the training and test data set in 80\PYGZpc{} : 20\PYGZpc{}}
\PYG{c+c1}{\PYGZsh{} assign random\PYGZus{}state to any value.This ensures consistency.}
\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{Y\PYGZus{}train}\PYG{p}{,} \PYG{n}{Y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size} \PYG{o}{=} \PYG{l+m+mf}{0.2}\PYG{p}{,} \PYG{n}{random\PYGZus{}state}\PYG{o}{=}\PYG{l+m+mi}{5}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Y\PYGZus{}train}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Y\PYGZus{}test}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
(404, 2)
(102, 2)
(404,)
(102,)
\end{sphinxVerbatim}

Then we use the linear regression functionality from \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{,} \PYG{n}{r2\PYGZus{}score}

\PYG{n}{lin\PYGZus{}model} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{lin\PYGZus{}model}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{Y\PYGZus{}train}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} model evaluation for training set}

\PYG{n}{y\PYGZus{}train\PYGZus{}predict} \PYG{o}{=} \PYG{n}{lin\PYGZus{}model}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}
\PYG{n}{rmse} \PYG{o}{=} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{Y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train\PYGZus{}predict}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{r2} \PYG{o}{=} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{Y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train\PYGZus{}predict}\PYG{p}{)}

\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{The model performance for training set}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{RMSE is }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{rmse}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{R2 score is }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{r2}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} model evaluation for testing set}

\PYG{n}{y\PYGZus{}test\PYGZus{}predict} \PYG{o}{=} \PYG{n}{lin\PYGZus{}model}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} root mean square error of the model}
\PYG{n}{rmse} \PYG{o}{=} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{Y\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}test\PYGZus{}predict}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} r\PYGZhy{}squared score of the model}
\PYG{n}{r2} \PYG{o}{=} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{Y\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}test\PYGZus{}predict}\PYG{p}{)}

\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{The model performance for testing set}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZ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YGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}
RMSE is 5.6371293350711955
R2 score is 0.6300745149331701


The model performance for testing set
\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}
RMSE is 5.137400784702912
R2 score is 0.6628996975186952
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} plotting the y\PYGZus{}test vs y\PYGZus{}pred}
\PYG{c+c1}{\PYGZsh{} ideally should have been a straight line}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{Y\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}test\PYGZus{}predict}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_140_0}.png}


\subsection{Reducing the number of degrees of freedom, overarching view}
\label{\detokenize{chapter4:reducing-the-number-of-degrees-of-freedom-overarching-view}}
Many Machine Learning problems involve thousands or even millions of
features for each training instance. Not only does this make training
extremely slow, it can also make it much harder to find a good
solution, as we will see. This problem is often referred to as the
curse of dimensionality.  Fortunately, in real\sphinxhyphen{}world problems, it is
often possible to reduce the number of features considerably, turning
an intractable problem into a tractable one.

Later  we will discuss some of the most popular dimensionality reduction
techniques: the principal component analysis (PCA), Kernel PCA, and
Locally Linear Embedding (LLE).

Principal component analysis and its various variants deal with the
problem of fitting a low\sphinxhyphen{}dimensional \sphinxhref{https://en.wikipedia.org/wiki/Affine\_space}{affine
subspace} to a set of of
data points in a high\sphinxhyphen{}dimensional space. With its family of methods it
is one of the most used tools in data modeling, compression and
visualization.


\subsection{Preprocessing our data}
\label{\detokenize{chapter4:preprocessing-our-data}}
Before we proceed however, we will discuss how to preprocess our
data. Till now and in connection with our previous examples we have
not met so many cases where we are too sensitive to the scaling of our
data. Normally the data may need a rescaling and/or may be sensitive
to extreme values. Scaling the data renders our inputs much more
suitable for the algorithms we want to employ.

\sphinxstylestrong{Scikit\sphinxhyphen{}Learn} has several functions which allow us to rescale the
data, normally resulting in much better results in terms of various
accuracy scores.  The \sphinxstylestrong{StandardScaler} function in \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}
ensures that for each feature/predictor we study the mean value is
zero and the variance is one (every column in the design/feature
matrix).  This scaling has the drawback that it does not ensure that
we have a particular maximum or minimum in our data set. Another
function included in \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} is the \sphinxstylestrong{MinMaxScaler} which
ensures that all features are exactly between \(0\) and \(1\). The


\subsection{More preprocessing}
\label{\detokenize{chapter4:more-preprocessing}}
The \sphinxstylestrong{Normalizer} scales each data
point such that the feature vector has a euclidean length of one. In other words, it
projects a data point on the circle (or sphere in the case of higher dimensions) with a
radius of 1. This means every data point is scaled by a different number (by the
inverse of it’s length).
This normalization is often used when only the direction (or angle) of the data matters,
not the length of the feature vector.

The \sphinxstylestrong{RobustScaler} works similarly to the StandardScaler in that it
ensures statistical properties for each feature that guarantee that
they are on the same scale. However, the RobustScaler uses the median
and quartiles, instead of mean and variance. This makes the
RobustScaler ignore data points that are very different from the rest
(like measurement errors). These odd data points are also called
outliers, and might often lead to trouble for other scaling
techniques.


\subsection{Simple preprocessing examples, Franke function and regression}
\label{\detokenize{chapter4:simple-preprocessing-examples-franke-function-and-regression}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Common imports}
\PYG{k+kn}{import} \PYG{n+nn}{os}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k}{as} \PYG{n+nn}{skl}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import}  \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{MinMaxScaler}\PYG{p}{,} \PYG{n}{StandardScaler}\PYG{p}{,} \PYG{n}{Normalizer}

\PYG{c+c1}{\PYGZsh{} Where to save the figures and data files}
\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{d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}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}		\PYG{c+c1}{\PYGZsh{} Number of elements in beta}
	\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{p}{(}\PYG{n}{N}\PYG{p}{,}\PYG{n}{l}\PYG{p}{)}\PYG{p}{)}

	\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{n}{n}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{:}
		\PYG{n}{q} \PYG{o}{=} \PYG{n+nb}{int}\PYG{p}{(}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{i}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}
		\PYG{k}{for} \PYG{n}{k} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{i}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{:}
			\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{q}\PYG{o}{+}\PYG{n}{k}\PYG{p}{]} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{i}\PYG{o}{\PYGZhy{}}\PYG{n}{k}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{k}\PYG{p}{)}

	\PYG{k}{return} \PYG{n}{X}


\PYG{c+c1}{\PYGZsh{} Making meshgrid of datapoints and compute Franke\PYGZsq{}s function}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{5}
\PYG{n}{N} \PYG{o}{=} \PYG{l+m+mi}{1000}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{N}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{N}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{z} \PYG{o}{=} \PYG{n}{FrankeFunction}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}
\PYG{n}{X} \PYG{o}{=} \PYG{n}{create\PYGZus{}X}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{n}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}    
\PYG{c+c1}{\PYGZsh{} split in training and test data}
\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,}\PYG{n}{z}\PYG{p}{,}\PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)}


\PYG{n}{clf} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} The mean squared error and R2 score}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{MSE before scaling: }\PYG{l+s+si}{\PYGZob{}:.2f\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{clf}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{,} \PYG{n}{y\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{R2 score before scaling }\PYG{l+s+si}{\PYGZob{}:.2f\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{clf}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{,}\PYG{n}{y\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}

\PYG{n}{scaler} \PYG{o}{=} \PYG{n}{StandardScaler}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{scaler}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}
\PYG{n}{X\PYGZus{}train\PYGZus{}scaled} \PYG{o}{=} \PYG{n}{scaler}\PYG{o}{.}\PYG{n}{transform}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}
\PYG{n}{X\PYGZus{}test\PYGZus{}scaled} \PYG{o}{=} \PYG{n}{scaler}\PYG{o}{.}\PYG{n}{transform}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}

\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Feature min values before scaling:}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{o}{.}\PYG{n}{min}\PYG{p}{(}\PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Feature max values before scaling:}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}

\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Feature min values after scaling:}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{X\PYGZus{}train\PYGZus{}scaled}\PYG{o}{.}\PYG{n}{min}\PYG{p}{(}\PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Feature max values after scaling:}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{X\PYGZus{}train\PYGZus{}scaled}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}

\PYG{n}{clf} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train\PYGZus{}scaled}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}


\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{MSE after  scaling: }\PYG{l+s+si}{\PYGZob{}:.2f\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{clf}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X\PYGZus{}test\PYGZus{}scaled}\PYG{p}{)}\PYG{p}{,} \PYG{n}{y\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{R2 score for  scaled data: }\PYG{l+s+si}{\PYGZob{}:.2f\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{clf}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}test\PYGZus{}scaled}\PYG{p}{,}\PYG{n}{y\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
MSE before scaling: 0.00
R2 score before scaling 0.99
Feature min values before scaling:
 [1.00000000e+00 3.31070195e\PYGZhy{}03 4.26950921e\PYGZhy{}04 1.09607474e\PYGZhy{}05
 1.41350724e\PYGZhy{}06 1.82287089e\PYGZhy{}07 3.62877676e\PYGZhy{}08 4.67970118e\PYGZhy{}09
 6.03498219e\PYGZhy{}10 7.78276403e\PYGZhy{}11 1.20137983e\PYGZhy{}10 1.54930958e\PYGZhy{}11
 1.99800273e\PYGZhy{}12 2.57664120e\PYGZhy{}13 3.32285826e\PYGZhy{}14 3.97741054e\PYGZhy{}13
 5.12930224e\PYGZhy{}14 6.61479151e\PYGZhy{}15 8.53049103e\PYGZhy{}16 1.10009933e\PYGZhy{}16
 1.41869739e\PYGZhy{}17]
Feature max values before scaling:
 [1.         0.99858638 0.99964819 0.99717477 0.99823507 0.99929651
 0.99576514 0.99682395 0.99788388 0.99894494 0.99435752 0.99541483
 0.99647326 0.99753282 0.99859351 0.99295188 0.99400769 0.99506463
 0.99612269 0.99718188 0.99824219]
Feature min values after scaling:
 [ 0.         \PYGZhy{}1.7247216  \PYGZhy{}1.76366767 \PYGZhy{}1.11821924 \PYGZhy{}1.1207329  \PYGZhy{}1.12330932
 \PYGZhy{}0.88380004 \PYGZhy{}0.88090074 \PYGZhy{}0.87801917 \PYGZhy{}0.87516765 \PYGZhy{}0.75358163 \PYGZhy{}0.74975133
 \PYGZhy{}0.74594528 \PYGZhy{}0.74216924 \PYGZhy{}0.73842878 \PYGZhy{}0.66775774 \PYGZhy{}0.66415352 \PYGZhy{}0.66058072
 \PYGZhy{}0.65704182 \PYGZhy{}0.65353917 \PYGZhy{}0.65007499]
Feature max values after scaling:
 [0.         1.71439421 1.7725581  2.19850437 2.24574687 2.29247479
 2.59145801 2.63154972 2.67112014 2.71016871 2.93008405 2.96590898
 3.00128975 3.03623229 3.07074298 3.23004415 3.26340549 3.29641768
 3.32908665 3.36141837 3.39341886]
MSE after  scaling: 0.00
R2 score for  scaled data: 0.99
\end{sphinxVerbatim}


\subsection{The singular value decomposition}
\label{\detokenize{chapter4:the-singular-value-decomposition}}
The examples we have looked at so far are cases where we normally can
invert the matrix \(\boldsymbol{X}^T\boldsymbol{X}\). Using a polynomial expansion as we
did both for the masses and the fitting of the equation of state,
leads to row vectors of the design matrix which are essentially
orthogonal due to the polynomial character of our model. Obtaining the inverse of the design matrix is then often done via a so\sphinxhyphen{}called LU, QR or Cholesky decomposition.

This may
however not the be case in general and a standard matrix inversion
algorithm based on say LU, QR or Cholesky decomposition may lead to singularities. We will see examples of this below.

There is however a way to partially circumvent this problem and also gain some insights about the ordinary least squares approach, and later shrinkage methods like Ridge and Lasso regressions.

This is given by the \sphinxstylestrong{Singular Value Decomposition} algorithm, perhaps
the most powerful linear algebra algorithm.  Let us look at a
different example where we may have problems with the standard matrix
inversion algorithm. Thereafter we dive into the math of the SVD.


\subsection{Linear Regression Problems}
\label{\detokenize{chapter4:linear-regression-problems}}
One of the typical problems we encounter with linear regression, in particular
when the matrix \(\boldsymbol{X}\) (our so\sphinxhyphen{}called design matrix) is high\sphinxhyphen{}dimensional,
are problems with near singular or singular matrices. The column vectors of \(\boldsymbol{X}\)
may be linearly dependent, normally referred to as super\sphinxhyphen{}collinearity.This means that the matrix may be rank deficient and it is basically impossible to
to model the data using linear regression. As an example, consider the matrix
\begin{equation*}
\begin{split}
\begin{align*}
\mathbf{X} & =  \left[
\begin{array}{rrr}
1 & -1 & 2
\\
1 & 0 & 1
\\
1 & 2  & -1
\\
1 & 1  & 0
\end{array} \right]
\end{align*}
\end{split}
\end{equation*}
The columns of \(\boldsymbol{X}\) are linearly dependent. We see this easily since the
the first column is the row\sphinxhyphen{}wise sum of the other two columns. The rank (more correct,
the column rank) of a matrix is the dimension of the space spanned by the
column vectors. Hence, the rank of \(\mathbf{X}\) is equal to the number
of linearly independent columns. In this particular case the matrix has rank 2.

Super\sphinxhyphen{}collinearity of an \((n \times p)\)\sphinxhyphen{}dimensional design matrix \(\mathbf{X}\) implies
that the inverse of the matrix \(\boldsymbol{X}^T\boldsymbol{X}\) (the matrix we need to invert to solve the linear regression equations) is non\sphinxhyphen{}invertible. If we have a square matrix that does not have an inverse, we say this matrix singular. The example here demonstrates this
\begin{equation*}
\begin{split}
\begin{align*}
\boldsymbol{X} & =  \left[
\begin{array}{rr}
1 & -1
\\
1 & -1
\end{array} \right].
\end{align*}
\end{split}
\end{equation*}
We see easily that  \(\mbox{det}(\boldsymbol{X}) = x_{11} x_{22} - x_{12} x_{21} = 1 \times (-1) - 1 \times (-1) = 0\). Hence, \(\mathbf{X}\) is singular and its inverse is undefined.
This is equivalent to saying that the matrix \(\boldsymbol{X}\) has at least an eigenvalue which is zero.


\subsection{Fixing the singularity}
\label{\detokenize{chapter4:fixing-the-singularity}}
If our design matrix \(\boldsymbol{X}\) which enters the linear regression problem




\begin{equation*}
\begin{split}
\begin{equation}
\boldsymbol{\beta}  =  (\boldsymbol{X}^{T} \boldsymbol{X})^{-1} \boldsymbol{X}^{T} \boldsymbol{y},
\label{_auto1} \tag{1}
\end{equation}
\end{split}
\end{equation*}
has linearly dependent column vectors, we will not be able to compute the inverse
of \(\boldsymbol{X}^T\boldsymbol{X}\) and we cannot find the parameters (estimators) \(\beta_i\).
The estimators are only well\sphinxhyphen{}defined if \((\boldsymbol{X}^{T}\boldsymbol{X})^{-1}\) exits.
This is more likely to happen when the matrix \(\boldsymbol{X}\) is high\sphinxhyphen{}dimensional. In this case it is likely to encounter a situation where
the regression parameters \(\beta_i\) cannot be estimated.

A cheap  \sphinxstyleemphasis{ad hoc} approach is  simply to add a small diagonal component to the matrix to invert, that is we change
\begin{equation*}
\begin{split}
\boldsymbol{X}^{T} \boldsymbol{X} \rightarrow \boldsymbol{X}^{T} \boldsymbol{X}+\lambda \boldsymbol{I},
\end{split}
\end{equation*}
where \(\boldsymbol{I}\) is the identity matrix.  When we discuss \sphinxstylestrong{Ridge} regression this is actually what we end up evaluating. The parameter \(\lambda\) is called a hyperparameter. More about this later.


\subsection{Basic math of the SVD}
\label{\detokenize{chapter4:basic-math-of-the-svd}}
From standard linear algebra we know that a square matrix \(\boldsymbol{X}\) can be diagonalized if and only it is
a so\sphinxhyphen{}called \sphinxhref{https://en.wikipedia.org/wiki/Normal\_matrix}{normal matrix}, that is if \(\boldsymbol{X}\in {\mathbb{R}}^{n\times n}\)
we have \(\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X}\) or if \(\boldsymbol{X}\in {\mathbb{C}}^{n\times n}\) we have \(\boldsymbol{X}\boldsymbol{X}^{\dagger}=\boldsymbol{X}^{\dagger}\boldsymbol{X}\).
The matrix has then a set of eigenpairs
\begin{equation*}
\begin{split}
(\lambda_1,\boldsymbol{u}_1),\dots, (\lambda_n,\boldsymbol{u}_n),
\end{split}
\end{equation*}
and the eigenvalues are given by the diagonal matrix
\begin{equation*}
\begin{split}
\boldsymbol{\Sigma}=\mathrm{Diag}(\lambda_1, \dots,\lambda_n).
\end{split}
\end{equation*}
The matrix \(\boldsymbol{X}\) can be written in terms of an orthogonal/unitary transformation \(\boldsymbol{U}\)
\begin{equation*}
\begin{split}
\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
\end{split}
\end{equation*}
with \(\boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I}\) or \(\boldsymbol{U}\boldsymbol{U}^{\dagger}=\boldsymbol{I}\).

Not all square matrices are diagonalizable. A matrix like the one discussed above
\begin{equation*}
\begin{split}
\boldsymbol{X} = \begin{bmatrix} 
1&  -1 \\
1& -1\\
\end{bmatrix}
\end{split}
\end{equation*}
is not diagonalizable, it is a so\sphinxhyphen{}called \sphinxhref{https://en.wikipedia.org/wiki/Defective\_matrix}{defective matrix}. It is easy to see that the condition
\(\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X}\) is not fulfilled.


\subsection{The SVD, a Fantastic Algorithm}
\label{\detokenize{chapter4:the-svd-a-fantastic-algorithm}}
However, and this is the strength of the SVD algorithm, any general
matrix \(\boldsymbol{X}\) can be decomposed in terms of a diagonal matrix and
two orthogonal/unitary matrices.  The \sphinxhref{https://en.wikipedia.org/wiki/Singular\_value\_decomposition}{Singular Value Decompostion
(SVD) theorem}
states that a general \(m\times n\) matrix \(\boldsymbol{X}\) can be written in
terms of a diagonal matrix \(\boldsymbol{\Sigma}\) of dimensionality \(m\times n\)
and two orthognal matrices \(\boldsymbol{U}\) and \(\boldsymbol{V}\), where the first has
dimensionality \(m \times m\) and the last dimensionality \(n\times n\).
We have then
\begin{equation*}
\begin{split}
\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T
\end{split}
\end{equation*}
As an example, the above defective matrix can be decomposed as
\begin{equation*}
\begin{split}
\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix}  1&  1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix}  2&  0 \\ 0& 0\\ \end{bmatrix}    \frac{1}{\sqrt{2}}\begin{bmatrix}  1&  -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
\end{split}
\end{equation*}
with eigenvalues \(\sigma_1=2\) and \(\sigma_2=0\).
The SVD exits always!

The SVD
decomposition (singular values) gives eigenvalues
\(\sigma_i\geq\sigma_{i+1}\) for all \(i\) and for dimensions larger than \(i=p\), the
eigenvalues (singular values) are zero.

In the general case, where our design matrix \(\boldsymbol{X}\) has dimension
\(n\times p\), the matrix is thus decomposed into an \(n\times n\)
orthogonal matrix \(\boldsymbol{U}\), a \(p\times p\) orthogonal matrix \(\boldsymbol{V}\)
and a diagonal matrix \(\boldsymbol{\Sigma}\) with \(r=\mathrm{min}(n,p)\)
singular values \(\sigma_i\geq 0\) on the main diagonal and zeros filling
the rest of the matrix.  There are at most \(p\) singular values
assuming that \(n > p\). In our regression examples for the nuclear
masses and the equation of state this is indeed the case, while for
the Ising model we have \(p > n\). These are often cases that lead to
near singular or singular matrices.

The columns of \(\boldsymbol{U}\) are called the left singular vectors while the columns of \(\boldsymbol{V}\) are the right singular vectors.


\subsection{Economy\sphinxhyphen{}size SVD}
\label{\detokenize{chapter4:economy-size-svd}}
If we assume that \(n > p\), then our matrix \(\boldsymbol{U}\) has dimension \(n
\times n\). The last \(n-p\) columns of \(\boldsymbol{U}\) become however
irrelevant in our calculations since they are multiplied with the
zeros in \(\boldsymbol{\Sigma}\).

The economy\sphinxhyphen{}size decomposition removes extra rows or columns of zeros
from the diagonal matrix of singular values, \(\boldsymbol{\Sigma}\), along with the columns
in either \(\boldsymbol{U}\) or \(\boldsymbol{V}\) that multiply those zeros in the expression.
Removing these zeros and columns can improve execution time
and reduce storage requirements without compromising the accuracy of
the decomposition.

If \(n > p\), we keep only the first \(p\) columns of \(\boldsymbol{U}\) and \(\boldsymbol{\Sigma}\) has dimension \(p\times p\).
If \(p > n\), then only the first \(n\) columns of \(\boldsymbol{V}\) are computed and \(\boldsymbol{\Sigma}\) has dimension \(n\times n\).
The \(n=p\) case is obvious, we retain the full SVD.
In general the economy\sphinxhyphen{}size SVD leads to less FLOPS and still conserving the desired accuracy.


\subsection{Codes for the SVD}
\label{\detokenize{chapter4:codes-for-the-svd}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{c+c1}{\PYGZsh{} SVD inversion}
\PYG{k}{def} \PYG{n+nf}{SVDinv}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}\PYG{p}{:}
    \PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{} Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).}
\PYG{l+s+sd}{    SVD is numerically more stable than the inversion algorithms provided by}
\PYG{l+s+sd}{    numpy and scipy.linalg at the cost of being slower.}
\PYG{l+s+sd}{    \PYGZsq{}\PYGZsq{}\PYGZsq{}}
    \PYG{n}{U}\PYG{p}{,} \PYG{n}{s}\PYG{p}{,} \PYG{n}{VT} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{svd}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{}    print(\PYGZsq{}test U\PYGZsq{})}
\PYG{c+c1}{\PYGZsh{}    print( (np.transpose(U) @ U \PYGZhy{} U @np.transpose(U)))}
\PYG{c+c1}{\PYGZsh{}    print(\PYGZsq{}test VT\PYGZsq{})}
\PYG{c+c1}{\PYGZsh{}    print( (np.transpose(VT) @ VT \PYGZhy{} VT @np.transpose(VT)))}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{U}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{s}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{VT}\PYG{p}{)}

    \PYG{n}{D} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{U}\PYG{p}{)}\PYG{p}{,}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{VT}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
    \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{VT}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{D}\PYG{p}{[}\PYG{n}{i}\PYG{p}{,}\PYG{n}{i}\PYG{p}{]}\PYG{o}{=}\PYG{n}{s}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}
    \PYG{n}{UT} \PYG{o}{=} \PYG{n}{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}\PYG{o}{.}\PYG{n}{transpose}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{o}{@} \PYG{n}{X}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Brute force inversion of super\PYGZhy{}collinear matrix}
\PYG{c+c1}{\PYGZsh{}B = np.linalg.inv(A)}
\PYG{c+c1}{\PYGZsh{}print(B)}
\PYG{n}{C} \PYG{o}{=} \PYG{n}{SVDinv}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{C}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[ 1. \PYGZhy{}1.  2.]
 [ 1.  0.  1.]
 [ 1.  2. \PYGZhy{}1.]
 [ 1.  1.  0.]]
[[ 4.  2.  2.]
 [ 2.  6. \PYGZhy{}4.]
 [ 2. \PYGZhy{}4.  6.]]
[[\PYGZhy{}9.57425734e\PYGZhy{}17  8.16496581e\PYGZhy{}01 \PYGZhy{}5.77350269e\PYGZhy{}01]
 [\PYGZhy{}7.07106781e\PYGZhy{}01  4.08248290e\PYGZhy{}01  5.77350269e\PYGZhy{}01]
 [ 7.07106781e\PYGZhy{}01  4.08248290e\PYGZhy{}01  5.77350269e\PYGZhy{}01]]
[1.00000000e+01 6.00000000e+00 9.10898112e\PYGZhy{}32]
[[ 3.33066907e\PYGZhy{}17 \PYGZhy{}7.07106781e\PYGZhy{}01  7.07106781e\PYGZhy{}01]
 [ 8.16496581e\PYGZhy{}01  4.08248290e\PYGZhy{}01  4.08248290e\PYGZhy{}01]
 [ 5.77350269e\PYGZhy{}01 \PYGZhy{}5.77350269e\PYGZhy{}01 \PYGZhy{}5.77350269e\PYGZhy{}01]]
[[\PYGZhy{}3.65939208e+30  3.65939208e+30  3.65939208e+30]
 [ 3.65939208e+30 \PYGZhy{}3.65939208e+30 \PYGZhy{}3.65939208e+30]
 [ 3.65939208e+30 \PYGZhy{}3.65939208e+30 \PYGZhy{}3.65939208e+30]]
\end{sphinxVerbatim}

The matrix \(\boldsymbol{X}\) has columns that are linearly dependent. The first
column is the row\sphinxhyphen{}wise sum of the other two columns. The rank of a
matrix (the column rank) is the dimension of space spanned by the
column vectors. The rank of the matrix is the number of linearly
independent columns, in this case just \(2\). We see this from the
singular values when running the above code. Running the standard
inversion algorithm for matrix inversion with \(\boldsymbol{X}^T\boldsymbol{X}\) results
in the program terminating due to a singular matrix.


\subsection{Mathematical Properties}
\label{\detokenize{chapter4:mathematical-properties}}
There are several interesting mathematical properties which will be
relevant when we are going to discuss the differences between say
ordinary least squares (OLS) and \sphinxstylestrong{Ridge} regression.

We have from OLS that the parameters of the linear approximation are given by
\begin{equation*}
\begin{split}
\boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}.
\end{split}
\end{equation*}
The matrix to invert can be rewritten in terms of our SVD decomposition as
\begin{equation*}
\begin{split}
\boldsymbol{X}^T\boldsymbol{X} = \boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T.
\end{split}
\end{equation*}
Using the orthogonality properties of \(\boldsymbol{U}\) we have
\begin{equation*}
\begin{split}
\boldsymbol{X}^T\boldsymbol{X} = \boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T =  \boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T,
\end{split}
\end{equation*}
with \(\boldsymbol{D}\) being a diagonal matrix with values along the diagonal given by the singular values squared.

This means that
\begin{equation*}
\begin{split}
(\boldsymbol{X}^T\boldsymbol{X})\boldsymbol{V} = \boldsymbol{V}\boldsymbol{D},
\end{split}
\end{equation*}
that is the eigenvectors of \((\boldsymbol{X}^T\boldsymbol{X})\) are given by the columns of the right singular matrix of \(\boldsymbol{X}\) and the eigenvalues are the squared singular values.  It is easy to show (show this) that
\begin{equation*}
\begin{split}
(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U} = \boldsymbol{U}\boldsymbol{D},
\end{split}
\end{equation*}
that is, the eigenvectors of \((\boldsymbol{X}\boldsymbol{X})^T\) are the columns of the left singular matrix and the eigenvalues are the same.

Going back to our OLS equation we have
\begin{equation*}
\begin{split}
\boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}.
\end{split}
\end{equation*}
We will come back to this expression when we discuss Ridge regression.


\subsection{Ridge and LASSO Regression}
\label{\detokenize{chapter4:ridge-and-lasso-regression}}
Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is
our optimization problem is
\begin{equation*}
\begin{split}
{\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}.
\end{split}
\end{equation*}
or we can state it as
\begin{equation*}
\begin{split}
{\displaystyle \min_{\boldsymbol{\beta}\in
{\mathbb{R}}^{p}}}\frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2,
\end{split}
\end{equation*}
where we have used the definition of  a norm\sphinxhyphen{}2 vector, that is
\begin{equation*}
\begin{split}
\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}.
\end{split}
\end{equation*}
By minimizing the above equation with respect to the parameters
\(\boldsymbol{\beta}\) we could then obtain an analytical expression for the
parameters \(\boldsymbol{\beta}\).  We can add a regularization parameter \(\lambda\) by
defining a new cost function to be optimized, that is
\begin{equation*}
\begin{split}
{\displaystyle \min_{\boldsymbol{\beta}\in
{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_2^2
\end{split}
\end{equation*}
which leads to the Ridge regression minimization problem where we
require that \(\vert\vert \boldsymbol{\beta}\vert\vert_2^2\le t\), where \(t\) is
a finite number larger than zero. By defining
\begin{equation*}
\begin{split}
C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1,
\end{split}
\end{equation*}
we have a new optimization equation
\begin{equation*}
\begin{split}
{\displaystyle \min_{\boldsymbol{\beta}\in
{\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1
\end{split}
\end{equation*}
which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator.

Here we have defined the norm\sphinxhyphen{}1 as
\begin{equation*}
\begin{split}
\vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert.
\end{split}
\end{equation*}

\subsection{More on Ridge Regression}
\label{\detokenize{chapter4:more-on-ridge-regression}}
Using the matrix\sphinxhyphen{}vector expression for Ridge regression,
\begin{equation*}
\begin{split}
C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\boldsymbol{\beta}^T\boldsymbol{\beta},
\end{split}
\end{equation*}
by taking the derivatives with respect to \(\boldsymbol{\beta}\) we obtain then
a slightly modified matrix inversion problem which for finite values
of \(\lambda\) does not suffer from singularity problems. We obtain
\begin{equation*}
\begin{split}
\boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y},
\end{split}
\end{equation*}
with \(\boldsymbol{I}\) being a \(p\times p\) identity matrix with the constraint that
\begin{equation*}
\begin{split}
\sum_{i=0}^{p-1} \beta_i^2 \leq t,
\end{split}
\end{equation*}
with \(t\) a finite positive number.

We see that Ridge regression is nothing but the standard
OLS with a modified diagonal term added to \(\boldsymbol{X}^T\boldsymbol{X}\). The
consequences, in particular for our discussion of the bias\sphinxhyphen{}variance tradeoff
are rather interesting.

Furthermore, if we use the result above in terms of the SVD decomposition (our analysis was done for the OLS method), we had
\begin{equation*}
\begin{split}
(\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U} = \boldsymbol{U}\boldsymbol{D}.
\end{split}
\end{equation*}
We can  analyse the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix \(\boldsymbol{U}\) as
\begin{equation*}
\begin{split}
\boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}
\end{split}
\end{equation*}
For Ridge regression this becomes
\begin{equation*}
\begin{split}
\boldsymbol{X}\boldsymbol{\beta}^{\mathrm{Ridge}} = \boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T+\lambda\boldsymbol{I} \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\sum_{j=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\frac{\sigma_j^2}{\sigma_j^2+\lambda}\boldsymbol{y},
\end{split}
\end{equation*}
with the vectors \(\boldsymbol{u}_j\) being the columns of \(\boldsymbol{U}\).


\subsection{Interpreting the Ridge results}
\label{\detokenize{chapter4:interpreting-the-ridge-results}}
Since \(\lambda \geq 0\), it means that compared to OLS, we have
\begin{equation*}
\begin{split}
\frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1.
\end{split}
\end{equation*}
Ridge regression finds the coordinates of \(\boldsymbol{y}\) with respect to the
orthonormal basis \(\boldsymbol{U}\), it then shrinks the coordinates by
\(\frac{\sigma_j^2}{\sigma_j^2+\lambda}\). Recall that the SVD has
eigenvalues ordered in a descending way, that is \(\sigma_i \geq
\sigma_{i+1}\).

For small eigenvalues \(\sigma_i\) it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom.
Actually, calculating the variance of \(\boldsymbol{X}\boldsymbol{v}_j\) shows that this quantity is equal to \(\sigma_j^2/n\).
With a parameter \(\lambda\) we can thus shrink the role of specific parameters.


\subsection{More interpretations}
\label{\detokenize{chapter4:more-interpretations}}
For the sake of simplicity, let us assume that the design matrix is orthonormal, that is
\begin{equation*}
\begin{split}
\boldsymbol{X}^T\boldsymbol{X}=(\boldsymbol{X}^T\boldsymbol{X})^{-1} =\boldsymbol{I}.
\end{split}
\end{equation*}
In this case the standard OLS results in
\begin{equation*}
\begin{split}
\boldsymbol{\beta}^{\mathrm{OLS}} = \boldsymbol{X}^T\boldsymbol{y}=\sum_{i=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\boldsymbol{y},
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
\boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{I}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\left(1+\lambda\right)^{-1}\boldsymbol{\beta}^{\mathrm{OLS}},
\end{split}
\end{equation*}
that is the Ridge estimator scales the OLS estimator by the inverse of a factor \(1+\lambda\), and
the Ridge estimator converges to zero when the hyperparameter goes to
infinity.

We will come back to more interpreations after we have gone through some of the statistical analysis part.

For more discussions of Ridge and Lasso regression, \sphinxhref{https://arxiv.org/abs/1509.09169}{Wessel van Wieringen’s} article is highly recommended.
Similarly, \sphinxhref{https://arxiv.org/abs/1803.08823}{Mehta et al’s article} is also recommended.




\subsection{A better understanding of regularization}
\label{\detokenize{chapter4:a-better-understanding-of-regularization}}
The parameter \(\lambda\) that we have introduced in the Ridge (and
Lasso as well) regression is often called a regularization parameter
or shrinkage parameter. It is common to call it a hyperparameter. What does it mean mathemtically?

Here we will first look at how to analyze the difference between the
standard OLS equations and the Ridge expressions in terms of a linear
algebra analysis using the SVD algorithm. Thereafter, we will link
(see the material on the bias\sphinxhyphen{}variance tradeoff below) these
observation to the statisical analysis of the results. In particular
we consider how the variance of the parameters \(\boldsymbol{\beta}\) is
affected by changing the parameter \(\lambda\).


\subsection{Decomposing the OLS and Ridge expressions}
\label{\detokenize{chapter4:decomposing-the-ols-and-ridge-expressions}}
We have our design matrix
\(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\). With the SVD we decompose it as
\begin{equation*}
\begin{split}
\boldsymbol{X} = \boldsymbol{U\Sigma V^T},
\end{split}
\end{equation*}
with \(\boldsymbol{U}\in {\mathbb{R}}^{n\times n}\), \(\boldsymbol{\Sigma}\in {\mathbb{R}}^{n\times p}\)
and \(\boldsymbol{V}\in {\mathbb{R}}^{p\times p}\).

The matrices \(\boldsymbol{U}\) and \(\boldsymbol{V}\) are unitary/orthonormal matrices, that is in case the matrices are real we have \(\boldsymbol{U}^T\boldsymbol{U}=\boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I}\) and \(\boldsymbol{V}^T\boldsymbol{V}=\boldsymbol{V}\boldsymbol{V}^T=\boldsymbol{I}\).


\subsection{Introducing the Covariance and Correlation functions}
\label{\detokenize{chapter4:introducing-the-covariance-and-correlation-functions}}
Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about
the definition of the covariance and the correlation function. These are quantities

Suppose we have defined two vectors
\(\hat{x}\) and \(\hat{y}\) with \(n\) elements each. The covariance matrix \(\boldsymbol{C}\) is defined as
\begin{equation*}
\begin{split}
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
                              \mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\
             \end{bmatrix},
\end{split}
\end{equation*}
where for example
\begin{equation*}
\begin{split}
\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}).
\end{split}
\end{equation*}
With this definition and recalling that the variance is defined as
\begin{equation*}
\begin{split}
\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2,
\end{split}
\end{equation*}
we can rewrite the covariance matrix as
\begin{equation*}
\begin{split}
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
                              \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\
             \end{bmatrix}.
\end{split}
\end{equation*}
The covariance takes values between zero and infinity and may thus
lead to problems with loss of numerical precision for particularly
large values. It is common to scale the covariance matrix by
introducing instead the correlation matrix defined via the so\sphinxhyphen{}called
correlation function
\begin{equation*}
\begin{split}
\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}.
\end{split}
\end{equation*}
The correlation function is then given by values \(\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]
\in [-1,1]\). This avoids eventual problems with too large values. We
can then define the correlation matrix for the two vectors \(\boldsymbol{x}\)
and \(\boldsymbol{y}\) as
\begin{equation*}
\begin{split}
\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\
                              \mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\
             \end{bmatrix},
\end{split}
\end{equation*}
In the above example this is the function we constructed using \sphinxstylestrong{pandas}.


\subsection{Correlation Function and Design/Feature Matrix}
\label{\detokenize{chapter4:correlation-function-and-design-feature-matrix}}
In our derivation of the various regression algorithms like \sphinxstylestrong{Ordinary Least Squares} or \sphinxstylestrong{Ridge regression}
we defined the design/feature matrix \(\boldsymbol{X}\) as
\begin{equation*}
\begin{split}
\boldsymbol{X}=\begin{bmatrix}
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
\dots & \dots & \dots & \dots \dots & \dots \\
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
\end{bmatrix},
\end{split}
\end{equation*}
with \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\), with the predictors/features \(p\)  refering to the column numbers and the
entries \(n\) being the row elements.
We can rewrite the design/feature matrix in terms of its column vectors as
\begin{equation*}
\begin{split}
\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix},
\end{split}
\end{equation*}
with a given vector
\begin{equation*}
\begin{split}
\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}.
\end{split}
\end{equation*}
With these definitions, we can now rewrite our \(2\times 2\)
correaltion/covariance matrix in terms of a moe general design/feature
matrix \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\). This leads to a \(p\times p\)
covariance matrix for the vectors \(\boldsymbol{x}_i\) with \(i=0,1,\dots,p-1\)
\begin{equation*}
\begin{split}
\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix}
\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1]  & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1]  & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0]   & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
\dots & \dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots & \dots \\
\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0]   & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}]  & \dots & \dots  & \mathrm{var}[\boldsymbol{x}_{p-1}]\\
\end{bmatrix},
\end{split}
\end{equation*}
and the correlation matrix
\begin{equation*}
\begin{split}
\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix}
1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1]  & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1  & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0]   & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
\dots & \dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots & \dots \\
\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0]   & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}]  & \dots & \dots  & 1\\
\end{bmatrix},
\end{split}
\end{equation*}

\subsection{Covariance Matrix Examples}
\label{\detokenize{chapter4:covariance-matrix-examples}}
The Numpy function \sphinxstylestrong{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
\sphinxstylestrong{np.vstack} function which takes each vector of dimension \(1\times n\)
and produces a \(2\times n\) matrix \(\boldsymbol{W}\)
\begin{equation*}
\begin{split}
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\
                          x_1 & y_1 \\
                          x_2 & y_2\\
                          \dots & \dots \\
                          x_{n-2} & y_{n-2}\\
                          x_{n-1} & y_{n-1} & 
             \end{bmatrix},
\end{split}
\end{equation*}
which in turn is converted into into the \(2\times 2\) covariance matrix
\(\boldsymbol{C}\) via the Numpy function \sphinxstylestrong{np.cov()}. We note that we can also calculate
the mean value of each set of samples \(\boldsymbol{x}\) etc using the Numpy
function \sphinxstylestrong{np.mean(x)}. We can also extract the eigenvalues of the
covariance matrix through the \sphinxstylestrong{np.linalg.eig()} function.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Importing various packages}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{W} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{vstack}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{C} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{n}{W}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{C}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.04144248019085487
4.194659964041423
[[ 1.10772753  3.41408669]
 [ 3.41408669 11.38266616]]
\end{sphinxVerbatim}


\subsection{Correlation Matrix}
\label{\detokenize{chapter4: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).

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{c+c1}{\PYGZsh{} define two vectors                                                                                           }
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{random}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{}scaling the x and y vectors                                                                                   }
\PYG{n}{x} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{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o}{/}\PYG{n}{variance\PYGZus{}x}
\PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{=} \PYG{n}{cov\PYGZus{}yy}\PYG{o}{/}\PYG{n}{variance\PYGZus{}y}
\PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{=} \PYG{n}{cov\PYGZus{}xy}\PYG{o}{/}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{variance\PYGZus{}y}\PYG{o}{*}\PYG{n}{variance\PYGZus{}x}\PYG{p}{)}
\PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{=} \PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{C}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
0.09052813127366242
1.5055457430392476
[[1.         0.56806976]
 [0.56806976 1.        ]]
\end{sphinxVerbatim}

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 \sphinxstylestrong{numpy} can be made more compact if we use \sphinxstylestrong{pandas}.


\subsection{Correlation Matrix with Pandas}
\label{\detokenize{chapter4:correlation-matrix-with-pandas}}
We whow here how we can set up the correlation matrix using \sphinxstylestrong{pandas}, as done in this simple code

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{n}{n} \PYG{o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us{}matrix}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
[[ 0.19619662 \PYGZhy{}0.78402323]
 [ 0.31757369  1.22304781]
 [\PYGZhy{}0.69338544 \PYGZhy{}1.67201132]
 [\PYGZhy{}0.83049344 \PYGZhy{}3.62540546]
 [ 1.07466493  3.158262  ]
 [\PYGZhy{}0.63779754 \PYGZhy{}0.28923407]
 [\PYGZhy{}0.50263516 \PYGZhy{}2.66065903]
 [ 0.59499124  1.87464536]
 [\PYGZhy{}0.05490614  0.80385431]
 [ 0.53579124  1.97152362]]
          0         1
0  0.196197 \PYGZhy{}0.784023
1  0.317574  1.223048
2 \PYGZhy{}0.693385 \PYGZhy{}1.672011
3 \PYGZhy{}0.830493 \PYGZhy{}3.625405
4  1.074665  3.158262
5 \PYGZhy{}0.637798 \PYGZhy{}0.289234
6 \PYGZhy{}0.502635 \PYGZhy{}2.660659
7  0.594991  1.874645
8 \PYGZhy{}0.054906  0.803854
9  0.535791  1.971524
          0         1
0  1.000000  0.896919
1  0.896919  1.000000
\end{sphinxVerbatim}

We expand this model to the Franke function discussed above.


\subsection{Correlation Matrix with Pandas and the Franke function}
\label{\detokenize{chapter4:correlation-matrix-with-pandas-and-the-franke-function}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Common imports}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}


\PYG{k}{def} \PYG{n+nf}{FrankeFunction}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)}\PYG{p}{:}
	\PYG{n}{term1} \PYG{o}{=} \PYG{l+m+mf}{0.75}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{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G{o}{*}\PYG{p}{(}\PYG{n}{i}\PYG{o}{\PYGZhy{}}\PYG{n}{k}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{k}\PYG{p}{)}

	\PYG{k}{return} \PYG{n}{X}


\PYG{c+c1}{\PYGZsh{} Making meshgrid of datapoints and compute Franke\PYGZsq{}s function}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{4}
\PYG{n}{N} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{N}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{N}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{z} \PYG{o}{=} \PYG{n}{FrankeFunction}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}
\PYG{n}{X} \PYG{o}{=} \PYG{n}{create\PYGZus{}X}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{n}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)}    

\PYG{n}{Xpd} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} subtract the mean values and set up the covariance matrix}
\PYG{n}{Xpd} \PYG{o}{=} \PYG{n}{Xpd} \PYG{o}{\PYGZhy{}} \PYG{n}{Xpd}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{covariance\PYGZus{}matrix} \PYG{o}{=} \PYG{n}{Xpd}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{p}{)}
\PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{covariance\PYGZus{}matrix}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
     0         1         2         3         4         5         6         7   \PYGZbs{}
0   0.0  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000   
1   0.0  0.089183  0.084797  0.089147  0.085535  0.082266  0.080527  0.077480   
2   0.0  0.084797  0.082978  0.087978  0.085822  0.083785  0.081609  0.079503   
3   0.0  0.089147  0.087978  0.094876  0.092810  0.090814  0.088924  0.086714   
4   0.0  0.085535  0.085822  0.092810  0.091736  0.090594  0.088276  0.086777   
5   0.0  0.082266  0.083785  0.090814  0.090594  0.090194  0.087493  0.086618   
6   0.0  0.080527  0.081609  0.088924  0.088276  0.087493  0.085397  0.084155   
7   0.0  0.077480  0.079503  0.086714  0.086777  0.086618  0.084155  0.083459   
8   0.0  0.074802  0.077623  0.084723  0.085401  0.085786  0.082993  0.082776   
9   0.0  0.072449  0.075952  0.082939  0.084154  0.085015  0.081923  0.082128   
10  0.0  0.071557  0.073969  0.080992  0.081340  0.081431  0.079151  0.078671   
11  0.0  0.069117  0.072168  0.079031  0.079900  0.080455  0.077870  0.077808   
12  0.0  0.066984  0.070584  0.077297  0.078621  0.079584  0.076722  0.077031   
13  0.0  0.065125  0.069200  0.075772  0.077496  0.078821  0.075706  0.076344   
14  0.0  0.063508  0.067997  0.074439  0.076518  0.078164  0.074816  0.075749   

          8         9         10        11        12        13        14  
0   0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  
1   0.074802  0.072449  0.071557  0.069117  0.066984  0.065125  0.063508  
2   0.077623  0.075952  0.073969  0.072168  0.070584  0.069200  0.067997  
3   0.084723  0.082939  0.080992  0.079031  0.077297  0.075772  0.074439  
4   0.085401  0.084154  0.081340  0.079900  0.078621  0.077496  0.076518  
5   0.085786  0.085015  0.081431  0.080455  0.079584  0.078821  0.078164  
6   0.082993  0.081923  0.079151  0.077870  0.076722  0.075706  0.074816  
7   0.082776  0.082128  0.078671  0.077808  0.077031  0.076344  0.075749  
8   0.082517  0.082246  0.078172  0.077682  0.077236  0.076846  0.076519  
9   0.082246  0.082311  0.077682  0.077525  0.077378  0.077256  0.077172  
10  0.078172  0.077682  0.074339  0.073633  0.072989  0.072416  0.071916  
11  0.077682  0.077525  0.073633  0.073260  0.072914  0.072607  0.072349  
12  0.077236  0.077378  0.072989  0.072914  0.072835  0.072771  0.072733  
13  0.076846  0.077256  0.072416  0.072607  0.072771  0.072926  0.073089  
14  0.076519  0.077172  0.071916  0.072349  0.072733  0.073089  0.073437  
\end{sphinxVerbatim}

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 and construct a correlation
matrix without these elements.


\subsection{Rewriting the Covariance and/or Correlation Matrix}
\label{\detokenize{chapter4:rewriting-the-covariance-and-or-correlation-matrix}}
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \(\boldsymbol{X}\) as
\begin{equation*}
\begin{split}
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}].
\end{split}
\end{equation*}
To see this let us simply look at a design matrix \(\boldsymbol{X}\in {\mathbb{R}}^{2\times 2}\)
\begin{equation*}
\begin{split}
\boldsymbol{X}=\begin{bmatrix}
x_{00} & x_{01}\\
x_{10} & x_{11}\\
\end{bmatrix}=\begin{bmatrix}
\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\
\end{bmatrix}.
\end{split}
\end{equation*}
If we then compute the expectation value
\begin{equation*}
\begin{split}
\mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\begin{bmatrix}
x_{00}^2+x_{01}^2 & x_{00}x_{10}+x_{01}x_{11}\\
x_{10}x_{00}+x_{11}x_{01} & x_{10}^2+x_{11}^2\\
\end{bmatrix},
\end{split}
\end{equation*}
which is just
\begin{equation*}
\begin{split}
\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\
                              \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\
             \end{bmatrix},
\end{split}
\end{equation*}
where we wrote \$\(\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]\)\( to indicate that this the covariance of the vectors \)\textbackslash{}boldsymbol\{x\}\( of the design/feature matrix \)\textbackslash{}boldsymbol\{X\}\$.

It is easy to generalize this to a matrix \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\).


\subsection{Linking with SVD}
\label{\detokenize{chapter4:linking-with-svd}}
See lecture september 11. More text to be added here soon.


\subsection{Where are we going?}
\label{\detokenize{chapter4:where-are-we-going}}
Before we proceed, we need to rethink what we have been doing. In our
eager to fit the data, we have omitted several important elements in
our regression analysis. In what follows we will
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
look at statistical properties, including a discussion of mean values, variance and the so\sphinxhyphen{}called bias\sphinxhyphen{}variance tradeoff

\item {} 
introduce resampling techniques like cross\sphinxhyphen{}validation, bootstrapping and jackknife and more

\end{enumerate}

This will allow us to link the standard linear algebra methods we have discussed above to a statistical interpretation of the methods.


\subsection{Resampling methods}
\label{\detokenize{chapter4:resampling-methods}}
Resampling methods are an indispensable tool in modern
statistics. They involve repeatedly drawing samples from a training
set and refitting a model of interest on each sample in order to
obtain additional information about the fitted model. For example, in
order to estimate the variability of a linear regression fit, we can
repeatedly draw different samples from the training data, fit a linear
regression to each new sample, and then examine the extent to which
the resulting fits differ. Such an approach may allow us to obtain
information that would not be available from fitting the model only
once using the original training sample.

Two resampling methods are often used in Machine Learning analyses,
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
The \sphinxstylestrong{bootstrap method}

\item {} 
and \sphinxstylestrong{Cross\sphinxhyphen{}Validation}

\end{enumerate}

In addition there are several other methods such as the Jackknife and the Blocking methods. We will discuss in particular
cross\sphinxhyphen{}validation and the bootstrap method.


\subsection{Resampling approaches can be computationally expensive}
\label{\detokenize{chapter4:resampling-approaches-can-be-computationally-expensive}}
Resampling approaches can be computationally expensive, because they
involve fitting the same statistical method multiple times using
different subsets of the training data. However, due to recent
advances in computing power, the computational requirements of
resampling methods generally are not prohibitive. In this chapter, we
discuss two of the most commonly used resampling methods,
cross\sphinxhyphen{}validation and the bootstrap. Both methods are important tools
in the practical application of many statistical learning
procedures. For example, cross\sphinxhyphen{}validation can be used to estimate the
test error associated with a given statistical learning method in
order to evaluate its performance, or to select the appropriate level
of flexibility. The process of evaluating a model’s performance is
known as model assessment, whereas the process of selecting the proper
level of flexibility for a model is known as model selection. The
bootstrap is widely used.


\subsection{Why resampling methods ?}
\label{\detokenize{chapter4:why-resampling-methods}}
\sphinxstylestrong{Statistical analysis.}
\begin{itemize}
\item {} 
Our simulations can be treated as \sphinxstyleemphasis{computer experiments}. This is particularly the case for Monte Carlo methods

\item {} 
The results can be analysed with the same statistical tools as we would use analysing experimental data.

\item {} 
As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.

\end{itemize}


\subsection{Statistical analysis}
\label{\detokenize{chapter4:statistical-analysis}}\begin{itemize}
\item {} 
As in other experiments, many numerical  experiments have two classes of errors:
\begin{itemize}
\item {} 
Statistical errors

\item {} 
Systematical errors

\end{itemize}

\item {} 
Statistical errors can be estimated using standard tools from statistics

\item {} 
Systematical errors are method specific and must be treated differently from case to case.

\end{itemize}




\subsection{Linking the regression analysis with a statistical interpretation}
\label{\detokenize{chapter4:linking-the-regression-analysis-with-a-statistical-interpretation}}
The
advantage of doing linear regression is that we actually end up with
analytical expressions for several statistical quantities.Standard least squares and Ridge regression  allow us to
derive quantities like the variance and other expectation values in a
rather straightforward way.

It is assumed that \(\varepsilon_i
\sim \mathcal{N}(0, \sigma^2)\) and the \(\varepsilon_{i}\) are
independent, i.e.:
\begin{equation*}
\begin{split}
\begin{align*} 
\mbox{Cov}(\varepsilon_{i_1},
\varepsilon_{i_2}) & = \left\{ \begin{array}{lcc} \sigma^2 & \mbox{if}
& i_1 = i_2, \\ 0 & \mbox{if} & i_1 \not= i_2.  \end{array} \right.
\end{align*}
\end{split}
\end{equation*}
The randomness of \(\varepsilon_i\) implies that
\(\mathbf{y}_i\) is also a random variable. In particular,
\(\mathbf{y}_i\) is normally distributed, because \(\varepsilon_i \sim
\mathcal{N}(0, \sigma^2)\) and \(\mathbf{X}_{i,\ast} \, \boldsymbol{\beta}\) is a
non\sphinxhyphen{}random scalar. To specify the parameters of the distribution of
\(\mathbf{y}_i\) we need to calculate its first two moments.

Recall that \(\boldsymbol{X}\) is a matrix of dimensionality \(n\times p\). The
notation above \(\mathbf{X}_{i,\ast}\) means that we are looking at the
row number \(i\) and perform a sum over all values \(p\).


\subsection{Assumptions made}
\label{\detokenize{chapter4:assumptions-made}}
The assumption we have made here can be summarized as (and this is going to be useful when we discuss the bias\sphinxhyphen{}variance trade off)
that there exists a function \(f(\boldsymbol{x})\) and  a normal distributed error \(\boldsymbol{\varepsilon}\sim \mathcal{N}(0, \sigma^2)\)
which describe our data
\begin{equation*}
\begin{split}
\boldsymbol{y} = f(\boldsymbol{x})+\boldsymbol{\varepsilon}
\end{split}
\end{equation*}
We approximate this function with our model from the solution of the linear regression equations, that is our
function \(f\) is approximated by \(\boldsymbol{\tilde{y}}\) where we want to minimize \((\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\), our MSE, with
\begin{equation*}
\begin{split}
\boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta}.
\end{split}
\end{equation*}

\subsection{Expectation value and variance}
\label{\detokenize{chapter4:expectation-value-and-variance}}
We can calculate the expectation value of \(\boldsymbol{y}\) for a given element \(i\)
\begin{equation*}
\begin{split}
\begin{align*} 
\mathbb{E}(y_i) & =
\mathbb{E}(\mathbf{X}_{i, \ast} \, \boldsymbol{\beta}) + \mathbb{E}(\varepsilon_i)
\, \, \, = \, \, \, \mathbf{X}_{i, \ast} \, \beta, 
\end{align*}
\end{split}
\end{equation*}
while
its variance is
\begin{equation*}
\begin{split}
\begin{align*} \mbox{Var}(y_i) & = \mathbb{E} \{ [y_i
- \mathbb{E}(y_i)]^2 \} \, \, \, = \, \, \, \mathbb{E} ( y_i^2 ) -
[\mathbb{E}(y_i)]^2  \\  & = \mathbb{E} [ ( \mathbf{X}_{i, \ast} \,
\beta + \varepsilon_i )^2] - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 \\ &
= \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 \varepsilon_i
\mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + \varepsilon_i^2 ] - ( \mathbf{X}_{i,
\ast} \, \beta)^2 \\  & = ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2
\mathbb{E}(\varepsilon_i) \mathbf{X}_{i, \ast} \, \boldsymbol{\beta} +
\mathbb{E}(\varepsilon_i^2 ) - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 
\\ & = \mathbb{E}(\varepsilon_i^2 ) \, \, \, = \, \, \,
\mbox{Var}(\varepsilon_i) \, \, \, = \, \, \, \sigma^2.  
\end{align*}
\end{split}
\end{equation*}
Hence, \(y_i \sim \mathcal{N}( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta}, \sigma^2)\), that is \(\boldsymbol{y}\) follows a normal distribution with
mean value \(\boldsymbol{X}\boldsymbol{\beta}\) and variance \(\sigma^2\) (not be confused with the singular values of the SVD).


\subsection{Expectation value and variance for \protect\(\boldsymbol{\beta}\protect\)}
\label{\detokenize{chapter4:expectation-value-and-variance-for-boldsymbol-beta}}
With the OLS expressions for the parameters \(\boldsymbol{\beta}\) we can evaluate the expectation value
\begin{equation*}
\begin{split}
\mathbb{E}(\boldsymbol{\beta}) = \mathbb{E}[ (\mathbf{X}^{\top} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbb{E}[ \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1} \mathbf{X}^{T}\mathbf{X}\boldsymbol{\beta}=\boldsymbol{\beta}.
\end{split}
\end{equation*}
This means that the estimator of the regression parameters is unbiased.

We can also calculate the variance

The variance of \(\boldsymbol{\beta}\) is
\begin{equation*}
\begin{split}
\begin{eqnarray*}
\mbox{Var}(\boldsymbol{\beta}) & = & \mathbb{E} \{ [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})] [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})]^{T} \}
\\
& = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}]^{T} \}
\\
% & = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}]^{T} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
% \\
% & = & \mathbb{E} \{ (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} \, \mathbf{Y}^{T} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1}  \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
% \\
& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \mathbb{E} \{ \mathbf{Y} \, \mathbf{Y}^{T} \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
\\
& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \{ \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \,  \mathbf{X}^{T} + \sigma^2 \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
% \\
% & = & (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^T \,  \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T % \mathbf{X})^{-1}
% \\
% & & + \, \, \sigma^2 \, (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T  \, \mathbf{X} \, (\mathbf{X}^T \mathbf{X})^{-1} - \boldsymbol{\beta} \boldsymbol{\beta}^T
\\
& = & \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}  + \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
\, \, \, = \, \, \, \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1},
\end{eqnarray*}
\end{split}
\end{equation*}
where we have used  that \(\mathbb{E} (\mathbf{Y} \mathbf{Y}^{T}) =
\mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} +
\sigma^2 \, \mathbf{I}_{nn}\). From \(\mbox{Var}(\boldsymbol{\beta}) = \sigma^2
\, (\mathbf{X}^{T} \mathbf{X})^{-1}\), one obtains an estimate of the
variance of the estimate of the \(j\)\sphinxhyphen{}th regression coefficient:
\(\boldsymbol{\sigma}^2 (\boldsymbol{\beta}_j ) = \boldsymbol{\sigma}^2 \sqrt{
[(\mathbf{X}^{T} \mathbf{X})^{-1}]_{jj} }\). This may be used to
construct a confidence interval for the estimates.

In a similar way, we can obtain analytical expressions for say the
expectation values of the parameters \(\boldsymbol{\beta}\) and their variance
when we employ Ridge regression, allowing us again to define a confidence interval.

It is rather straightforward to show that
\begin{equation*}
\begin{split}
\mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big]=(\mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} (\mathbf{X}^{\top} \mathbf{X})\boldsymbol{\beta}^{\mathrm{OLS}}.
\end{split}
\end{equation*}
We see clearly that
\(\mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big] \not= \boldsymbol{\beta}^{\mathrm{OLS}}\) for any \(\lambda > 0\). We say then that the ridge estimator is biased.

We can also compute the variance as
\begin{equation*}
\begin{split}
\mbox{Var}[\boldsymbol{\beta}^{\mathrm{Ridge}}]=\sigma^2[  \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}  \mathbf{X}^{T} \mathbf{X} \{ [  \mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T},
\end{split}
\end{equation*}
and it is easy to see that if the parameter \(\lambda\) goes to infinity then the variance of Ridge parameters \(\boldsymbol{\beta}\) goes to zero.

With this, we can compute the difference
\begin{equation*}
\begin{split}
\mbox{Var}[\boldsymbol{\beta}^{\mathrm{OLS}}]-\mbox{Var}(\boldsymbol{\beta}^{\mathrm{Ridge}})=\sigma^2 [  \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}[ 2\lambda\mathbf{I} + \lambda^2 (\mathbf{X}^{T} \mathbf{X})^{-1} ] \{ [  \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}.
\end{split}
\end{equation*}
The difference is non\sphinxhyphen{}negative definite since each component of the
matrix product is non\sphinxhyphen{}negative definite.
This means the variance we obtain with the standard OLS will always for \(\lambda > 0\) be larger than the variance of \(\boldsymbol{\beta}\) obtained with the Ridge estimator. This has interesting consequences when we discuss the so\sphinxhyphen{}called bias\sphinxhyphen{}variance trade\sphinxhyphen{}off below.


\subsection{Resampling methods}
\label{\detokenize{chapter4:id9}}
With all these analytical equations for both the OLS and Ridge
regression, we will now outline how to assess a given model. This will
lead us to a discussion of the so\sphinxhyphen{}called bias\sphinxhyphen{}variance tradeoff (see
below) and so\sphinxhyphen{}called resampling methods.

One of the quantities we have discussed as a way to measure errors is
the mean\sphinxhyphen{}squared error (MSE), mainly used for fitting of continuous
functions. Another choice is the absolute error.

In the discussions below we will focus on the MSE and in particular since we will split the data into test and training data,
we discuss the
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
prediction error or simply the \sphinxstylestrong{test error} \(\mathrm{Err_{Test}}\), where we have a fixed training set and the test error is the MSE arising from the data reserved for testing. We discuss also the

\item {} 
training error \(\mathrm{Err_{Train}}\), which is the average loss over the training data.

\end{enumerate}

As our model becomes more and more complex, more of the training data tends to  used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
For a certain level of complexity the test error will reach minimum, before starting to increase again. The
training error reaches a saturation.


\subsection{Resampling methods: Jackknife and Bootstrap}
\label{\detokenize{chapter4:resampling-methods-jackknife-and-bootstrap}}
Two famous
resampling methods are the \sphinxstylestrong{independent bootstrap} and \sphinxstylestrong{the jackknife}.

The jackknife is a special case of the independent bootstrap. Still, the jackknife was made
popular prior to the independent bootstrap. And as the popularity of
the independent bootstrap soared, new variants, such as \sphinxstylestrong{the dependent bootstrap}.

The Jackknife and independent bootstrap work for
independent, identically distributed random variables.
If these conditions are not
satisfied, the methods will fail.  Yet, it should be said that if the data are
independent, identically distributed, and we only want to estimate the
variance of \(\overline{X}\) (which often is the case), then there is no
need for bootstrapping.


\subsection{Resampling methods: Jackknife}
\label{\detokenize{chapter4:resampling-methods-jackknife}}
The Jackknife works by making many replicas of the estimator \(\widehat{\theta}\).
The jackknife is a resampling method where we systematically leave out one observation from the vector of observed values \(\boldsymbol{x} = (x_1,x_2,\cdots,X_n)\).
Let \(\boldsymbol{x}_i\) denote the vector
\begin{equation*}
\begin{split}
\boldsymbol{x}_i = (x_1,x_2,\cdots,x_{i-1},x_{i+1},\cdots,x_n),
\end{split}
\end{equation*}
which equals the vector \(\boldsymbol{x}\) with the exception that observation
number \(i\) is left out. Using this notation, define
\(\widehat{\theta}_i\) to be the estimator
\(\widehat{\theta}\) computed using \(\vec{X}_i\).


\subsection{Jackknife code example}
\label{\detokenize{chapter4:jackknife-code-example}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{numpy} \PYG{k+kn}{import} \PYG{o}{*}
\PYG{k+kn}{from} \PYG{n+nn}{numpy}\PYG{n+nn}{.}\PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{randint}\PYG{p}{,} \PYG{n}{randn}
\PYG{k+kn}{from} \PYG{n+nn}{time} \PYG{k+kn}{import} \PYG{n}{time}

\PYG{k}{def} \PYG{n+nf}{jackknife}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{;}\PYG{n}{t} \PYG{o}{=} \PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{;} \PYG{n}{inds} \PYG{o}{=} \PYG{n}{arange}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{;} \PYG{n}{t0} \PYG{o}{=} \PYG{n}{time}\PYG{p}{(}\PYG{p}{)}
    \PYG{c+c1}{\PYGZsh{}\PYGZsh{} \PYGZsq{}jackknifing\PYGZsq{} by leaving out an observation for each i                                                                                                                      }
    \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{t}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{stat}\PYG{p}{(}\PYG{n}{delete}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{n}{i}\PYG{p}{)} \PYG{p}{)}

    \PYG{c+c1}{\PYGZsh{} analysis                                                                                                                                                                     }
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Runtime: }\PYG{l+s+si}{\PYGZpc{}g}\PYG{l+s+s2}{ sec}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{time}\PYG{p}{(}\PYG{p}{)}\PYG{o}{\PYGZhy{}}\PYG{n}{t0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{;} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Jackknife Statistics :}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{original           bias      std. error}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}8g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}14g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}15g}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{stat}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{,}\PYG{p}{(}\PYG{n}{n}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n}\PYG{p}{,} \PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{var}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{o}{.}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)}

    \PYG{k}{return} \PYG{n}{t}


\PYG{c+c1}{\PYGZsh{} Returns mean of data samples                                                                                                                                                     }
\PYG{k}{def} \PYG{n+nf}{stat}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{mean}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}


\PYG{n}{mu}\PYG{p}{,} \PYG{n}{sigma} \PYG{o}{=} \PYG{l+m+mi}{100}\PYG{p}{,} \PYG{l+m+mi}{15}
\PYG{n}{datapoints} \PYG{o}{=} \PYG{l+m+mi}{10000}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{mu} \PYG{o}{+} \PYG{n}{sigma}\PYG{o}{*}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{datapoints}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} jackknife returns the data sample                                                                                                                                                }
\PYG{n}{t} \PYG{o}{=} \PYG{n}{jackknife}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
Runtime: 0.389789 sec
Jackknife Statistics :
original           bias      std. error
 100.259        100.249        0.149656
\end{sphinxVerbatim}


\subsection{Resampling methods: Bootstrap}
\label{\detokenize{chapter4:resampling-methods-bootstrap}}
Bootstrapping is a nonparametric approach to statistical inference
that substitutes computation for more traditional distributional
assumptions and asymptotic results. Bootstrapping offers a number of
advantages:
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
The bootstrap is quite general, although there are some cases in which it fails.

\item {} 
Because it does not require distributional assumptions (such as normally distributed errors), the bootstrap can provide more accurate inferences when the data are not well behaved or when the sample size is small.

\item {} 
It is possible to apply the bootstrap to statistics with sampling distributions that are difficult to derive, even asymptotically.

\item {} 
It is relatively simple to apply the bootstrap to complex data\sphinxhyphen{}collection plans (such as stratified and clustered samples).

\end{enumerate}


\subsection{Resampling methods: Bootstrap background}
\label{\detokenize{chapter4:resampling-methods-bootstrap-background}}
Since \(\widehat{\theta} = \widehat{\theta}(\boldsymbol{X})\) is a function of random variables,
\(\widehat{\theta}\) itself must be a random variable. Thus it has
a pdf, call this function \(p(\boldsymbol{t})\). The aim of the bootstrap is to
estimate \(p(\boldsymbol{t})\) by the relative frequency of
\(\widehat{\theta}\). You can think of this as using a histogram
in the place of \(p(\boldsymbol{t})\). If the relative frequency closely
resembles \(p(\vec{t})\), then using numerics, it is straight forward to
estimate all the interesting parameters of \(p(\boldsymbol{t})\) using point
estimators.


\subsection{Resampling methods: More Bootstrap background}
\label{\detokenize{chapter4:resampling-methods-more-bootstrap-background}}
In the case that \(\widehat{\theta}\) has
more than one component, and the components are independent, we use the
same estimator on each component separately.  If the probability
density function of \(X_i\), \(p(x)\), had been known, then it would have
been straight forward to do this by:
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
Drawing lots of numbers from \(p(x)\), suppose we call one such set of numbers \((X_1^*, X_2^*, \cdots, X_n^*)\).

\item {} 
Then using these numbers, we could compute a replica of \(\widehat{\theta}\) called \(\widehat{\theta}^*\).

\end{enumerate}

By repeated use of (1) and (2), many
estimates of \(\widehat{\theta}\) could have been obtained. The
idea is to use the relative frequency of \(\widehat{\theta}^*\)
(think of a histogram) as an estimate of \(p(\boldsymbol{t})\).


\subsection{Resampling methods: Bootstrap approach}
\label{\detokenize{chapter4:resampling-methods-bootstrap-approach}}
But
unless there is enough information available about the process that
generated \(X_1,X_2,\cdots,X_n\), \(p(x)\) is in general
unknown. Therefore, \sphinxhref{https://projecteuclid.org/euclid.aos/1176344552}{Efron in 1979}  asked the
question: What if we replace \(p(x)\) by the relative frequency
of the observation \(X_i\); if we draw observations in accordance with
the relative frequency of the observations, will we obtain the same
result in some asymptotic sense? The answer is yes.

Instead of generating the histogram for the relative
frequency of the observation \(X_i\), just draw the values
\((X_1^*,X_2^*,\cdots,X_n^*)\) with replacement from the vector
\(\boldsymbol{X}\).


\subsection{Resampling methods: Bootstrap steps}
\label{\detokenize{chapter4:resampling-methods-bootstrap-steps}}
The independent bootstrap works like this:
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
Draw with replacement \(n\) numbers for the observed variables \(\boldsymbol{x} = (x_1,x_2,\cdots,x_n)\).

\item {} 
Define a vector \(\boldsymbol{x}^*\) containing the values which were drawn from \(\boldsymbol{x}\).

\item {} 
Using the vector \(\boldsymbol{x}^*\) compute \(\widehat{\theta}^*\) by evaluating \(\widehat \theta\) under the observations \(\boldsymbol{x}^*\).

\item {} 
Repeat this process \(k\) times.

\end{enumerate}

When you are done, you can draw a histogram of the relative frequency
of \(\widehat \theta^*\). This is your estimate of the probability
distribution \(p(t)\). Using this probability distribution you can
estimate any statistics thereof. In principle you never draw the
histogram of the relative frequency of \(\widehat{\theta}^*\). Instead
you use the estimators corresponding to the statistic of interest. For
example, if you are interested in estimating the variance of \(\widehat
\theta\), apply the etsimator \(\widehat \sigma^2\) to the values
\(\widehat \theta ^*\).


\subsection{Code example for the Bootstrap method}
\label{\detokenize{chapter4:code-example-for-the-bootstrap-method}}
The following code starts with a Gaussian distribution with mean value
\(\mu =100\) and variance \(\sigma=15\). We use this to generate the data
used in the bootstrap analysis. The bootstrap analysis returns a data
set after a given number of bootstrap operations (as many as we have
data points). This data set consists of estimated mean values for each
bootstrap operation. The histogram generated by the bootstrap method
shows that the distribution for these mean values is also a Gaussian,
centered around the mean value \(\mu=100\) but with standard deviation
\(\sigma/\sqrt{n}\), where \(n\) is the number of bootstrap samples (in
this case the same as the number of original data points). The value
of the standard deviation is what we expect from the central limit
theorem.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{numpy} \PYG{k+kn}{import} \PYG{o}{*}
\PYG{k+kn}{from} \PYG{n+nn}{numpy}\PYG{n+nn}{.}\PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{randint}\PYG{p}{,} \PYG{n}{randn}
\PYG{k+kn}{from} \PYG{n+nn}{time} \PYG{k+kn}{import} \PYG{n}{time}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{mlab} \PYG{k}{as} \PYG{n+nn}{mlab}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}

\PYG{c+c1}{\PYGZsh{} Returns mean of bootstrap samples                                                                                                                                                }
\PYG{k}{def} \PYG{n+nf}{stat}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{mean}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Bootstrap algorithm}
\PYG{k}{def} \PYG{n+nf}{bootstrap}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,} \PYG{n}{statistic}\PYG{p}{,} \PYG{n}{R}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{t} \PYG{o}{=} \PYG{n}{zeros}\PYG{p}{(}\PYG{n}{R}\PYG{p}{)}\PYG{p}{;} \PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{;} \PYG{n}{inds} \PYG{o}{=} \PYG{n}{arange}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{;} \PYG{n}{t0} \PYG{o}{=} \PYG{n}{time}\PYG{p}{(}\PYG{p}{)}
    \PYG{c+c1}{\PYGZsh{} non\PYGZhy{}parametric bootstrap         }
    \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{R}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{t}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{statistic}\PYG{p}{(}\PYG{n}{data}\PYG{p}{[}\PYG{n}{randint}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{n}{n}\PYG{p}{,}\PYG{n}{n}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)}

    \PYG{c+c1}{\PYGZsh{} analysis    }
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Runtime: }\PYG{l+s+si}{\PYGZpc{}g}\PYG{l+s+s2}{ sec}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{time}\PYG{p}{(}\PYG{p}{)}\PYG{o}{\PYGZhy{}}\PYG{n}{t0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{;} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Bootstrap Statistics :}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{original           bias      std. error}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}8g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}8g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}14g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}15g}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{statistic}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{,} \PYG{n}{std}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{,}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{,}\PYG{n}{std}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
    \PYG{k}{return} \PYG{n}{t}


\PYG{n}{mu}\PYG{p}{,} \PYG{n}{sigma} \PYG{o}{=} \PYG{l+m+mi}{100}\PYG{p}{,} \PYG{l+m+mi}{15}
\PYG{n}{datapoints} \PYG{o}{=} \PYG{l+m+mi}{10000}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{mu} \PYG{o}{+} \PYG{n}{sigma}\PYG{o}{*}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{datapoints}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} bootstrap returns the data sample                                    }
\PYG{n}{t} \PYG{o}{=} \PYG{n}{bootstrap}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{,} \PYG{n}{datapoints}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} the histogram of the bootstrapped  data                                                                                                    }
\PYG{n}{n}\PYG{p}{,} \PYG{n}{binsboot}\PYG{p}{,} \PYG{n}{patches} \PY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PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{99.5}\PYG{p}{,} \PYG{l+m+mf}{100.6}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{3.0}\PYG{p}{]}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{grid}\PYG{p}{(}\PYG{k+kc}{True}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
Runtime: 2.00056 sec
Bootstrap Statistics :
original           bias      std. error
 99.9087  15.0416        99.9098        0.150465
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{g+gt}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}}
\PYG{n+ne}{AttributeError}\PYG{g+gWhitespace}{                            }Traceback (most recent call last)
\PYG{o}{\PYGZlt{}}\PYG{n}{ipython}\PYG{o}{\PYGZhy{}}\PYG{n+nb}{input}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{29}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{53990135e988}\PYG{o}{\PYGZgt{}} \PYG{o+ow}{in} \PYG{o}{\PYGZlt{}}\PYG{n}{module}\PYG{o}{\PYGZgt{}}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{29} \PYG{n}{t} \PYG{o}{=} \PYG{n}{bootstrap}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{,} \PYG{n}{datapoints}\PYG{p}{)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{30} \PYG{c+c1}{\PYGZsh{} the histogram of the bootstrapped  data}
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{31} \PYG{n}{n}\PYG{p}{,} \PYG{n}{binsboot}\PYG{p}{,} \PYG{n}{patches} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{hist}\PYG{p}{(}\PYG{n}{t}\PYG{p}{,} \PYG{l+m+mi}{50}\PYG{p}{,} \PYG{n}{normed}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{facecolor}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{red}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.75}\PYG{p}{)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{32} 
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{33} \PYG{c+c1}{\PYGZsh{} add a \PYGZsq{}best fit\PYGZsq{} line}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/pyplot.py} in \PYG{n+ni}{hist}\PYG{n+nt}{(x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, data, **kwargs)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{2603}         \PYG{n}{orientation}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{vertical}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{rwidth}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{n}{log}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{2604}         \PYG{n}{label}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{n}{stacked}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{,} \PYG{o}{*}\PYG{p}{,} \PYG{n}{data}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{2605}     \PYG{k}{return} \PYG{n}{gca}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{hist}\PYG{p}{(}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{2606}         \PYG{n}{x}\PYG{p}{,} \PYG{n}{bins}\PYG{o}{=}\PYG{n}{bins}\PYG{p}{,} \PYG{n+nb}{range}\PYG{o}{=}\PYG{n+nb}{range}\PYG{p}{,} \PYG{n}{density}\PYG{o}{=}\PYG{n}{density}\PYG{p}{,} \PYG{n}{weights}\PYG{o}{=}\PYG{n}{weights}\PYG{p}{,}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{2607}         \PYG{n}{cumulative}\PYG{o}{=}\PYG{n}{cumulative}\PYG{p}{,} \PYG{n}{bottom}\PYG{o}{=}\PYG{n}{bottom}\PYG{p}{,} \PYG{n}{histtype}\PYG{o}{=}\PYG{n}{histtype}\PYG{p}{,}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}.py} in \PYG{n+ni}{inner}\PYG{n+nt}{(ax, data, *args, **kwargs)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1563}     \PYG{k}{def} \PYG{n+nf}{inner}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{o}{*}\PYG{n}{args}\PYG{p}{,} \PYG{n}{data}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)}\PYG{p}{:}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1564}         \PYG{k}{if} \PYG{n}{data} \PYG{o+ow}{is} \PYG{k+kc}{None}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1565}             \PYG{k}{return} \PYG{n}{func}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{o}{*}\PYG{n+nb}{map}\PYG{p}{(}\PYG{n}{sanitize\PYGZus{}sequence}\PYG{p}{,} \PYG{n}{args}\PYG{p}{)}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1566} 
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1567}         \PYG{n}{bound} \PYG{o}{=} \PYG{n}{new\PYGZus{}sig}\PYG{o}{.}\PYG{n}{bind}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{o}{*}\PYG{n}{args}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/axes/\PYGZus{}axes.py} in \PYG{n+ni}{hist}\PYG{n+nt}{(self, x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{6817}             \PYG{k}{if} \PYG{n}{patch}\PYG{p}{:}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{6818}                 \PYG{n}{p} \PYG{o}{=} \PYG{n}{patch}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}
\PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{6819}                 \PYG{n}{p}\PYG{o}{.}\PYG{n}{update}\PYG{p}{(}\PYG{n}{kwargs}\PYG{p}{)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{6820}                 \PYG{k}{if} \PYG{n}{lbl} \PYG{o+ow}{is} \PYG{o+ow}{not} \PYG{k+kc}{None}\PYG{p}{:}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{6821}                     \PYG{n}{p}\PYG{o}{.}\PYG{n}{set\PYGZus{}label}\PYG{p}{(}\PYG{n}{lbl}\PYG{p}{)}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/artist.py} in \PYG{n+ni}{update}\PYG{n+nt}{(self, props)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1004} 
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1005}         \PYG{k}{with} \PYG{n}{cbook}\PYG{o}{.}\PYG{n}{\PYGZus{}setattr\PYGZus{}cm}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{eventson}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1006}             \PYG{n}{ret} \PYG{o}{=} \PYG{p}{[}\PYG{n}{\PYGZus{}update\PYGZus{}property}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v}\PYG{p}{)} \PYG{k}{for} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v} \PYG{o+ow}{in} \PYG{n}{props}\PYG{o}{.}\PYG{n}{items}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1007} 
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1008}         \PYG{k}{if} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{ret}\PYG{p}{)}\PYG{p}{:}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/artist.py} in \PYG{n+ni}{\PYGZlt{}listcomp\PYGZgt{}}\PYG{n+nt}{(.0)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1004} 
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1005}         \PYG{k}{with} \PYG{n}{cbook}\PYG{o}{.}\PYG{n}{\PYGZus{}setattr\PYGZus{}cm}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{eventson}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1006}             \PYG{n}{ret} \PYG{o}{=} \PYG{p}{[}\PYG{n}{\PYGZus{}update\PYGZus{}property}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v}\PYG{p}{)} \PYG{k}{for} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v} \PYG{o+ow}{in} \PYG{n}{props}\PYG{o}{.}\PYG{n}{items}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1007} 
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1008}         \PYG{k}{if} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{ret}\PYG{p}{)}\PYG{p}{:}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/artist.py} in \PYG{n+ni}{\PYGZus{}update\PYGZus{}property}\PYG{n+nt}{(self, k, v)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{999}                 \PYG{n}{func} \PYG{o}{=} \PYG{n+nb}{getattr}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{set\PYGZus{}}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{+} \PYG{n}{k}\PYG{p}{,} \PYG{k+kc}{None}\PYG{p}{)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1000}                 \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{callable}\PYG{p}{(}\PYG{n}{func}\PYG{p}{)}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1001}                     \PYG{k}{raise} \PYG{n+ne}{AttributeError}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+si}{\PYGZob{}!r\PYGZcb{}}\PYG{l+s+s1}{ object has no property }\PYG{l+s+si}{\PYGZob{}!r\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1002}                                          \PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n+nb}{type}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{o}{.}\PYG{n+nv+vm}{\PYGZus{}\PYGZus{}name\PYGZus{}\PYGZus{}}\PYG{p}{,} \PYG{n}{k}\PYG{p}{)}\PYG{p}{)}
\PYG{g+gWhitespace}{   }\PYG{l+m+mi}{1003}                 \PYG{k}{return} \PYG{n}{func}\PYG{p}{(}\PYG{n}{v}\PYG{p}{)}

\PYG{n+ne}{AttributeError}: \PYGZsq{}Rectangle\PYGZsq{} object has no property \PYGZsq{}normed\PYGZsq{}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_278_2}.png}




\subsection{Various steps in cross\sphinxhyphen{}validation}
\label{\detokenize{chapter4:various-steps-in-cross-validation}}
When the repetitive splitting of the data set is done randomly,
samples may accidently end up in a fast majority of the splits in
either training or test set. Such samples may have an unbalanced
influence on either model building or prediction evaluation. To avoid
this \(k\)\sphinxhyphen{}fold cross\sphinxhyphen{}validation structures the data splitting. The
samples are divided into \(k\) more or less equally sized exhaustive and
mutually exclusive subsets. In turn (at each split) one of these
subsets plays the role of the test set while the union of the
remaining subsets constitutes the training set. Such a splitting
warrants a balanced representation of each sample in both training and
test set over the splits. Still the division into the \(k\) subsets
involves a degree of randomness. This may be fully excluded when
choosing \(k=n\). This particular case is referred to as leave\sphinxhyphen{}one\sphinxhyphen{}out
cross\sphinxhyphen{}validation (LOOCV).




\subsection{How to set up the cross\sphinxhyphen{}validation for Ridge and/or Lasso}
\label{\detokenize{chapter4:how-to-set-up-the-cross-validation-for-ridge-and-or-lasso}}\begin{itemize}
\item {} 
Define a range of interest for the penalty parameter.

\item {} 
Divide the data set into training and test set comprising samples \(\{1, \ldots, n\} \setminus i\) and \(\{ i \}\), respectively.

\item {} 
Fit the linear regression model by means of ridge estimation  for each \(\lambda\) in the grid using the training set, and the corresponding estimate of the error variance \(\boldsymbol{\sigma}_{-i}^2(\lambda)\), as

\end{itemize}
\begin{equation*}
\begin{split}
\begin{align*}
\boldsymbol{\beta}_{-i}(\lambda) & =  ( \boldsymbol{X}_{-i, \ast}^{T}
\boldsymbol{X}_{-i, \ast} + \lambda \boldsymbol{I}_{pp})^{-1}
\boldsymbol{X}_{-i, \ast}^{T} \boldsymbol{y}_{-i}
\end{align*}
\end{split}
\end{equation*}\begin{itemize}
\item {} 
Evaluate the prediction performance of these models on the test set by \(\log\{L[y_i, \boldsymbol{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}\). Or, by the prediction error \(|y_i - \boldsymbol{X}_{i, \ast} \boldsymbol{\beta}_{-i}(\lambda)|\), the relative error, the error squared or the R2 score function.

\item {} 
Repeat the first three steps  such that each sample plays the role of the test set once.

\item {} 
Average the prediction performances of the test sets at each grid point of the penalty bias/parameter. It is an estimate of the prediction performance of the model corresponding to this value of the penalty parameter on novel data. It is defined as

\end{itemize}
\begin{equation*}
\begin{split}
\begin{align*}
\frac{1}{n} \sum_{i = 1}^n \log\{L[y_i, \mathbf{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}.
\end{align*}
\end{split}
\end{equation*}

\subsection{Cross\sphinxhyphen{}validation in brief}
\label{\detokenize{chapter4:cross-validation-in-brief}}
For the various values of \(k\)
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
shuffle the dataset randomly.

\item {} 
Split the dataset into \(k\) groups.

\item {} 
For each unique group:

\end{enumerate}

a. Decide which group to use as set for test data

b. Take the remaining groups as a training data set

c. Fit a model on the training set and evaluate it on the test set

d. Retain the evaluation score and discard the model
\begin{enumerate}
\sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}%
\item {} 
Summarize the model using the sample of model evaluation scores

\end{enumerate}


\subsection{Code Example for Cross\sphinxhyphen{}validation and \protect\(k\protect\)\sphinxhyphen{}fold Cross\sphinxhyphen{}validation}
\label{\detokenize{chapter4:code-example-for-cross-validation-and-k-fold-cross-validation}}
The code here uses Ridge regression with cross\sphinxhyphen{}validation (CV)  resampling and \(k\)\sphinxhyphen{}fold CV in order to fit a specific polynomial.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{KFold}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{Ridge}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures}

\PYG{c+c1}{\PYGZsh{} A seed just to ensure that the random numbers are the same for every run.}
\PYG{c+c1}{\PYGZsh{} Useful for eventual debugging.}
\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{3155}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Generate the data.}
\PYG{n}{nsamples} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{nsamples}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{nsamples}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{}\PYGZsh{} Cross\PYGZhy{}validation on Ridge regression using KFold only}

\PYG{c+c1}{\PYGZsh{} Decide degree on polynomial to fit}
\PYG{n}{poly} \PYG{o}{=} \PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree} \PYG{o}{=} \PYG{l+m+mi}{6}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Decide which values of lambda to use}
\PYG{n}{nlambdas} \PYG{o}{=} \PYG{l+m+mi}{500}
\PYG{n}{lambdas} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{nlambdas}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Initialize a KFold instance}
\PYG{n}{k} \PYG{o}{=} \PYG{l+m+mi}{5}
\PYG{n}{kfold} \PYG{o}{=} \PYG{n}{KFold}\PYG{p}{(}\PYG{n}{n\PYGZus{}splits} \PYG{o}{=} \PYG{n}{k}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Perform the cross\PYGZhy{}validation to estimate MSE}
\PYG{n}{scores\PYGZus{}KFold} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n}{nlambdas}\PYG{p}{,} \PYG{n}{k}\PYG{p}{)}\PYG{p}{)}

\PYG{n}{i} \PYG{o}{=} \PYG{l+m+mi}{0}
\PYG{k}{for} \PYG{n}{lmb} \PYG{o+ow}{in} \PYG{n}{lambdas}\PYG{p}{:}
    \PYG{n}{ridge} \PYG{o}{=} \PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha} \PYG{o}{=} \PYG{n}{lmb}\PYG{p}{)}
    \PYG{n}{j} \PYG{o}{=} \PYG{l+m+mi}{0}
    \PYG{k}{for} \PYG{n}{train\PYGZus{}inds}\PYG{p}{,} \PYG{n}{test\PYGZus{}inds} \PYG{o+ow}{in} \PYG{n}{kfold}\PYG{o}{.}\PYG{n}{split}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{xtrain} \PYG{o}{=} \PYG{n}{x}\PYG{p}{[}\PYG{n}{train\PYGZus{}inds}\PYG{p}{]}
        \PYG{n}{ytrain} \PYG{o}{=} \PYG{n}{y}\PYG{p}{[}\PYG{n}{train\PYGZus{}inds}\PYG{p}{]}

        \PYG{n}{xtest} \PYG{o}{=} \PYG{n}{x}\PYG{p}{[}\PYG{n}{test\PYGZus{}inds}\PYG{p}{]}
        \PYG{n}{ytest} \PYG{o}{=} \PYG{n}{y}\PYG{p}{[}\PYG{n}{test\PYGZus{}inds}\PYG{p}{]}

        \PYG{n}{Xtrain} \PYG{o}{=} \PYG{n}{poly}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{xtrain}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}
        \PYG{n}{ridge}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{Xtrain}\PYG{p}{,} \PYG{n}{ytrain}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}

        \PYG{n}{Xtest} \PYG{o}{=} \PYG{n}{poly}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{xtest}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}
        \PYG{n}{ypred} \PYG{o}{=} \PYG{n}{ridge}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{Xtest}\PYG{p}{)}

        \PYG{n}{scores\PYGZus{}KFold}\PYG{p}{[}\PYG{n}{i}\PYG{p}{,}\PYG{n}{j}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{ypred} \PYG{o}{\PYGZhy{}} \PYG{n}{ytest}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{n}{np}\PYG{o}{.}\PYG{n}{size}\PYG{p}{(}\PYG{n}{ypred}\PYG{p}{)}

        \PYG{n}{j} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1}
    \PYG{n}{i} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1}


\PYG{n}{estimated\PYGZus{}mse\PYGZus{}KFold} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{scores\PYGZus{}KFold}\PYG{p}{,} \PYG{n}{axis} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{}\PYGZsh{} Cross\PYGZhy{}validation using cross\PYGZus{}val\PYGZus{}score from sklearn along with KFold}

\PYG{c+c1}{\PYGZsh{} kfold is an instance initialized above as:}
\PYG{c+c1}{\PYGZsh{} kfold = KFold(n\PYGZus{}splits = k)}

\PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{nlambdas}\PYG{p}{)}
\PYG{n}{i} \PYG{o}{=} \PYG{l+m+mi}{0}
\PYG{k}{for} \PYG{n}{lmb} \PYG{o+ow}{in} \PYG{n}{lambdas}\PYG{p}{:}
    \PYG{n}{ridge} \PYG{o}{=} \PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha} \PYG{o}{=} \PYG{n}{lmb}\PYG{p}{)}

    \PYG{n}{X} \PYG{o}{=} \PYG{n}{poly}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}
    \PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{ridge}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{n}{kfold}\PYG{p}{)}

    \PYG{c+c1}{\PYGZsh{} cross\PYGZus{}val\PYGZus{}score return an array containing the estimated negative mse for every fold.}
    \PYG{c+c1}{\PYGZsh{} we have to the the mean of every array in order to get an estimate of the mse of the model}
    \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds}\PYG{p}{)}

    \PYG{n}{i} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1}

\PYG{c+c1}{\PYGZsh{}\PYGZsh{} Plot and compare the slightly different ways to perform cross\PYGZhy{}validation}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{,} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{cross\PYGZus{}val\PYGZus{}score}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{,} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}KFold}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r\PYGZhy{}\PYGZhy{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{KFold}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10(lambda)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{mse}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{The bias\sphinxhyphen{}variance tradeoff}
\label{\detokenize{chapter4:the-bias-variance-tradeoff}}
We will discuss the bias\sphinxhyphen{}variance tradeoff in the context of
continuous predictions such as regression. However, many of the
intuitions and ideas discussed here also carry over to classification
tasks. Consider a dataset \(\mathcal{L}\) consisting of the data
\(\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}\).

Let us assume that the true data is generated from a noisy model
\begin{equation*}
\begin{split}
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}
\end{split}
\end{equation*}
where \(\epsilon\) is normally distributed with mean zero and standard deviation \(\sigma^2\).

In our derivation of the ordinary least squares method we defined then
an approximation to the function \(f\) in terms of the parameters
\(\boldsymbol{\beta}\) and the design matrix \(\boldsymbol{X}\) which embody our model,
that is \(\boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta}\).

Thereafter we found the parameters \(\boldsymbol{\beta}\) by optimizing the means squared error via the so\sphinxhyphen{}called cost function
\begin{equation*}
\begin{split}
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
\end{split}
\end{equation*}
We can rewrite this as
\begin{equation*}
\begin{split}
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
\end{split}
\end{equation*}
The three terms represent the square of the bias of the learning
method, which can be thought of as the error caused by the simplifying
assumptions built into the method. The second term represents the
variance of the chosen model and finally the last terms is variance of
the error \(\boldsymbol{\epsilon}\).

To derive this equation, we need to recall that the variance of \(\boldsymbol{y}\) and \(\boldsymbol{\epsilon}\) are both equal to \(\sigma^2\). The mean value of \(\boldsymbol{\epsilon}\) is by definition equal to zero. Furthermore, the function \(f\) is not a stochastics variable, idem for \(\boldsymbol{\tilde{y}}\).
We use a more compact notation in terms of the expectation value
\begin{equation*}
\begin{split}
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}})^2\right],
\end{split}
\end{equation*}
and adding and subtracting \(\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\) we get
\begin{equation*}
\begin{split}
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}}+\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right],
\end{split}
\end{equation*}
which, using the abovementioned expectation values can be rewritten as
\begin{equation*}
\begin{split}
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right]+\mathrm{Var}\left[\boldsymbol{\tilde{y}}\right]+\sigma^2,
\end{split}
\end{equation*}
that is the rewriting in terms of the so\sphinxhyphen{}called bias, the variance of the model \(\boldsymbol{\tilde{y}}\) and the variance of \(\boldsymbol{\epsilon}\).


\subsection{Example code for Bias\sphinxhyphen{}Variance tradeoff}
\label{\detokenize{chapter4:example-code-for-bias-variance-tradeoff}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}pipeline}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{utils} \PYG{k+kn}{import} \PYG{n}{resample}

\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{2018}\PYG{p}{)}

\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{500}
\PYG{n}{n\PYGZus{}boostraps} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{degree} \PYG{o}{=} \PYG{l+m+mi}{18}  \PYG{c+c1}{\PYGZsh{} A quite high value, just to show.}
\PYG{n}{noise} \PYG{o}{=} \PYG{l+m+mf}{0.1}

\PYG{c+c1}{\PYGZsh{} Make data set.}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{0.1}\PYG{p}{,} \PYG{n}{x}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Hold out some test data that is never used in training.}
\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Combine x transformation and model into one operation.}
\PYG{c+c1}{\PYGZsh{} Not neccesary, but convenient.}
\PYG{n}{model} \PYG{o}{=} \PYG{n}{make\PYGZus{}pipeline}\PYG{p}{(}\PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{=}\PYG{n}{degree}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{n}{fit\PYGZus{}intercept}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} The following (m x n\PYGZus{}bootstraps) matrix holds the column vectors y\PYGZus{}pred}
\PYG{c+c1}{\PYGZsh{} for each bootstrap iteration.}
\PYG{n}{y\PYGZus{}pred} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{empty}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{)}
\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}} \PYG{o}{=} \PYG{n}{resample}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}

    \PYG{c+c1}{\PYGZsh{} Evaluate the new model on the same test data each time.}
    \PYG{n}{y\PYGZus{}pred}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Note: Expectations and variances taken w.r.t. different training}
\PYG{c+c1}{\PYGZsh{} data sets, hence the axis=1. Subsequent means are taken across the test data}
\PYG{c+c1}{\PYGZsh{} set in order to obtain a total value, but before this we have error/bias/variance}
\PYG{c+c1}{\PYGZsh{} calculated per data point in the test set.}
\PYG{c+c1}{\PYGZsh{} Note 2: The use of keepdims=True is important in the calculation of bias as this }
\PYG{c+c1}{\PYGZsh{} maintains the column vector form. Dropping this yields very unexpected results.}
\PYG{n}{error} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{y\PYGZus{}pred}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{p}{)}
\PYG{n}{bias} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{p}{)}
\PYG{n}{variance} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{var}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True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}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{f(x)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}test}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Data points}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Pred}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{Understanding what happens}
\label{\detokenize{chapter4:understanding-what-happens}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}pipeline}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{utils} \PYG{k+kn}{import} \PYG{n}{resample}

\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{2018}\PYG{p}{)}

\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{400}
\PYG{n}{n\PYGZus{}boostraps} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{maxdegree} \PYG{o}{=} \PYG{l+m+mi}{30}


\PYG{c+c1}{\PYGZsh{} Make data set.}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{0.1}\PYG{p}{,} \PYG{n}{x}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}
\PYG{n}{error} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)}
\PYG{n}{bias} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)}
\PYG{n}{variance} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)}
\PYG{n}{polydegree} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)}
\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)}

\PYG{k}{for} \PYG{n}{degree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{model} \PYG{o}{=} \PYG{n}{make\PYGZus{}pipeline}\PYG{p}{(}\PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{=}\PYG{n}{degree}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{n}{fit\PYGZus{}intercept}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{)}
    \PYG{n}{y\PYGZus{}pred} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{empty}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{)}
    \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}} \PYG{o}{=} \PYG{n}{resample}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}
        \PYG{n}{y\PYGZus{}pred}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}

    \PYG{n}{polydegree}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{degree}
    \PYG{n}{error}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{y\PYGZus{}pred}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{p}{)}
    \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{p}{)}
    \PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{var}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Polynomial degree:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{degree}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Error:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{error}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Bias\PYGZca{}2:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Var:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ \PYGZgt{}= }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ + }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ = }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{error}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{o}{+}\PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{,} \PYG{n}{error}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{bias}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
Polynomial degree: 0
Error: 0.2937910450030775
Bias\PYGZca{}2: 0.2929212799917661
Var: 0.0008697650113114119
0.2937910450030775 \PYGZgt{}= 0.2929212799917661 + 0.0008697650113114119 = 0.2937910450030775
Polynomial degree: 1
Error: 0.06894146856540674
Bias\PYGZca{}2: 0.06832043024896824
Var: 0.0006210383164384989
0.06894146856540674 \PYGZgt{}= 0.06832043024896824 + 0.0006210383164384989 = 0.06894146856540674
Polynomial degree: 2
Error: 0.06106765054837855
Bias\PYGZca{}2: 0.060547654220995305
Var: 0.0005199963273832372
0.06106765054837855 \PYGZgt{}= 0.060547654220995305 + 0.0005199963273832372 = 0.061067650548378545
Polynomial degree: 3
Error: 0.03346202229536659
Bias\PYGZca{}2: 0.0331409564680546
Var: 0.00032106582731199456
0.03346202229536659 \PYGZgt{}= 0.0331409564680546 + 0.00032106582731199456 = 0.03346202229536659
Polynomial degree: 4
Error: 0.0335277871704832
Bias\PYGZca{}2: 0.03311607538577367
Var: 0.0004117117847095335
0.0335277871704832 \PYGZgt{}= 0.03311607538577367 + 0.0004117117847095335 = 0.03352778717048321
Polynomial degree: 5
Error: 0.025517151530854786
Bias\PYGZca{}2: 0.024968890209256463
Var: 0.0005482613215983259
0.025517151530854786 \PYGZgt{}= 0.024968890209256463 + 0.0005482613215983259 = 0.02551715153085479
Polynomial degree: 6
Error: 0.01994607606842793
Bias\PYGZca{}2: 0.019502076889868637
Var: 0.00044399917855929527
0.01994607606842793 \PYGZgt{}= 0.019502076889868637 + 0.00044399917855929527 = 0.019946076068427934
Polynomial degree: 7
Error: 0.018695928655417676
Bias\PYGZca{}2: 0.01797984009000237
Var: 0.0007160885654153078
0.018695928655417676 \PYGZgt{}= 0.01797984009000237 + 0.0007160885654153078 = 0.01869592865541768
Polynomial degree: 8
Error: 0.010736105188369479
Bias\PYGZca{}2: 0.010376602508045063
Var: 0.00035950268032441344
0.010736105188369479 \PYGZgt{}= 0.010376602508045063 + 0.00035950268032441344 = 0.010736105188369477
Polynomial degree: 9
Error: 0.01101329065273084
Bias\PYGZca{}2: 0.010539027867197629
Var: 0.0004742627855332104
0.01101329065273084 \PYGZgt{}= 0.010539027867197629 + 0.0004742627855332104 = 0.01101329065273084
Polynomial degree: 10
Error: 0.010972468815261078
Bias\PYGZca{}2: 0.010593565969983903
Var: 0.00037890284527716995
0.010972468815261078 \PYGZgt{}= 0.010593565969983903 + 0.00037890284527716995 = 0.010972468815261073
Polynomial degree: 11
Error: 0.01084055593776807
Bias\PYGZca{}2: 0.010348475861989281
Var: 0.0004920800757787882
0.01084055593776807 \PYGZgt{}= 0.010348475861989281 + 0.0004920800757787882 = 0.01084055593776807
Polynomial degree: 12
Error: 0.010192472149429362
Bias\PYGZca{}2: 0.009610568640072627
Var: 0.0005819035093567355
0.010192472149429362 \PYGZgt{}= 0.009610568640072627 + 0.0005819035093567355 = 0.010192472149429362
Polynomial degree: 13
Error: 0.010312285920590011
Bias\PYGZca{}2: 0.009802534263801815
Var: 0.0005097516567881938
0.010312285920590011 \PYGZgt{}= 0.009802534263801815 + 0.0005097516567881938 = 0.01031228592059001
Polynomial degree: 14
Error: 0.010722455299595876
Bias\PYGZca{}2: 0.01008891676024437
Var: 0.0006335385393515036
0.010722455299595876 \PYGZgt{}= 0.01008891676024437 + 0.0006335385393515036 = 0.010722455299595875
Polynomial degree: 15
Error: 0.011155437503231998
Bias\PYGZca{}2: 0.010311761228670724
Var: 0.0008436762745612778
0.011155437503231998 \PYGZgt{}= 0.010311761228670724 + 0.0008436762745612778 = 0.011155437503232002
Polynomial degree: 16
Error: 0.011028026782676708
Bias\PYGZca{}2: 0.010223572382311492
Var: 0.0008044544003652116
0.011028026782676708 \PYGZgt{}= 0.010223572382311492 + 0.0008044544003652116 = 0.011028026782676703
Polynomial degree: 17
Error: 0.011628743129658555
Bias\PYGZca{}2: 0.010533948734129592
Var: 0.001094794395528961
0.011628743129658555 \PYGZgt{}= 0.010533948734129592 + 0.001094794395528961 = 0.011628743129658553
Polynomial degree: 18
Error: 0.014371682171531027
Bias\PYGZca{}2: 0.010922362242870073
Var: 0.0034493199286609573
0.014371682171531027 \PYGZgt{}= 0.010922362242870073 + 0.0034493199286609573 = 0.01437168217153103
Polynomial degree: 19
Error: 0.026986306199342624
Bias\PYGZca{}2: 0.01214176442858653
Var: 0.014844541770756087
0.026986306199342624 \PYGZgt{}= 0.01214176442858653 + 0.014844541770756087 = 0.026986306199342617
Polynomial degree: 20
Error: 0.012249244024160728
Bias\PYGZca{}2: 0.01006785246285396
Var: 0.002181391561306766
0.012249244024160728 \PYGZgt{}= 0.01006785246285396 + 0.002181391561306766 = 0.012249244024160727
Polynomial degree: 21
Error: 0.014973172820830053
Bias\PYGZca{}2: 0.010154371176360328
Var: 0.00481880164446972
0.014973172820830053 \PYGZgt{}= 0.010154371176360328 + 0.00481880164446972 = 0.014973172820830048
Polynomial degree: 22
Error: 0.014186606932681737
Bias\PYGZca{}2: 0.009594131981212376
Var: 0.0045924749514693625
0.014186606932681737 \PYGZgt{}= 0.009594131981212376 + 0.0045924749514693625 = 0.014186606932681738
Polynomial degree: 23
Error: 0.025574552577788824
Bias\PYGZca{}2: 0.009477519033249752
Var: 0.016097033544539077
0.025574552577788824 \PYGZgt{}= 0.009477519033249752 + 0.016097033544539077 = 0.02557455257778883
Polynomial degree: 24
Error: 0.03147298632679604
Bias\PYGZca{}2: 0.009565267585507206
Var: 0.021907718741288846
0.03147298632679604 \PYGZgt{}= 0.009565267585507206 + 0.021907718741288846 = 0.03147298632679605
Polynomial degree: 25
Error: 0.03929027799369515
Bias\PYGZca{}2: 0.009776269005896726
Var: 0.029514008987798424
0.03929027799369515 \PYGZgt{}= 0.009776269005896726 + 0.029514008987798424 = 0.03929027799369515
Polynomial degree: 26
Error: 0.15813256009613183
Bias\PYGZca{}2: 0.013239726753028333
Var: 0.14489283334310352
0.15813256009613183 \PYGZgt{}= 0.013239726753028333 + 0.14489283334310352 = 0.15813256009613186
Polynomial degree: 27
Error: 0.1360840943498259
Bias\PYGZca{}2: 0.01326608592145169
Var: 0.12281800842837416
0.1360840943498259 \PYGZgt{}= 0.01326608592145169 + 0.12281800842837416 = 0.13608409434982585
Polynomial degree: 28
Error: 0.7210723692205014
Bias\PYGZca{}2: 0.04436186918146108
Var: 0.6767105000390408
0.7210723692205014 \PYGZgt{}= 0.04436186918146108 + 0.6767105000390408 = 0.7210723692205019
Polynomial degree: 29
Error: 0.48454430745837984
Bias\PYGZca{}2: 0.011809368338879722
Var: 0.4727349391195001
0.48454430745837984 \PYGZgt{}= 0.011809368338879722 + 0.4727349391195001 = 0.48454430745837984
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter4_300_1}.png}




\subsection{Summing up}
\label{\detokenize{chapter4:summing-up}}
The bias\sphinxhyphen{}variance tradeoff summarizes the fundamental tension in
machine learning, particularly supervised learning, between the
complexity of a model and the amount of training data needed to train
it.  Since data is often limited, in practice it is often useful to
use a less\sphinxhyphen{}complex model with higher bias, that is  a model whose asymptotic
performance is worse than another model because it is easier to
train and less sensitive to sampling noise arising from having a
finite\sphinxhyphen{}sized training dataset (smaller variance).

The above equations tell us that in
order to minimize the expected test error, we need to select a
statistical learning method that simultaneously achieves low variance
and low bias. Note that variance is inherently a nonnegative quantity,
and squared bias is also nonnegative. Hence, we see that the expected
test MSE can never lie below \(Var(\epsilon)\), the irreducible error.

What do we mean by the variance and bias of a statistical learning
method? The variance refers to the amount by which our model would change if we
estimated it using a different training data set. Since the training
data are used to fit the statistical learning method, different
training data sets  will result in a different estimate. But ideally the
estimate for our model should not vary too much between training
sets. However, if a method has high variance  then small changes in
the training data can result in large changes in the model. In general, more
flexible statistical methods have higher variance.

You may also find this recent \sphinxhref{https://www.pnas.org/content/116/32/15849}{article} of interest.


\subsection{Another Example from Scikit\sphinxhyphen{}Learn’s Repository}
\label{\detokenize{chapter4:another-example-from-scikit-learn-s-repository}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}}
\PYG{l+s+sd}{============================}
\PYG{l+s+sd}{Underfitting vs. Overfitting}
\PYG{l+s+sd}{============================}

\PYG{l+s+sd}{This example demonstrates the problems of underfitting and overfitting and}
\PYG{l+s+sd}{how we can use linear regression with polynomial features to approximate}
\PYG{l+s+sd}{nonlinear functions. The plot shows the function that we want to approximate,}
\PYG{l+s+sd}{which is a part of the cosine function. In addition, the samples from the}
\PYG{l+s+sd}{real function and the approximations of different models are displayed. The}
\PYG{l+s+sd}{models have polynomial features of different degrees. We can see that a}
\PYG{l+s+sd}{linear function (polynomial with degree 1) is not sufficient to fit the}
\PYG{l+s+sd}{training samples. This is called **underfitting**. A polynomial of degree 4}
\PYG{l+s+sd}{approximates the true function almost perfectly. However, for higher degrees}
\PYG{l+s+sd}{the model will **overfit** the training data, i.e. it learns the noise of the}
\PYG{l+s+sd}{training data.}
\PYG{l+s+sd}{We evaluate quantitatively **overfitting** / **underfitting** by using}
\PYG{l+s+sd}{cross\PYGZhy{}validation. We calculate the mean squared error (MSE) on the validation}
\PYG{l+s+sd}{set, the higher, the less likely the model generalizes correctly from the}
\PYG{l+s+sd}{training data.}
\PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}}

\PYG{n+nb}{print}\PYG{p}{(}\PYG{n+nv+vm}{\PYGZus{}\PYGZus{}doc\PYGZus{}\PYGZus{}}\PYG{p}{)}

\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{Pipeline}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}


\PYG{k}{def} \PYG{n+nf}{true\PYGZus{}fun}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{pi} \PYG{o}{*} \PYG{n}{X}\PYG{p}{)}

\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}

\PYG{n}{n\PYGZus{}samples} \PYG{o}{=} \PYG{l+m+mi}{30}
\PYG{n}{degrees} \PYG{o}{=} \PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{15}\PYG{p}{]}

\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{n}{n\PYGZus{}samples}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{true\PYGZus{}fun}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{n\PYGZus{}samples}\PYG{p}{)} \PYG{o}{*} \PYG{l+m+mf}{0.1}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{14}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)}
\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{degrees}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{ax} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplot}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{degrees}\PYG{p}{)}\PYG{p}{,} \PYG{n}{i} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{setp}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{n}{xticks}\PYG{o}{=}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{yticks}\PYG{o}{=}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}

    \PYG{n}{polynomial\PYGZus{}features} \PYG{o}{=} \PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{=}\PYG{n}{degrees}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,}
                                             \PYG{n}{include\PYGZus{}bias}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}
    \PYG{n}{linear\PYGZus{}regression} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}
    \PYG{n}{pipeline} \PYG{o}{=} \PYG{n}{Pipeline}\PYG{p}{(}\PYG{p}{[}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{polynomial\PYGZus{}features}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{polynomial\PYGZus{}features}\PYG{p}{)}\PYG{p}{,}
                         \PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{linear\PYGZus{}regression}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{linear\PYGZus{}regression}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)}
    \PYG{n}{pipeline}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}

    \PYG{c+c1}{\PYGZsh{} Evaluate the models using crossvalidation}
    \PYG{n}{scores} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{pipeline}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,}
                             \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{l+m+mi}{10}\PYG{p}{)}

    \PYG{n}{X\PYGZus{}test} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{100}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{pipeline}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Model}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\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}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{best}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Degree }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{MSE = }\PYG{l+s+si}{\PYGZob{}:.2e\PYGZcb{}}\PYG{l+s+s2}{(+/\PYGZhy{} }\PYG{l+s+si}{\PYGZob{}:.2e\PYGZcb{}}\PYG{l+s+s2}{)}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}
        \PYG{n}{degrees}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{scores}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{scores}\PYG{o}{.}\PYG{n}{std}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{More examples on bootstrap and cross\sphinxhyphen{}validation and errors}
\label{\detokenize{chapter4:more-examples-on-bootstrap-and-cross-validation-and-errors}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Common imports}
\PYG{k+kn}{import} \PYG{n+nn}{os}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{utils} \PYG{k+kn}{import} \PYG{n}{resample}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}
\PYG{c+c1}{\PYGZsh{} Where to save the figures and data files}
\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{EoS.csv}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Read the EoS data as  csv file and organize the data into two arrays with density and energies}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}csv}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{EoS}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{Energies} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Density} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{c+c1}{\PYGZsh{}  The design matrix now as function of various polytrops}

\PYG{n}{Maxpolydegree} \PYG{o}{=} \PYG{l+m+mi}{30}
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{Density}\PYG{p}{)}\PYG{p}{,}\PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{1.0}
\PYG{n}{testerror} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)}
\PYG{n}{trainingerror} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)}
\PYG{n}{polynomial} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)}

\PYG{n}{trials} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{k}{for} \PYG{n}{polydegree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{polynomial}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{polydegree}
    \PYG{k}{for} \PYG{n}{degree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} loop over trials in order to estimate the expectation value of the MSE}
    \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{0.0}
    \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{0.0}
    \PYG{k}{for} \PYG{n}{samples} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{trials}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)}
        \PYG{n}{model} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{n}{fit\PYGZus{}intercept}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}
        \PYG{n}{ypred} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{)}
        \PYG{n}{ytilde} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{)}
        \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{+}\PYG{o}{=} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{p}{,} \PYG{n}{ytilde}\PYG{p}{)}
        \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{+}\PYG{o}{=} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{ypred}\PYG{p}{)} 

    \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{/}\PYG{o}{=} \PYG{n}{trials}
    \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{/}\PYG{o}{=} \PYG{n}{trials}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Degree of polynomial: }\PYG{l+s+si}{\PYGZpc{}3d}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{\PYGZpc{}} \PYG{n}{polynomial}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error on training data: }\PYG{l+s+si}{\PYGZpc{}.8f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]}\PYG{p}{)}
    \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error on test data: }\PYG{l+s+si}{\PYGZpc{}.8f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polynomial}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{trainingerror}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Training Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polynomial}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{testerror}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Test Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Polynomial degree}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10[MSE]}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}




\subsection{The same example but now with cross\sphinxhyphen{}validation}
\label{\detokenize{chapter4:the-same-example-but-now-with-cross-validation}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{c+c1}{\PYGZsh{} Common imports}
\PYG{k+kn}{import} \PYG{n+nn}{os}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{KFold}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}


\PYG{c+c1}{\PYGZsh{} Where to save the figures and data files}
\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{EoS.csv}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Read the EoS data as  csv file and organize the data into two arrays with density and energies}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}csv}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{EoS} \PYG{o}{=} \PYG{n}{EoS}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{Energies} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{Density} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{c+c1}{\PYGZsh{}  The design matrix now as function of various polytrops}

\PYG{n}{Maxpolydegree} \PYG{o}{=} \PYG{l+m+mi}{30}
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{Density}\PYG{p}{)}\PYG{p}{,}\PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{1.0}
\PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)}
\PYG{n}{polynomial} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)}
\PYG{n}{k} \PYG{o}{=}\PYG{l+m+mi}{5}
\PYG{n}{kfold} \PYG{o}{=} \PYG{n}{KFold}\PYG{p}{(}\PYG{n}{n\PYGZus{}splits} \PYG{o}{=} \PYG{n}{k}\PYG{p}{)}

\PYG{k}{for} \PYG{n}{polydegree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{polynomial}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{polydegree}
    \PYG{k}{for} \PYG{n}{degree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{)}\PYG{p}{:}
        \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)}
        \PYG{n}{OLS} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} loop over trials in order to estimate the expectation value of the MSE}
    \PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{OLS}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{n}{kfold}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{}[:, np.newaxis]}
    \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polynomial}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Test Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Polynomial degree}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10[MSE]}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{Cross\sphinxhyphen{}validation with Ridge}
\label{\detokenize{chapter4:cross-validation-with-ridge}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{KFold}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{Ridge}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures}

\PYG{c+c1}{\PYGZsh{} A seed just to ensure that the random numbers are the same for every run.}
\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{3155}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Generate the data.}
\PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100}
\PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{0.1}\PYG{p}{,} \PYG{n}{x}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Decide degree on polynomial to fit}
\PYG{n}{poly} \PYG{o}{=} \PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree} \PYG{o}{=} \PYG{l+m+mi}{10}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Decide which values of lambda to use}
\PYG{n}{nlambdas} \PYG{o}{=} \PYG{l+m+mi}{500}
\PYG{n}{lambdas} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{nlambdas}\PYG{p}{)}
\PYG{c+c1}{\PYGZsh{} Initialize a KFold instance}
\PYG{n}{k} \PYG{o}{=} \PYG{l+m+mi}{5}
\PYG{n}{kfold} \PYG{o}{=} \PYG{n}{KFold}\PYG{p}{(}\PYG{n}{n\PYGZus{}splits} \PYG{o}{=} \PYG{n}{k}\PYG{p}{)}
\PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{nlambdas}\PYG{p}{)}
\PYG{n}{i} \PYG{o}{=} \PYG{l+m+mi}{0}
\PYG{k}{for} \PYG{n}{lmb} \PYG{o+ow}{in} \PYG{n}{lambdas}\PYG{p}{:}
    \PYG{n}{ridge} \PYG{o}{=} \PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha} \PYG{o}{=} \PYG{n}{lmb}\PYG{p}{)}
    \PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{ridge}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{n}{kfold}\PYG{p}{)}
    \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds}\PYG{p}{)}
    \PYG{n}{i} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{,} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{cross\PYGZus{}val\PYGZus{}score}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10(lambda)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{MSE}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{The Ising model}
\label{\detokenize{chapter4:the-ising-model}}
The one\sphinxhyphen{}dimensional Ising model with nearest neighbor interaction, no
external field and a constant coupling constant \(J\) is given by




\begin{equation*}
\begin{split}
\begin{equation}
    H = -J \sum_{k}^L s_k s_{k + 1},
\label{_auto2} \tag{2}
\end{equation}
\end{split}
\end{equation*}
where \(s_i \in \{-1, 1\}\) and \(s_{N + 1} = s_1\). The number of spins
in the system is determined by \(L\). For the one\sphinxhyphen{}dimensional system
there is no phase transition.

We will look at a system of \(L = 40\) spins with a coupling constant of
\(J = 1\). To get enough training data we will generate 10000 states
with their respective energies.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{mpl\PYGZus{}toolkits}\PYG{n+nn}{.}\PYG{n+nn}{axes\PYGZus{}grid1} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}axes\PYGZus{}locatable}
\PYG{k+kn}{import} \PYG{n+nn}{seaborn} \PYG{k}{as} \PYG{n+nn}{sns}
\PYG{k+kn}{import} \PYG{n+nn}{scipy}\PYG{n+nn}{.}\PYG{n+nn}{linalg} \PYG{k}{as} \PYG{n+nn}{scl}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{import} \PYG{n+nn}{tqdm}
\PYG{n}{sns}\PYG{o}{.}\PYG{n}{set}\PYG{p}{(}\PYG{n}{color\PYGZus{}codes}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}
\PYG{n}{cmap\PYGZus{}args}\PYG{o}{=}\PYG{n+nb}{dict}\PYG{p}{(}\PYG{n}{vmin}\PYG{o}{=}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{vmax}\PYG{o}{=}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{seismic}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{L} \PYG{o}{=} \PYG{l+m+mi}{40}
\PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{int}\PYG{p}{(}\PYG{l+m+mf}{1e4}\PYG{p}{)}

\PYG{n}{spins} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{choice}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{J} \PYG{o}{=} \PYG{l+m+mf}{1.0}

\PYG{n}{energies} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}

\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{energies}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{o}{\PYGZhy{}} \PYG{n}{J} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{roll}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

Here we use ordinary least squares
regression to predict the energy for the nearest neighbor
one\sphinxhyphen{}dimensional Ising model on a ring, i.e., the endpoints wrap
around. We will use linear regression to fit a value for
the coupling constant to achieve this.


\subsection{Reformulating the problem to suit regression}
\label{\detokenize{chapter4:reformulating-the-problem-to-suit-regression}}
A more general form for the one\sphinxhyphen{}dimensional Ising model is




\begin{equation*}
\begin{split}
\begin{equation}
    H = - \sum_j^L \sum_k^L s_j s_k J_{jk}.
\label{_auto3} \tag{3}
\end{equation}
\end{split}
\end{equation*}
Here we allow for interactions beyond the nearest neighbors and a state dependent
coupling constant. This latter expression can be formulated as
a matrix\sphinxhyphen{}product




\begin{equation*}
\begin{split}
\begin{equation}
    \boldsymbol{H} = \boldsymbol{X} J,
\label{_auto4} \tag{4}
\end{equation}
\end{split}
\end{equation*}
where \(X_{jk} = s_j s_k\) and \(J\) is a matrix which consists of the
elements \(-J_{jk}\). This form of writing the energy fits perfectly
with the form utilized in linear regression, that is




\begin{equation*}
\begin{split}
\begin{equation}
    \boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{\epsilon},
\label{_auto5} \tag{5}
\end{equation}
\end{split}
\end{equation*}
We split the data in training and test data as discussed in the previous example

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}
\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{X}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{outer}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{energies}
\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{Linear regression}
\label{\detokenize{chapter4:linear-regression}}
In the ordinary least squares method we choose the cost function




\begin{equation*}
\begin{split}
\begin{equation}
    C(\boldsymbol{X}, \boldsymbol{\beta})= \frac{1}{n}\left\{(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})\right\}.
\label{_auto6} \tag{6}
\end{equation}
\end{split}
\end{equation*}
We then find the extremal point of \(C\) by taking the derivative with respect to \(\boldsymbol{\beta}\) as discussed above.
This yields the expression for \(\boldsymbol{\beta}\) to be
\begin{equation*}
\begin{split}
\boldsymbol{\beta} = \frac{\boldsymbol{X}^T \boldsymbol{y}}{\boldsymbol{X}^T \boldsymbol{X}},
\end{split}
\end{equation*}
which immediately imposes some requirements on \(\boldsymbol{X}\) as there must exist
an inverse of \(\boldsymbol{X}^T \boldsymbol{X}\). If the expression we are modeling contains an
intercept, i.e., a constant term, we must make sure that the
first column of \(\boldsymbol{X}\) consists of \(1\). We do this here

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{X\PYGZus{}train\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(}
    \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{,}
    \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}
\PYG{p}{)}
\PYG{n}{X\PYGZus{}test\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(}
    \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{,}
    \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}
\PYG{p}{)}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k}{def} \PYG{n+nf}{ols\PYGZus{}inv}\PYG{p}{(}\PYG{n}{x}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{)} \PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZgt{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{:}
    \PYG{k}{return} \PYG{n}{scl}\PYG{o}{.}\PYG{n}{inv}\PYG{p}{(}\PYG{n}{x}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{x}\PYG{p}{)} \PYG{o}{@} \PYG{p}{(}\PYG{n}{x}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{y}\PYG{p}{)}
\PYG{n}{beta} \PYG{o}{=} \PYG{n}{ols\PYGZus{}inv}\PYG{p}{(}\PYG{n}{X\PYGZus{}train\PYGZus{}own}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{Singular Value decomposition}
\label{\detokenize{chapter4:singular-value-decomposition}}
Doing the inversion directly turns out to be a bad idea since the matrix
\(\boldsymbol{X}^T\boldsymbol{X}\) is singular. An alternative approach is to use the \sphinxstylestrong{singular
value decomposition}. Using the definition of the Moore\sphinxhyphen{}Penrose
pseudoinverse we can write the equation for \(\boldsymbol{\beta}\) as
\begin{equation*}
\begin{split}
\boldsymbol{\beta} = \boldsymbol{X}^{+}\boldsymbol{y},
\end{split}
\end{equation*}
where the pseudoinverse of \(\boldsymbol{X}\) is given by
\begin{equation*}
\begin{split}
\boldsymbol{X}^{+} = \frac{\boldsymbol{X}^T}{\boldsymbol{X}^T\boldsymbol{X}}.
\end{split}
\end{equation*}
Using singular value decomposition we can decompose the matrix  \(\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma} \boldsymbol{V}^T\),
where \(\boldsymbol{U}\) and \(\boldsymbol{V}\) are orthogonal(unitary) matrices and \(\boldsymbol{\Sigma}\) contains the singular values (more details below).
where \(X^{+} = V\Sigma^{+} U^T\). This reduces the equation for
\(\omega\) to




\begin{equation*}
\begin{split}
\begin{equation}
    \boldsymbol{\beta} = \boldsymbol{V}\boldsymbol{\Sigma}^{+} \boldsymbol{U}^T \boldsymbol{y}.
\label{_auto7} \tag{7}
\end{equation}
\end{split}
\end{equation*}
Note that solving this equation by actually doing the pseudoinverse
(which is what we will do) is not a good idea as this operation scales
as \(\mathcal{O}(n^3)\), where \(n\) is the number of elements in a
general matrix. Instead, doing \(QR\)\sphinxhyphen{}factorization and solving the
linear system as an equation would reduce this down to
\(\mathcal{O}(n^2)\) operations.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k}{def} \PYG{n+nf}{ols\PYGZus{}svd}\PYG{p}{(}\PYG{n}{x}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{)} \PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZgt{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{:}
    \PYG{n}{u}\PYG{p}{,} \PYG{n}{s}\PYG{p}{,} \PYG{n}{v} \PYG{o}{=} \PYG{n}{scl}\PYG{o}{.}\PYG{n}{svd}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}
    \PYG{k}{return} \PYG{n}{v}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{scl}\PYG{o}{.}\PYG{n}{pinv}\PYG{p}{(}\PYG{n}{scl}\PYG{o}{.}\PYG{n}{diagsvd}\PYG{p}{(}\PYG{n}{s}\PYG{p}{,} \PYG{n}{u}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{v}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{o}{@} \PYG{n}{u}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{y}
\end{sphinxVerbatim}

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{beta} \PYG{o}{=} \PYG{n}{ols\PYGZus{}svd}\PYG{p}{(}\PYG{n}{X\PYGZus{}train\PYGZus{}own}\PYG{p}{,}\PYG{n}{y\PYGZus{}train}\PYG{p}{)}
\end{sphinxVerbatim}

When extracting the \(J\)\sphinxhyphen{}matrix  we need to make sure that we remove the intercept, as is done here

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{J} \PYG{o}{=} \PYG{n}{beta}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{:}\PYG{p}{]}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}
\end{sphinxVerbatim}

A way of looking at the coefficients in \(J\) is to plot the matrices as images.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{OLS}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)}
\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

It is interesting to note that OLS
considers both \(J_{j, j + 1} = -0.5\) and \(J_{j, j - 1} = -0.5\) as
valid matrix elements for \(J\).
In our discussion below on hyperparameters and Ridge and Lasso regression we will see that
this problem can be removed, partly and only with Lasso regression.

In this case our matrix inversion was actually possible. The obvious question now is what is the mathematics behind the SVD?


\subsection{The one\sphinxhyphen{}dimensional Ising model}
\label{\detokenize{chapter4:the-one-dimensional-ising-model}}
Let us bring back the Ising model again, but now with an additional
focus on Ridge and Lasso regression as well. We repeat some of the
basic parts of the Ising model and the setup of the training and test
data.  The one\sphinxhyphen{}dimensional Ising model with nearest neighbor
interaction, no external field and a constant coupling constant \(J\) is
given by




\begin{equation*}
\begin{split}
\begin{equation}
    H = -J \sum_{k}^L s_k s_{k + 1},
\label{_auto8} \tag{8}
\end{equation}
\end{split}
\end{equation*}
where \(s_i \in \{-1, 1\}\) and \(s_{N + 1} = s_1\). The number of spins in the system is determined by \(L\). For the one\sphinxhyphen{}dimensional system there is no phase transition.

We will look at a system of \(L = 40\) spins with a coupling constant of \(J = 1\). To get enough training data we will generate 10000 states with their respective energies.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}
\PYG{k+kn}{from} \PYG{n+nn}{mpl\PYGZus{}toolkits}\PYG{n+nn}{.}\PYG{n+nn}{axes\PYGZus{}grid1} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}axes\PYGZus{}locatable}
\PYG{k+kn}{import} \PYG{n+nn}{seaborn} \PYG{k}{as} \PYG{n+nn}{sns}
\PYG{k+kn}{import} \PYG{n+nn}{scipy}\PYG{n+nn}{.}\PYG{n+nn}{linalg} \PYG{k}{as} \PYG{n+nn}{scl}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{import} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k}{as} \PYG{n+nn}{skl}
\PYG{k+kn}{import} \PYG{n+nn}{tqdm}
\PYG{n}{sns}\PYG{o}{.}\PYG{n}{set}\PYG{p}{(}\PYG{n}{color\PYGZus{}codes}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}
\PYG{n}{cmap\PYGZus{}args}\PYG{o}{=}\PYG{n+nb}{dict}\PYG{p}{(}\PYG{n}{vmin}\PYG{o}{=}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{vmax}\PYG{o}{=}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{seismic}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{L} \PYG{o}{=} \PYG{l+m+mi}{40}
\PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{int}\PYG{p}{(}\PYG{l+m+mf}{1e4}\PYG{p}{)}

\PYG{n}{spins} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{choice}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{J} \PYG{o}{=} \PYG{l+m+mf}{1.0}

\PYG{n}{energies} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}

\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{energies}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{o}{\PYGZhy{}} \PYG{n}{J} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{roll}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)}
\end{sphinxVerbatim}

A more general form for the one\sphinxhyphen{}dimensional Ising model is




\begin{equation*}
\begin{split}
\begin{equation}
    H = - \sum_j^L \sum_k^L s_j s_k J_{jk}.
\label{_auto9} \tag{9}
\end{equation}
\end{split}
\end{equation*}
Here we allow for interactions beyond the nearest neighbors and a more
adaptive coupling matrix. This latter expression can be formulated as
a matrix\sphinxhyphen{}product on the form




\begin{equation*}
\begin{split}
\begin{equation}
    H = X J,
\label{_auto10} \tag{10}
\end{equation}
\end{split}
\end{equation*}
where \(X_{jk} = s_j s_k\) and \(J\) is the matrix consisting of the
elements \(-J_{jk}\). This form of writing the energy fits perfectly
with the form utilized in linear regression, viz.




\begin{equation*}
\begin{split}
\begin{equation}
    \boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{\epsilon}.
\label{_auto11} \tag{11}
\end{equation}
\end{split}
\end{equation*}
We organize the data as we did above

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}
\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{X}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{outer}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{energies}
\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.96}\PYG{p}{)}

\PYG{n}{X\PYGZus{}train\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(}
    \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{,}
    \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}
\PYG{p}{)}

\PYG{n}{X\PYGZus{}test\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(}
    \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{,}
    \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}
\PYG{p}{)}
\end{sphinxVerbatim}

We will do all fitting with \sphinxstylestrong{Scikit\sphinxhyphen{}Learn},

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{clf} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}
\end{sphinxVerbatim}

When  extracting the \(J\)\sphinxhyphen{}matrix we make sure to remove the intercept

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{J\PYGZus{}sk} \PYG{o}{=} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}
\end{sphinxVerbatim}

And then we plot the results

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J\PYGZus{}sk}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{LinearRegression from Scikit\PYGZhy{}learn}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)}
\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

The results perfectly with our previous discussion where we used our own code.


\subsection{Ridge regression}
\label{\detokenize{chapter4:ridge-regression}}
Having explored the ordinary least squares we move on to ridge
regression. In ridge regression we include a \sphinxstylestrong{regularizer}. This
involves a new cost function which leads to a new estimate for the
weights \(\boldsymbol{\beta}\). This results in a penalized regression problem. The
cost function is given by

1
3
6

\textless{}
\textless{}
\textless{}
!
!
M
A
T
H
\_
B
L
O
C
K

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{\PYGZus{}lambda} \PYG{o}{=} \PYG{l+m+mf}{0.1}
\PYG{n}{clf\PYGZus{}ridge} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}
\PYG{n}{J\PYGZus{}ridge\PYGZus{}sk} \PYG{o}{=} \PYG{n}{clf\PYGZus{}ridge}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}
\PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J\PYGZus{}ridge\PYGZus{}sk}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Ridge from Scikit\PYGZhy{}learn}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)}
\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{LASSO regression}
\label{\detokenize{chapter4:lasso-regression}}
In the \sphinxstylestrong{Least Absolute Shrinkage and Selection Operator} (LASSO)\sphinxhyphen{}method we get a third cost function.




\begin{equation*}
\begin{split}
\begin{equation}
    C(\boldsymbol{X}, \boldsymbol{\beta}; \lambda) = (\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y}) + \lambda \sqrt{\boldsymbol{\beta}^T\boldsymbol{\beta}}.
\label{_auto13} \tag{13}
\end{equation}
\end{split}
\end{equation*}
Finding the extremal point of this cost function is not so straight\sphinxhyphen{}forward as in least squares and ridge. We will therefore rely solely on the function \sphinxcode{\sphinxupquote{Lasso}} from \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{clf\PYGZus{}lasso} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Lasso}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}
\PYG{n}{J\PYGZus{}lasso\PYGZus{}sk} \PYG{o}{=} \PYG{n}{clf\PYGZus{}lasso}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}
\PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)}
\PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J\PYGZus{}lasso\PYGZus{}sk}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Lasso from Scikit\PYGZhy{}learn}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)}
\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

It is quite striking how LASSO breaks the symmetry of the coupling
constant as opposed to ridge and OLS. We get a sparse solution with
\(J_{j, j + 1} = -1\).


\subsection{Performance as  function of the regularization parameter}
\label{\detokenize{chapter4:performance-as-function-of-the-regularization-parameter}}
We see how the different models perform for a different set of values for \(\lambda\).

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{lambdas} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}

\PYG{n}{train\PYGZus{}errors} \PYG{o}{=} \PYG{p}{\PYGZob{}}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}
\PYG{p}{\PYGZcb{}}

\PYG{n}{test\PYGZus{}errors} \PYG{o}{=} \PYG{p}{\PYGZob{}}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}
\PYG{p}{\PYGZcb{}}

\PYG{n}{plot\PYGZus{}counter} \PYG{o}{=} \PYG{l+m+mi}{1}

\PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{32}\PYG{p}{,} \PYG{l+m+mi}{54}\PYG{p}{)}\PYG{p}{)}

\PYG{k}{for} \PYG{n}{i}\PYG{p}{,} \PYG{n}{\PYGZus{}lambda} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{tqdm}\PYG{o}{.}\PYG{n}{tqdm}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:}
    \PYG{k}{for} \PYG{n}{key}\PYG{p}{,} \PYG{n}{method} \PYG{o+ow}{in} \PYG{n+nb}{zip}\PYG{p}{(}
        \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,}
        \PYG{p}{[}\PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{p}{,} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Lasso}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{p}{]}
    \PYG{p}{)}\PYG{p}{:}
        \PYG{n}{method} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}

        \PYG{n}{train\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)}
        \PYG{n}{test\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}test}\PYG{p}{)}

        \PYG{n}{omega} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}

        \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplot}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{plot\PYGZus{}counter}\PYG{p}{)}
        \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{omega}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)}
        \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+s2}{, \PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{lambda = }\PYG{l+s+si}{\PYGZpc{}.4f}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{key}\PYG{p}{,} \PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{p}{)}
        \PYG{n}{plot\PYGZus{}counter} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

We see that LASSO reaches a good solution for low
values of \(\lambda\), but will “wither” when we increase \(\lambda\) too
much. Ridge is more stable over a larger range of values for
\(\lambda\), but eventually also fades away.


\subsection{Finding the optimal value of \protect\(\lambda\protect\)}
\label{\detokenize{chapter4:finding-the-optimal-value-of-lambda}}
To determine which value of \(\lambda\) is best we plot the accuracy of
the models when predicting the training and the testing set. We expect
the accuracy of the training set to be quite good, but if the accuracy
of the testing set is much lower this tells us that we might be
subject to an overfit model. The ideal scenario is an accuracy on the
testing set that is close to the accuracy of the training set.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)}

\PYG{n}{colors} \PYG{o}{=} \PYG{p}{\PYGZob{}}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{y}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,}
    \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{c}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{p}{\PYGZcb{}}

\PYG{k}{for} \PYG{n}{key} \PYG{o+ow}{in} \PYG{n}{train\PYGZus{}errors}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{semilogx}\PYG{p}{(}
        \PYG{n}{lambdas}\PYG{p}{,}
        \PYG{n}{train\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{,}
        \PYG{n}{colors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{,}
        \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Train }\PYG{l+s+si}{\PYGZob{}0\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{key}\PYG{p}{)}\PYG{p}{,}
        \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mf}{4.0}
    \PYG{p}{)}

\PYG{k}{for} \PYG{n}{key} \PYG{o+ow}{in} \PYG{n}{test\PYGZus{}errors}\PYG{p}{:}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{semilogx}\PYG{p}{(}
        \PYG{n}{lambdas}\PYG{p}{,}
        \PYG{n}{test\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{,}
        \PYG{n}{colors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZhy{}\PYGZhy{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,}
        \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Test }\PYG{l+s+si}{\PYGZob{}0\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{key}\PYG{p}{)}\PYG{p}{,}
        \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mf}{4.0}
    \PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{n}{loc}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{best}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{lambda\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}R\PYGZca{}2\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{tick\PYGZus{}params}\PYG{p}{(}\PYG{n}{labelsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)}
\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

From the above figure we can see that LASSO with \(\lambda = 10^{-2}\)
achieves a very good accuracy on the test set. This by far surpasses the
other models for all values of \(\lambda\).




\section{Data Analysis and Machine Learning: Logistic Regression}
\label{\detokenize{chapter5:data-analysis-and-machine-learning-logistic-regression}}\label{\detokenize{chapter5::doc}}




\sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

Date: \sphinxstylestrong{Oct 17, 2019}

Copyright 1999\sphinxhyphen{}2019, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license




\subsection{Logistic Regression}
\label{\detokenize{chapter5:logistic-regression}}
In linear regression our main interest was centered on learning the
coefficients of a functional fit (say a polynomial) in order to be
able to predict the response of a continuous variable on some unseen
data. The fit to the continuous variable \(y_i\) is based on some
independent variables \(\hat{x}_i\). Linear regression resulted in
analytical expressions for standard ordinary Least Squares or Ridge
regression (in terms of matrices to invert) for several quantities,
ranging from the variance and thereby the confidence intervals of the
parameters \(\hat{\beta}\) to the mean squared error. If we can invert
the product of the design matrices, linear regression gives then a
simple recipe for fitting our data.

Classification problems, however, are concerned with outcomes taking
the form of discrete variables (i.e. categories). We may for example,
on the basis of DNA sequencing for a number of patients, like to find
out which mutations are important for a certain disease; or based on
scans of various patients’ brains, figure out if there is a tumor or
not; or given a specific physical system, we’d like to identify its
state, say whether it is an ordered or disordered system (typical
situation in solid state physics); or classify the status of a
patient, whether she/he has a stroke or not and many other similar
situations.

The most common situation we encounter when we apply logistic
regression is that of two possible outcomes, normally denoted as a
binary outcome, true or false, positive or negative, success or
failure etc.


\subsection{Optimization and Deep learning}
\label{\detokenize{chapter5:optimization-and-deep-learning}}
Logistic regression will also serve as our stepping stone towards
neural network algorithms and supervised deep learning. For logistic
learning, the minimization of the cost function leads to a non\sphinxhyphen{}linear
equation in the parameters \(\hat{\beta}\). The optimization of the
problem calls therefore for minimization algorithms. This forms the
bottle neck of all machine learning algorithms, namely how to find
reliable minima of a multi\sphinxhyphen{}variable function. This leads us to the
family of gradient descent methods. The latter are the working horses
of basically all modern machine learning algorithms.

We note also that many of the topics discussed here on logistic
regression are also commonly used in modern supervised Deep Learning
models, as we will see later.




\subsection{Basics}
\label{\detokenize{chapter5:basics}}
We consider the case where the dependent variables, also called the
responses or the outcomes, \(y_i\) are discrete and only take values
from \(k=0,\dots,K-1\) (i.e. \(K\) classes).

The goal is to predict the
output classes from the design matrix \(\hat{X}\in\mathbb{R}^{n\times p}\)
made of \(n\) samples, each of which carries \(p\) features or predictors. The
primary goal is to identify the classes to which new unseen samples
belong.

Let us specialize to the case of two classes only, with outputs
\(y_i=0\) and \(y_i=1\). Our outcomes could represent the status of a
credit card user that could default or not on her/his credit card
debt. That is
\begin{equation*}
\begin{split}
y_i = \begin{bmatrix} 0 & \mathrm{no}\\  1 & \mathrm{yes} \end{bmatrix}.
\end{split}
\end{equation*}

\subsection{Linear classifier}
\label{\detokenize{chapter5:linear-classifier}}
Before moving to the logistic model, let us try to use our linear
regression model to classify these two outcomes. We could for example
fit a linear model to the default case if \(y_i > 0.5\) and the no
default case \(y_i \leq 0.5\).

We would then have our
weighted linear combination, namely




\begin{equation*}
\begin{split}
\begin{equation}
\hat{y} = \hat{X}^T\hat{\beta} +  \hat{\epsilon},
\label{_auto1} \tag{1}
\end{equation}
\end{split}
\end{equation*}
where \(\hat{y}\) is a vector representing the possible outcomes, \(\hat{X}\) is our
\(n\times p\) design matrix and \(\hat{\beta}\) represents our estimators/predictors.


\subsection{Some selected properties}
\label{\detokenize{chapter5:some-selected-properties}}
The main problem with our function is that it takes values on the
entire real axis. In the case of logistic regression, however, the
labels \(y_i\) are discrete variables. A typical example is the credit
card data discussed below here, where we can set the state of
defaulting the debt to \(y_i=1\) and not to \(y_i=0\) for one the persons
in the data set (see the full example below).

One simple way to get a discrete output is to have sign
functions that map the output of a linear regressor to values \(\{0,1\}\),
\(f(s_i)=sign(s_i)=1\) if \(s_i\ge 0\) and 0 if otherwise.
We will encounter this model in our first demonstration of neural networks. Historically it is called the “perceptron” model in the machine learning
literature. This model is extremely simple. However, in many cases it is more
favorable to use a \textasciigrave{}\textasciigrave{}soft” classifier that outputs
the probability of a given category. This leads us to the logistic function.


\subsection{The logistic function}
\label{\detokenize{chapter5:the-logistic-function}}
The perceptron is an example of a \textasciigrave{}\textasciigrave{}hard classification” model. We
will encounter this model when we discuss neural networks as
well. Each datapoint is deterministically assigned to a category (i.e
\(y_i=0\) or \(y_i=1\)). In many cases, it is favorable to have a “soft”
classifier that outputs the probability of a given category rather
than a single value. For example, given \(x_i\), the classifier
outputs the probability of being in a category \(k\).  Logistic regression
is the most common example of a so\sphinxhyphen{}called soft classifier. In logistic
regression, the probability that a data point \(x_i\)
belongs to a category \(y_i=\{0,1\}\) is given by the so\sphinxhyphen{}called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,
\begin{equation*}
\begin{split}
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
\end{split}
\end{equation*}
Note that \(1-p(t)= p(-t)\).


\subsection{Examples of likelihood functions used in logistic regression and nueral networks}
\label{\detokenize{chapter5:examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks}}
The following code plots the logistic function, the step function and other functions we will encounter from here and on.

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{o}{\PYGZpc{}}\PYG{k}{matplotlib} inline

\PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}The sigmoid function (or the logistic curve) is a}
\PYG{l+s+sd}{function that takes any real number, z, and outputs a number (0,1).}
\PYG{l+s+sd}{It is useful in neural networks for assigning weights on a relative scale.}
\PYG{l+s+sd}{The value z is the weighted sum of parameters involved in the learning algorithm.\PYGZdq{}\PYGZdq{}\PYGZdq{}}

\PYG{k+kn}{import} \PYG{n+nn}{numpy}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG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{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}
\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}title}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{tanh function}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}

\PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter5_7_0}.png}

\noindent\sphinxincludegraphics{{chapter5_7_1}.png}

\noindent\sphinxincludegraphics{{chapter5_7_2}.png}


\subsection{Two parameters}
\label{\detokenize{chapter5:two-parameters}}
We assume now that we have two classes with \(y_i\) either \(0\) or \(1\). Furthermore we assume also that we have only two parameters \(\beta\) in our fitting of the Sigmoid function, that is we define probabilities
\begin{equation*}
\begin{split}
\begin{align*}
p(y_i=1|x_i,\hat{\beta}) &= \frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}},\nonumber\\
p(y_i=0|x_i,\hat{\beta}) &= 1 - p(y_i=1|x_i,\hat{\beta}),
\end{align*}
\end{split}
\end{equation*}
where \(\hat{\beta}\) are the weights we wish to extract from data, in our case \(\beta_0\) and \(\beta_1\).

Note that we used
\begin{equation*}
\begin{split}
p(y_i=0\vert x_i, \hat{\beta}) = 1-p(y_i=1\vert x_i, \hat{\beta}).
\end{split}
\end{equation*}



\subsection{Maximum likelihood}
\label{\detokenize{chapter5:maximum-likelihood}}
In order to define the total likelihood for all possible outcomes from adataset \(\mathcal{D}=\{(y_i,x_i)\}\), with the binary labels
\(y_i\in\{0,1\}\) and where the data points are drawn independently, we use the so\sphinxhyphen{}called \sphinxhref{https://en.wikipedia.org/wiki/Maximum\_likelihood\_estimation}{Maximum Likelihood Estimation} (MLE) principle.
We aim thus at maximizing
the probability of seeing the observed data. We can then approximate the
likelihood in terms of the product of the individual probabilities of a specific outcome \(y_i\), that is
\begin{equation*}
\begin{split}
\begin{align*}
P(\mathcal{D}|\hat{\beta})& = \prod_{i=1}^n \left[p(y_i=1|x_i,\hat{\beta})\right]^{y_i}\left[1-p(y_i=1|x_i,\hat{\beta}))\right]^{1-y_i}\nonumber \\
\end{align*}
\end{split}
\end{equation*}
from which we obtain the log\sphinxhyphen{}likelihood and our \sphinxstylestrong{cost/loss} function
\begin{equation*}
\begin{split}
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left( y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right]\right).
\end{split}
\end{equation*}

\subsection{The cost function rewritten}
\label{\detokenize{chapter5:the-cost-function-rewritten}}
Reordering the logarithms, we can rewrite the \sphinxstylestrong{cost/loss} function as
\begin{equation*}
\begin{split}
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n  \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
\end{split}
\end{equation*}
The maximum likelihood estimator is defined as the set of parameters that maximize the log\sphinxhyphen{}likelihood where we maximize with respect to \(\beta\).
Since the cost (error) function is just the negative log\sphinxhyphen{}likelihood, for logistic regression we have that
\begin{equation*}
\begin{split}
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n  \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
\end{split}
\end{equation*}
This equation is known in statistics as the \sphinxstylestrong{cross entropy}. Finally, we note that just as in linear regression,
in practice we often supplement the cross\sphinxhyphen{}entropy with additional regularization terms, usually \(L_1\) and \(L_2\) regularization as we did for Ridge and Lasso regression.


\subsection{Minimizing the cross entropy}
\label{\detokenize{chapter5:minimizing-the-cross-entropy}}
The cross entropy is a convex function of the weights \(\hat{\beta}\) and,
therefore, any local minimizer is a global minimizer.

Minimizing this
cost function with respect to the two parameters \(\beta_0\) and \(\beta_1\) we obtain
\begin{equation*}
\begin{split}
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_0} = -\sum_{i=1}^n  \left(y_i -\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right),
\end{split}
\end{equation*}
and
\begin{equation*}
\begin{split}
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_1} = -\sum_{i=1}^n  \left(y_ix_i -x_i\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\end{split}
\end{equation*}

\subsection{A more compact expression}
\label{\detokenize{chapter5:a-more-compact-expression}}
Let us now define a vector \(\hat{y}\) with \(n\) elements \(y_i\), an
\(n\times p\) matrix \(\hat{X}\) which contains the \(x_i\) values and a
vector \(\hat{p}\) of fitted probabilities \(p(y_i\vert x_i,\hat{\beta})\). We can rewrite in a more compact form the first
derivative of cost function as
\begin{equation*}
\begin{split}
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}} = -\hat{X}^T\left(\hat{y}-\hat{p}\right).
\end{split}
\end{equation*}
If we in addition define a diagonal matrix \(\hat{W}\) with elements
\(p(y_i\vert x_i,\hat{\beta})(1-p(y_i\vert x_i,\hat{\beta})\), we can obtain a compact expression of the second derivative as
\begin{equation*}
\begin{split}
\frac{\partial^2 \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}\partial \hat{\beta}^T} = \hat{X}^T\hat{W}\hat{X}.
\end{split}
\end{equation*}

\subsection{Extending to more predictors}
\label{\detokenize{chapter5:extending-to-more-predictors}}
Within a binary classification problem, we can easily expand our model to include multiple predictors. Our ratio between likelihoods is then with \(p\) predictors
\begin{equation*}
\begin{split}
\log{ \frac{p(\hat{\beta}\hat{x})}{1-p(\hat{\beta}\hat{x})}} = \beta_0+\beta_1x_1+\beta_2x_2+\dots+\beta_px_p.
\end{split}
\end{equation*}
Here we defined \(\hat{x}=[1,x_1,x_2,\dots,x_p]\) and \(\hat{\beta}=[\beta_0, \beta_1, \dots, \beta_p]\) leading to
\begin{equation*}
\begin{split}
p(\hat{\beta}\hat{x})=\frac{ \exp{(\beta_0+\beta_1x_1+\beta_2x_2+\dots+\beta_px_p)}}{1+\exp{(\beta_0+\beta_1x_1+\beta_2x_2+\dots+\beta_px_p)}}.
\end{split}
\end{equation*}

\subsection{Including more classes}
\label{\detokenize{chapter5:including-more-classes}}
Till now we have mainly focused on two classes, the so\sphinxhyphen{}called binary
system. Suppose we wish to extend to \(K\) classes.  Let us for the sake
of simplicity assume we have only two predictors. We have then
following model

1
5

\textless{}
\textless{}
\textless{}
!
!
M
A
T
H
\_
B
L
O
C
K
\begin{equation*}
\begin{split}
\log{\frac{p(C=2\vert x)}{p(K\vert x)}} = \beta_{20}+\beta_{21}x_1,
\end{split}
\end{equation*}
and so on till the class \(C=K-1\) class
\begin{equation*}
\begin{split}
\log{\frac{p(C=K-1\vert x)}{p(K\vert x)}} = \beta_{(K-1)0}+\beta_{(K-1)1}x_1,
\end{split}
\end{equation*}
and the model is specified in term of \(K-1\) so\sphinxhyphen{}called log\sphinxhyphen{}odds or
\sphinxstylestrong{logit} transformations.


\subsection{More classes}
\label{\detokenize{chapter5:more-classes}}
In our discussion of neural networks we will encounter the above again
in terms of a slightly modified function, the so\sphinxhyphen{}called \sphinxstylestrong{Softmax} function.

The softmax function is used in various multiclass classification
methods, such as multinomial logistic regression (also known as
softmax regression), multiclass linear discriminant analysis, naive
Bayes classifiers, and artificial neural networks.  Specifically, in
multinomial logistic regression and linear discriminant analysis, the
input to the function is the result of \(K\) distinct linear functions,
and the predicted probability for the \(k\)\sphinxhyphen{}th class given a sample
vector \(\hat{x}\) and a weighting vector \(\hat{\beta}\) is (with two
predictors):
\begin{equation*}
\begin{split}
p(C=k\vert \mathbf {x} )=\frac{\exp{(\beta_{k0}+\beta_{k1}x_1)}}{1+\sum_{l=1}^{K-1}\exp{(\beta_{l0}+\beta_{l1}x_1)}}.
\end{split}
\end{equation*}
It is easy to extend to more predictors. The final class is
\begin{equation*}
\begin{split}
p(C=K\vert \mathbf {x} )=\frac{1}{1+\sum_{l=1}^{K-1}\exp{(\beta_{l0}+\beta_{l1}x_1)}},
\end{split}
\end{equation*}
and they sum to one. Our earlier discussions were all specialized to
the case with two classes only. It is easy to see from the above that
what we derived earlier is compatible with these equations.

To find the optimal parameters we would typically use a gradient
descent method.  Newton’s method and gradient descent methods are
discussed in the material on \sphinxhref{https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html}{optimization
methods}.


\subsection{A simple classification problem}
\label{\detokenize{chapter5:a-simple-classification-problem}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn} \PYG{k+kn}{import} \PYG{n}{datasets}\PYG{p}{,} \PYG{n}{linear\PYGZus{}model}
\PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt}


\PYG{k}{def} \PYG{n+nf}{generate\PYGZus{}data}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}
    \PYG{n}{X}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{datasets}\PYG{o}{.}\PYG{n}{make\PYGZus{}moons}\PYG{p}{(}\PYG{l+m+mi}{200}\PYG{p}{,} \PYG{n}{noise}\PYG{o}{=}\PYG{l+m+mf}{0.20}\PYG{p}{)}
    \PYG{k}{return} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y}


\PYG{k}{def} \PYG{n+nf}{visualize}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{clf}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{plot\PYGZus{}decision\PYGZus{}boundary}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}

\PYG{k}{def} \PYG{n+nf}{plot\PYGZus{}decision\PYGZus{}boundary}\PYG{p}{(}\PYG{n}{pred\PYGZus{}func}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{:}
    \PYG{c+c1}{\PYGZsh{} Set min and max values and give it some padding}
    \PYG{n}{x\PYGZus{}min}\PYG{p}{,} \PYG{n}{x\PYGZus{}max} \PYG{o}{=} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{.}\PYG{n}{min}\PYG{p}{(}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{o}{.}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)} \PYG{o}{+} \PYG{o}{.}\PYG{l+m+mi}{5}
    \PYG{n}{y\PYGZus{}min}\PYG{p}{,} \PYG{n}{y\PYGZus{}max} \PYG{o}{=} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{.}\PYG{n}{min}\PYG{p}{(}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{o}{.}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)} \PYG{o}{+} \PYG{o}{.}\PYG{l+m+mi}{5}
    \PYG{n}{h} \PYG{o}{=} \PYG{l+m+mf}{0.01}
    \PYG{c+c1}{\PYGZsh{} Generate a grid of points with distance h between them}
    \PYG{n}{xx}\PYG{p}{,} \PYG{n}{yy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{meshgrid}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{n}{x\PYGZus{}min}\PYG{p}{,} \PYG{n}{x\PYGZus{}max}\PYG{p}{,} \PYG{n}{h}\PYG{p}{)}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{n}{y\PYGZus{}min}\PYG{p}{,} \PYG{n}{y\PYGZus{}max}\PYG{p}{,} \PYG{n}{h}\PYG{p}{)}\PYG{p}{)}
    \PYG{c+c1}{\PYGZsh{} Predict the function value for the whole gid}
    \PYG{n}{Z} \PYG{o}{=} \PYG{n}{pred\PYGZus{}func}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{c\PYGZus{}}\PYG{p}{[}\PYG{n}{xx}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{yy}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)}
    \PYG{n}{Z} \PYG{o}{=} \PYG{n}{Z}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{xx}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)}
    \PYG{c+c1}{\PYGZsh{} Plot the contour and training examples}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{contourf}\PYG{p}{(}\PYG{n}{xx}\PYG{p}{,} \PYG{n}{yy}\PYG{p}{,} \PYG{n}{Z}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{n}{plt}\PYG{o}{.}\PYG{n}{cm}\PYG{o}{.}\PYG{n}{Spectral}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n}{c}\PYG{o}{=}\PYG{n}{y}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{n}{plt}\PYG{o}{.}\PYG{n}{cm}\PYG{o}{.}\PYG{n}{Spectral}\PYG{p}{)}
    \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)}


\PYG{k}{def} \PYG{n+nf}{classify}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{clf} \PYG{o}{=} \PYG{n}{linear\PYGZus{}model}\PYG{o}{.}\PYG{n}{LogisticRegressionCV}\PYG{p}{(}\PYG{p}{)}
    \PYG{n}{clf}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}
    \PYG{k}{return} \PYG{n}{clf}


\PYG{k}{def} \PYG{n+nf}{main}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:}
    \PYG{n}{X}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{generate\PYGZus{}data}\PYG{p}{(}\PYG{p}{)}
    \PYG{c+c1}{\PYGZsh{} visualize(X, y)}
    \PYG{n}{clf} \PYG{o}{=} \PYG{n}{classify}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}
    \PYG{n}{visualize}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{clf}\PYG{p}{)}

\PYG{k}{if} \PYG{n+nv+vm}{\PYGZus{}\PYGZus{}name\PYGZus{}\PYGZus{}} \PYG{o}{==} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZus{}\PYGZus{}main\PYGZus{}\PYGZus{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:}
    \PYG{n}{main}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}

\noindent\sphinxincludegraphics{{chapter5_42_0}.png}


\subsection{The Credit Card example}
\label{\detokenize{chapter5:the-credit-card-example}}
Here we use the the \sphinxhref{https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients}{credit card data}.
The data are from an extensive database from Taiwan and include more than ten predictors.

For categorical data \sphinxhyphen{}Scikit\sphinxhyphen{}Learn\sphinxhyphen{} provides a so\sphinxhyphen{}called \sphinxstylestrong{one\sphinxhyphen{}hot encoder}.
This is called one\sphinxhyphen{}hot
encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
\sphinxstylestrong{Scikit\sphinxhyphen{}Learn} provides a OneHotEncoder encoder to convert integer categorical values into one\sphinxhyphen{}hot

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{OneHotEncoder}
\PYG{n}{encoder} \PYG{o}{=} \PYG{n}{OneHotEncoder}\PYG{p}{(}\PYG{p}{)}
\end{sphinxVerbatim}


\subsection{How to read the Credit Card data}
\label{\detokenize{chapter5:how-to-read-the-credit-card-data}}
\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd}
\PYG{k+kn}{import} \PYG{n+nn}{os}
\PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np}


\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{OneHotEncoder}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{compose} \PYG{k+kn}{import} \PYG{n}{ColumnTransformer}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{StandardScaler}\PYG{p}{,} \PYG{n}{OneHotEncoder}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{confusion\PYGZus{}matrix}\PYG{p}{,} \PYG{n}{accuracy\PYGZus{}score}\PYG{p}{,} \PYG{n}{roc\PYGZus{}auc\PYGZus{}score}

\PYG{c+c1}{\PYGZsh{} Trying to set the seed}
\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}
\PYG{k+kn}{import} \PYG{n+nn}{random}
\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Reading file into data frame}
\PYG{n}{cwd} \PYG{o}{=} \PYG{n}{os}\PYG{o}{.}\PYG{n}{getcwd}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{filename} \PYG{o}{=} \PYG{n}{cwd} \PYG{o}{+} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{/default of credit card clients.xls}\PYG{l+s+s1}{\PYGZsq{}}
\PYG{n}{nanDict} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}}
\PYG{n}{df} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}excel}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{,} \PYG{n}{header}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{skiprows}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{index\PYGZus{}col}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{na\PYGZus{}values}\PYG{o}{=}\PYG{n}{nanDict}\PYG{p}{)}

\PYG{n}{df}\PYG{o}{.}\PYG{n}{rename}\PYG{p}{(}\PYG{n}{index}\PYG{o}{=}\PYG{n+nb}{str}\PYG{p}{,} \PYG{n}{columns}\PYG{o}{=}\PYG{p}{\PYGZob{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{default payment next month}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{defaultPaymentNextMonth}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{inplace}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Features and targets }
\PYG{n}{X} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{loc}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{df}\PYG{o}{.}\PYG{n}{columns} \PYG{o}{!=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{defaultPaymentNextMonth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{o}{.}\PYG{n}{values}
\PYG{n}{y} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{loc}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{df}\PYG{o}{.}\PYG{n}{columns} \PYG{o}{==} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{defaultPaymentNextMonth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{o}{.}\PYG{n}{values}

\PYG{c+c1}{\PYGZsh{} Categorical variables to one\PYGZhy{}hot\PYGZsq{}s}
\PYG{n}{onehotencoder} \PYG{o}{=} \PYG{n}{OneHotEncoder}\PYG{p}{(}\PYG{n}{categories}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{auto}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}

\PYG{n}{X} \PYG{o}{=} \PYG{n}{ColumnTransformer}\PYG{p}{(}
    \PYG{p}{[}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{onehotencoder}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)}\PYG{p}{,}\PYG{p}{]}\PYG{p}{,}
    \PYG{n}{remainder}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{passthrough}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}

\PYG{n}{y}\PYG{o}{.}\PYG{n}{shape}

\PYG{c+c1}{\PYGZsh{} Train\PYGZhy{}test split}
\PYG{n}{trainingShare} \PYG{o}{=} \PYG{l+m+mf}{0.5} 
\PYG{n}{seed}  \PYG{o}{=} \PYG{l+m+mi}{1}
\PYG{n}{XTrain}\PYG{p}{,} \PYG{n}{XTest}\PYG{p}{,} \PYG{n}{yTrain}\PYG{p}{,} \PYG{n}{yTest}\PYG{o}{=}\PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{train\PYGZus{}size}\PYG{o}{=}\PYG{n}{trainingShare}\PYG{p}{,} \PYGZbs{}
                                              \PYG{n}{test\PYGZus{}size} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{\PYGZhy{}}\PYG{n}{trainingShare}\PYG{p}{,}
                                             \PYG{n}{random\PYGZus{}state}\PYG{o}{=}\PYG{n}{seed}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Input Scaling}
\PYG{n}{sc} \PYG{o}{=} \PYG{n}{StandardScaler}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{XTrain} \PYG{o}{=} \PYG{n}{sc}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{XTrain}\PYG{p}{)}
\PYG{n}{XTest} \PYG{o}{=} \PYG{n}{sc}\PYG{o}{.}\PYG{n}{transform}\PYG{p}{(}\PYG{n}{XTest}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} One\PYGZhy{}hot\PYGZsq{}s of the target vector}
\PYG{n}{Y\PYGZus{}train\PYGZus{}onehot}\PYG{p}{,} \PYG{n}{Y\PYGZus{}test\PYGZus{}onehot} \PYG{o}{=} \PYG{n}{onehotencoder}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{yTrain}\PYG{p}{)}\PYG{p}{,} \PYG{n}{onehotencoder}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{yTest}\PYG{p}{)}

\PYG{c+c1}{\PYGZsh{} Remove instances with zeros only for past bill statements or paid amounts}
\PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{}}
\PYG{l+s+sd}{df = df.drop(df[(df.BILL\PYGZus{}AMT1 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.BILL\PYGZus{}AMT2 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.BILL\PYGZus{}AMT3 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.BILL\PYGZus{}AMT4 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.BILL\PYGZus{}AMT5 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.BILL\PYGZus{}AMT6 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.PAY\PYGZus{}AMT1 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.PAY\PYGZus{}AMT2 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.PAY\PYGZus{}AMT3 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.PAY\PYGZus{}AMT4 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.PAY\PYGZus{}AMT5 == 0) \PYGZam{}}
\PYG{l+s+sd}{                (df.PAY\PYGZus{}AMT6 == 0)].index)}
\PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{}}
\PYG{n}{df} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{drop}\PYG{p}{(}\PYG{n}{df}\PYG{p}{[}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT1} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT2} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT3} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT4} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT5} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT6} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{]}\PYG{o}{.}\PYG{n}{index}\PYG{p}{)}

\PYG{n}{df} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{drop}\PYG{p}{(}\PYG{n}{df}\PYG{p}{[}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT1} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT2} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT3} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT4} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT5} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}}
                \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT6} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{]}\PYG{o}{.}\PYG{n}{index}\PYG{p}{)}

\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LogisticRegression}
\PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{GridSearchCV}

\PYG{n}{lambdas}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{7}\PYG{p}{,}\PYG{l+m+mi}{13}\PYG{p}{)}
\PYG{n}{parameters} \PYG{o}{=} \PYG{p}{[}\PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{C}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{l+m+mf}{1.}\PYG{o}{/}\PYG{n}{lambdas}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{solver}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:}\PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lbfgs}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{\PYGZcb{}}\PYG{p}{]}\PYG{c+c1}{\PYGZsh{}*len(parameters)\PYGZcb{}]}
\PYG{n}{scoring} \PYG{o}{=} \PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{accuracy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{roc\PYGZus{}auc}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}
\PYG{n}{logReg} \PYG{o}{=} \PYG{n}{LogisticRegression}\PYG{p}{(}\PYG{p}{)}
\PYG{n}{gridSearch} \PYG{o}{=} \PYG{n}{GridSearchCV}\PYG{p}{(}\PYG{n}{logReg}\PYG{p}{,} \PYG{n}{parameters}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{scoring}\PYG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\PYG{l+m+mi}{4}\PYG{n}{edad39a9543}\PYG{o}{\PYGZgt{}} \PYG{o+ow}{in} \PYG{o}{\PYGZlt{}}\PYG{n}{module}\PYG{o}{\PYGZgt{}}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{19} \PYG{n}{filename} \PYG{o}{=} \PYG{n}{cwd} \PYG{o}{+} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{/default of credit card clients.xls}\PYG{l+s+s1}{\PYGZsq{}}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{20} \PYG{n}{nanDict} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}}
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{21} \PYG{n}{df} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}excel}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{,} \PYG{n}{header}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{skiprows}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{index\PYGZus{}col}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{na\PYGZus{}values}\PYG{o}{=}\PYG{n}{nanDict}\PYG{p}{)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{22} 
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{23} \PYG{n}{df}\PYG{o}{.}\PYG{n}{rename}\PYG{p}{(}\PYG{n}{index}\PYG{o}{=}\PYG{n+nb}{str}\PYG{p}{,} \PYG{n}{columns}\PYG{o}{=}\PYG{p}{\PYGZob{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{default payment next month}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{defaultPaymentNextMonth}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{inplace}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}base.py} in \PYG{n+ni}{read\PYGZus{}excel}\PYG{n+nt}{(io, sheet\PYGZus{}name, header, names, index\PYGZus{}col, usecols, squeeze, dtype, engine, converters, true\PYGZus{}values, false\PYGZus{}values, skiprows, nrows, na\PYGZus{}values, keep\PYGZus{}default\PYGZus{}na, verbose, parse\PYGZus{}dates, date\PYGZus{}parser, thousands, comment, skipfooter, convert\PYGZus{}float, mangle\PYGZus{}dupe\PYGZus{}cols, **kwds)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{302} 
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{303}     \PYG{k}{if} \PYG{o+ow}{not} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{io}\PYG{p}{,} \PYG{n}{ExcelFile}\PYG{p}{)}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{304}         \PYG{n}{io} \PYG{o}{=} \PYG{n}{ExcelFile}\PYG{p}{(}\PYG{n}{io}\PYG{p}{,} \PYG{n}{engine}\PYG{o}{=}\PYG{n}{engine}\PYG{p}{)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{305}     \PYG{k}{elif} \PYG{n}{engine} \PYG{o+ow}{and} \PYG{n}{engine} \PYG{o}{!=} \PYG{n}{io}\PYG{o}{.}\PYG{n}{engine}\PYG{p}{:}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{306}         \PYG{k}{raise} \PYG{n+ne}{ValueError}\PYG{p}{(}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}base.py} in \PYG{n+ni}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{n+nt}{(self, io, engine)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{822}         \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}io} \PYG{o}{=} \PYG{n}{stringify\PYGZus{}path}\PYG{p}{(}\PYG{n}{io}\PYG{p}{)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{823} 
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{824}         \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}reader} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}engines}\PYG{p}{[}\PYG{n}{engine}\PYG{p}{]}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}io}\PYG{p}{)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{825} 
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{826}     \PYG{k}{def} \PYG{n+nf}{\PYGZus{}\PYGZus{}fspath\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}xlrd.py} in \PYG{n+ni}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{n+nt}{(self, filepath\PYGZus{}or\PYGZus{}buffer)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{19}         \PYG{n}{err\PYGZus{}msg} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Install xlrd \PYGZgt{}= 1.0.0 for Excel support}\PYG{l+s+s2}{\PYGZdq{}}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{20}         \PYG{n}{import\PYGZus{}optional\PYGZus{}dependency}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{xlrd}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{extra}\PYG{o}{=}\PYG{n}{err\PYGZus{}msg}\PYG{p}{)}
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{21}         \PYG{n+nb}{super}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n+nf+fm}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{22} 
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{23}     \PYG{n+nd}{@property}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}base.py} in \PYG{n+ni}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{n+nt}{(self, filepath\PYGZus{}or\PYGZus{}buffer)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{351}             \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{book} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{load\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{352}         \PYG{k}{elif} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{,} \PYG{n+nb}{str}\PYG{p}{)}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{353}             \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{book} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{load\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{354}         \PYG{k}{elif} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{,} \PYG{n+nb}{bytes}\PYG{p}{)}\PYG{p}{:}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{355}             \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{book} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{load\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{BytesIO}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)}\PYG{p}{)}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}xlrd.py} in \PYG{n+ni}{load\PYGZus{}workbook}\PYG{n+nt}{(self, filepath\PYGZus{}or\PYGZus{}buffer)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{34}             \PYG{k}{return} \PYG{n}{open\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{file\PYGZus{}contents}\PYG{o}{=}\PYG{n}{data}\PYG{p}{)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{35}         \PYG{k}{else}\PYG{p}{:}
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{36}             \PYG{k}{return} \PYG{n}{open\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)}
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{37} 
\PYG{g+gWhitespace}{     }\PYG{l+m+mi}{38}     \PYG{n+nd}{@property}

\PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/xlrd/\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}.py} in \PYG{n+ni}{open\PYGZus{}workbook}\PYG{n+nt}{(filename, logfile, verbosity, use\PYGZus{}mmap, file\PYGZus{}contents, encoding\PYGZus{}override, formatting\PYGZus{}info, on\PYGZus{}demand, ragged\PYGZus{}rows)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{109}     \PYG{k}{else}\PYG{p}{:}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{110}         \PYG{n}{filename} \PYG{o}{=} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{expanduser}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{)}
\PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{111}         \PYG{k}{with} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{rb}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{k}{as} \PYG{n}{f}\PYG{p}{:}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{112}             \PYG{n}{peek} \PYG{o}{=} \PYG{n}{f}\PYG{o}{.}\PYG{n}{read}\PYG{p}{(}\PYG{n}{peeksz}\PYG{p}{)}
\PYG{g+gWhitespace}{    }\PYG{l+m+mi}{113}     \PYG{k}{if} \PYG{n}{peek} \PYG{o}{==} \PYG{l+s+sa}{b}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{PK}\PYG{l+s+se}{\PYGZbs{}x03}\PYG{l+s+se}{\PYGZbs{}x04}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{c+c1}{\PYGZsh{} a ZIP file}

\PYG{n+ne}{FileNotFoundError}: [Errno 2] No such file or directory: \PYGZsq{}/Users/hjensen/Teaching/FYS\PYGZhy{}STK4150/doc/src/LectureNotes/default of credit card clients.xls\PYGZsq{}
\end{sphinxVerbatim}







\renewcommand{\indexname}{Index}
\printindex
\end{document}