More text on how to read

This commit is contained in:
mhjensen
2018-05-26 13:05:30 -04:00
parent ccccc6ed1b
commit f90a232c25
10 changed files with 1844 additions and 1473 deletions
+334 -292
View File
@@ -69,67 +69,52 @@ div { text-align: justify; text-justify: inter-word; }
('Software and needed installations', 2, None, '___sec1'),
('Python installers', 2, None, '___sec2'),
('Installing R, C++, cython or Julia', 2, None, '___sec3'),
('Installing R, C++, cython or Julia', 2, None, '___sec4'),
('Introduction to Jupyter notebook and available tools',
('Installing R, C++, cython, Numba etc', 2, None, '___sec4'),
('Simple linear regression model using _scikit-learn_',
2,
None,
'___sec5'),
('Representing data, more examples', 2, None, '___sec6'),
('Simple regression model', 2, None, '___sec7'),
('Simple regression model, now using _scikit-learn_',
2,
None,
'___sec8'),
('Simple regression model with gradient descent',
2,
None,
'___sec9'),
('Simple regression model with stochastic gradient descent',
2,
None,
'___sec10'),
('Polynomial regression', 2, None, '___sec11'),
('Predator-Prey model from ecology', 2, None, '___sec12'),
('Case study from Hudson bay', 2, None, '___sec13'),
('Hudson bay data', 2, None, '___sec14'),
('Plotting the data', 2, None, '___sec15'),
('Predator-Prey model from ecology', 2, None, '___sec6'),
('Case study from Hudson bay', 2, None, '___sec7'),
('Hudson bay data', 2, None, '___sec8'),
('Plotting the data', 2, None, '___sec9'),
('Hares and lynx in Hudson bay from 1900 to 1920',
2,
None,
'___sec16'),
'___sec10'),
('Why now create a computer model for the hare and lynx '
'populations?',
2,
None,
'___sec17'),
('The traditional (top-down) approach', 2, None, '___sec18'),
('Basic mathematics notation', 2, None, '___sec19'),
'___sec11'),
('The traditional (top-down) approach', 2, None, '___sec12'),
('Basic mathematics notation', 2, None, '___sec13'),
('Basic dynamics of the population of hares',
2,
None,
'___sec20'),
('Basic dynamics of the population of lynx', 2, None, '___sec21'),
('Evolution equations', 2, None, '___sec22'),
('Adapt the model to the Hudson Bay case', 2, None, '___sec23'),
'___sec14'),
('Basic dynamics of the population of lynx', 2, None, '___sec15'),
('Evolution equations', 2, None, '___sec16'),
('Adapt the model to the Hudson Bay case', 2, None, '___sec17'),
('The program', 2, None, '___sec18'),
('The plot', 2, None, '___sec19'),
('Linear regression in Python', 2, None, '___sec20'),
('Linear Least squares in R', 2, None, '___sec21'),
('Non-Linear Least squares in R', 2, None, '___sec22'),
('Example: ecoli lab experiment', 2, None, '___sec23'),
('The program', 2, None, '___sec24'),
('The plot', 2, None, '___sec25'),
('Linear regression in Python', 2, None, '___sec26'),
('Linear Least squares in R', 2, None, '___sec27'),
('Non-Linear Least squares in R', 2, None, '___sec28'),
('Example: ecoli lab experiment', 2, None, '___sec29'),
('The program', 2, None, '___sec30'),
('The output', 2, None, '___sec31'),
('Parameter estimation', 2, None, '___sec32'),
('The output', 2, None, '___sec25'),
('Parameter estimation', 2, None, '___sec26'),
('A program relevant for the biological problem',
2,
None,
'___sec33'),
('Simulating financial transcations', 2, None, '___sec34'),
('Simulation of Transactions', 3, None, '___sec35'),
'___sec27'),
('Simulating financial transactions', 2, None, '___sec28'),
('Simulation of Transactions', 3, None, '___sec29'),
('Particle in one dimension an velocity distribution',
2,
None,
'___sec36')]}
'___sec30')]}
end of tocinfo -->
<body>
@@ -171,7 +156,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>May 22, 2018</h4></center> <!-- date -->
<center><h4>May 26, 2018</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -179,28 +164,38 @@ MathJax.Hub.Config({
<h2 id="___sec0">Introduction </h2>
<p>
Before we proceed there are several practicalities with data
analysis and software tools we would like to present. These tools will
help us in our understanding of various machine learning algorithms.
<p>
Our emphasis here is on understanding the mathematical aspects of
different algorithms, however, where possible we will emphasize the
Our emphasis throughout this series of lectures
is 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-on
and top-down approach machine learning. The aim is thus to start with
and top-down approach to machine learning. The aim is thus to start with
relevant data 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 start with a simple
third-order polynomial with random noise added, and using the Python
algorithms themselves. The examples we will use in the beginning, start with simple
polynomials with random noise added, and using the Python
software package <a href="http://scikit-learn.org/stable/" target="_blank">Scikit-learn</a> we
will introduce various machine learning algorithm s to make fits of
the data data and predictions. We move thereafter to more interesting
will introduce various machine learning algorithms to make fits of
the data and predictions. We move thereafter to more interesting
cases such as the simulation of financial transactions or disease
models. These are examples where we can easily set up the data and
then use machine learning algorithms using included in for example <b>scikit-learn</b>. Another model we
will consider is the so-called Ising model. Here we will use this
model to produce data for selected spin configurations and attempt to classify the data.
Finally, our last example consists of economic data from the OECD.
then use machine learning algorithms included in for example
<b>scikit-learn</b>. Another model we will consider is the so-called Ising
model. Here we will use this model to produce data for selected spin
configurations and attempt to classify the data. Finally, our last
example consists of economic data from the OECD.
<p>
All these examples will serve us the purpose of getting us 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 (and R) packages for machine
learning and statistical data analysis. In the lectures on linear
algebra we cover in more detail various programming features of languages like Python and C++ (and other), we will also look into more specific linear functions which
are relevant for the various algorithms we will discuss. Here, we will
mainly focus on two specific Python packages for Machine Learning,
scikit-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.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -208,29 +203,30 @@ Finally, our last example consists of economic data from the OECD.
<h2 id="___sec1">Software and needed installations </h2>
<p>
We will make intensive use of python as programming language and the myriad of available libraries.
Furthermore, you will find IPython/Jupyter notebooks invaluable in your work.
You can run <b>R</b> codes in the Jupyter/IPython notebooks, with the immediate benefit of visualizing your data.
We will make extensive use of Python as programming language and its
myriad of available libraries. Furthermore, you will find
IPython/Jupyter notebooks invaluable in your work. You can run <b>R</b>
codes in the Jupyter/IPython notebooks, with the immediate benefit of
visualizing your data. You can also use compiled languages like C++,
Rust, Fortran etc if you prefer. The focus in these lectures will be
on Python, but we will provide many code examples for those of you who
prefer R or compiled languages. You can integrate C++ codes and R in for example
a Jupyter notebook.
<p>
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
we recommend that you install the following Python packages via <b>pip</b> as
<ol>
<li> pip install numpy scipy matplotlib ipython scikit-learn mglearn sympy pandas pillow</li>
</ol>
For Python3, replace <b>pip</b> with <b>pip3</b>.
If you have Python installed (we recommend Python3) and you feel
pretty familiar with installing different packages, we recommend that
you install the following Python packages via <b>pip</b> as o pip install
numpy scipy matplotlib ipython scikit-learn mglearn sympy pandas
pillow For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>
For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
for a seamless installation of additional software via for example
For OSX users we recommend also, after having installed Xcode, to
install <b>brew</b>. Brew allows for a seamless installation of additional
software via for example o brew install python3
<ol>
<li> brew install python3</li>
</ol>
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
<p>
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution,
you can use <b>pip</b> as well and simply install Python as
<ol>
@@ -243,54 +239,268 @@ etc etc.
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec2">Python installers </h2>
If you don't want to perform these operations separately, we recommend two widely used distrubutions which set up
all relevant dependencies for Python, namely
<ol>
<li> <a href="https://docs.anaconda.com/" target="_blank">Anaconda</a> Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system <b>conda</b></li>
<li> <a href="https://www.enthought.com/product/canopy/" target="_blank">Enthought canopy</a> is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.</li>
</ol>
<p>
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 o
<a href="https://docs.anaconda.com/" target="_blank">Anaconda</a>, which is an open source
distribution of the Python and R programming languages for large-scale
data processing, predictive analytics, and scientific computing, that
aims to simplify package management and deployment. Package versions
are managed by the package management system <b>conda</b> o <a href="https://www.enthought.com/product/canopy/" target="_blank">Enthought
canopy</a> is a Python
distribution for scientific and analytic computing distribution and
analysis environment, available for free and under a commercial
license.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec3">Installing R, C++, cython or Julia </h2>
<p>
You will also find it convenient to utilize R.
Jupyter/Ipython notebook allows you run <b>R</b> code interactively in your browser. The software library <b>R</b> is
tuned to statistically analysis and allows for an easy usage of the tools we will discuss in these texts.
You will also find it convenient to utilize R. Although we will mainly
use Python during lectures and in various projects and exercises, we
provide a full R set of codes for the same examples. Those of you
already familiar with R should feel free to continue using R, keeping
however an eye on the parallel Python set ups. Similarly, if you are a
Python afecionado, feel free to explore R as well. Jupyter/Ipython
notebook allows you to run <b>R</b> codes interactively in your
browser. The software library <b>R</b> is tuned to statistically analysis
and allows for an easy usage of the tools we will discuss in these
texts.
<p>
To install <b>R</b> with Jupyter notebook <a href="https://mpacer.org/maths/r-kernel-for-ipython-notebook" target="_blank">following the link here</a>
To install <b>R</b> with Jupyter notebook <a href="https://mpacer.org/maths/r-kernel-for-ipython-notebook" target="_blank">following the link
here</a>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec4">Installing R, C++, cython or Julia </h2>
<h2 id="___sec4">Installing R, C++, cython, Numba etc </h2>
<p>
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++ as programming languages.
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.
<p>
To add more entropy, <b>cython</b> can also be used when running your notebooks. It means that Python with the Jupyter/IPython notebook
setup allows you to integrate widely popular softwares and tools for scientific computing. With its versatility,
including symbolic operations, Python offers a unique computational environment. Your Jupyter/IPython notebook
can easily be converted into a nicely rendered <b>PDF</b> file or a Latex file for further processing. For example, convert to latex as
To add more entropy, <b>cython</b> can also be used when running your
notebooks. It means that Python with the Jupyter/IPython notebook
setup allows you to integrate widely popular softwares and tools for
scientific computing. Similarly, the
<a href="https://numba.pydata.org/" target="_blank">Numba Python package</a> delivers increased performance
capabilities with minimal rewrites of your codes. With its
versatility, including symbolic operations, Python offers a unique
computational environment. Your Jupyter/IPython notebook can easily be
converted into a nicely rendered <b>PDF</b> file or a Latex file for
further processing. For example, convert to latex as
<p>
<!-- code=text typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pycod jupyter nbconvert filename.ipynb --to latex
</pre></div>
<p>
And to add more versatility, symbolic Python package <a href="http://www.sympy.org/en/index.html" target="_blank">SymPy</a> is Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) and is entirely written in Python.
<p>
Finally, if you wish to use the light mark-up language <a href="https://github.com/hplgit/doconce" target="_blank">doconce</a> you can convert a standard ascii text file into various HTML
formats, ipython notebooks, latex files, pdf files etc with minimal edits.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec5">Simple linear regression model using <b>scikit-learn</b> </h2>
We start with perhaps our simplest possible example, using <b>scikit-learn</b> to perform linear regression analysis on a data set produced by us.
What follows is a simple Python code where we have defined function \( y \) in terms of the variable \( x \). Both are defined as vectors of dimension \( 1\times 100 \). The entries to 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.
<p>
The Numpy functions are imported used the <b>import numpy as np</b>
statement and the random number generator for the uniform distribution
is called using the function <b>np.random.rand()</b>, 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 <b>randn()</b> 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 \)
$$
y = 2x+N(0,1),
$$
<p>
where \( N(0,1) \) represents random numbers generated by the normal
distribution. From <b>scikit-learn</b> we import then the
<b>LinearRegression</b> functionality and make a prediction \( \tilde{y} =
\alpha + \beta x \) using the function <b>fit(x,y)</b>. We call the set of
data \( (\hat{x},\hat{y}) \) for our training data. The Python package
<b>scikit-learn</b> 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.
<p>
For plotting we use the Python package
<a href="https://matplotlib.org/" target="_blank">matplotlib</a> which produces publication
quality figures. Feel free to explore the extensive
<a href="https://matplotlib.org/gallery/index.html" target="_blank">gallery</a> of examples. In
this example we plot our original values of \( x \) and \( y \) as well as the
prediction <b>ypredict</b> (\( \tilde{y} \)), which attempts at fitting our
data with a straight line.
<p>
The Python follows here.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>jupyter nbconvert filename<span style="color: #666666">.</span>ipynb <span style="color: #666666">--</span>to latex
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">2*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
linreg <span style="color: #666666">=</span> LinearRegression()
linreg<span style="color: #666666">.</span>fit(x,y)
xnew <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[<span style="color: #666666">0</span>],[<span style="color: #666666">1</span>]])
ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666">.</span>predict(xnew)
plt<span style="color: #666666">.</span>plot(xnew, ypredict, <span style="color: #BA2121">&quot;r-&quot;</span>)
plt<span style="color: #666666">.</span>plot(x, y ,<span style="color: #BA2121">&#39;ro&#39;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>,<span style="color: #666666">1.0</span>,<span style="color: #666666">0</span>, <span style="color: #666666">5.0</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&#39;$x$&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&#39;$y$&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Simple Linear Regression&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
If you use the light mark-up language <b>doconce</b> you can convert a standard ascii text file into various HTML
formats, ipython notebooks, latex files, pdf files etc.
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
$$
y = 10x+0.01N(0,1),
$$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
where \( x \) is defined as before. Does the fit look better? Indeed, by
reducing the role of 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 <b>cost</b> function.
<h2 id="___sec5">Introduction to Jupyter notebook and available tools </h2>
<p>
We need more rigorous criteria in defining whether we have succeed 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 <em>cost</em> function is the so-called \( \chi^2 \)
function
$$ \chi^2 = \frac{1}{n}
\sum_{i=0}^{n-1}\frac{(y_i-\tilde{y}_i)^2}{\sigma_i^2},
$$
<p>
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.
<p>
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 <b>gradient</b> 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 your \( \chi^2 \) function becomes smaller.
<p>
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 as
$$
\epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}.
$$
We can modify easily the above Python code and plot the relative instead
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">5*</span>x<span style="color: #666666">+0.01*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
linreg <span style="color: #666666">=</span> LinearRegression()
linreg<span style="color: #666666">.</span>fit(x,y)
ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666">.</span>predict(x)
plt<span style="color: #666666">.</span>plot(x, np<span style="color: #666666">.</span>abs(ypredict<span style="color: #666666">-</span>y)<span style="color: #666666">/</span><span style="color: #008000">abs</span>(y), <span style="color: #BA2121">&quot;ro&quot;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>,<span style="color: #666666">1.0</span>,<span style="color: #666666">0.0</span>, <span style="color: #666666">0.5</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&#39;$x$&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&#39;$\epsilon_{\mathrm{relative}}$&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Relative error&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
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.
<p>
As mentioned above, <b>scikit-learn</b> 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 scikit-learn.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error, r2_score
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">2*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
linreg <span style="color: #666666">=</span> LinearRegression()
linreg<span style="color: #666666">.</span>fit(x,y)
ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666">.</span>predict(x)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;Coefficients: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, linreg<span style="color: #666666">.</span>coef_)
<span style="color: #408080; font-style: italic"># The mean squared error </span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Mean squared error: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> mean_squared_error(y, ypredict))
<span style="color: #408080; font-style: italic"># Explained variance score: 1 is perfect prediction </span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;Variance score: </span><span style="color: #BB6688; font-weight: bold">%.2f</span><span style="color: #BA2121">&#39;</span> <span style="color: #666666">%</span> r2_score(y, ypredict))
plt<span style="color: #666666">.</span>plot(x, ypredict, <span style="color: #BA2121">&quot;r-&quot;</span>)
plt<span style="color: #666666">.</span>plot(x, y ,<span style="color: #BA2121">&#39;ro&#39;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>,<span style="color: #666666">1.0</span>,<span style="color: #666666">0</span>, <span style="color: #666666">5.0</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&#39;$x$&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&#39;$y$&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Linear Regression fit &#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
We will come to the definition of these outputs later.
<p>
Another useful Python package is
<a href="https://pandas.pydata.org/" target="_blank">pandas</a>, which is an open source library
providing high-performance, easy-to-use data structures and data
analysis tools for Python. The following simple example shows an example on how we can, in an easy way make tables of our data. Here we define a data set which includes names, city of residence and age, and displays the data in an easy to read way. We will see repeated use of <b>pandas</b>, in particular in connection with classification of data.
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -323,175 +533,7 @@ display(data_pandas)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec6">Representing data, more examples </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">scipy</span> <span style="color: #008000; font-weight: bold">import</span> sparse
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">mglearn</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">sklearn</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeRegressor
x, y <span style="color: #666666">=</span> mglearn<span style="color: #666666">.</span>datasets<span style="color: #666666">.</span>make_wave(n_samples<span style="color: #666666">=100</span>)
line <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-3</span>,<span style="color: #666666">3</span>,<span style="color: #666666">1000</span>,endpoint<span style="color: #666666">=</span><span style="color: #008000">False</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>,<span style="color: #666666">1</span>)
reg <span style="color: #666666">=</span> DecisionTreeRegressor(min_samples_split<span style="color: #666666">=3</span>)<span style="color: #666666">.</span>fit(x,y)
plt<span style="color: #666666">.</span>plot(line, reg<span style="color: #666666">.</span>predict(line), label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;decision tree&quot;</span>)
regline <span style="color: #666666">=</span> LinearRegression()<span style="color: #666666">.</span>fit(x,y)
plt<span style="color: #666666">.</span>plot(line, regline<span style="color: #666666">.</span>predict(line), label<span style="color: #666666">=</span> <span style="color: #BA2121">&quot;Linear Regression&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
</div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec7">Simple regression model </h2>
Add info about the equations
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">random</span> <span style="color: #008000; font-weight: bold">import</span> random, seed
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
x <span style="color: #666666">=</span> <span style="color: #666666">2*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
xb <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[np<span style="color: #666666">.</span>ones((<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)), x]
theta <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>inv(xb<span style="color: #666666">.</span>T<span style="color: #666666">.</span>dot(xb))<span style="color: #666666">.</span>dot(xb<span style="color: #666666">.</span>T)<span style="color: #666666">.</span>dot(y)
xnew <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[<span style="color: #666666">0</span>],[<span style="color: #666666">2</span>]])
xbnew <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[np<span style="color: #666666">.</span>ones((<span style="color: #666666">2</span>,<span style="color: #666666">1</span>)), xnew]
ypredict <span style="color: #666666">=</span> xbnew<span style="color: #666666">.</span>dot(theta)
plt<span style="color: #666666">.</span>plot(xnew, ypredict, <span style="color: #BA2121">&quot;r-&quot;</span>)
plt<span style="color: #666666">.</span>plot(x, y ,<span style="color: #BA2121">&#39;ro&#39;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>,<span style="color: #666666">2.0</span>,<span style="color: #666666">0</span>, <span style="color: #666666">15.0</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&#39;$x$&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&#39;$y$&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Linear Regression&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">Simple regression model, now using <b>scikit-learn</b> </h2>
Add info about the equations
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">random</span> <span style="color: #008000; font-weight: bold">import</span> random, seed
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LinearRegression
x <span style="color: #666666">=</span> <span style="color: #666666">2*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
linreg <span style="color: #666666">=</span> LinearRegression()
linreg<span style="color: #666666">.</span>fit(x,y)
xnew <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[<span style="color: #666666">0</span>],[<span style="color: #666666">2</span>]])
ypredict <span style="color: #666666">=</span> linreg<span style="color: #666666">.</span>predict(xnew)
plt<span style="color: #666666">.</span>plot(xnew, ypredict, <span style="color: #BA2121">&quot;r-&quot;</span>)
plt<span style="color: #666666">.</span>plot(x, y ,<span style="color: #BA2121">&#39;ro&#39;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>,<span style="color: #666666">2.0</span>,<span style="color: #666666">0</span>, <span style="color: #666666">15.0</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&#39;$x$&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&#39;$y$&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Random numbers &#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">Simple regression model with gradient descent </h2>
Add info about the equations, play around with different learning rates
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">math</span> <span style="color: #008000; font-weight: bold">import</span> exp, sqrt
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">random</span> <span style="color: #008000; font-weight: bold">import</span> random, seed
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
x <span style="color: #666666">=</span> <span style="color: #666666">2*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
xb <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[np<span style="color: #666666">.</span>ones((<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)), x]
theta_linreg <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>inv(xb<span style="color: #666666">.</span>T<span style="color: #666666">.</span>dot(xb))<span style="color: #666666">.</span>dot(xb<span style="color: #666666">.</span>T)<span style="color: #666666">.</span>dot(y)
<span style="color: #008000; font-weight: bold">print</span>(theta_linreg)
theta <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">2</span>,<span style="color: #666666">1</span>)
eta <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>
Niterations <span style="color: #666666">=</span> <span style="color: #666666">1000</span>
m <span style="color: #666666">=</span> <span style="color: #666666">100</span>
<span style="color: #008000; font-weight: bold">for</span> <span style="color: #008000">iter</span> <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(Niterations):
gradients <span style="color: #666666">=</span> <span style="color: #666666">2.0/</span>m<span style="color: #666666">*</span>xb<span style="color: #666666">.</span>T<span style="color: #666666">.</span>dot(xb<span style="color: #666666">.</span>dot(theta)<span style="color: #666666">-</span>y)
theta <span style="color: #666666">-=</span> eta<span style="color: #666666">*</span>gradients
<span style="color: #008000; font-weight: bold">print</span>(theta)
xnew <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[<span style="color: #666666">0</span>],[<span style="color: #666666">2</span>]])
xbnew <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[np<span style="color: #666666">.</span>ones((<span style="color: #666666">2</span>,<span style="color: #666666">1</span>)), xnew]
ypredict <span style="color: #666666">=</span> xbnew<span style="color: #666666">.</span>dot(theta)
ypredict2 <span style="color: #666666">=</span> xbnew<span style="color: #666666">.</span>dot(theta_linreg)
plt<span style="color: #666666">.</span>plot(xnew, ypredict, <span style="color: #BA2121">&quot;r-&quot;</span>)
plt<span style="color: #666666">.</span>plot(xnew, ypredict2, <span style="color: #BA2121">&quot;b-&quot;</span>)
plt<span style="color: #666666">.</span>plot(x, y ,<span style="color: #BA2121">&#39;ro&#39;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>,<span style="color: #666666">2.0</span>,<span style="color: #666666">0</span>, <span style="color: #666666">15.0</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&#39;$x$&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&#39;$y$&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Random numbers &#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Simple regression model with stochastic gradient descent </h2>
Add info about the equations, play around with different learning rates
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">math</span> <span style="color: #008000; font-weight: bold">import</span> exp, sqrt
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">random</span> <span style="color: #008000; font-weight: bold">import</span> random, seed
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> SGDRegressor
x <span style="color: #666666">=</span> <span style="color: #666666">2*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)
xb <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[np<span style="color: #666666">.</span>ones((<span style="color: #666666">100</span>,<span style="color: #666666">1</span>)), x]
theta_linreg <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>inv(xb<span style="color: #666666">.</span>T<span style="color: #666666">.</span>dot(xb))<span style="color: #666666">.</span>dot(xb<span style="color: #666666">.</span>T)<span style="color: #666666">.</span>dot(y)
<span style="color: #008000; font-weight: bold">print</span>(theta_linreg)
sgdreg <span style="color: #666666">=</span> SGDRegressor(n_iter <span style="color: #666666">=</span> <span style="color: #666666">50</span>, penalty<span style="color: #666666">=</span><span style="color: #008000">None</span>, eta0<span style="color: #666666">=0.1</span>)
sgdreg<span style="color: #666666">.</span>fit(x,y<span style="color: #666666">.</span>ravel())
<span style="color: #008000; font-weight: bold">print</span>(sgdreg<span style="color: #666666">.</span>intercept_, sgdreg<span style="color: #666666">.</span>coef_)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">Polynomial regression </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">Predator-Prey model from ecology </h2>
<h2 id="___sec6">Predator-Prey model from ecology </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -515,7 +557,7 @@ scientific method:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Case study from Hudson bay </h2>
<h2 id="___sec7">Case study from Hudson bay </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -536,7 +578,7 @@ Here we start by
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">Hudson bay data </h2>
<h2 id="___sec8">Hudson bay data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -585,7 +627,7 @@ One reason that this particular system has been so extensively studied is that t
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec15">Plotting the data </h2>
<h2 id="___sec9">Plotting the data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -621,7 +663,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec16">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
<h2 id="___sec10">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
<p>
<br /><br /><center><p><img src="fig/Hudson_Bay_data.png" align="bottom" width=700></p></center><br /><br />
@@ -629,7 +671,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">Why now create a computer model for the hare and lynx populations? </h2>
<h2 id="___sec11">Why now create a computer model for the hare and lynx populations? </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -662,7 +704,7 @@ climate and other complicating factors. How significant are these?
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec18">The traditional (top-down) approach </h2>
<h2 id="___sec12">The traditional (top-down) approach </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -695,7 +737,7 @@ ODEs</em> (which cannot be solved)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec19">Basic mathematics notation </h2>
<h2 id="___sec13">Basic mathematics notation </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -715,7 +757,7 @@ ODEs</em> (which cannot be solved)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec20">Basic dynamics of the population of hares </h2>
<h2 id="___sec14">Basic dynamics of the population of hares </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -743,7 +785,7 @@ $$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec21">Basic dynamics of the population of lynx </h2>
<h2 id="___sec15">Basic dynamics of the population of lynx </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -772,7 +814,7 @@ $$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec22">Evolution equations </h2>
<h2 id="___sec16">Evolution equations </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -801,7 +843,7 @@ Note:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec23">Adapt the model to the Hudson Bay case </h2>
<h2 id="___sec17">Adapt the model to the Hudson Bay case </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -824,7 +866,7 @@ Note:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec24">The program </h2>
<h2 id="___sec18">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -884,7 +926,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec25">The plot </h2>
<h2 id="___sec19">The plot </h2>
<p>
<br /><br /><center><p><img src="fig/Hudson_Bay_sim.png" align="bottom" width=700></p></center><br /><br />
@@ -895,7 +937,7 @@ If we perform a least-square fitting, we can find optimal values for the paramet
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec26">Linear regression in Python </h2>
<h2 id="___sec20">Linear regression in Python </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -928,7 +970,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec27">Linear Least squares in R </h2>
<h2 id="___sec21">Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -962,7 +1004,7 @@ predict(linearMod,<span style="color: #B00040">data.frame</span>(Year<span style
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec28">Non-Linear Least squares in R </h2>
<h2 id="___sec22">Non-Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -996,7 +1038,7 @@ text(<span style="color: #666666">0</span>, <span style="color: #666666">0.5</sp
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec29">Example: ecoli lab experiment </h2>
<h2 id="___sec23">Example: ecoli lab experiment </h2>
<p>
<div class="alert alert-block alert-notice alert-text-normal">
@@ -1029,7 +1071,7 @@ The population grows faster and faster. <a href="http://www.zo.utexas.edu/course
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec30">The program </h2>
<h2 id="___sec24">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -1062,7 +1104,7 @@ r <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>
% if FORMAT != 'ipynb':
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec31">The output </h2>
<h2 id="___sec25">The output </h2>
<p>
@@ -1094,7 +1136,7 @@ N[20]=86.7
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec32">Parameter estimation </h2>
<h2 id="___sec26">Parameter estimation </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -1123,7 +1165,7 @@ Use experimental data in the fraction, say \( t_1=600 \), \( t_2=1200 \),
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec33">A program relevant for the biological problem </h2>
<h2 id="___sec27">A program relevant for the biological problem </h2>
<p>
<!-- exact r = 0.000694 -->
@@ -1171,7 +1213,7 @@ Change <code>r</code> in the program and play around to make a better fit!
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec34">Simulating financial transcations </h2>
<h2 id="___sec28">Simulating financial transactions </h2>
<p>
The aim here is to simulate financial transactions among financial agents
@@ -1245,7 +1287,7 @@ exponentially decreases with \( m' \).
We assume that we have \( N=500 \) agents. In each simulation, we need a sufficiently large number of transactions, say \( 10^7 \). Our aim is find the final equilibrium distribution \( w_m \). In order to do that we would need
several runs of the above simulations, at least \( 10^3-10^4 \) runs (experiments).
<h3 id="___sec35">Simulation of Transactions </h3>
<h3 id="___sec29">Simulation of Transactions </h3>
Our task is to first set up an algorithm which simulates the above transactions with an initial
amount \( m_0 \).
@@ -1378,7 +1420,7 @@ where \( c_{ij} \) represents the number of previous interactions that have tak
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec36">Particle in one dimension an velocity distribution </h2>
<h2 id="___sec30">Particle in one dimension an velocity distribution </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->