added files
This commit is contained in:
@@ -8,3 +8,8 @@ translating doconce text in Introduction.do.txt to ipynb
|
||||
Failed to remove ans_at_end environment
|
||||
Failed to remove sol_at_end environment
|
||||
output in Introduction.ipynb
|
||||
translating doconce text in Introduction.do.txt to ipynb
|
||||
*** replacing \bm{...} by \boldsymbol{...} (\bm is not supported by MathJax)
|
||||
Failed to remove ans_at_end environment
|
||||
Failed to remove sol_at_end environment
|
||||
output in Introduction.ipynb
|
||||
|
||||
@@ -0,0 +1,404 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Introduction to Applied Data Analysis and Machine Learning\n",
|
||||
"\n",
|
||||
"During the last two decades there has been a swift and amazing\n",
|
||||
"development of Machine Learning techniques and algorithms that impact\n",
|
||||
"many areas in not only Science and Technology but also the Humanities,\n",
|
||||
"Social Sciences, Medicine, Law, indeed, almost all possible\n",
|
||||
"disciplines. The applications are incredibly many, from self-driving\n",
|
||||
"cars to solving high-dimensional differential equations or complicated\n",
|
||||
"quantum mechanical many-body problems. Machine Learning is perceived\n",
|
||||
"by many as one of the main disruptive techniques nowadays. \n",
|
||||
"\n",
|
||||
"Statistics, Data science and Machine Learning form important\n",
|
||||
"fields of research in modern science. They describe how to learn and\n",
|
||||
"make predictions from data, as well as allowing us to extract\n",
|
||||
"important correlations about physical process and the underlying laws\n",
|
||||
"of motion in large data sets. The latter, big data sets, appear\n",
|
||||
"frequently in essentially all disciplines, from the traditional\n",
|
||||
"Science, Technology, Mathematics and Engineering fields to Life\n",
|
||||
"Science, Law, education research, the Humanities and the Social\n",
|
||||
"Sciences.\n",
|
||||
"\n",
|
||||
"It has become more\n",
|
||||
"and more common to see research projects on big data in for example\n",
|
||||
"the Social Sciences where extracting patterns from complicated survey\n",
|
||||
"data is one of many research directions. Having a solid grasp of data\n",
|
||||
"analysis and machine learning is thus becoming central to scientific\n",
|
||||
"computing in many fields, and competences and skills within the fields\n",
|
||||
"of machine learning and scientific computing are nowadays strongly\n",
|
||||
"requested by many potential employers. The latter cannot be\n",
|
||||
"overstated, familiarity with machine learning has almost become a\n",
|
||||
"prerequisite for many of the most exciting employment opportunities,\n",
|
||||
"whether they are in bioinformatics, life science, physics or finance,\n",
|
||||
"in the private or the public sector. This author has had several\n",
|
||||
"students or met students who have been hired recently based on their\n",
|
||||
"skills and competences in scientific computing and data science, often\n",
|
||||
"with marginal knowledge of machine learning.\n",
|
||||
"\n",
|
||||
"Machine learning is a subfield of computer science, and is closely\n",
|
||||
"related to computational statistics. It evolved from the study of\n",
|
||||
"pattern recognition in artificial intelligence (AI) research, and has\n",
|
||||
"made contributions to AI tasks like computer vision, natural language\n",
|
||||
"processing and speech recognition. Many of the methods we will study are also \n",
|
||||
"strongly rooted in basic mathematics and physics research. \n",
|
||||
"\n",
|
||||
"Ideally, machine learning represents the science of giving computers\n",
|
||||
"the ability to learn without being explicitly programmed. The idea is\n",
|
||||
"that there exist generic algorithms which can be used to find patterns\n",
|
||||
"in a broad class of data sets without having to write code\n",
|
||||
"specifically for each problem. The algorithm will build its own logic\n",
|
||||
"based on the data. You should however always keep in mind that\n",
|
||||
"machines and algorithms are to a large extent developed by humans. The\n",
|
||||
"insights and knowledge we have about a specific system, play a central\n",
|
||||
"role when we develop a specific machine learning algorithm. \n",
|
||||
"\n",
|
||||
"Machine learning is an extremely rich field, in spite of its young\n",
|
||||
"age. The increases we have seen during the last three decades in\n",
|
||||
"computational capabilities have been followed by developments of\n",
|
||||
"methods and techniques for analyzing and handling large date sets,\n",
|
||||
"relying heavily on statistics, computer science and mathematics. The\n",
|
||||
"field is rather new and developing rapidly. Popular software packages\n",
|
||||
"written in Python for machine learning like\n",
|
||||
"[Scikit-learn](http://scikit-learn.org/stable/),\n",
|
||||
"[Tensorflow](https://www.tensorflow.org/),\n",
|
||||
"[PyTorch](http://pytorch.org/) and [Keras](https://keras.io/), all\n",
|
||||
"freely available at their respective GitHub sites, encompass\n",
|
||||
"communities of developers in the thousands or more. And the number of\n",
|
||||
"code developers and contributors keeps increasing. Not all the\n",
|
||||
"algorithms and methods can be given a rigorous mathematical\n",
|
||||
"justification, opening up thereby large rooms for experimenting and\n",
|
||||
"trial and error and thereby exciting new developments. However, a\n",
|
||||
"solid command of linear algebra, multivariate theory, probability\n",
|
||||
"theory, statistical data analysis, understanding errors and Monte\n",
|
||||
"Carlo methods are central elements in a proper understanding of many\n",
|
||||
"of algorithms and methods we will discuss.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Learning outcomes\n",
|
||||
"\n",
|
||||
"These sets of lectures aim at giving you an overview of central aspects of\n",
|
||||
"statistical data analysis as well as some of the central algorithms\n",
|
||||
"used in machine learning. We will introduce a variety of central\n",
|
||||
"algorithms and methods essential for studies of data analysis and\n",
|
||||
"machine learning. \n",
|
||||
"\n",
|
||||
"Hands-on projects and experimenting with data and algorithms plays a central role in\n",
|
||||
"these lectures, and our hope is, through the various\n",
|
||||
"projects and exercises, to expose you to fundamental\n",
|
||||
"research problems in these fields, with the aim to reproduce state of\n",
|
||||
"the art scientific results. You will learn to develop and\n",
|
||||
"structure codes for studying these systems, get acquainted with\n",
|
||||
"computing facilities and learn to handle large scientific projects. A\n",
|
||||
"good scientific and ethical conduct is emphasized throughout the\n",
|
||||
"course. More specifically, you will\n",
|
||||
"\n",
|
||||
"1. Learn about basic data analysis, Bayesian statistics, Monte Carlo methods, data optimization and machine learning;\n",
|
||||
"\n",
|
||||
"2. Be capable of extending the acquired knowledge to other systems and cases;\n",
|
||||
"\n",
|
||||
"3. Have an understanding of central algorithms used in data analysis and machine learning;\n",
|
||||
"\n",
|
||||
"4. 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;\n",
|
||||
"\n",
|
||||
"5. Understand methods for regression and classification;\n",
|
||||
"\n",
|
||||
"6. Learn about neural network, genetic algorithms and Boltzmann machines;\n",
|
||||
"\n",
|
||||
"7. 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).\n",
|
||||
"\n",
|
||||
"There are several topics we will cover here, spanning from \n",
|
||||
"statistical data analysis and its basic concepts such as expectation\n",
|
||||
"values, variance, covariance, correlation functions and errors, via\n",
|
||||
"well-known probability distribution functions like the uniform\n",
|
||||
"distribution, the binomial distribution, the Poisson distribution and\n",
|
||||
"simple and multivariate normal distributions to central elements of\n",
|
||||
"Bayesian statistics and modeling. We will also remind the reader about\n",
|
||||
"central elements from linear algebra and standard methods based on\n",
|
||||
"linear algebra used to optimize (minimize) functions (the family of gradient descent methods)\n",
|
||||
"and the Singular-value decomposition and\n",
|
||||
"least square methods for parameterizing data.\n",
|
||||
"\n",
|
||||
"We will also cover Monte Carlo methods, Markov chains, well-known\n",
|
||||
"algorithms for sampling stochastic events like the Metropolis-Hastings\n",
|
||||
"and Gibbs sampling methods. An important aspect of all our\n",
|
||||
"calculations is a proper estimation of errors. Here we will also\n",
|
||||
"discuss famous resampling techniques like the blocking, the bootstrapping\n",
|
||||
"and the jackknife methods and the infamous bias-variance tradeoff. \n",
|
||||
"\n",
|
||||
"The second part of the material covers several algorithms used in\n",
|
||||
"machine learning.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Machine Learning, a small (and probably biased) introduction\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Ideally, machine learning represents the science of giving computers\n",
|
||||
"the ability to learn without being explicitly programmed. The idea is\n",
|
||||
"that there exist generic algorithms which can be used to find patterns\n",
|
||||
"in a broad class of data sets without having to write code\n",
|
||||
"specifically for each problem. The algorithm will build its own logic\n",
|
||||
"based on the data. You should however always keep in mind that\n",
|
||||
"machines and algorithms are to a large extent developed by humans. The\n",
|
||||
"insights and knowledge we have about a specific system, play a central\n",
|
||||
"role when we develop a specific machine learning algorithm. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Machine learning is an extremely rich field, in spite of its young\n",
|
||||
"age. The increases we have seen during the last decades in\n",
|
||||
"computational capabilities have been followed by developments of\n",
|
||||
"methods and techniques for analyzing and handling large date sets,\n",
|
||||
"relying heavily on statistics, computer science and mathematics. The\n",
|
||||
"field is rather new and developing rapidly. Popular software libraries\n",
|
||||
"written in Python for machine learning like\n",
|
||||
"[Scikit-learn](http://scikit-learn.org/stable/),\n",
|
||||
"[Tensorflow](https://www.tensorflow.org/),\n",
|
||||
"[PyTorch](http://pytorch.org/) and [Keras](https://keras.io/), all\n",
|
||||
"freely available at their respective GitHub sites, encompass\n",
|
||||
"communities of developers in the thousands or more. And the number of\n",
|
||||
"code developers and contributors keeps increasing.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Not all the\n",
|
||||
"algorithms and methods can be given a rigorous mathematical\n",
|
||||
"justification (for example decision trees and random forests), opening up thereby large rooms for experimenting and\n",
|
||||
"trial and error and thereby exciting new developments. However, a\n",
|
||||
"solid command of linear algebra, multivariate theory, probability\n",
|
||||
"theory, statistical data analysis, understanding errors and Monte\n",
|
||||
"Carlo methods are central elements in a proper understanding of many\n",
|
||||
"of the algorithms and methods we will discuss.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The approaches to machine learning are many, but are often split into\n",
|
||||
"two main categories. In *supervised learning* we know the answer to a\n",
|
||||
"problem, and let the computer deduce the logic behind it. On the other\n",
|
||||
"hand, *unsupervised learning* is a method for finding patterns and\n",
|
||||
"relationship in data sets without any prior knowledge of the system.\n",
|
||||
"Some authours also operate with a third category, namely\n",
|
||||
"*reinforcement learning*. This is a paradigm of learning inspired by\n",
|
||||
"behavioral psychology, where learning is achieved by trial-and-error,\n",
|
||||
"solely from rewards and punishment.\n",
|
||||
"\n",
|
||||
"Another way to categorize machine learning tasks is to consider the\n",
|
||||
"desired output of a system. Some of the most common tasks are:\n",
|
||||
"\n",
|
||||
"* 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-written ones. Classification is often supervised learning.\n",
|
||||
"\n",
|
||||
"* 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.\n",
|
||||
"\n",
|
||||
"* Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning.\n",
|
||||
"\n",
|
||||
"The methods we cover have three main topics in common, irrespective of\n",
|
||||
"whether we deal with supervised or unsupervised learning.\n",
|
||||
"\n",
|
||||
"* 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. \n",
|
||||
"\n",
|
||||
"* 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. \n",
|
||||
"\n",
|
||||
"* The last ingredient is a so-called **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. \n",
|
||||
"\n",
|
||||
"At the heart of basically all Machine Learning algorithms we will encounter so-called minimization or optimization algorithms. A large family of such methods are so-called **gradient methods**.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## A Frequentist approach to data analysis\n",
|
||||
"\n",
|
||||
"When you hear phrases like **predictions and estimations** and\n",
|
||||
"**correlations and causations**, what do you think of? May be you think\n",
|
||||
"of the difference between classifying new data points and generating\n",
|
||||
"new data points.\n",
|
||||
"Or perhaps you consider that correlations represent some kind of symmetric statements like\n",
|
||||
"if $A$ is correlated with $B$, then $B$ is correlated with\n",
|
||||
"$A$. Causation on the other hand is directional, that is if $A$ causes $B$, $B$ does not\n",
|
||||
"necessarily cause $A$.\n",
|
||||
"\n",
|
||||
"These concepts are in some sense the difference between machine\n",
|
||||
"learning and statistics. In machine learning and prediction based\n",
|
||||
"tasks, we are often interested in developing algorithms that are\n",
|
||||
"capable of learning patterns from given data in an automated fashion,\n",
|
||||
"and then using these learned patterns to make predictions or\n",
|
||||
"assessments of newly given data. In many cases, our primary concern\n",
|
||||
"is the quality of the predictions or assessments, and we are less\n",
|
||||
"concerned about the underlying patterns that were learned in order\n",
|
||||
"to make these predictions.\n",
|
||||
"\n",
|
||||
"In machine learning we normally use [a so-called frequentist approach](https://en.wikipedia.org/wiki/Frequentist_inference),\n",
|
||||
"where the aim is to make predictions and find correlations. We focus\n",
|
||||
"less on for example extracting a probability distribution function (PDF). The PDF can be\n",
|
||||
"used in turn to make estimations and find causations such as given $A$\n",
|
||||
"what is the likelihood of finding $B$.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## What is a good model?\n",
|
||||
"\n",
|
||||
"In science and engineering we often end up in situations where we want to infer (or learn) a\n",
|
||||
"quantitative model $M$ for a given set of sample points $\\boldsymbol{X} \\in [x_1, x_2,\\dots x_N]$.\n",
|
||||
"\n",
|
||||
"As we will see repeatedely in these lectures, we could try to fit these data points to a model given by a\n",
|
||||
"straight line, or if we wish to be more sophisticated to a more complex\n",
|
||||
"function.\n",
|
||||
"\n",
|
||||
"The reason for inferring such a model is that it\n",
|
||||
"serves many useful purposes. On the one hand, the model can reveal information\n",
|
||||
"encoded in the data or underlying mechanisms from which the data were generated. For instance, we could discover important\n",
|
||||
"corelations that relate interesting physics interpretations.\n",
|
||||
"\n",
|
||||
"In addition, it can simplify the representation of the given data set and help\n",
|
||||
"us in making predictions about future data samples.\n",
|
||||
"\n",
|
||||
"A first important consideration to keep in mind is that inferring the *correct* model\n",
|
||||
"for a given data set is an elusive, if not impossible, task. The fundamental difficulty\n",
|
||||
"is that if we are not specific about what we mean by a *correct* model, there\n",
|
||||
"could easily be many different models that fit the given data set *equally well*.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The central question is this: what leads us to say that a model is correct or\n",
|
||||
"optimal for a given data set? To make the model inference problem well posed, i.e.,\n",
|
||||
"to guarantee that there is a unique optimal model for the given data, we need to\n",
|
||||
"impose additional assumptions or restrictions on the class of models considered. To\n",
|
||||
"this end, we should not be looking for just any model that can describe the data.\n",
|
||||
"Instead, we should look for a **model** $M$ that is the best among a restricted class\n",
|
||||
"of models. In addition, to make the model inference problem computationally\n",
|
||||
"tractable, we need to specify how restricted the class of models needs to be. A\n",
|
||||
"common strategy is to start \n",
|
||||
"with the simplest possible class of models that is just necessary to describe the data\n",
|
||||
"or solve the problem at hand. More precisely, the model class should be rich enough\n",
|
||||
"to contain at least one model that can fit the data to a desired accuracy and yet be\n",
|
||||
"restricted enough that it is relatively simple to find the best model for the given data.\n",
|
||||
"\n",
|
||||
"Thus, the most popular strategy is to start from the\n",
|
||||
"simplest class of models and increase the complexity of the models only when the\n",
|
||||
"simpler models become inadequate. For instance, if we work with a regression problem to fit a set of sample points, one\n",
|
||||
"may first try the simplest class of models, namely linear models, followed obviously by more complex models.\n",
|
||||
"\n",
|
||||
"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.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Choice of Programming Language\n",
|
||||
"\n",
|
||||
"Python plays nowadays a central role in the development of machine\n",
|
||||
"learning techniques and tools for data analysis. In particular, seen\n",
|
||||
"the wealth of machine learning and data analysis libraries written in\n",
|
||||
"Python, easy to use libraries with immediate visualization(and not the\n",
|
||||
"least impressive galleries of existing examples), the popularity of the\n",
|
||||
"Jupyter notebook framework with the possibility to run **R** codes or\n",
|
||||
"compiled programs written in C++, and much more made our choice of\n",
|
||||
"programming language for this series of lectures easy. However,\n",
|
||||
"since the focus here is not only on using existing Python libraries such\n",
|
||||
"as **Scikit-Learn**, **Tensorflow** and **Pytorch**, but also on developing your own\n",
|
||||
"algorithms and codes, we will as far as possible present many of these\n",
|
||||
"algorithms either as a Python codes or C++ or Fortran (or other languages) codes. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Data handling, machine learning and ethical aspects\n",
|
||||
"\n",
|
||||
"In most of the cases we will study, we will either generate the data\n",
|
||||
"to analyze ourselves (both for supervised learning and unsupervised\n",
|
||||
"learning) or we will recur again and again to data present in say\n",
|
||||
"**Scikit-Learn** or **Tensorflow**. Many of the examples we end up\n",
|
||||
"dealing with are from a privacy and data protection point of view,\n",
|
||||
"rather inoccuous and boring results of numerical\n",
|
||||
"calculations. However, this does not hinder us from developing a sound\n",
|
||||
"ethical attitude to the data we use, how we analyze the data and how\n",
|
||||
"we handle the data.\n",
|
||||
"\n",
|
||||
"The most immediate and simplest possible ethical aspects deal with our\n",
|
||||
"approach to the scientific process. Nowadays, with version control\n",
|
||||
"software like [Git](https://git-scm.com/) and various online\n",
|
||||
"repositories like [Github](https://github.com/),\n",
|
||||
"[Gitlab](https://about.gitlab.com/) etc, we can easily make our codes\n",
|
||||
"and data sets we have used, freely and easily accessible to a wider\n",
|
||||
"community. This helps us almost automagically in making our science\n",
|
||||
"reproducible. The large open-source development communities involved\n",
|
||||
"in say [Scikit-Learn](http://scikit-learn.org/stable/),\n",
|
||||
"[Tensorflow](https://www.tensorflow.org/),\n",
|
||||
"[PyTorch](http://pytorch.org/) and [Keras](https://keras.io/), are\n",
|
||||
"all excellent examples of this. The codes can be tested and improved\n",
|
||||
"upon continuosly, helping thereby our scientific community at large in\n",
|
||||
"developing data analysis and machine learning tools. It is much\n",
|
||||
"easier today to gain traction and acceptance for making your science\n",
|
||||
"reproducible. From a societal stand, this is an important element\n",
|
||||
"since many of the developers are employees of large public institutions like\n",
|
||||
"universities and research labs. Our fellow taxpayers do deserve to get\n",
|
||||
"something back for their bucks.\n",
|
||||
"\n",
|
||||
"However, this more mechanical aspect of the ethics of science (in\n",
|
||||
"particular the reproducibility of scientific results) is something\n",
|
||||
"which is obvious and everybody should do so as part of the dialectics of\n",
|
||||
"science. The fact that many scientists are not willing to share their codes or \n",
|
||||
"data is detrimental to the scientific discourse.\n",
|
||||
"\n",
|
||||
"Before we proceed, we should add a disclaimer. Even though\n",
|
||||
"we may dream of computers developing some kind of higher learning\n",
|
||||
"capabilities, at the end (even if the artificial intelligence\n",
|
||||
"community keeps touting our ears full of fancy futuristic avenues), it is we, yes you reading these lines,\n",
|
||||
"who end up constructing and instructing, via various algorithms, the\n",
|
||||
"machine learning approaches. Self-driving cars for example, rely on sofisticated\n",
|
||||
"programs which take into account all possible situations a car can\n",
|
||||
"encounter. In addition, extensive usage of training data from GPS\n",
|
||||
"information, maps etc, are typically fed into the software for\n",
|
||||
"self-driving cars. Adding to this various sensors and cameras that\n",
|
||||
"feed information to the programs, there are zillions of ethical issues\n",
|
||||
"which arise from this.\n",
|
||||
"\n",
|
||||
"For self-driving cars, where basically many of the standard machine\n",
|
||||
"learning algorithms discussed here enter into the codes, at a certain\n",
|
||||
"stage we have to make choices. Yes, we , the lads and lasses who wrote\n",
|
||||
"a program for a specific brand of a self-driving car. As an example,\n",
|
||||
"all carmakers have as their utmost priority the security of the\n",
|
||||
"driver and the accompanying passengers. A famous European carmaker, which is\n",
|
||||
"one of the leaders in the market of self-driving cars, had **if**\n",
|
||||
"statements of the following type: suppose there are two obstacles in\n",
|
||||
"front of you and you cannot avoid to collide with one of them. One of\n",
|
||||
"the obstacles is a monstertruck while the other one is a kindergarten\n",
|
||||
"class trying to cross the road. The self-driving car algo would then\n",
|
||||
"opt for the hitting the small folks instead of the monstertruck, since\n",
|
||||
"the likelihood of surving a collision with our future citizens, is\n",
|
||||
"much higher.\n",
|
||||
"\n",
|
||||
"This leads to serious ethical aspects. Why should we opt for such an\n",
|
||||
"option? Who decides and who is entitled to make such choices? Keep in\n",
|
||||
"mind that many of the algorithms you will encounter in this series of\n",
|
||||
"lectures or hear about later, are indeed based on simple programming\n",
|
||||
"instructions. And you are very likely to be one of the people who may\n",
|
||||
"end up writing such a code. Thus, developing a sound ethical attitude\n",
|
||||
"to what we do, an approach well beyond the simple mechanistic one of\n",
|
||||
"making our science available and reproducible, is much needed. The\n",
|
||||
"example of the self-driving cars is just one of infinitely many cases\n",
|
||||
"where we have to make choices. When you analyze data on economic\n",
|
||||
"inequalities, who guarantees that you are not weighting some data in a\n",
|
||||
"particular way, perhaps because you dearly want a specific conclusion\n",
|
||||
"which may support your political views? Or what about the recent\n",
|
||||
"claims that a famous IT company like Apple has a sexist bias on the\n",
|
||||
"their recently [launched credit card](https://qz.com/1748321/the-role-of-goldman-sachs-algorithms-in-the-apple-credit-card-scandal/)?\n",
|
||||
"\n",
|
||||
"We do not have the answers here, nor will we venture into a deeper\n",
|
||||
"discussions of these aspects, but we want you think over these topics\n",
|
||||
"in a more overarching way. A statistical data analysis with its dry\n",
|
||||
"numbers and graphs meant to guide the eye, does not necessarily\n",
|
||||
"reflect the truth, whatever that is. As a scientist, and after a\n",
|
||||
"university education, you are supposedly a better citizen, with an\n",
|
||||
"improved critical view and understanding of the scientific method, and\n",
|
||||
"perhaps some deeper understanding of the ethics of science at\n",
|
||||
"large. Use these insights. Be a critical citizen. You owe it to our\n",
|
||||
"society."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,583 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Logistic Regression\n",
|
||||
"\n",
|
||||
"## Introduction\n",
|
||||
"In linear regression our main interest was centered on learning the\n",
|
||||
"coefficients of a functional fit (say a polynomial) in order to be\n",
|
||||
"able to predict the response of a continuous variable on some unseen\n",
|
||||
"data. The fit to the continuous variable $y_i$ is based on some\n",
|
||||
"independent variables $\\hat{x}_i$. Linear regression resulted in\n",
|
||||
"analytical expressions for standard ordinary Least Squares or Ridge\n",
|
||||
"regression (in terms of matrices to invert) for several quantities,\n",
|
||||
"ranging from the variance and thereby the confidence intervals of the\n",
|
||||
"parameters $\\hat{\\beta}$ to the mean squared error. If we can invert\n",
|
||||
"the product of the design matrices, linear regression gives then a\n",
|
||||
"simple recipe for fitting our data.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Classification problems, however, are concerned with outcomes taking\n",
|
||||
"the form of discrete variables (i.e. categories). We may for example,\n",
|
||||
"on the basis of DNA sequencing for a number of patients, like to find\n",
|
||||
"out which mutations are important for a certain disease; or based on\n",
|
||||
"scans of various patients' brains, figure out if there is a tumor or\n",
|
||||
"not; or given a specific physical system, we'd like to identify its\n",
|
||||
"state, say whether it is an ordered or disordered system (typical\n",
|
||||
"situation in solid state physics); or classify the status of a\n",
|
||||
"patient, whether she/he has a stroke or not and many other similar\n",
|
||||
"situations.\n",
|
||||
"\n",
|
||||
"The most common situation we encounter when we apply logistic\n",
|
||||
"regression is that of two possible outcomes, normally denoted as a\n",
|
||||
"binary outcome, true or false, positive or negative, success or\n",
|
||||
"failure etc.\n",
|
||||
"\n",
|
||||
"Logistic regression will also serve as our stepping stone towards\n",
|
||||
"neural network algorithms and supervised deep learning. For logistic\n",
|
||||
"learning, the minimization of the cost function leads to a non-linear\n",
|
||||
"equation in the parameters $\\hat{\\beta}$. The optimization of the\n",
|
||||
"problem calls therefore for minimization algorithms. This forms the\n",
|
||||
"bottle neck of all machine learning algorithms, namely how to find\n",
|
||||
"reliable minima of a multi-variable function. This leads us to the\n",
|
||||
"family of gradient descent methods. The latter are the working horses\n",
|
||||
"of basically all modern machine learning algorithms.\n",
|
||||
"\n",
|
||||
"We note also that many of the topics discussed here on logistic \n",
|
||||
"regression are also commonly used in modern supervised Deep Learning\n",
|
||||
"models, as we will see later.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Basics\n",
|
||||
"\n",
|
||||
"We consider the case where the dependent variables, also called the\n",
|
||||
"responses or the outcomes, $y_i$ are discrete and only take values\n",
|
||||
"from $k=0,\\dots,K-1$ (i.e. $K$ classes).\n",
|
||||
"\n",
|
||||
"The goal is to predict the\n",
|
||||
"output classes from the design matrix $\\hat{X}\\in\\mathbb{R}^{n\\times p}$\n",
|
||||
"made of $n$ samples, each of which carries $p$ features or predictors. The\n",
|
||||
"primary goal is to identify the classes to which new unseen samples\n",
|
||||
"belong.\n",
|
||||
"\n",
|
||||
"Let us specialize to the case of two classes only, with outputs\n",
|
||||
"$y_i=0$ and $y_i=1$. Our outcomes could represent the status of a\n",
|
||||
"credit card user that could default or not on her/his credit card\n",
|
||||
"debt. That is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"y_i = \\begin{bmatrix} 0 & \\mathrm{no}\\\\ 1 & \\mathrm{yes} \\end{bmatrix}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Before moving to the logistic model, let us try to use our linear\n",
|
||||
"regression model to classify these two outcomes. We could for example\n",
|
||||
"fit a linear model to the default case if $y_i > 0.5$ and the no\n",
|
||||
"default case $y_i \\leq 0.5$.\n",
|
||||
"\n",
|
||||
"We would then have our \n",
|
||||
"weighted linear combination, namely"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<!-- Equation labels as ordinary links -->\n",
|
||||
"<div id=\"_auto1\"></div>\n",
|
||||
"\n",
|
||||
"$$\n",
|
||||
"\\begin{equation}\n",
|
||||
"\\hat{y} = \\hat{X}^T\\hat{\\beta} + \\hat{\\epsilon},\n",
|
||||
"\\label{_auto1} \\tag{1}\n",
|
||||
"\\end{equation}\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"where $\\hat{y}$ is a vector representing the possible outcomes, $\\hat{X}$ is our\n",
|
||||
"$n\\times p$ design matrix and $\\hat{\\beta}$ represents our estimators/predictors.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The main problem with our function is that it takes values on the\n",
|
||||
"entire real axis. In the case of logistic regression, however, the\n",
|
||||
"labels $y_i$ are discrete variables. A typical example is the credit\n",
|
||||
"card data discussed below here, where we can set the state of\n",
|
||||
"defaulting the debt to $y_i=1$ and not to $y_i=0$ for one the persons\n",
|
||||
"in the data set (see the full example below).\n",
|
||||
"\n",
|
||||
"One simple way to get a discrete output is to have sign\n",
|
||||
"functions that map the output of a linear regressor to values $\\{0,1\\}$,\n",
|
||||
"$f(s_i)=sign(s_i)=1$ if $s_i\\ge 0$ and 0 if otherwise. \n",
|
||||
"We will encounter this model in our first demonstration of neural networks. Historically it is called the \"perceptron\" model in the machine learning\n",
|
||||
"literature. This model is extremely simple. However, in many cases it is more\n",
|
||||
"favorable to use a ``soft\" classifier that outputs\n",
|
||||
"the probability of a given category. This leads us to the logistic function.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## The logistic function\n",
|
||||
"\n",
|
||||
"The perceptron is an example of a ``hard classification\" model. We\n",
|
||||
"will encounter this model when we discuss neural networks as\n",
|
||||
"well. Each datapoint is deterministically assigned to a category (i.e\n",
|
||||
"$y_i=0$ or $y_i=1$). In many cases, it is favorable to have a \"soft\"\n",
|
||||
"classifier that outputs the probability of a given category rather\n",
|
||||
"than a single value. For example, given $x_i$, the classifier\n",
|
||||
"outputs the probability of being in a category $k$. Logistic regression\n",
|
||||
"is the most common example of a so-called soft classifier. In logistic\n",
|
||||
"regression, the probability that a data point $x_i$\n",
|
||||
"belongs to a category $y_i=\\{0,1\\}$ is given by the so-called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(t) = \\frac{1}{1+\\mathrm \\exp{-t}}=\\frac{\\exp{t}}{1+\\mathrm \\exp{t}}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Note that $1-p(t)= p(-t)$.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The following code plots the logistic function, the step function and other functions we will encounter from here and on."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"\n",
|
||||
"\"\"\"The sigmoid function (or the logistic curve) is a\n",
|
||||
"function that takes any real number, z, and outputs a number (0,1).\n",
|
||||
"It is useful in neural networks for assigning weights on a relative scale.\n",
|
||||
"The value z is the weighted sum of parameters involved in the learning algorithm.\"\"\"\n",
|
||||
"\n",
|
||||
"import numpy\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import math as mt\n",
|
||||
"\n",
|
||||
"z = numpy.arange(-5, 5, .1)\n",
|
||||
"sigma_fn = numpy.vectorize(lambda z: 1/(1+numpy.exp(-z)))\n",
|
||||
"sigma = sigma_fn(z)\n",
|
||||
"\n",
|
||||
"fig = plt.figure()\n",
|
||||
"ax = fig.add_subplot(111)\n",
|
||||
"ax.plot(z, sigma)\n",
|
||||
"ax.set_ylim([-0.1, 1.1])\n",
|
||||
"ax.set_xlim([-5,5])\n",
|
||||
"ax.grid(True)\n",
|
||||
"ax.set_xlabel('z')\n",
|
||||
"ax.set_title('sigmoid function')\n",
|
||||
"\n",
|
||||
"plt.show()\n",
|
||||
"\n",
|
||||
"\"\"\"Step Function\"\"\"\n",
|
||||
"z = numpy.arange(-5, 5, .02)\n",
|
||||
"step_fn = numpy.vectorize(lambda z: 1.0 if z >= 0.0 else 0.0)\n",
|
||||
"step = step_fn(z)\n",
|
||||
"\n",
|
||||
"fig = plt.figure()\n",
|
||||
"ax = fig.add_subplot(111)\n",
|
||||
"ax.plot(z, step)\n",
|
||||
"ax.set_ylim([-0.5, 1.5])\n",
|
||||
"ax.set_xlim([-5,5])\n",
|
||||
"ax.grid(True)\n",
|
||||
"ax.set_xlabel('z')\n",
|
||||
"ax.set_title('step function')\n",
|
||||
"\n",
|
||||
"plt.show()\n",
|
||||
"\n",
|
||||
"\"\"\"tanh Function\"\"\"\n",
|
||||
"z = numpy.arange(-2*mt.pi, 2*mt.pi, 0.1)\n",
|
||||
"t = numpy.tanh(z)\n",
|
||||
"\n",
|
||||
"fig = plt.figure()\n",
|
||||
"ax = fig.add_subplot(111)\n",
|
||||
"ax.plot(z, t)\n",
|
||||
"ax.set_ylim([-1.0, 1.0])\n",
|
||||
"ax.set_xlim([-2*mt.pi,2*mt.pi])\n",
|
||||
"ax.grid(True)\n",
|
||||
"ax.set_xlabel('z')\n",
|
||||
"ax.set_title('tanh function')\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Two parameters\n",
|
||||
"\n",
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\begin{align*}\n",
|
||||
"p(y_i=1|x_i,\\hat{\\beta}) &= \\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}},\\nonumber\\\\\n",
|
||||
"p(y_i=0|x_i,\\hat{\\beta}) &= 1 - p(y_i=1|x_i,\\hat{\\beta}),\n",
|
||||
"\\end{align*}\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"where $\\hat{\\beta}$ are the weights we wish to extract from data, in our case $\\beta_0$ and $\\beta_1$. \n",
|
||||
"\n",
|
||||
"Note that we used"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(y_i=0\\vert x_i, \\hat{\\beta}) = 1-p(y_i=1\\vert x_i, \\hat{\\beta}).\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Maximum likelihood\n",
|
||||
"\n",
|
||||
"In order to define the total likelihood for all possible outcomes from a \n",
|
||||
"dataset $\\mathcal{D}=\\{(y_i,x_i)\\}$, with the binary labels\n",
|
||||
"$y_i\\in\\{0,1\\}$ and where the data points are drawn independently, we use the so-called [Maximum Likelihood Estimation](https://en.wikipedia.org/wiki/Maximum_likelihood_estimation) (MLE) principle. \n",
|
||||
"We aim thus at maximizing \n",
|
||||
"the probability of seeing the observed data. We can then approximate the \n",
|
||||
"likelihood in terms of the product of the individual probabilities of a specific outcome $y_i$, that is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\begin{align*}\n",
|
||||
"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 \\\\\n",
|
||||
"\\end{align*}\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"from which we obtain the log-likelihood and our **cost/loss** function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\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).\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Reordering the logarithms, we can rewrite the **cost/loss** function as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\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).\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to $\\beta$.\n",
|
||||
"Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\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).\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This equation is known in statistics as the **cross entropy**. Finally, we note that just as in linear regression, \n",
|
||||
"in practice we often supplement the cross-entropy with additional regularization terms, usually $L_1$ and $L_2$ regularization as we did for Ridge and Lasso regression.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The cross entropy is a convex function of the weights $\\hat{\\beta}$ and,\n",
|
||||
"therefore, any local minimizer is a global minimizer. \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Minimizing this\n",
|
||||
"cost function with respect to the two parameters $\\beta_0$ and $\\beta_1$ we obtain"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\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),\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"and"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\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).\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let us now define a vector $\\hat{y}$ with $n$ elements $y_i$, an\n",
|
||||
"$n\\times p$ matrix $\\hat{X}$ which contains the $x_i$ values and a\n",
|
||||
"vector $\\hat{p}$ of fitted probabilities $p(y_i\\vert x_i,\\hat{\\beta})$. We can rewrite in a more compact form the first\n",
|
||||
"derivative of cost function as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial \\mathcal{C}(\\hat{\\beta})}{\\partial \\hat{\\beta}} = -\\hat{X}^T\\left(\\hat{y}-\\hat{p}\\right).\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If we in addition define a diagonal matrix $\\hat{W}$ with elements \n",
|
||||
"$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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\frac{\\partial^2 \\mathcal{C}(\\hat{\\beta})}{\\partial \\hat{\\beta}\\partial \\hat{\\beta}^T} = \\hat{X}^T\\hat{W}\\hat{X}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Within a binary classification problem, we can easily expand our model to include multiple predictors. Our ratio between likelihoods is then with $p$ predictors"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\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.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"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)}}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Including more classes\n",
|
||||
"\n",
|
||||
"Till now we have mainly focused on two classes, the so-called binary\n",
|
||||
"system. Suppose we wish to extend to $K$ classes. Let us for the sake\n",
|
||||
"of simplicity assume we have only two predictors. We have then\n",
|
||||
"following model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"1\n",
|
||||
"5\n",
|
||||
" \n",
|
||||
"<\n",
|
||||
"<\n",
|
||||
"<\n",
|
||||
"!\n",
|
||||
"!\n",
|
||||
"M\n",
|
||||
"A\n",
|
||||
"T\n",
|
||||
"H\n",
|
||||
"_\n",
|
||||
"B\n",
|
||||
"L\n",
|
||||
"O\n",
|
||||
"C\n",
|
||||
"K"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=2\\vert x)}{p(K\\vert x)}} = \\beta_{20}+\\beta_{21}x_1,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"and so on till the class $C=K-1$ class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\log{\\frac{p(C=K-1\\vert x)}{p(K\\vert x)}} = \\beta_{(K-1)0}+\\beta_{(K-1)1}x_1,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"and the model is specified in term of $K-1$ so-called log-odds or\n",
|
||||
"**logit** transformations.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"In our discussion of neural networks we will encounter the above again\n",
|
||||
"in terms of a slightly modified function, the so-called **Softmax** function.\n",
|
||||
"\n",
|
||||
"The softmax function is used in various multiclass classification\n",
|
||||
"methods, such as multinomial logistic regression (also known as\n",
|
||||
"softmax regression), multiclass linear discriminant analysis, naive\n",
|
||||
"Bayes classifiers, and artificial neural networks. Specifically, in\n",
|
||||
"multinomial logistic regression and linear discriminant analysis, the\n",
|
||||
"input to the function is the result of $K$ distinct linear functions,\n",
|
||||
"and the predicted probability for the $k$-th class given a sample\n",
|
||||
"vector $\\hat{x}$ and a weighting vector $\\hat{\\beta}$ is (with two\n",
|
||||
"predictors):"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"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)}}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"It is easy to extend to more predictors. The final class is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"p(C=K\\vert \\mathbf {x} )=\\frac{1}{1+\\sum_{l=1}^{K-1}\\exp{(\\beta_{l0}+\\beta_{l1}x_1)}},\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"and they sum to one. Our earlier discussions were all specialized to\n",
|
||||
"the case with two classes only. It is easy to see from the above that\n",
|
||||
"what we derived earlier is compatible with these equations.\n",
|
||||
"\n",
|
||||
"To find the optimal parameters we would typically use a gradient\n",
|
||||
"descent method. Newton's method and gradient descent methods are\n",
|
||||
"discussed in the material on [optimization\n",
|
||||
"methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html)."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,17 +1,13 @@
|
||||
TITLE: Data Analysis and Machine Learning: Elements of Probability Theory and Statistical Data Analysis
|
||||
AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
|
||||
DATE: today
|
||||
======= Elements of Probability Theory and Statistical Data Analysis =======
|
||||
|
||||
|
||||
!split
|
||||
===== To do list =====
|
||||
|
||||
* add math about MVN and define MLE and other quantities
|
||||
* rewrite about covariance matrix
|
||||
* add KL theorem
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Domains and probabilities =====
|
||||
!bblock
|
||||
Consider the following simple example, namely the tossing of two dice, resulting in the following possible values
|
||||
@@ -29,7 +25,7 @@ To this domain we have the corresponding *probabilities*
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Tossing the dice =====
|
||||
!bblock
|
||||
The numbers in the domain are the outcomes of the physical process of tossing say two dice.
|
||||
@@ -55,14 +51,14 @@ appear in a random order. After 11 throws the results may look like
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Stochastic variables =====
|
||||
!bblock
|
||||
|
||||
_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)_.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Stochastic variables and the main concepts, the discrete case =====
|
||||
!bblock
|
||||
There are two main concepts associated with a stochastic variable. The
|
||||
@@ -83,7 +79,7 @@ p(x) = \mathrm{Prob}(X=x).
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Stochastic variables and the main concepts, the continuous case =====
|
||||
!bblock
|
||||
In the continuous case, the PDF does not directly depict the
|
||||
@@ -104,7 +100,7 @@ numbers chosen as if by chance from some specified PDF so that the
|
||||
selection of a large set of these numbers reproduces this PDF.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== The cumulative probability =====
|
||||
!bblock
|
||||
Of interest to us is the *cumulative probability
|
||||
@@ -125,7 +121,7 @@ p(x) = \frac{d}{dx}P(x).
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Properties of PDFs =====
|
||||
!bblock
|
||||
|
||||
@@ -150,7 +146,7 @@ both discrete and continuous PDFs, this condition is
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Important distributions, the uniform distribution =====
|
||||
!bblock
|
||||
The first one
|
||||
@@ -172,7 +168,7 @@ p(x)dx = dx & \in [0,1].
|
||||
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.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Gaussian distribution =====
|
||||
!bblock
|
||||
The second one is the Gaussian Distribution
|
||||
@@ -231,7 +227,7 @@ plt.show()
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Exponential distribution =====
|
||||
!bblock
|
||||
Another important distribution in science is the exponential distribution
|
||||
@@ -242,7 +238,7 @@ p(x) = \alpha\exp{-(\alpha x)}.
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Expectation values =====
|
||||
!bblock
|
||||
Let $h(x)$ be an arbitrary continuous function on the domain of the stochastic
|
||||
@@ -267,7 +263,7 @@ follows
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Stochastic variables and the main concepts, mean values =====
|
||||
!bblock
|
||||
The zero-th moment $\langle 1\rangle$ is just the normalization condition of
|
||||
@@ -289,7 +285,7 @@ Qualitatively it represents the centroid or the average value of the
|
||||
PDF and is therefore simply called the expectation value of $p(x)$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Stochastic variables and the main concepts, central moments, the variance =====
|
||||
!bblock
|
||||
|
||||
@@ -320,7 +316,7 @@ qualitatively as the ``spread'' of $p$ around its mean.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions =====
|
||||
!bblock
|
||||
|
||||
@@ -344,7 +340,7 @@ while $P(x)$ is the cumulative probability.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions =====
|
||||
!bblock
|
||||
With a PDF we can compute expectation values of selected quantities such as
|
||||
@@ -365,7 +361,7 @@ in the case of a continuous PDF. We have already defined the mean value $\mu$
|
||||
and the variance $\sigma^2$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== The three famous Probability Distribution Functions =====
|
||||
!bblock
|
||||
|
||||
@@ -399,7 +395,7 @@ with variance
|
||||
\end{equation*}
|
||||
!et
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions, the normal distribution =====
|
||||
!bblock
|
||||
Finally, we have the so-called univariate normal distribution, or just the _normal distribution_
|
||||
@@ -425,7 +421,7 @@ which becomes with a suitable change of variables
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions, the normal distribution =====
|
||||
!bblock
|
||||
Similarly, the variance becomes
|
||||
@@ -453,7 +449,7 @@ centered around zero and with a variance $\sigma^2=1$, leading to
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions, the cumulative distribution =====
|
||||
!bblock
|
||||
|
||||
@@ -470,7 +466,7 @@ which is difficult to evaluate in a quick way.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions, other important distribution =====
|
||||
!bblock
|
||||
|
||||
@@ -492,7 +488,7 @@ The sequence of binomial trials is characterized by the following definitions
|
||||
* 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$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions, the binomial distribution =====
|
||||
!bblock
|
||||
|
||||
@@ -535,7 +531,7 @@ which we rewrite as
|
||||
!eblock
|
||||
The variance is slightly trickier to get. It reads $\sigma^2=ny(1-y)$.
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions, Poisson's distribution =====
|
||||
!bblock
|
||||
|
||||
@@ -559,7 +555,7 @@ and the variance is $\sigma^2=\lambda$.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Probability Distribution Functions, Poisson's distribution =====
|
||||
!bblock
|
||||
An example of applications of the Poisson distribution could be the counting
|
||||
@@ -576,7 +572,7 @@ the binomial distribution we can show that
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Meet the covariance! =====
|
||||
!bblock
|
||||
An important quantity in a statistical analysis is the so-called covariance.
|
||||
@@ -604,7 +600,7 @@ with
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Meet the covariance in matrix disguise =====
|
||||
!bblock
|
||||
If we consider the above covariance as a matrix
|
||||
@@ -619,7 +615,7 @@ all the off-diagonal elements are zero if the stochastic variables are
|
||||
uncorrelated.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Covariance =====
|
||||
!bc pycod
|
||||
# Importing various packages
|
||||
@@ -651,7 +647,7 @@ print(c)
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Meet the covariance, uncorrelated events =====
|
||||
!bblock
|
||||
|
||||
@@ -683,7 +679,7 @@ Cov(X_i, X_j) = 0 \hspace{0.1cm} (i\neq j).
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Numerical experiments and the covariance =====
|
||||
!bblock
|
||||
|
||||
@@ -704,7 +700,7 @@ chain of values
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Numerical experiments and the covariance =====
|
||||
!bblock
|
||||
We will call these
|
||||
@@ -722,7 +718,7 @@ $\mu_X^{\phantom X}$ and the variance $\sigma_X^{\phantom X}$.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Numerical experiments and the covariance, actual situations =====
|
||||
!bblock
|
||||
In practical situations however, a sample is always of finite size. Let that
|
||||
@@ -742,7 +738,7 @@ with its square root being the *standard deviation of the sample*.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Numerical experiments and the covariance, our observables =====
|
||||
!bblock
|
||||
You can think of the above observables as a set of quantities which define
|
||||
@@ -770,7 +766,7 @@ label{eq:exptvariance}
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Numerical experiments and the covariance, the sample variance =====
|
||||
!bblock
|
||||
|
||||
@@ -791,7 +787,7 @@ exact values for the mean value $\mu_X$, the variance $\mathrm{Var}(X)$
|
||||
and the covariance $\mathrm{Cov}(X,Y)$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Numerical experiments and the covariance, central limit theorem =====
|
||||
!bblock
|
||||
|
||||
@@ -813,7 +809,7 @@ standard deviation, given by
|
||||
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.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Definition of Correlation Functions and Standard Deviation =====
|
||||
!bblock
|
||||
Our estimate of the true average $\mu_{X}$ is the sample mean $\langle X_m \rangle$
|
||||
@@ -845,7 +841,7 @@ and the last term is nothing but the covariance which arises when $k\ne l$.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Definition of Correlation Functions and Standard Deviation =====
|
||||
!bblock
|
||||
Our estimate of the true average $\mu_{X}$ is the sample mean $\langle X_m \rangle$
|
||||
@@ -863,7 +859,7 @@ measurements to calculate the cross terms. Therefore, all measurements
|
||||
have to be stored throughout the experiment.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Definition of Correlation Functions and Standard Deviation =====
|
||||
!bblock
|
||||
|
||||
@@ -887,7 +883,7 @@ $f_d$
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Definition of Correlation Functions and Standard Deviation =====
|
||||
!bblock
|
||||
The value of $f_d$ reflects the correlation between measurements
|
||||
@@ -905,7 +901,7 @@ which gives us a useful measure of the correlation pair correlation
|
||||
starting always at $1$ for $d=0$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Definition of Correlation Functions and Standard Deviation, sample variance =====
|
||||
!bblock
|
||||
|
||||
@@ -938,7 +934,7 @@ equals 1.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Definition of Correlation Functions and Standard Deviation =====
|
||||
!bblock
|
||||
From the point of view of
|
||||
@@ -961,7 +957,7 @@ more practically oriented methods like the blocking technique.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Code to compute the Covariance matrix and the Covariance =====
|
||||
!bc pycod
|
||||
# Importing various packages
|
||||
@@ -1003,7 +999,7 @@ Eigvals, Eigvecs = np.linalg.eig(c)
|
||||
print(Eigvals)
|
||||
!ec
|
||||
|
||||
!split
|
||||
|
||||
======= Random Numbers =======
|
||||
!bblock
|
||||
|
||||
@@ -1020,7 +1016,7 @@ or Monte Carlo computer work.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Random Numbers, better name: pseudo random numbers =======
|
||||
!bblock
|
||||
|
||||
@@ -1040,7 +1036,7 @@ numbers, hopefully abiding to the following criteria:
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG =======
|
||||
!bblock
|
||||
The most common random number generators are based on so-called
|
||||
@@ -1067,7 +1063,7 @@ that is if we were to evaluate $(13)\mathrm{MOD}(9)$, the outcome is the remaind
|
||||
of the division $13/9$, namely $4$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG and periodic outputs =======
|
||||
!bblock
|
||||
|
||||
@@ -1097,7 +1093,7 @@ which still, with $N_0=2$, results in $11,38,11,38,11,38,\dots$, a period of
|
||||
just $2$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG and its period =======
|
||||
!bblock
|
||||
Typical periods for the random generators provided in the program library
|
||||
@@ -1115,7 +1111,7 @@ number is the sum of the $l-i$th and $l-j$th values with modulo $M$,
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG, other examples =======
|
||||
!bblock
|
||||
Such a generator again produces a sequence of pseudorandom numbers
|
||||
@@ -1141,7 +1137,7 @@ followed by
|
||||
which according to the authors has a period larger than $2^{94}$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG, other examples =======
|
||||
!bblock
|
||||
Instead of using modular addition, we could use the bitwise
|
||||
@@ -1165,7 +1161,7 @@ it is given by $m\wedge n$.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG, RAN0 =======
|
||||
!bblock
|
||||
|
||||
@@ -1203,7 +1199,7 @@ $q$ and $r$ are chosen so that $r < q$.
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG, RAN0 =======
|
||||
!bblock
|
||||
|
||||
@@ -1220,7 +1216,7 @@ $[N_{i-1}/q]$ just yields a constant which is multiplied with $M$.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG, RAN0 =======
|
||||
!bblock
|
||||
We can now rewrite Eq. (ref{eq:rntrick1}) as
|
||||
@@ -1250,7 +1246,7 @@ label{eq:rntrick4}
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG, RAN0 =======
|
||||
!bblock
|
||||
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
|
||||
@@ -1268,7 +1264,7 @@ set to $0$.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Random number generator RNG, RAN0 code =======
|
||||
!bblock
|
||||
|
||||
@@ -1302,7 +1298,7 @@ double ran0(long &idum)
|
||||
!ec
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Properties of Selected Random Number Generators =====
|
||||
!bblock
|
||||
|
||||
@@ -1321,7 +1317,7 @@ Two additional measures are the standard deviation $\sigma$ and the mean
|
||||
$\mu=\langle x\rangle$.
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Properties of Selected Random Number Generators =====
|
||||
!bblock
|
||||
For the uniform distribution, the mean value $\mu$ is then
|
||||
@@ -1340,7 +1336,7 @@ while the standard deviation is
|
||||
!et
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Properties of Selected Random Number Generators =====
|
||||
!bblock
|
||||
The various random number generators produce results which agree rather well with
|
||||
@@ -1365,7 +1361,7 @@ these limiting values.
|
||||
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Simple demonstration of RNGs using python =====
|
||||
!bblock
|
||||
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.
|
||||
@@ -1396,7 +1392,7 @@ plt.show()
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Properties of Selected Random Number Generators =====
|
||||
!bblock
|
||||
Since our random numbers, which are typically generated via a linear congruential algorithm,
|
||||
@@ -1427,7 +1423,7 @@ assumption for approximating $\sigma_N$ is no longer valid.
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== 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)$.
|
||||
!bc pycod
|
||||
@@ -1479,7 +1475,7 @@ For the remaining values we notice that there are still non-zero values for the
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Correlation function and which random number generators should I use =====
|
||||
!bblock
|
||||
The program here computes the correlation function for one of the standard functions included with the c++ compiler.
|
||||
@@ -1555,7 +1551,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
======= Which RNG should I use? =======
|
||||
!bblock
|
||||
* C++ has a class called _random_. The "random class":"http://www.cplusplus.com/reference/random/" 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 "Mersenne twister random number engine":"http://www.cplusplus.com/reference/random/mersenne_twister_engine/" has a period of $2^{19937}$.
|
||||
@@ -1564,7 +1560,7 @@ int main(int argc, char* argv[])
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== How to use the Mersenne generator =====
|
||||
!bblock
|
||||
The following part of a c++ code (from project 4) sets up the uniform distribution for $x\in [0,1]$.
|
||||
@@ -1588,7 +1584,7 @@ int ix = (int) (RandomNumberGenerator(gen)*NSpins);
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Why blocking? =====
|
||||
!bblock Statistical analysis
|
||||
* Monte Carlo simulations can be treated as *computer experiments*
|
||||
@@ -1601,7 +1597,7 @@ A very good article which explains blocking is H. Flyvbjerg and H. G. Petersen,
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Why blocking? =====
|
||||
!bblock Statistical analysis
|
||||
* As in other experiments, Monte Carlo experiments have two classes of errors:
|
||||
@@ -1612,7 +1608,7 @@ A very good article which explains blocking is H. Flyvbjerg and H. G. Petersen,
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== 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.
|
||||
@@ -1700,7 +1696,7 @@ int main(int argc, char* argv[])
|
||||
!ec
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== What is blocking? =====
|
||||
!bblock Blocking
|
||||
* Say that we have a set of samples from a Monte Carlo experiment
|
||||
@@ -1719,7 +1715,7 @@ int main(int argc, char* argv[])
|
||||
where $\tau$ is the correlation time (the time between a sample and the next uncorrelated sample) and $\Delta t$ is time between each sample
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== What is blocking? =====
|
||||
!bblock Blocking
|
||||
* If $\Delta t\gg\tau$ our first estimate of $\sigma$ still holds
|
||||
@@ -1730,7 +1726,7 @@ int main(int argc, char* argv[])
|
||||
* The correlation time $\tau$ would be a good choice
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== What is blocking? =====
|
||||
!bblock Blocking
|
||||
* Problem: We don't know $\tau$ or it is too expensive to compute
|
||||
@@ -1739,7 +1735,7 @@ int main(int argc, char* argv[])
|
||||
* When the std. dev. stops increasing the blocks are uncorrelated
|
||||
!eblock
|
||||
|
||||
!split
|
||||
|
||||
===== Implementation =====
|
||||
!bblock
|
||||
* Do a Monte Carlo simulation, storing all samples to file
|
||||
@@ -1756,7 +1752,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== 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 "following python file":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py" reads data from file with the output from every Monte Carlo cycle.
|
||||
@@ -1797,7 +1793,7 @@ The "following python file":"https://github.com/CompPhysics/MachineLearning/blob
|
||||
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== The Bootstrap method =====
|
||||
|
||||
The Bootstrap resampling method is also very popular. It is very simple:
|
||||
@@ -1811,7 +1807,7 @@ that it produces the correct standard deviation.
|
||||
|
||||
This method is very useful for small ensembles of data points.
|
||||
|
||||
!split
|
||||
|
||||
===== 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.
|
||||
@@ -1820,7 +1816,7 @@ 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.
|
||||
|
||||
!split
|
||||
|
||||
===== Bootstrapping, recipe =====
|
||||
Let us assume we have done this $K$ times and thus have $K$ sets of $N$
|
||||
data values each.
|
||||
@@ -1844,7 +1840,7 @@ The difference is called bias and gives an idea on how far away the result may b
|
||||
the true $\angle\theta\rangle$. As final result for the observable one quotes $\angle\theta\rangle = \tilde{\theta} \pm \sigma_{\tilde{\theta}}$ .
|
||||
|
||||
|
||||
!split
|
||||
|
||||
===== Bootstrapping, "code":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py" =====
|
||||
!bc
|
||||
# Bootstrap
|
||||
@@ -1858,7 +1854,7 @@ the true $\angle\theta\rangle$. As final result for the observable one quotes $\
|
||||
self.bootStd = np.std(bootVec)
|
||||
!ec
|
||||
|
||||
!split
|
||||
|
||||
===== Jackknife, "code":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py" =====
|
||||
!bc
|
||||
# Jackknife
|
||||
|
||||
Reference in New Issue
Block a user