This commit is contained in:
Christian Forssen
2018-05-15 08:20:11 +02:00
161 changed files with 1488772 additions and 949 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
# FYS-STK3155/4155 Applied Data Analysis and Machine Learning
# FYS-STK3155/4155 Applied Data Analysis and Machine Learning, http://www.uio.no/studier/emner/matnat/fys/FYS-STK4155/index-eng.html
[![Build Status](https://travis-ci.org/CompPhysics/MachineLearning.svg?branch=master)](https://travis-ci.org/CompPhysics/MachineLearning)
File diff suppressed because one or more lines are too long
+922
View File
@@ -0,0 +1,922 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <p style=\"text-align: right;\"> Nicolas Dronchi </p>\n",
"\n",
"#### <p style=\"text-align: right;\"> Yitian, Jake, David </p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Day 23 In-Class Assignment: Artificial Neural Network\n",
"\n",
"</p>\n",
"\n",
"<img src= \"https://ml4a.github.io/images/temp_fig_mnist.png\" width=500px>\n",
"<p style=\"text-align: right;\">From: Machine Learning for Artists - https://ml4a.github.io/</p>\n",
"\n",
"\n",
"\n",
"1. **Scientific motivation** \n",
" - Data Analysis / Pattern Recognition\n",
"2. **Modeling tools** \n",
" - Artificial Neural networks\n",
" - Error Calculations\n",
"3. **Programming concepts** \n",
" - More Debugging\n",
" - Selecting and using libraries\n",
"4. **Python Programming Concepts** \n",
" - More Understanding classes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Agenda for today's class \n",
"\n",
"</p>\n",
"1. Review pre-class assignment\n",
"1. Modify code to be more flexible\n",
"1. Use our ANN on the \"Digits\" dataset\n",
"1. Finding/Using Neural Networks Libraries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"# 1. Review pre-class assignment\n",
"\n",
"Below we summarize the steps involved in designing and training a feed-forward artificial neural network. We will use the [partSix.py](./partSix.py) file provided in the \"Neural Networks Demystified\" module which can be downloaded from github:\n",
"\n",
" git clone https://github.com/stephencwelch/Neural-Networks-Demystified\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# %load partSix.py\n",
"# Neural Networks Demystified\n",
"# Part 6: Training\n",
"#\n",
"# Supporting code for short YouTube series on artificial neural networks.\n",
"#\n",
"# Stephen Welch\n",
"# @stephencwelch\n",
"\n",
"\n",
"## ----------------------- Part 1 ---------------------------- ##\n",
"import numpy as np\n",
"\n",
"# X = (hours sleeping, hours studying), y = Score on test\n",
"X = np.array(([3,5], [5,1], [10,2]), dtype=float)\n",
"y = np.array(([75], [82], [93]), dtype=float)\n",
"\n",
"# Normalize\n",
"X = X/np.amax(X, axis=0)\n",
"y = y/100 #Max test score is 100\n",
"\n",
"## ----------------------- Part 5 ---------------------------- ##\n",
"\n",
"class Neural_Network(object):\n",
" def __init__(self):\n",
" #Define Hyperparameters\n",
" self.inputLayerSize = 2\n",
" self.outputLayerSize = 1\n",
" self.hiddenLayerSize = 3\n",
"\n",
" #Weights (parameters)\n",
" self.W1 = np.random.randn(self.inputLayerSize,self.hiddenLayerSize)\n",
" self.W2 = np.random.randn(self.hiddenLayerSize,self.outputLayerSize)\n",
"\n",
" def forward(self, X):\n",
" #Propogate inputs though network\n",
" self.z2 = np.dot(X, self.W1)\n",
" self.a2 = self.sigmoid(self.z2)\n",
" self.z3 = np.dot(self.a2, self.W2)\n",
" yHat = self.sigmoid(self.z3)\n",
" return yHat\n",
"\n",
" def sigmoid(self, z):\n",
" #Apply sigmoid activation function to scalar, vector, or matrix\n",
" return 1/(1+np.exp(-z))\n",
"\n",
" def sigmoidPrime(self,z):\n",
" #Gradient of sigmoid\n",
" return np.exp(-z)/((1+np.exp(-z))**2)\n",
"\n",
" def costFunction(self, X, y):\n",
" #Compute cost for given X,y, use weights already stored in class.\n",
" self.yHat = self.forward(X)\n",
" J = 0.5*sum((y-self.yHat)**2)\n",
" return J\n",
"\n",
" def costFunctionPrime(self, X, y):\n",
" #Compute derivative with respect to W and W2 for a given X and y:\n",
" self.yHat = self.forward(X)\n",
"\n",
" delta3 = np.multiply(-(y-self.yHat), self.sigmoidPrime(self.z3))\n",
" dJdW2 = np.dot(self.a2.T, delta3)\n",
"\n",
" delta2 = np.dot(delta3, self.W2.T)*self.sigmoidPrime(self.z2)\n",
" dJdW1 = np.dot(X.T, delta2)\n",
"\n",
" return dJdW1, dJdW2\n",
"\n",
" #Helper Functions for interacting with other classes:\n",
" def getParams(self):\n",
" #Get W1 and W2 unrolled into vector:\n",
" params = np.concatenate((self.W1.ravel(), self.W2.ravel()))\n",
" return params\n",
"\n",
" def setParams(self, params):\n",
" #Set W1 and W2 using single paramater vector.\n",
" W1_start = 0\n",
" W1_end = self.hiddenLayerSize * self.inputLayerSize\n",
" self.W1 = np.reshape(params[W1_start:W1_end], (self.inputLayerSize , self.hiddenLayerSize))\n",
" W2_end = W1_end + self.hiddenLayerSize*self.outputLayerSize\n",
" self.W2 = np.reshape(params[W1_end:W2_end], (self.hiddenLayerSize, self.outputLayerSize))\n",
"\n",
" def computeGradients(self, X, y):\n",
" dJdW1, dJdW2 = self.costFunctionPrime(X, y)\n",
" return np.concatenate((dJdW1.ravel(), dJdW2.ravel()))\n",
"\n",
"def computeNumericalGradient(N, X, y):\n",
" paramsInitial = N.getParams()\n",
" numgrad = np.zeros(paramsInitial.shape)\n",
" perturb = np.zeros(paramsInitial.shape)\n",
" e = 1e-4\n",
"\n",
" for p in range(len(paramsInitial)):\n",
" #Set perturbation vector\n",
" perturb[p] = e\n",
" N.setParams(paramsInitial + perturb)\n",
" loss2 = N.costFunction(X, y)\n",
"\n",
" N.setParams(paramsInitial - perturb)\n",
" loss1 = N.costFunction(X, y)\n",
"\n",
" #Compute Numerical Gradient\n",
" numgrad[p] = (loss2 - loss1) / (2*e)\n",
"\n",
" #Return the value we changed to zero:\n",
" perturb[p] = 0\n",
"\n",
" #Return Params to original value:\n",
" N.setParams(paramsInitial)\n",
"\n",
" return numgrad\n",
"\n",
"## ----------------------- Part 6 ---------------------------- ##\n",
"from scipy import optimize\n",
"\n",
"\n",
"class trainer(object):\n",
" def __init__(self, N):\n",
" #Make Local reference to network:\n",
" self.N = N\n",
"\n",
" def callbackF(self, params):\n",
" self.N.setParams(params)\n",
" self.J.append(self.N.costFunction(self.X, self.y))\n",
"\n",
" def costFunctionWrapper(self, params, X, y):\n",
" self.N.setParams(params)\n",
" cost = self.N.costFunction(X, y)\n",
" grad = self.N.computeGradients(X,y)\n",
" return cost, grad\n",
"\n",
" def train(self, X, y):\n",
" #Make an internal variable for the callback function:\n",
" self.X = X\n",
" self.y = y\n",
"\n",
" #Make empty list to store costs:\n",
" self.J = []\n",
"\n",
" params0 = self.N.getParams()\n",
"\n",
" options = {'maxiter': 200, 'disp' : True}\n",
" _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', \\\n",
" args=(X, y), options=options, callback=self.callbackF)\n",
"\n",
" self.N.setParams(_res.x)\n",
" self.optimizationResults = _res\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input Data [[0.3 1. ]\n",
" [0.5 0.2]\n",
" [1. 0.4]]\n",
"Output Data [[0.75]\n",
" [0.82]\n",
" [0.93]]\n"
]
}
],
"source": [
"print(\"Input Data\", X)\n",
"print(\"Output Data\", y)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Untrained Output [[0.40080444]\n",
" [0.43447789]\n",
" [0.42423465]]\n"
]
}
],
"source": [
"#Untrained Random Network\n",
"NN = Neural_Network()\n",
"y1 = NN.forward(X)\n",
"print(\"Untrained Output\", y1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 0.000000\n",
" Iterations: 51\n",
" Function evaluations: 56\n",
" Gradient evaluations: 56\n"
]
}
],
"source": [
"#Training step\n",
"T = trainer(NN)\n",
"T.train(X,y)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trained Output [[0.75003127]\n",
" [0.81998936]\n",
" [0.92991079]]\n"
]
}
],
"source": [
"#Trained Network\n",
"y2 = NN.forward(X)\n",
"print(\"Trained Output\",y2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"&#9989; **DO THIS:** Calculate and compare the [mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error) for untrained network (```y1```) and the trained network (```y2```). "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.17545447]\n",
"[3.01663395e-09]\n"
]
}
],
"source": [
"#Put your code here\n",
"def MSE(y, yhat):\n",
" return (1/len(y1))*sum((y-yhat)**2)\n",
"\n",
"print(MSE(y, y1))\n",
"print(MSE(y, y2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"\n",
"# 2. Modify code to be more flexible\n",
"\n",
"The code for our Neural Network example above assumes an input layer size of 2, hidden layer size of 3 and an output layer size of 1. \n",
"\n",
"\n",
"&#9989; **DO THIS:** Modify the code in Section 1 above so that the user can specify these as inputs when creating the Neural_Network object. The default values should stay the same. Rerun the above example to make sure it still works. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Neural_Network(Neural_Network):\n",
" def __init__(self,insize, outsize, hiddensize):\n",
" #Define Hyperparameters\n",
" self.inputLayerSize = insize\n",
" self.outputLayerSize = outsize\n",
" self.hiddenLayerSize = hiddensize\n",
"\n",
" #Weights (parameters)\n",
" self.W1 = np.random.randn(self.inputLayerSize,self.hiddenLayerSize)\n",
" self.W2 = np.random.randn(self.hiddenLayerSize,self.outputLayerSize)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Untrained Output [[0.40867166]\n",
" [0.36286813]\n",
" [0.35807708]]\n"
]
}
],
"source": [
"#Untrained Random Network\n",
"NN = Neural_Network(2,1,5)\n",
"y1 = NN.forward(X)\n",
"print(\"Untrained Output\", y1)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 0.000000\n",
" Iterations: 51\n",
" Function evaluations: 53\n",
" Gradient evaluations: 53\n"
]
}
],
"source": [
"T = trainer(NN)\n",
"T.train(X,y)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trained Output [[0.75000449]\n",
" [0.81997275]\n",
" [0.92997594]]\n"
]
}
],
"source": [
"#Trained Network\n",
"y2 = NN.forward(X)\n",
"print(\"Trained Output\",y2)\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.21752347]\n",
"[4.47242084e-10]\n"
]
}
],
"source": [
"print(MSE(y, y1))\n",
"print(MSE(y, y2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"# 3. Use our ANN on the \"Digits\" dataset.\n",
"\n",
"Here is the code copied from out previous Machine Learning Module which downloads the \"digits\" dataset and separates it into training and testing sets. "
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAEICAYAAAByNDmmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAABSBJREFUeJzt3SGPnVsVgOG1b6pAdEiuIYgWzU3af9BaVBsMkpEEx3/A\n4CoIuhZDqgiuHTRiBk3CCMRNLgkDigTxIYrvFTP7TN95HnlyctbOSd7sky85Wes4jgGavjj1AYC7\nI3AIEziECRzCBA5hAocwgUOYwB+wtdbTtdYf1lr/XGt9vdb6zVrr0anPxe0R+MP225n5Zma+PzPP\nZ+bFzPzipCfiVgn8YfvhzPzuOI7/HMfx9cz8cWZ+dOIzcYsE/rC9mZmfrrW+s9b6wcz8eD5GToTA\nH7Y/zcxXM/Pvmfn7zPx5Zt6d9ETcKoE/UGutL+bjbf37mfnuzHw5M9+bmV+f8lzcruXfZA/TWuvL\n+fiA7ew4jn/9/7XXM/Or4zi+OunhuDVu8AfqOI5/zMzfZubna61Ha62zmfnZzPzltCfjNgn8YfvJ\nfHyw9s3M/HVm/jszvzzpibhVfqJDmBscwgQOYQKHMIFD2F39cyj55O7m5mbrvPPz822zLi8vt83a\n+T1++PBh26yZmefPn+8ctz71Bjc4hAkcwgQOYQKHMIFDmMAhTOAQJnAIEziECRzCBA5hAocwgUOY\nwCFM4BAmcAgTOIQJHMIEDmEChzCBQ5jAIUzgECZwCBM4hAkcwu5qddE2O9fgvHz5ctusmZmrq6tt\ns168eLFt1sXFxbZZ79692zZrZvvqok9yg0OYwCFM4BAmcAgTOIQJHMIEDmEChzCBQ5jAIUzgECZw\nCBM4hAkcwgQOYQKHMIFDmMAhTOAQJnAIEziECRzCBA5hAocwgUOYwCHss19d9ObNm22zdq4Smpl5\n//79tlnX19fbZu1cXXTfVgnt5gaHMIFDmMAhTOAQJnAIEziECRzCBA5hAocwgUOYwCFM4BAmcAgT\nOIQJHMIEDmEChzCBQ5jAIUzgECZwCBM4hAkcwgQOYQKHMIFDmMAh7LPfTbZz99Tjx4+3zZrZu3dt\n526yJ0+ebJv1+vXrbbPuIzc4hAkcwgQOYQKHMIFDmMAhTOAQJnAIEziECRzCBA5hAocwgUOYwCFM\n4BAmcAgTOIQJHMIEDmEChzCBQ5jAIUzgECZwCBM4hAkcwtZxHHfxuXfyoae2c73PzMz5+fm2WRcX\nF9tmPXv2bNusy8vLbbNOYH3qDW5wCBM4hAkcwgQOYQKHMIFDmMAhTOAQJnAIEziECRzCBA5hAocw\ngUOYwCFM4BAmcAgTOIQJHMIEDmEChzCBQ5jAIUzgECZwCBM4hD069QE+J0+fPt067+bmZuu8Xa6u\nrrbNevv27bZZM3vXTX0bbnAIEziECRzCBA5hAocwgUOYwCFM4BAmcAgTOIQJHMIEDmEChzCBQ5jA\nIUzgECZwCBM4hAkcwgQOYQKHMIFDmMAhTOAQJnAIEziEWV10j+1c8VNVXf/0bbnBIUzgECZwCBM4\nhAkcwgQOYQKHMIFDmMAhTOAQJnAIEziECRzCBA5hAocwgUOYwCFM4BAmcAgTOIQJHMIEDmEChzCB\nQ5jAIUzgECZwCLOb7B579erVtlnX19fbZp2dnW2bdX5+vm3WfeQGhzCBQ5jAIUzgECZwCBM4hAkc\nwgQOYQKHMIFDmMAhTOAQJnAIEziECRzCBA5hAocwgUOYwCFM4BAmcAgTOIQJHMIEDmEChzCBQ9g6\njuPUZwDuiBscwgQOYQKHMIFDmMAhTOAQJnAIEziECRzCBA5hAocwgUOYwCFM4BAmcAgTOIQJHMIE\nDmEChzCBQ5jAIUzgECZwCPsfV3ODr+2GeukAAAAASUVORK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x250a64caf60>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"import matplotlib.pylab as plt\n",
"import numpy as np\n",
"from sklearn.datasets import fetch_lfw_people, load_digits\n",
"from sklearn.cross_validation import train_test_split\n",
"\n",
"sk_data = load_digits();\n",
"\n",
"#Cool slider to browse all of the images.\n",
"from ipywidgets import interact\n",
"def browse_images(images, labels, categories):\n",
" n = len(images)\n",
" def view_image(i):\n",
" plt.imshow(images[i], cmap=plt.cm.gray_r, interpolation='nearest')\n",
" plt.title('%s' % categories[labels[i]])\n",
" plt.axis('off')\n",
" plt.show()\n",
" interact(view_image, i=(0,n-1))\n",
"browse_images(sk_data.images, sk_data.target, sk_data.target_names)\n",
"\n",
"\n",
"feature_vectors = sk_data.data\n",
"class_labels = sk_data.target\n",
"categories = sk_data.target_names\n",
"\n",
"N, h, w = sk_data.images.shape\n",
"train_vectors, test_vectors, train_labels, test_labels = train_test_split(feature_vectors, class_labels, test_size=0.25, random_state=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following is copied and pasted from Section 1 and rewritten it to use the training and testing sets above. \n",
"\n",
"&#9989; **DO THIS:** Make changes to and finish the following code to work with the \"digits\" data. Some of the work has already been done for you. Please consider the following when making changes:\n",
"\n",
"* For this new input to work, you need to transform the training and testing data into a format that can work with the class that was developed. Use the example from above and the functions such as ```type``` and ```shape``` to figure out how to transform the data into inputs suitable for training the Neural Network. This will be the first step before you can run the example code below.\n",
"* Modify the number of Input, Output and Hidden layers to match the new problem. (I've supplied \"?\" for now, you should think about what these could/should be.)\n",
"* Make sure your inputs and outputs are normalized between zero (0) and one (1). "
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1347, 64)\n",
"(1347, 1)\n",
"[[0.22222222]\n",
" [0.66666667]\n",
" [0.66666667]\n",
" ...\n",
" [1. ]\n",
" [0.11111111]\n",
" [0.55555556]]\n"
]
}
],
"source": [
"train_vectors = train_vectors/train_vectors.max()\n",
"\n",
"train_vectors = train_vectors\n",
"train_labels = train_labels.reshape(1347,1)\n",
"train_labels = train_labels/train_labels.max()\n",
"print(train_vectors.shape)\n",
"print(train_labels.shape)\n",
"print(train_labels)"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.14594026e-02],\n",
" [1.83245292e-03],\n",
" [2.81387785e-03],\n",
" ...,\n",
" [1.97903197e-02],\n",
" [2.87355695e-02],\n",
" [8.32694764e-06]])"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Run the training. \n",
"# X = np.array(([3,5], [5,1], [10,2]), dtype=float) 2,1,3\n",
"# y = np.array(([75], [82], [93]), dtype=float)\n",
"\n",
"NN = Neural_Network(64,1,10) #len(train_vectors)\n",
"NN.forward(train_vectors)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-139-4310d4908f49>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mT\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mNN\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mT\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtrain_vectors\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrain_labels\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-2-71cf4520db29>\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(self, X, y)\u001b[0m\n\u001b[1;32m 141\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 142\u001b[0m \u001b[0moptions\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'maxiter'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m200\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'disp'\u001b[0m \u001b[1;33m:\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 143\u001b[0;31m \u001b[0m_res\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0moptimize\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mminimize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcostFunctionWrapper\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mjac\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'BFGS'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moptions\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcallbackF\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 144\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mN\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msetParams\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_res\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[0;32mC:\\Users\\Maxwell\\AppData\\Roaming\\Python\\Python36\\site-packages\\scipy\\optimize\\_minimize.py\u001b[0m in \u001b[0;36mminimize\u001b[0;34m(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)\u001b[0m\n\u001b[1;32m 479\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0m_minimize_cg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfun\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mjac\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 480\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mmeth\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'bfgs'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 481\u001b[0;31m \u001b[1;32mreturn\u001b[0m \u001b[0m_minimize_bfgs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfun\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mjac\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcallback\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 482\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mmeth\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'newton-cg'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 483\u001b[0m return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,\n",
"\u001b[0;32mC:\\Users\\Maxwell\\AppData\\Roaming\\Python\\Python36\\site-packages\\scipy\\optimize\\optimize.py\u001b[0m in \u001b[0;36m_minimize_bfgs\u001b[0;34m(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)\u001b[0m\n\u001b[1;32m 1003\u001b[0m \u001b[0mA1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mI\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0msk\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0myk\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mrhok\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 1004\u001b[0m \u001b[0mA2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mI\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0myk\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0msk\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mrhok\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1005\u001b[0;31m Hk = numpy.dot(A1, numpy.dot(Hk, A2)) + (rhok * sk[:, numpy.newaxis] *\n\u001b[0m\u001b[1;32m 1006\u001b[0m sk[numpy.newaxis, :])\n\u001b[1;32m 1007\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"T = trainer(NN)\n",
"T.train(train_vectors, train_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"pred_labels = NN.forward(train_vectors)\n",
"\n",
"print(\"Training Data error\", np.sum(np.sqrt((train_labels - pred_labels)*(train_labels-pred_labels)))/len(train_vectors))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pred_labels = NN.forward(test_vectors)\n",
"\n",
"print(\"Testing Data error\", np.sum(np.sqrt((test_labels - pred_labels)*(test_labels-pred_labels)))/len(test_vectors))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"# Pay attention to how the plotting code rescales the data labels,\n",
"# if you scaled them differently, you may need to change this code.\n",
"def plot_gallery(images, true_titles, pred_titles, h, w, n_row=5, n_col=5):\n",
" \"\"\"Helper function to plot a gallery of portraits\"\"\"\n",
" plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))\n",
" plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)\n",
" for i in range(n_row * n_col):\n",
" plt.subplot(n_row, n_col, i + 1)\n",
" plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray_r)\n",
" plt.title(np.round(pred_titles[i]*10, 2)) \n",
" plt.xlabel('Actual='+str(true_titles[i]), size=9)\n",
" plt.xticks(())\n",
" plt.yticks(())\n",
"\n",
"plot_gallery(test_vectors, test_labels, pred_labels, h,w)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"&#9989; **DO THIS:** Modify the parameters of the neural network to get the best fit of the data. Consider also changing the training data you're providing to see how this changes your fit. Is it possible to change the number of input layers or output layers? If so, how you might you do it?\n",
"\n",
"Record your thoughts below along with your final best fit parameters/data. **Once you've come up with your best training data and neural network parameters, post your data/parameters the Slack channel for your section.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Picking large hidden layer values takes a long time to train. I found 25 works well, 10 is questionable, and 64 takes too long."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"# 4. Finding/Using Neural Networks Libraries\n",
"In this section we will repeat both examples from above (Grades and Digits) using a python neural network library. \n",
"\n",
"&#9989; Do This - As a group, find examples of neural network packages in python. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**&#9989; DO THIS** - Pick a package (or packages) you find interesting and get them working in this notebook. I suggest that each group member try to pick a different package and spend about 10 minutes trying to install and get it working. After about 10 minutes compare notes and pick the one the group will think is the easiest. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question :** What package did you pick? Please include any installation code needed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Put your installation code here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"&#9989; **DO THIS** - Create an example to demonstrate that the Neural Network is working. Preferably using an example that comes with the provided NN Package. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Put your example code here \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"&#9989; **DO THIS** - Reproduce the results from the \"Grade\" example above using ```X``` and ```y```:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Put your Grade example code here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"&#9989; **DO THIS** - Reproduce the results from the \"Digits\" example above:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Put your Digits example code here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question:** What settings worked the best for the 'Digits' data? How did you find these settings?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=8 color=\"#009600\">&#9998;</font> Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question:** What part did you have the most trouble figuring out to get this assignment working?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=8 color=\"#009600\">&#9998;</font> Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"# Assignment Wrap-up\n",
"\n",
"Fill out the following Google Form before submitting your assignment to D2L!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.display import HTML\n",
"HTML(\n",
"\"\"\"\n",
"<iframe \n",
"\tsrc=\"https://goo.gl/forms/nRQj6A0xZHgrS4WK2\" \n",
"\twidth=\"80%\" \n",
"\theight=\"500px\" \n",
"\tframeborder=\"0\" \n",
"\tmarginheight=\"0\" \n",
"\tmarginwidth=\"0\">\n",
"\tLoading...\n",
"</iframe>\n",
"\"\"\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-----\n",
"### Congratulations, we're done!\n",
"\n",
"Now, you just need to submit this assignment by uploading it to the course <a href=\"https://d2l.msu.edu/\">Desire2Learn</a> web page for today's dropbox (Don't forget to add your names in the first cell).\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"&#169; Copyright 2017, Michigan State University Board of Trustees"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
},
"widgets": {
"state": {
"065d2168353641f29ca4ec30f7f110b9": {
"views": [
{
"cell_index": 18
}
]
}
},
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import random
# initialize the rng with a seed
random.seed()
# Hard coding of input parameters
Agents = 500
MCcounts = 1000
Transactions = 100000
startMoney = 1.0
Lambda = 0.0
FinancialAgents = startMoney*np.ones(Agents)
for i in range (1, MCcounts, 1):
for j in range (1, Transactions, 1):
agent_i = int(Agents*random.random())
agent_j = int(Agents*random.random())
epsilon = random.random()
if agent_i != agent_j:
m1 = Lambda*FinancialAgents[agent_i] + (1-Lambda)*epsilon*(FinancialAgents[agent_i] + FinancialAgents[agent_j])
m2 = Lambda*FinancialAgents[agent_j] + (1-Lambda)*(1-epsilon)*(FinancialAgents[agent_i] + FinancialAgents[agent_j])
FinancialAgents[agent_i] = m1
FinancialAgents[agent_j] = m2
# the histogram of the data
n, bins, patches = plt.hist(FinancialAgents, 50, facecolor='green')
plt.xlabel('$x$')
plt.ylabel('Distribution of wealth')
plt.title(r'Money')
plt.axis([0, 10, 0, 500])
plt.grid(True)
plt.show()
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,82 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'src/Hudson_Bay.csv'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-7c0c8f9a022b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;31m# Load in data file\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloadtxt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'src/Hudson_Bay.csv'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdelimiter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m','\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mskiprows\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;31m# Make arrays containing x-axis and hares and lynx populations\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0myear\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/anaconda3/lib/python3.6/site-packages/numpy/lib/npyio.py\u001b[0m in \u001b[0;36mloadtxt\u001b[0;34m(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)\u001b[0m\n\u001b[1;32m 896\u001b[0m \u001b[0mfh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'U'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 897\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 898\u001b[0;31m \u001b[0mfh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 899\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 900\u001b[0m \u001b[0mfh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'src/Hudson_Bay.csv'"
]
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy import sparse\n",
"import pandas as pd\n",
"from IPython.display import display\n",
"import mglearn\n",
"import sklearn\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.tree import DecisionTreeRegressor\n",
"\n",
"# Load in data file\n",
"data = np.loadtxt('src/Hudson_Bay.csv', delimiter=',', skiprows=1)\n",
"# Make arrays containing x-axis and hares and lynx populations\n",
"#How do you import this data to IPython? Ask Morten\n",
"year = data[:,0]\n",
"hares = data[:,1]\n",
"lynx = data[:,2]\n",
"\n",
"plt.plot(year, hares ,'b-+', year, lynx, 'r-o')\n",
"plt.axis([1900,1920,0, 100.0])\n",
"plt.xlabel(r'Year')\n",
"plt.ylabel(r'Numbers of hares and lynx ')\n",
"plt.legend(('Hares','Lynx'), loc='upper right')\n",
"plt.title(r'Population of hares and lynx from 1900-1920 (x1000)}')\n",
"plt.savefig('Hudson_Bay_data.pdf')\n",
"plt.savefig('Hudson_Bay_data.png')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,270 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import random\n",
"class Network(object):\n",
" \n",
" def _init_(self, sizes):\n",
" self.num_layers=len(sizes)\n",
" self.sizes=sizes\n",
" self.biases=[np.random.randn(y,1) for y in sizes[1:]]\n",
" self.weights=[np.random.randn(y,x) for x,y in zip(sizes[:-1], sizes[1:])]\n",
"\n",
"#sizes is the number of neurons in each layer\n",
"#for example, say n_1st_layer=3, n_2nd_layer=3, n_3rd_layer=1, then net=Network([3,3,1])\n",
"\n",
"#The biases and weights are initialized randomly, using Gaussian distributions of mean=0, stdev=1\n",
"#z is a vector (or a np.array)\n",
"\n",
" def feedforward(self,a):\n",
" #returns output w/ 'a' as an input\n",
" for b, w in zip(self.biases, self.weights):\n",
" a=sigmoid(np.dot(w,b)+b)\n",
" return a\n",
" \n",
"#Apply a Stochastic Gradient Descent (SGD) method:\n",
" def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):\n",
" \"\"\"Trains network using batches incorporating SGD. The network will be evaluated against the\n",
" test data after each epoch, with partial progress being printed out (this is useful for tracking,\n",
" but slows the process.)\"\"\"\n",
" if test_data: n_test=len(test_data)\n",
" n=len(training_data)\n",
" for j in xrange(epochs):\n",
" random.shuffle(training_data)\n",
" mini_batches=[training_data[k:k+mini_batch_size] for k in xrange(o,n,mini_batch_size)]\n",
" for mini_batch in mini_batches:\n",
" self.update_mini_batch(mini_batch, eta)\n",
" if test_data:\n",
" print (\"Epoch {0}: {1}/{2}\".format(j, self.evaluate(test_data), n_test))\n",
" else:\n",
" print (\"Epoch {0} complete\".format(j))\n",
" \n",
" \n",
" def update_mini_batch(self, mini_batch, eta):\n",
" #updates w and b using backpropagation to a single mini batch. eta is the learning rate.\"\n",
" nabla_b=[np.zeros(b.shape) for b in self.biases]\n",
" nabla_w=[np.zeros(w.shape) for w in self.weights]\n",
" for x,y in mini_batch:\n",
" delta_nabla_b, delta_nabla_w=self.backprop(x,y)\n",
" nabla_b=[nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]\n",
" nabla_w=[nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]\n",
" self.weights=[w-(eta/len(mini_batch))*nw for w, nw in zip(self.weights, nabla_w)]\n",
" self.biases=[b-(eta/len(mini_batch))*nb for b, nb in zip(self.biases, nabla_b)]\n",
" \n",
" def backprop(self, x, y):\n",
" \"\"\"Return a tuple ``(nabla_b, nabla_w)`` representing the\n",
" gradient for the cost function C_x. ``nabla_b`` and\n",
" ``nabla_w`` are layer-by-layer lists of numpy arrays, similar\n",
" to ``self.biases`` and ``self.weights``.\"\"\"\n",
" nabla_b = [np.zeros(b.shape) for b in self.biases]\n",
" nabla_w = [np.zeros(w.shape) for w in self.weights]\n",
" # feedforward\n",
" activation = x\n",
" activations = [x] # list to store all the activations, layer by layer\n",
" zs = [] # list to store all the z vectors, layer by layer\n",
" for b, w in zip(self.biases, self.weights):\n",
" z = np.dot(w, activation)+b\n",
" zs.append(z)\n",
" activation = sigmoid(z)\n",
" activations.append(activation)\n",
" # backward pass\n",
" delta = self.cost_derivative(activations[-1], y) * \\\n",
" sigmoid_prime(zs[-1])\n",
" nabla_b[-1] = delta\n",
" nabla_w[-1] = np.dot(delta, activations[-2].transpose())\n",
" # Note that the variable l in the loop below is used a little\n",
" # differently to the notation in Chapter 2 of the book. Here,\n",
" # l = 1 means the last layer of neurons, l = 2 is the\n",
" # second-last layer, and so on. It's a renumbering of the\n",
" # scheme in the book, used here to take advantage of the fact\n",
" # that Python can use negative indices in lists.\n",
" for l in xrange(2, self.num_layers):\n",
" z = zs[-l]\n",
" sp = sigmoid_prime(z)\n",
" delta = np.dot(self.weights[-l+1].transpose(), delta) * sp\n",
" nabla_b[-l] = delta\n",
" nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())\n",
" return (nabla_b, nabla_w)\n",
"\n",
" def evaluate(self, test_data):\n",
" \"\"\"Return the number of test inputs for which the neural\n",
" network outputs the correct result. Note that the neural\n",
" network's output is assumed to be the index of whichever\n",
" neuron in the final layer has the highest activation.\"\"\"\n",
" test_results = [(np.argmax(self.feedforward(x)), y)\n",
" for (x, y) in test_data]\n",
" return sum(int(x == y) for (x, y) in test_results)\n",
"\n",
" def cost_derivative(self, output_activations, y):\n",
" \"\"\"Return the vector of partial derivatives \\partial C_x /\n",
" \\partial a for the output activations.\"\"\"\n",
" return (output_activations-y)\n",
" \n",
" \n",
" \n",
"#Functions\n",
"def sigmoid(z):\n",
" return 1.0/(1.0+np.exp(-z))\n",
"\n",
"def sigmoid_prime(z):\n",
" return sigmoid(z)*(1-sigmoid(z))\n",
"\n",
"network=Network()"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Network' object has no attribute 'Network'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-35-768076401f8b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 86\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 88\u001b[0;31m \u001b[0mnet\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnetwork\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mNetwork\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m784\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 89\u001b[0m \u001b[0mnet\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSGD\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtraining_data\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtest_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtest_data\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'Network' object has no attribute 'Network'"
]
}
],
"source": [
"# %load neural-networks-and-deep-learning/src/mnist_loader.py\n",
"\"\"\"\n",
"mnist_loader\n",
"~~~~~~~~~~~~\n",
"\n",
"A library to load the MNIST image data. For details of the data\n",
"structures that are returned, see the doc strings for ``load_data``\n",
"and ``load_data_wrapper``. In practice, ``load_data_wrapper`` is the\n",
"function usually called by our neural network code.\n",
"\"\"\"\n",
"\n",
"#### Libraries\n",
"# Standard library\n",
"import pickle\n",
"import gzip\n",
"\n",
"# Third-party libraries\n",
"import numpy as np\n",
"\n",
"def load_data():\n",
" \"\"\"Return the MNIST data as a tuple containing the training data,\n",
" the validation data, and the test data.\n",
"\n",
" The ``training_data`` is returned as a tuple with two entries.\n",
" The first entry contains the actual training images. This is a\n",
" numpy ndarray with 50,000 entries. Each entry is, in turn, a\n",
" numpy ndarray with 784 values, representing the 28 * 28 = 784\n",
" pixels in a single MNIST image.\n",
"\n",
" The second entry in the ``training_data`` tuple is a numpy ndarray\n",
" containing 50,000 entries. Those entries are just the digit\n",
" values (0...9) for the corresponding images contained in the first\n",
" entry of the tuple.\n",
"\n",
" The ``validation_data`` and ``test_data`` are similar, except\n",
" each contains only 10,000 images.\n",
"\n",
" This is a nice data format, but for use in neural networks it's\n",
" helpful to modify the format of the ``training_data`` a little.\n",
" That's done in the wrapper function ``load_data_wrapper()``, see\n",
" below.\n",
" \"\"\"\n",
" f = gzip.open('../data/mnist.pkl.gz', 'rb')\n",
" training_data, validation_data, test_data = cPickle.load(f)\n",
" f.close()\n",
" return (training_data, validation_data, test_data)\n",
"\n",
"def load_data_wrapper():\n",
" \"\"\"Return a tuple containing ``(training_data, validation_data,\n",
" test_data)``. Based on ``load_data``, but the format is more\n",
" convenient for use in our implementation of neural networks.\n",
"\n",
" In particular, ``training_data`` is a list containing 50,000\n",
" 2-tuples ``(x, y)``. ``x`` is a 784-dimensional numpy.ndarray\n",
" containing the input image. ``y`` is a 10-dimensional\n",
" numpy.ndarray representing the unit vector corresponding to the\n",
" correct digit for ``x``.\n",
"\n",
" ``validation_data`` and ``test_data`` are lists containing 10,000\n",
" 2-tuples ``(x, y)``. In each case, ``x`` is a 784-dimensional\n",
" numpy.ndarry containing the input image, and ``y`` is the\n",
" corresponding classification, i.e., the digit values (integers)\n",
" corresponding to ``x``.\n",
"\n",
" Obviously, this means we're using slightly different formats for\n",
" the training data and the validation / test data. These formats\n",
" turn out to be the most convenient for use in our neural network\n",
" code.\"\"\"\n",
" tr_d, va_d, te_d = load_data()\n",
" training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]\n",
" training_results = [vectorized_result(y) for y in tr_d[1]]\n",
" training_data = zip(training_inputs, training_results)\n",
" validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]\n",
" validation_data = zip(validation_inputs, va_d[1])\n",
" test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]\n",
" test_data = zip(test_inputs, te_d[1])\n",
" return (training_data, validation_data, test_data)\n",
"\n",
"def vectorized_result(j):\n",
" \"\"\"Return a 10-dimensional unit vector with a 1.0 in the jth\n",
" position and zeroes elsewhere. This is used to convert a digit\n",
" (0...9) into a corresponding desired output from the neural\n",
" network.\"\"\"\n",
" e = np.zeros((10, 1))\n",
" e[j] = 1.0\n",
" return e\n",
"\n",
"net=network.Network([784,30,30])\n",
"net.SGD(training_data,30,10,3,test_data=test_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import mglearn\n",
"import numpy as np\n",
"import pandas as pd\n",
"import os\n",
"from scipy import signal\n",
"from sklearn.datasets import load_boston\n",
"from sklearn.preprocessing import MinMaxScaler, PolynomialFeatures\n",
"from mglearn.make_blobs import make_blobs\n",
"\n",
"#DATA_PATH = os.path.join(os.path.dirname(__file__), \"data\")\n",
"\n",
"\n",
"def make_forge():\n",
" # a carefully hand-designed dataset lol\n",
" X, y = make_blobs(centers=2, random_state=4, n_samples=30)\n",
" y[np.array([7, 27])] = 0\n",
" mask = np.ones(len(X), dtype=np.bool)\n",
" mask[np.array([0, 1, 5, 26])] = 0\n",
" X, y = X[mask], y[mask]\n",
" return X, y\n",
"\n",
"\n",
"def make_wave(n_samples=100):\n",
" rnd = np.random.RandomState(42)\n",
" x = rnd.uniform(-3, 3, size=n_samples)\n",
" y_no_noise = (np.sin(4 * x) + x)\n",
" y = (y_no_noise + rnd.normal(size=len(x))) / 2\n",
" return x.reshape(-1, 1), y\n",
"\n",
"\n",
"def load_extended_boston():\n",
" boston = load_boston()\n",
" X = boston.data\n",
"\n",
" X = MinMaxScaler().fit_transform(boston.data)\n",
" X = PolynomialFeatures(degree=2, include_bias=False).fit_transform(X)\n",
" return X, boston.target\n",
"\n",
"\n",
"def load_citibike():\n",
" data_mine = pd.read_csv(os.path.join(DATA_PATH, \"citibike.csv\"))\n",
" data_mine['one'] = 1\n",
" data_mine['starttime'] = pd.to_datetime(data_mine.starttime)\n",
" data_starttime = data_mine.set_index(\"starttime\")\n",
" data_resampled = data_starttime.resample(\"3h\").sum().fillna(0)\n",
" return data_resampled.one\n",
"\n",
"\n",
"def make_signals():\n",
" # fix a random state seed\n",
" rng = np.random.RandomState(42)\n",
" n_samples = 2000\n",
" time = np.linspace(0, 8, n_samples)\n",
" # create three signals\n",
" s1 = np.sin(2 * time) # Signal 1 : sinusoidal signal\n",
" s2 = np.sign(np.sin(3 * time)) # Signal 2 : square signal\n",
" s3 = signal.sawtooth(2 * np.pi * time) # Signal 3: saw tooth signal\n",
"\n",
" # concatenate the signals, add noise\n",
" S = np.c_[s1, s2, s3]\n",
" S += 0.2 * rng.normal(size=S.shape)\n",
"\n",
" S /= S.std(axis=0) # Standardize data\n",
" S -= S.min()\n",
" return S\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from scipy import optimize\n",
"\n",
"class Neural_Network(object):\n",
" def __init__(self, Lambda=0): \n",
" #Define Hyperparameters\n",
" self.inputLayerSize = 2\n",
" self.outputLayerSize = 1\n",
" self.hiddenLayerSize = 3\n",
" \n",
" #Weights (parameters)\n",
" self.W1 = np.random.randn(self.inputLayerSize,self.hiddenLayerSize)\n",
" self.W2 = np.random.randn(self.hiddenLayerSize,self.outputLayerSize)\n",
" \n",
" #Regularization Parameter:\n",
" self.Lambda = Lambda\n",
" \n",
" def forward(self, X):\n",
" #Propogate inputs though network\n",
" self.z2 = np.dot(X, self.W1)\n",
" self.a2 = self.sigmoid(self.z2)\n",
" self.z3 = np.dot(self.a2, self.W2)\n",
" yHat = self.sigmoid(self.z3) \n",
" return yHat\n",
" \n",
" def sigmoid(self, z):\n",
" #Apply sigmoid activation function to scalar, vector, or matrix\n",
" return 1/(1+np.exp(-z))\n",
" \n",
" def sigmoidPrime(self,z):\n",
" #Gradient of sigmoid\n",
" return np.exp(-z)/((1+np.exp(-z))**2)\n",
" \n",
" def costFunction(self, X, y):\n",
" #Compute cost for given X,y, use weights already stored in class.\n",
" self.yHat = self.forward(X)\n",
" J = 0.5*sum((y-self.yHat)**2)/X.shape[0] + (self.Lambda/2)*(np.sum(self.W1**2)+np.sum(self.W2**2))\n",
" return J\n",
" \n",
" def costFunctionPrime(self, X, y):\n",
" #Compute derivative with respect to W and W2 for a given X and y:\n",
" self.yHat = self.forward(X)\n",
" \n",
" delta3 = np.multiply(-(y-self.yHat), self.sigmoidPrime(self.z3))\n",
" #Add gradient of regularization term:\n",
" dJdW2 = np.dot(self.a2.T, delta3)/X.shape[0] + self.Lambda*self.W2\n",
" \n",
" delta2 = np.dot(delta3, self.W2.T)*self.sigmoidPrime(self.z2)\n",
" #Add gradient of regularization term:\n",
" dJdW1 = np.dot(X.T, delta2)/X.shape[0] + self.Lambda*self.W1\n",
" \n",
" return dJdW1, dJdW2\n",
" \n",
" #Helper functions for interacting with other methods/classes\n",
" def getParams(self):\n",
" #Get W1 and W2 Rolled into vector:\n",
" params = np.concatenate((self.W1.ravel(), self.W2.ravel()))\n",
" return params\n",
" \n",
" def setParams(self, params):\n",
" #Set W1 and W2 using single parameter vector:\n",
" W1_start = 0\n",
" W1_end = self.hiddenLayerSize*self.inputLayerSize\n",
" self.W1 = np.reshape(params[W1_start:W1_end], \\\n",
" (self.inputLayerSize, self.hiddenLayerSize))\n",
" W2_end = W1_end + self.hiddenLayerSize*self.outputLayerSize\n",
" self.W2 = np.reshape(params[W1_end:W2_end], \\\n",
" (self.hiddenLayerSize, self.outputLayerSize))\n",
" \n",
" def computeGradients(self, X, y):\n",
" dJdW1, dJdW2 = self.costFunctionPrime(X, y)\n",
" return np.concatenate((dJdW1.ravel(), dJdW2.ravel()))\n",
" \n",
" \n",
"class trainer(object):\n",
" def __init__(self, N):\n",
" #Make Local reference to network:\n",
" self.N = N\n",
" \n",
" def callbackF(self, params):\n",
" self.N.setParams(params)\n",
" self.J.append(self.N.costFunction(self.X, self.y))\n",
" self.testJ.append(self.N.costFunction(self.testX, self.testY))\n",
" \n",
" def costFunctionWrapper(self, params, X, y):\n",
" self.N.setParams(params)\n",
" cost = self.N.costFunction(X, y)\n",
" grad = self.N.computeGradients(X,y)\n",
" return cost, grad\n",
" \n",
" def train(self, trainX, trainY, testX, testY):\n",
" #Make an internal variable for the callback function:\n",
" self.X = trainX\n",
" self.y = trainY\n",
" \n",
" self.testX = testX\n",
" self.testY = testY\n",
"\n",
" #Make empty list to store training costs:\n",
" self.J = []\n",
" self.testJ = []\n",
" \n",
" params0 = self.N.getParams()\n",
"\n",
" options = {'maxiter': 200, 'disp' : True}\n",
" _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', \\\n",
" args=(trainX, trainY), options=options, callback=self.callbackF)\n",
"\n",
" self.N.setParams(_res.x)\n",
" self.optimizationResults = _res\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "train() missing 1 required positional argument: 'testY'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-e816aa3fc208>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 114\u001b[0m \u001b[0mtrainX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtestX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrainY\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtestY\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrain_test_split\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miris\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'data'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0miris\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'target'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrandom_state\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 116\u001b[0;31m \u001b[0miris_train\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrainX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrainY\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtestX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtestY\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: train() missing 1 required positional argument: 'testY'"
]
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from sklearn.datasets import load_iris\n",
"iris=load_iris()\n",
"\n",
"from scipy import optimize\n",
"\n",
"class Neural_Network(object):\n",
" def __init__(self, Lambda=0): \n",
" #Define Hyperparameters\n",
" self.inputLayerSize = 2\n",
" self.outputLayerSize = 1\n",
" self.hiddenLayerSize = 3\n",
" \n",
" #Weights (parameters)\n",
" self.W1 = np.random.randn(self.inputLayerSize,self.hiddenLayerSize)\n",
" self.W2 = np.random.randn(self.hiddenLayerSize,self.outputLayerSize)\n",
" \n",
" #Regularization Parameter:\n",
" self.Lambda = Lambda\n",
" \n",
" def forward(self, X):\n",
" #Propogate inputs though network\n",
" self.z2 = np.dot(X, self.W1)\n",
" self.a2 = self.sigmoid(self.z2)\n",
" self.z3 = np.dot(self.a2, self.W2)\n",
" yHat = self.sigmoid(self.z3) \n",
" return yHat\n",
" \n",
" def sigmoid(self, z):\n",
" #Apply sigmoid activation function to scalar, vector, or matrix\n",
" return 1/(1+np.exp(-z))\n",
" \n",
" def sigmoidPrime(self,z):\n",
" #Gradient of sigmoid\n",
" return np.exp(-z)/((1+np.exp(-z))**2)\n",
" \n",
" def costFunction(self, X, y):\n",
" #Compute cost for given X,y, use weights already stored in class.\n",
" self.yHat = self.forward(X)\n",
" J = 0.5*sum((y-self.yHat)**2)/X.shape[0] + (self.Lambda/2)*(np.sum(self.W1**2)+np.sum(self.W2**2))\n",
" return J\n",
" \n",
" def costFunctionPrime(self, X, y):\n",
" #Compute derivative with respect to W and W2 for a given X and y:\n",
" self.yHat = self.forward(X)\n",
" \n",
" delta3 = np.multiply(-(y-self.yHat), self.sigmoidPrime(self.z3))\n",
" #Add gradient of regularization term:\n",
" dJdW2 = np.dot(self.a2.T, delta3)/X.shape[0] + self.Lambda*self.W2\n",
" \n",
" delta2 = np.dot(delta3, self.W2.T)*self.sigmoidPrime(self.z2)\n",
" #Add gradient of regularization term:\n",
" dJdW1 = np.dot(X.T, delta2)/X.shape[0] + self.Lambda*self.W1\n",
" \n",
" return dJdW1, dJdW2\n",
" \n",
" #Helper functions for interacting with other methods/classes\n",
" def getParams(self):\n",
" #Get W1 and W2 Rolled into vector:\n",
" params = np.concatenate((self.W1.ravel(), self.W2.ravel()))\n",
" return params\n",
" \n",
" def setParams(self, params):\n",
" #Set W1 and W2 using single parameter vector:\n",
" W1_start = 0\n",
" W1_end = self.hiddenLayerSize*self.inputLayerSize\n",
" self.W1 = np.reshape(params[W1_start:W1_end], \\\n",
" (self.inputLayerSize, self.hiddenLayerSize))\n",
" W2_end = W1_end + self.hiddenLayerSize*self.outputLayerSize\n",
" self.W2 = np.reshape(params[W1_end:W2_end], \\\n",
" (self.hiddenLayerSize, self.outputLayerSize))\n",
" \n",
" def computeGradients(self, X, y):\n",
" dJdW1, dJdW2 = self.costFunctionPrime(X, y)\n",
" return np.concatenate((dJdW1.ravel(), dJdW2.ravel()))\n",
" \n",
" \n",
"class trainer(object):\n",
" def __init__(self, N):\n",
" #Make Local reference to network:\n",
" self.N = N\n",
" \n",
" def callbackF(self, params):\n",
" self.N.setParams(params)\n",
" self.J.append(self.N.costFunction(self.X, self.y))\n",
" self.testJ.append(self.N.costFunction(self.testX, self.testY))\n",
" \n",
" def costFunctionWrapper(self, params, X, y):\n",
" self.N.setParams(params)\n",
" cost = self.N.costFunction(X, y)\n",
" grad = self.N.computeGradients(X,y)\n",
" return cost, grad\n",
" \n",
" def train(self, trainX, trainY, testX, testY):\n",
" #Make an internal variable for the callback function:\n",
" self.X = trainX\n",
" self.y = trainY\n",
" \n",
" self.testX = testX\n",
" self.testY = testY\n",
"\n",
" #Make empty list to store training costs:\n",
" self.J = []\n",
" self.testJ = []\n",
" \n",
" params0 = self.N.getParams()\n",
"\n",
" options = {'maxiter': 200, 'disp' : True}\n",
" _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', \\\n",
" args=(trainX, trainY), options=options, callback=self.callbackF)\n",
"\n",
" self.N.setParams(_res.x)\n",
" self.optimizationResults = _res\n",
"\n",
"from sklearn.model_selection import train_test_split\n",
"trainX, testX, trainY, testY=train_test_split(iris['data'], iris['target'], random_state=0)\n",
"\n",
"iris_train=trainer.train(trainX, trainY, testX, testY)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 126.84247142 125.01176842]\n"
]
}
],
"source": [
"from sklearn.preprocessing import PolynomialFeatures\n",
"from sklearn import linear_model\n",
"import matplotlib.pyplot as plt \n",
"\n",
"X = [[0.44, 0.68], [0.99, 0.23]]\n",
"vector = [109.85, 155.72]\n",
"predict= [[0.49, 0.18], [0.47, 0.22]]\n",
"\n",
"poly = PolynomialFeatures(degree=2)\n",
"X_ = poly.fit_transform(X)\n",
"predict_ = poly.fit_transform(predict)\n",
"\n",
"clf = linear_model.LinearRegression()\n",
"clf.fit(X_, vector)\n",
"y=clf.predict(predict_)\n",
"print (clf.predict(predict_))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,84 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Output after training: [[ 6.55109972e-03 9.93684857e-01 9.93925710e-01 6.62304973e-03]\n",
" [ 1.71082162e-03 9.97516440e-01 9.97766376e-01 1.82685927e-03]\n",
" [ 2.05800960e-03 9.98268211e-01 9.97548919e-01 1.77362990e-03]\n",
" [ 5.35659849e-04 9.99320839e-01 9.99100767e-01 4.87503198e-04]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"#sigmoid\n",
"def nonlin(x, deriv=False):\n",
" if (deriv==True):\n",
" return x*(1-x)\n",
" return 1/(1+np.exp(-x))\n",
"\n",
"#input data\n",
"x=np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])\n",
"\n",
"#output data\n",
"y=np.array([0,1,1,0]).T\n",
"\n",
"#seed random numbers to make calculation\n",
"np.random.seed(1)\n",
"\n",
"#initialize weights with mean=0\n",
"syn0=2*np.random.random((3,4))-1\n",
"\n",
"for iter in range(10000):\n",
" #forward propogation\n",
" l0=x\n",
" l1=nonlin(np.dot(l0,syn0))\n",
" l1_error=y-l1\n",
" #multiply error by slope of sigmoid at values of l1\n",
" l1_delta=l1_error*nonlin(l1,True)\n",
" #update weights\n",
" syn0+=np.dot(l0.T, l1_delta)\n",
" \n",
"print(\"Output after training: \",l1 )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,157 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n",
"Extracting /tmp/data/train-images-idx3-ubyte.gz\n",
"Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n",
"Extracting /tmp/data/train-labels-idx1-ubyte.gz\n",
"Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n",
"Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n",
"Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n",
"Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n",
"WARNING:tensorflow:From <ipython-input-1-92265976c34d>:45: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"\n",
"Future major versions of TensorFlow will allow gradients to flow\n",
"into the labels input on backprop by default.\n",
"\n",
"See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
"\n",
"WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\n",
"Instructions for updating:\n",
"Use `tf.global_variables_initializer` instead.\n",
"Epoch 0 completed out of 10 loss: 1664569.60834\n",
"Epoch 1 completed out of 10 loss: 414550.968437\n",
"Epoch 2 completed out of 10 loss: 229022.354944\n",
"Epoch 3 completed out of 10 loss: 136420.393392\n",
"Epoch 4 completed out of 10 loss: 88019.3560204\n",
"Epoch 5 completed out of 10 loss: 56024.820509\n",
"Epoch 6 completed out of 10 loss: 37434.4423951\n",
"Epoch 7 completed out of 10 loss: 29640.3100017\n",
"Epoch 8 completed out of 10 loss: 24399.9572706\n",
"Epoch 9 completed out of 10 loss: 23351.0056713\n",
"Accuracy: 0.9534\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"from tensorflow.examples.tutorials.mnist import input_data\n",
"mnist=input_data.read_data_sets(\"/tmp/data/\", one_hot=True) #one component is on, all others are off\n",
"#10 classes, 0 through 9\n",
"#one-hot outputs 0=[1,0,0,0,0,0,0,0,0], being the 1 is in the algorithm's guess (0)\n",
"#3=[0,0,0,1,0,0,0,0,0]\n",
"n_nodes_hl1=500 #hl1 = hidden layer 1\n",
"n_nodes_hl2=500\n",
"n_nodes_hl3=500\n",
"n_classes=10 #number of categories\n",
"batch_size=100 #divies up the data to be more efficient, as opposed to loading all samples at once\n",
"\n",
"x=tf.placeholder('float',[None, 784])\n",
"y=tf.placeholder('float')\n",
"\n",
"def neural_network_model(data):\n",
" #(inputs*weights)+biases\n",
" hidden_1_layer={'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])), \n",
" 'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}\n",
" \n",
" hidden_2_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), \n",
" 'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}\n",
" \n",
" hidden_3_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), \n",
" 'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}\n",
" \n",
" output_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),\n",
" 'biases': tf.Variable(tf.random_normal([n_classes]))}\n",
" \n",
" l1=tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])\n",
" l1=tf.nn.relu(l1)\n",
" \n",
" l2=tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])\n",
" l2=tf.nn.relu(l2)\n",
" \n",
" l3=tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])\n",
" l3=tf.nn.relu(l3)\n",
" \n",
" output=tf.matmul(l3, output_layer['weights'])+ output_layer['biases']\n",
" \n",
" return output\n",
" \n",
"def train_neural_network(x):\n",
" prediction=neural_network_model(x)\n",
" cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))\n",
" optimizer=tf.train.AdamOptimizer().minimize(cost)\n",
" \n",
" hm_epochs=10\n",
" \n",
" with tf.Session() as sess:\n",
" sess.run(tf.initialize_all_variables())\n",
" \n",
" for epoch in range(hm_epochs):\n",
" epoch_loss=0\n",
" for _ in range(int(mnist.train.num_examples/batch_size)):\n",
" epoch_x,epoch_y=mnist.train.next_batch(batch_size)\n",
" _,c=sess.run([optimizer,cost], feed_dict={x:epoch_x, y:epoch_y})\n",
" epoch_loss+=c\n",
" print('Epoch', epoch, 'completed out of ', hm_epochs, 'loss:', epoch_loss)\n",
" \n",
" correct=tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))\n",
" \n",
" accuracy=tf.reduce_mean(tf.cast(correct, 'float'))\n",
" print('Accuracy:', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))\n",
" \n",
"\n",
" \n",
" \n",
"train_neural_network(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,101 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0. 0. 5. ..., 0. 0. 0.]\n",
" [ 0. 0. 0. ..., 10. 0. 0.]\n",
" [ 0. 0. 0. ..., 16. 9. 0.]\n",
" ..., \n",
" [ 0. 0. 1. ..., 6. 0. 0.]\n",
" [ 0. 0. 2. ..., 12. 0. 0.]\n",
" [ 0. 0. 10. ..., 12. 1. 0.]]\n",
"[[ 0. 0. 5. ..., 0. 0. 0.]\n",
" [ 0. 0. 0. ..., 10. 0. 0.]\n",
" [ 0. 0. 0. ..., 16. 9. 0.]\n",
" ..., \n",
" [ 0. 0. 1. ..., 6. 0. 0.]\n",
" [ 0. 0. 2. ..., 12. 0. 0.]\n",
" [ 0. 0. 10. ..., 12. 1. 0.]]\n",
"(1796, 64)\n",
"prediction: [0 1 2 ..., 8 9 8]\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAACvtJREFUeJzt3X+o3XUdx/HXy+vm2pwz0kJ2Z0uU\nkVQ6GTMZCW0VM0Un9McGCo3ggqE4CkTrHwv6V+yPEGQ6JZdSU0PMtJGKSrbc5kzn3WQtbbepU8LU\naZub7/64Z7DWjfO9O5/vj/vu+YCL98fhft6H+bzf7z33nO/HESEAOZ3Q9gAA6kPgQGIEDiRG4EBi\nBA4kRuBAYgQOJEbgQGIEDiR2Yh3fdLpPihmaVce3bpVnnNToegfnurG14sOhxtaavnd/Y2tl9S/t\n18E40Pd/kFoCn6FZutDL6vjWrRo6e0Gj673241r+eSb08YtzGlvrzJv/0NhaWW2K31e6HafoQGIE\nDiRG4EBiBA4kRuBAYgQOJEbgQGIEDiRWKXDby23vtL3L9o11DwWgjL6B2x6S9DNJl0g6V9Iq2+fW\nPRiAwVU5gi+WtCsidkfEQUn3Sbqi3rEAlFAl8LmS9hz18VjvcwA6rsqrGSZ6xcp/XUzd9oikEUma\noZkDjgWghCpH8DFJ8476eFjS3mNvFBG3R8SiiFg0Tc2+rBLAxKoE/pykc2x/zvZ0SSslPVTvWABK\n6HuKHhGHbF8r6TFJQ5LujIjttU8GYGCVrigQEY9IeqTmWQAUxjPZgMQIHEiMwIHECBxIjMCBxAgc\nSIzAgcQIHEisua0zEhj59W8aXW/FrPebW+yi5pZ6ZXVzWxetWXxlY2tJ0uE39zW6Xj8cwYHECBxI\njMCBxAgcSIzAgcQIHEiMwIHECBxIjMCBxKrsbHKn7X22X2piIADlVDmC3yVpec1zAKhB38Aj4ilJ\n/2hgFgCF8Ts4kFixV5OxdRHQPcWO4GxdBHQPp+hAYlX+THavpGclLbA9Zvs79Y8FoIQqe5OtamIQ\nAOVxig4kRuBAYgQOJEbgQGIEDiRG4EBiBA4kRuBAYlN+66IPrrywsbVWzNrW2FqS9Pnbv9vYWsNP\nfNjYWhvvXdfYWn+95uzG1pKkM29m6yIADSFwIDECBxIjcCAxAgcSI3AgMQIHEiNwIDECBxIjcCCx\nKhddnGf7Cdujtrfbvr6JwQAMrspz0Q9J+n5EbLU9W9IW2xsj4uWaZwMwoCp7k70eEVt7778naVTS\n3LoHAzC4Sb2azPZ8SQslbZrga2xdBHRM5QfZbJ8s6X5JayLi3WO/ztZFQPdUCtz2NI3HvT4iHqh3\nJAClVHkU3ZLukDQaEbfUPxKAUqocwZdIulrSUtvbem/frHkuAAVU2ZvsGUluYBYAhfFMNiAxAgcS\nI3AgMQIHEiNwIDECBxIjcCAxAgcSm/J7kx2Yk/dn1Alf/Gdja41pTmNrNem0Fw63PUKr8tYBgMCB\nzAgcSIzAgcQIHEiMwIHECBxIjMCBxAgcSKzKRRdn2P6T7Rd6Wxf9qInBAAyuylNVD0haGhHv9y6f\n/Izt30bEH2ueDcCAqlx0MSS93/twWu8t6hwKQBlVNz4Ysr1N0j5JGyNiwq2LbG+2vfkjHSg9J4Dj\nUCnwiDgcEedLGpa02PYXJrgNWxcBHTOpR9Ej4h1JT0paXss0AIqq8ij66bZP7b3/CUlfk7Sj7sEA\nDK7Ko+hnSLrb9pDGfyD8MiIerncsACVUeRT9zxrfExzAFMMz2YDECBxIjMCBxAgcSIzAgcQIHEiM\nwIHECBxIbMpvXfTJu55tbK3FuqaxtSTpJz/8eXOLfam5pdAcjuBAYgQOJEbgQGIEDiRG4EBiBA4k\nRuBAYgQOJEbgQGKVA+9dG/1521yPDZgiJnMEv17SaF2DACiv6s4mw5IulbS23nEAlFT1CH6rpBsk\nfVzjLAAKq7LxwWWS9kXElj63Y28yoGOqHMGXSLrc9quS7pO01PY9x96IvcmA7ukbeETcFBHDETFf\n0kpJj0fEVbVPBmBg/B0cSGxSV3SJiCc1vrsogCmAIziQGIEDiRE4kBiBA4kROJAYgQOJETiQGIED\niU35rYua1OQ2SZJ0211nN7peU1bs3dbYWrNfeaextSTpcKOr9ccRHEiMwIHECBxIjMCBxAgcSIzA\ngcQIHEiMwIHECBxIrNIz2XpXVH1P40/UORQRi+ocCkAZk3mq6lcj4u3aJgFQHKfoQGJVAw9Jv7O9\nxfZInQMBKKfqKfqSiNhr+9OSNtreERFPHX2DXvgjkjRDMwuPCeB4VDqCR8Te3n/3SXpQ0uIJbsPW\nRUDHVNl8cJbt2Ufel/QNSS/VPRiAwVU5Rf+MpAdtH7n9LyLi0VqnAlBE38AjYrek8xqYBUBh/JkM\nSIzAgcQIHEiMwIHECBxIjMCBxAgcSIzAgcTYumgSPrjywkbXe/u8oUbXa05zWxf9v+MIDiRG4EBi\nBA4kRuBAYgQOJEbgQGIEDiRG4EBiBA4kVilw26fa3mB7h+1R2xfVPRiAwVV9qupPJT0aEd+yPV3i\nwufAVNA3cNunSLpY0rclKSIOSjpY71gASqhyin6WpLckrbP9vO21veujA+i4KoGfKOkCSbdFxEJJ\n+yXdeOyNbI/Y3mx780c6UHhMAMejSuBjksYiYlPv4w0aD/4/sHUR0D19A4+INyTtsb2g96llkl6u\ndSoARVR9FP06Set7j6DvlrS6vpEAlFIp8IjYJmlRzbMAKIxnsgGJETiQGIEDiRE4kBiBA4kROJAY\ngQOJETiQGIEDibE32SQcmNPsz8MvL3+xsbXWnfl0Y2ut/ttXGlvr8Padja3VRRzBgcQIHEiMwIHE\nCBxIjMCBxAgcSIzAgcQIHEiMwIHE+gZue4HtbUe9vWt7TRPDARhM36eqRsROSedLku0hSX+X9GDN\ncwEoYLKn6Msk/SUiXqtjGABlTfbFJisl3TvRF2yPSBqRpBlsPgp0QuUjeG/Tg8sl/Wqir7N1EdA9\nkzlFv0TS1oh4s65hAJQ1mcBX6X+cngPopkqB254p6euSHqh3HAAlVd2b7ANJn6p5FgCF8Uw2IDEC\nBxIjcCAxAgcSI3AgMQIHEiNwIDECBxJzRJT/pvZbkib7ktLTJL1dfJhuyHrfuF/t+WxEnN7vRrUE\nfjxsb46IRW3PUYes94371X2cogOJETiQWJcCv73tAWqU9b5xvzquM7+DAyivS0dwAIV1InDby23v\ntL3L9o1tz1OC7Xm2n7A9anu77evbnqkk20O2n7f9cNuzlGT7VNsbbO/o/dtd1PZMg2j9FL13rfVX\nNH7FmDFJz0laFREvtzrYgGyfIemMiNhqe7akLZJWTPX7dYTt70laJOmUiLis7XlKsX23pKcjYm3v\nQqMzI+Kdtuc6Xl04gi+WtCsidkfEQUn3Sbqi5ZkGFhGvR8TW3vvvSRqVNLfdqcqwPSzpUklr256l\nJNunSLpY0h2SFBEHp3LcUjcCnytpz1EfjylJCEfYni9poaRN7U5SzK2SbpD0cduDFHaWpLckrev9\n+rHW9qy2hxpEFwL3BJ9L89C+7ZMl3S9pTUS82/Y8g7J9maR9EbGl7VlqcKKkCyTdFhELJe2XNKUf\nE+pC4GOS5h318bCkvS3NUpTtaRqPe31EZLki7RJJl9t+VeO/Ti21fU+7IxUzJmksIo6caW3QePBT\nVhcCf07SObY/13tQY6Wkh1qeaWC2rfHf5UYj4pa25yklIm6KiOGImK/xf6vHI+KqlscqIiLekLTH\n9oLep5ZJmtIPik52b7LiIuKQ7WslPSZpSNKdEbG95bFKWCLpakkv2t7W+9wPIuKRFmdCf9dJWt87\n2OyWtLrleQbS+p/JANSnC6foAGpC4EBiBA4kRuBAYgQOJEbgQGIEDiRG4EBi/wYWKZOShpwGCQAA\nAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x1a17854da0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy\n",
"from sklearn import datasets\n",
"from sklearn import svm\n",
"digits=datasets.load_digits()\n",
"\n",
"clf=svm.SVC(gamma=0.001, C=100)\n",
"print(digits.data)\n",
"x,y=digits.data[:-1], digits.target[:-1]\n",
"clf.fit(x,y)\n",
"\n",
"print (digits.data)\n",
"print (x.shape)\n",
"\n",
"print(\"prediction:\", clf.predict(digits.data))\n",
"plt.imshow(digits.images[-2])\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,117 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Bounce_Rate Visitors\n",
"Day \n",
"1 65 43\n",
"2 72 53\n",
"3 62 34\n",
"4 64 45\n",
"5 54 64\n",
"6 66 34\n",
"Day\n",
"1 43\n",
"2 53\n",
"3 34\n",
"4 45\n",
"5 64\n",
"6 34\n",
"Name: Visitors, dtype: int64\n",
" Bounce_Rate Visitors\n",
"Day \n",
"1 65 43\n",
"2 72 53\n",
"3 62 34\n",
"4 64 45\n",
"5 54 64\n",
"6 66 34\n",
"[43, 53, 34, 45, 64, 34]\n",
"[[65 43]\n",
" [72 53]\n",
" [62 34]\n",
" [64 45]\n",
" [54 64]\n",
" [66 34]]\n"
]
}
],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib import style\n",
"style.use('ggplot')\n",
"import numpy as np\n",
"\n",
"web_stats={'Day':[1,2,3,4,5,6],\n",
" 'Visitors':[43,53,34,45,64,34],\n",
" 'Bounce_Rate':[65,72,62,64,54,66]}\n",
"\n",
"df=pd.DataFrame(web_stats)\n",
"\n",
"#print(df) #df stands for data frame\n",
"#print(df.head()) #prints the first 5 rows\n",
"#print(df.tail()) #prints the last 5 rows\n",
"#Specifying the number in the parentheses gives that number of rows\n",
"#print(df.head(2))\n",
"#print(df.tail(2))\n",
"\n",
"#df=df.set_index('Day')\n",
"\n",
"#OR you can do this:\n",
"df.set_index('Day', inplace=True)\n",
"print(df)\n",
"\n",
"#print (df['Visitors']) #prints specific column OR\n",
"print (df.Visitors)\n",
"\n",
"#referencing multiple columns\n",
"print (df[['Bounce_Rate','Visitors']])\n",
"\n",
"#making a list out of a column; this only work with one column because more than one would\n",
"#treat the dictionary like an array, which it isn't\n",
"print (df.Visitors.tolist())\n",
"\n",
"#to make it an array\n",
"print (np.array(df[['Bounce_Rate','Visitors']]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,106 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tensor(\"Mul_11:0\", shape=(4,), dtype=int32)\n",
"[ 5 12 21 32]\n",
"[ 5 12 21 32]\n",
"30\n",
"30\n",
"30\n",
"30\n"
]
}
],
"source": [
"# Import `tensorflow`\n",
"import tensorflow as tf\n",
"import os\n",
"\n",
"# Initialize two constants\n",
"x1 = tf.constant([1,2,3,4])\n",
"x2 = tf.constant([5,6,7,8])\n",
"\n",
"# Multiply\n",
"result = tf.multiply(x1, x2)\n",
"\n",
"# Print the result\n",
"print(result)\n",
"\n",
"# Intialize the Session\n",
"sess = tf.Session()\n",
"\n",
"# Print the result\n",
"print(sess.run(result))\n",
"\n",
"# Close the session\n",
"sess.close()\n",
"\n",
"#Or you can run the session like so:\n",
"with tf.Session() as sess:\n",
" output = sess.run(result)\n",
" print(output)\n",
"\n",
" \n",
"y1=tf.constant(5)\n",
"y2=tf.constant(6)\n",
"result=tf.multiply(y1, y2)\n",
"sess=tf.Session()\n",
"print(sess.run(result))\n",
"sess.close()\n",
"\n",
"#or\n",
"\n",
"with tf.Session() as sess:\n",
" print (sess.run(result))\n",
" \n",
"#this closes the session automatically\n",
"\n",
"#try this:\n",
"with tf.Session() as sess:\n",
" output=sess.run(result)\n",
" print (output)\n",
" \n",
"print (output)\n",
"#you can't run sess.run(result) outside of the with action"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,112 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import nltk\n",
"from nltk.tokenize import word_tokenize\n",
"from nltk.stem import WordNetLemmatizer\n",
"import numpy as np\n",
"import random\n",
"import pickle\n",
"from collections import Counter\n",
"\n",
"lemmatizer=WordNetLemmatizer()\n",
"hm_lines=1000000\n",
"\n",
"def create_lexicon(pos,neg):\n",
" lexicon=[]\n",
" for fi in [pos,neg]:\n",
" with open(fi, 'ri') as f:\n",
" contents=f.readlines()\n",
" for l in contents[:hm_lines]:\n",
" all_words=word_tokenize(l.lower())\n",
" lexicon+=list(all_words)\n",
" \n",
" \n",
" lexicon=[lemmatizer.lemmatize(i) for i in lexicon] \n",
" w_counts=Counter(lexicon)\n",
" l2=[]\n",
" for w in w_counts:\n",
" if 1000 > w_counts[w] >50:\n",
" l2.append(w)\n",
" \n",
" return l2\n",
" \n",
" \n",
"def sample_handling(sample, lexicon, classification):\n",
" featureset=[]\n",
" with open(sample, 'ri') as f:\n",
" contents=f.readlines()\n",
" for l in contents[:hm_lines]:\n",
" current_words=word_tokenize(l.lower())\n",
" current_words=[lemmatizer.lemmatize(i) for i in current_words]\n",
" features=np.zeros(len(lexicon))\n",
" for word in current_words:\n",
" if word.lower() in lexicon:\n",
" index_value=lexicon.index(word.lower())\n",
" feature[index_value]+=1\n",
" features=list(features)\n",
" featureset.append([features, classification])\n",
" \n",
" return featureset\n",
" \n",
" \n",
" \n",
" \n",
"def creat_featuresets_and_labels(pos, neg, test_size=0.1):\n",
" lexicon=create_lexicon(pos,neg)\n",
" features=[]\n",
" features+=sample_handling('pos.txt', lexicon, [1,0])\n",
" features+=sample_handling('neg.txt', lexicon, [0,1])\n",
" random.shuffle(features)\n",
" features=np.array(features)\n",
" testing_size=int(test_size*len(features))\n",
" train_x=list(features[:,0][:-testing_size]) #creates a list of the 0th element of every list in the overall list\n",
" train_y=list(features[:,1][:-testing_size])\n",
" \n",
" test_x=list(features[:,0][-testing_size:]) \n",
" test_y=list(features[:,1][-testing_size:])\n",
" \n",
" return train_x, train_y, test_x, test_y\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+22
View File
@@ -0,0 +1,22 @@
Year,Hares (x1000),Lynx (x1000)
1900,30.0,4.0
1901,47.2,6.1
1902,70.2,9.8
1903,77.4,35.2
1904,36.3,59.4
1905,20.6,41.7
1906,18.1,19.0
1907,21.4,13.0
1908,22.0,8.3
1909,25.4,9.1
1910,27.1,7.4
1911,40.3,8.0
1912,57,12.3
1913,76.6,19.5
1914,52.3,45.7
1915,19.5,51.1
1916,11.2,29.7
1917,7.6,15.8
1918,14.6,9.7
1919,16.2,10.1
1920,24.7,8.6
1 Year Hares (x1000) Lynx (x1000)
2 1900 30.0 4.0
3 1901 47.2 6.1
4 1902 70.2 9.8
5 1903 77.4 35.2
6 1904 36.3 59.4
7 1905 20.6 41.7
8 1906 18.1 19.0
9 1907 21.4 13.0
10 1908 22.0 8.3
11 1909 25.4 9.1
12 1910 27.1 7.4
13 1911 40.3 8.0
14 1912 57 12.3
15 1913 76.6 19.5
16 1914 52.3 45.7
17 1915 19.5 51.1
18 1916 11.2 29.7
19 1917 7.6 15.8
20 1918 14.6 9.7
21 1919 16.2 10.1
22 1920 24.7 8.6
+43
View File
@@ -0,0 +1,43 @@
import numpy as np
import matplotlib.pyplot as plt
def solver(m, H0, L0, dt, a, b, c, d, t0):
"""Solve the difference equations for H and L over m years
with time step dt (measured in years."""
num_intervals = int(m/float(dt))
t = np.linspace(t0, t0 + m, num_intervals+1)
H = np.zeros(t.size)
L = np.zeros(t.size)
print 'Init:', H0, L0, dt
H[0] = H0
L[0] = L0
for n in range(0, len(t)-1):
H[n+1] = H[n] + a*dt*H[n] - b*dt*H[n]*L[n]
L[n+1] = L[n] + d*dt*H[n]*L[n] - c*dt*L[n]
return H, L, t
# Load in data file
data = np.loadtxt('Hudson_Bay.csv', delimiter=',', skiprows=1)
# Make arrays containing x-axis and hares and lynx populations
t_e = data[:,0]
H_e = data[:,1]
L_e = data[:,2]
# Simulate using the model
H, L, t = solver(m=20, H0=34.91, L0=3.857, dt=0.1,
a=0.4807, b=0.02482, c=0.9272, d=0.02756,
t0=1900)
# Visualize simulations and data
plt.plot(t_e, H_e, 'b-+', t_e, L_e, 'r-o', t, H, 'm--', t, L, 'k--')
plt.xlabel('Year')
plt.ylabel('Numbers of hares and lynx')
plt.axis([1900, 1920, 0, 140])
plt.title(r'Population of hares and lynx 1900-1920 (x1000)')
plt.legend(('H_e', 'L_e', 'H', 'L'), loc='upper left')
plt.savefig('Hudson_Bay_sim.pdf')
plt.savefig('Hudson_Bay_sim.png')
plt.show()
+12
View File
@@ -0,0 +1,12 @@
import numpy as np
t = np.linspace(0, 10, 21) # 20 intervals in [0, 10]
dt = t[1] - t[0]
N = np.zeros(t.size)
N[0] = 1
r = 0.5
for n in range(0, N.size-1, 1):
N[n+1] = N[n] + r*dt*N[n]
print 'N[%d]=%.1f' % (n+1, N[n+1])
+11
View File
@@ -0,0 +1,11 @@
0,100
600,140
1200,250
1800,360
2400,480
3000,820
3600,1300
4200,1700
4800,2900
5400,3900
6000,7000
1 0 100
2 600 140
3 1200 250
4 1800 360
5 2400 480
6 3000 820
7 3600 1300
8 4200 1700
9 4800 2900
10 5400 3900
11 6000 7000
+27
View File
@@ -0,0 +1,27 @@
import numpy as np
# Estimate r
data = np.loadtxt('ecoli.csv', delimiter=',')
t_e = data[:,0]
N_e = data[:,1]
i = 2 # Data point (i,i+1) used to estimate r
r = (N_e[i+1] - N_e[i])/(N_e[i]*(t_e[i+1] - t_e[i]))
print 'Estimated r=%.5f' % r
# Can experiment with r values and see if the model can
# match the data better
T = 1200 # cell can divide after T sec
t_max = 5*T # 5 generations in experiment
t = np.linspace(0, t_max, 1000)
dt = t[1] - t[0]
N = np.zeros(t.size)
N[0] = 100
for n in range(0, len(t)-1, 1):
N[n+1] = N[n] + r*dt*N[n]
import matplotlib.pyplot as plt
plt.plot(t, N, 'r-', t_e, N_e, 'bo')
plt.xlabel('time [s]'); plt.ylabel('N')
plt.legend(['model', 'experiment'], loc='upper left')
plt.show()
+27
View File
@@ -0,0 +1,27 @@
import numpy as np
data = np.loadtxt('ecoli.csv', delimiter=',')
t_experiment = data[:,0]
N_experiment = data[:,1]
def error(p):
r = p[0]
T = 1200 # cell can divide after T sec
t_max = 5*T # 5 generations in experiment
t = np.linspace(0, t_max, len(t_experiment))
dt = (t[1] - t[0])
N = np.zeros(t.size)
N[0] = 100
for n in range(0, len(t)-1, 1):
N[n+1] = N[n] + r*dt*N[n]
e = np.sqrt(np.sum((N - N_experiment)**2))/N[0] # error measure
e = abs(N[-1] - N_experiment[-1])/N[0]
print 'r=', r, 'e=',e
return e
from scipy.optimize import minimize
p = minimize(error, [0.0006], tol=1E-5)
print p
+19
View File
@@ -0,0 +1,19 @@
import numpy as np
from matplotlib import pyplot as plt
# Load in data file
data = np.loadtxt('Hudson_Bay.dat', delimiter=',', skiprows=1)
# Make arrays containing x-axis and hares and lynx populations
year = data[:,0]
hares = data[:,1]
lynx = data[:,2]
plt.plot(year, hares ,'b-+', year, lynx, 'r-o')
plt.axis([1900,1920,0, 100.0])
plt.xlabel(r'Year')
plt.ylabel(r'Numbers of hares and lynx ')
plt.legend(('Hares','Lynx'), loc='upper right')
plt.title(r'Population of hares and lynx from 1900-1920 (x1000)}')
plt.savefig('Hudson_Bay_data.pdf')
plt.savefig('Hudson_Bay_data.png')
plt.show()
@@ -0,0 +1,19 @@
# ResamplingAnalysisScripts
## Sample Scripts for data Analysis
So far this is a simple python script (should be made parallel...) to perform resampling of a data set. Methods used are __Bootstrapping__, __Jackknife__ and __Blocking__.
## Usage
Simply run `python analysis.py FILENAME.xxx [NLINES]`
Where `FILENAME` is expected to have a 3 charachter extension `NLINES` (optional) is the number of lines in the file to read and process (default is the whole file, but it gets very slow above 2-3 hundred thousand entries)
Ouput is located into the `FILENAME/` folder.
If more than 10⁵ lines are specified the autocorrelation function won't be computed, as it would take too long.
The `gaussian.dat` dataset has been generated with numpy, as a proof of concept. It represents a normally distributed set of 5x10⁵ elements with `std = 0.05`. One will notice that the estimate on the error of the central value is greatly improved by all resampling methods.
`energy.dat` is an autocorrelated data set, with autocorrelation time of roughly 200. It is useful to see the use of blocking on this dataset as a convenient method to estimate the autocorrelation time (compare the elapsed time on the different methods).
In the `plaquette.dat` file there is a small data set (just 1000 samples) and it shows the strenght of using resampling methods to better estimate the error on the central value as opposed to the standard deviation.
@@ -0,0 +1,223 @@
from sys import argv
from os import mkdir, path
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
from matplotlib.font_manager import FontProperties
# Timing Decorator
def timeFunction(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print '%s Function Took: \t %0.3f s' % (f.func_name.title(), (time2-time1))
return ret
return wrap
class dataAnalysisClass:
# General Init functions
def __init__(self, fileName, size=0):
self.inputFileName = fileName
self.loadData(size)
self.createOutputFolder()
self.avg = np.average(self.data)
self.var = np.var(self.data)
self.std = np.std(self.data)
def loadData(self, size=0):
if size != 0:
with open(self.inputFileName) as inputFile:
self.data = np.zeros(size)
for x in xrange(size):
self.data[x] = float(next(inputFile))
else:
self.data = np.loadtxt(self.inputFileName)
# Statistical Analysis with Multiple Methods
def runAllAnalyses(self):
if len(self.data) <= 100000:
print "Autocorrelation..."
self.autocorrelation()
print "Bootstrap..."
self.bootstrap()
print "Jackknife..."
self.jackknife()
print "Blocking..."
self.blocking()
# Standard Autocorrelation
@timeFunction
def autocorrelation(self):
self.acf = np.zeros(len(self.data)/2)
for k in range(0, len(self.data)/2):
self.acf[k] = np.corrcoef(np.array([self.data[0:len(self.data)-k], \
self.data[k:len(self.data)]]))[0,1]
# Bootstrap
@timeFunction
def bootstrap(self, nBoots = 1000):
bootVec = np.zeros(nBoots)
for k in range(0,nBoots):
bootVec[k] = np.average(np.random.choice(self.data, len(self.data)))
self.bootAvg = np.average(bootVec)
self.bootVar = np.var(bootVec)
self.bootStd = np.std(bootVec)
# Jackknife
@timeFunction
def jackknife(self):
jackknVec = np.zeros(len(self.data))
for k in range(0,len(self.data)):
jackknVec[k] = np.average(np.delete(self.data, k))
self.jackknAvg = self.avg - (len(self.data) - 1) * (np.average(jackknVec) - self.avg)
self.jackknVar = float(len(self.data) - 1) * np.var(jackknVec)
self.jackknStd = np.sqrt(self.jackknVar)
# Blocking
@timeFunction
def blocking(self, blockSizeMax = 500):
blockSizeMin = 1
self.blockSizes = []
self.meanVec = []
self.varVec = []
for i in range(blockSizeMin, blockSizeMax):
if(len(self.data) % i != 0):
pass#continue
blockSize = i
meanTempVec = []
varTempVec = []
startPoint = 0
endPoint = blockSize
while endPoint <= len(self.data):
meanTempVec.append(np.average(self.data[startPoint:endPoint]))
startPoint = endPoint
endPoint += blockSize
mean, var = np.average(meanTempVec), np.var(meanTempVec)/len(meanTempVec)
self.meanVec.append(mean)
self.varVec.append(var)
self.blockSizes.append(blockSize)
self.blockingAvg = np.average(self.meanVec[-200:])
self.blockingVar = (np.average(self.varVec[-200:]))
self.blockingStd = np.sqrt(self.blockingVar)
# Plot of Data, Autocorrelation Function and Histogram
def plotAll(self):
self.createOutputFolder()
if len(self.data) <= 100000:
self.plotAutocorrelation()
self.plotData()
self.plotHistogram()
self.plotBlocking()
# Create Output Plots Folder
def createOutputFolder(self):
self.outName = self.inputFileName[:-4]
if not path.exists(self.outName):
mkdir(self.outName)
# Plot the Dataset, Mean and Std
def plotData(self):
# Far away plot
font = {'fontname':'serif'}
plt.plot(range(0, len(self.data)), self.data, 'r-', linewidth=1)
plt.plot([0, len(self.data)], [self.avg, self.avg], 'b-', linewidth=1)
plt.plot([0, len(self.data)], [self.avg + self.std, self.avg + self.std], 'g--', linewidth=1)
plt.plot([0, len(self.data)], [self.avg - self.std, self.avg - self.std], 'g--', linewidth=1)
plt.ylim(self.avg - 5*self.std, self.avg + 5*self.std)
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.4f'))
plt.xlim(0, len(self.data))
plt.ylabel(self.outName.title() + ' Monte Carlo Evolution', **font)
plt.xlabel('MonteCarlo History', **font)
plt.title(self.outName.title(), **font)
plt.savefig(self.outName + "/data.eps")
plt.savefig(self.outName + "/data.png")
plt.clf()
# Plot Histogram of Dataset and Gaussian around it
def plotHistogram(self):
binNumber = 50
font = {'fontname':'serif'}
count, bins, ignore = plt.hist(self.data, bins=np.linspace(self.avg - 5*self.std, self.avg + 5*self.std, binNumber))
plt.plot([self.avg, self.avg], [0,np.max(count)+10], 'b-', linewidth=1)
plt.ylim(0,np.max(count)+10)
plt.ylabel(self.outName.title() + ' Histogram', **font)
plt.xlabel(self.outName.title() , **font)
plt.title('Counts', **font)
#gaussian
norm = 0
for i in range(0,len(bins)-1):
norm += (bins[i+1]-bins[i])*count[i]
plt.plot(bins, norm/(self.std * np.sqrt(2 * np.pi)) * np.exp( - (bins - self.avg)**2 / (2 * self.std**2) ), linewidth=1, color='r')
plt.savefig(self.outName + "/hist.eps")
plt.savefig(self.outName + "/hist.png")
plt.clf()
# Plot the Autocorrelation Function
def plotAutocorrelation(self):
font = {'fontname':'serif'}
plt.plot(range(1, len(self.data)/2), self.acf[1:], 'r-')
plt.ylim(-1, 1)
plt.xlim(0, len(self.data)/2)
plt.ylabel('Autocorrelation Function', **font)
plt.xlabel('Lag', **font)
plt.title('Autocorrelation', **font)
plt.savefig(self.outName + "/autocorrelation.eps")
plt.savefig(self.outName + "/autocorrelation.png")
plt.clf()
def plotBlocking(self):
font = {'fontname':'serif'}
plt.plot(self.blockSizes, self.varVec, 'r-')
plt.ylabel('Variance', **font)
plt.xlabel('Block Size', **font)
plt.title('Blocking', **font)
plt.savefig(self.outName + "/blocking.eps")
plt.savefig(self.outName + "/blocking.png")
plt.clf()
# Print Stuff to the Terminal
def printOutput(self):
print "\nSample Size: \t", len(self.data)
print "\n=========================================\n"
print "Sample Average: \t", self.avg
print "Sample Variance:\t", self.var
print "Sample Std: \t", self.std
print "\n=========================================\n"
print "Bootstrap Average: \t", self.bootAvg
print "Bootstrap Variance:\t", self.bootVar
print "Bootstrap Error: \t", self.bootStd
print "\n=========================================\n"
print "Jackknife Average: \t", self.jackknAvg
print "Jackknife Variance:\t", self.jackknVar
print "Jackknife Error: \t", self.jackknStd
print "\n=========================================\n"
print "Blocking Average: \t", self.blockingAvg
print "Blocking Variance:\t", self.blockingVar
print "Blocking Error: \t", self.blockingStd, "\n"
# Initialize the class
if len(argv) > 2:
dataAnalysis = dataAnalysisClass(argv[1], int(argv[2]))
else:
dataAnalysis = dataAnalysisClass(argv[1])
# Run Analyses
dataAnalysis.runAllAnalyses()
# Plot the data
dataAnalysis.plotAll()
# Print Some Output
dataAnalysis.printOutput()
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

@@ -0,0 +1,364 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Project on Machine Learning">
<title>Project on Machine Learning</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Machine learning (ML) approaches to data from Ising model '
'calculations',
2,
None,
'___sec0'),
('Introduction', 3, None, '___sec1'),
('Part a): Producing the data', 3, None, '___sec2'),
('Part b): Estimating the coupling constant of the '
'one-dimensional Ising model',
3,
None,
'___sec3'),
('Part c): Determine the phase of the two-dimensional Ising '
'model',
3,
None,
'___sec4'),
('Part d): Classifying the Ising model phase using neural '
'networks',
3,
None,
'___sec5'),
('Part e): Summary', 3, None, '___sec6'),
('Background literature', 2, None, '___sec7'),
('Introduction to numerical projects', 2, None, '___sec8'),
('Software and needed installations', 2, None, '___sec9')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="Project-bs.html">Project on Machine Learning</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="#___sec0" style="font-size: 80%;"><b>Machine learning (ML) approaches to data from Ising model calculations</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec1" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Introduction</a></li>
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part a): Producing the data</a></li>
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part b): Estimating the coupling constant of the one-dimensional Ising model</a></li>
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part c): Determine the phase of the two-dimensional Ising model</a></li>
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part d): Classifying the Ising model phase using neural networks</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part e): Summary</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;"><b>Background literature</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;"><b>Introduction to numerical projects</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;"><b>Software and needed installations</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0000"></a>
<!-- ------------------- main content ---------------------- -->
<div class="jumbotron">
<center><h1>Project on Machine Learning</h1></center> <!-- document title -->
<p>
<!-- author(s): <a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a> -->
<center>
<b><a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a></b>
</center>
<p>
<!-- institution -->
<center><b>Department of Physics, University of Oslo, Norway</b></center>
<br>
<p>
<center><h4>May 2018</h4></center> <!-- date -->
<br>
<p>
</div> <!-- end jumbotron -->
<h2 id="___sec0" class="anchor">Machine learning (ML) approaches to data from Ising model calculations </h2>
<h3 id="___sec1" class="anchor">Introduction </h3>
<p>
The aim of this project is to use an already developed Monte Carlo program for the one-dimensional and two-dimensional <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Programs/IsingModel" target="_self">Ising model</a>, in order to produce the spin configurations for a series of energies \( E_i \) (10000 in total) for a system of \( L=40 \) spins in one dimension and \( L=40\times 40 \) in two dimensions at three different temperatures.
In its simplest form the energy of the Ising model is expressed as, without an externally applied magnetic field,
$$
E=-J\sum_{< kl >}^{N}s_ks_l
$$
with
\( s_k=\pm 1 \). The quantity \( N \) represents the total number of spins and \( J \) is a coupling
constant expressing the strength of the interaction between
neighboring spins. The symbol \( < kl> \) indicates that we sum over
nearest neighbors only. We will assume that we have a ferromagnetic
ordering, viz \( J> 0 \). We will use periodic boundary conditions and
the Metropolis algorithm only. The spins take values \( -1 \) and \( +1 \) only.
<p>
We will use the Ising model to generate our training data and will focus mainly on supervised training. We will follow closely the recent article of <a href="https://arxiv.org/abs/1803.08823" target="_self">Mehta et al, arXiv 1803.08823</a>. This article stands out as an excellent review on machine learning (ML) algorithms applied to typical physics problems. The added benefit is that each figure and model presented in <a href="https://physics.bu.edu/~pankajm/MLnotebooks.html" target="_self">this article is accompanied by its jupyter notebook</a>. This means that we can start using these and compare with our own results. In case you wish to use their data for the Ising model, their data can be downloaded from the same link which lists to the jupyter notebooks. See also at the end of the project description for more information on how to install various Python packages.
<p>
With the abovementioned configurations we will determine, using first various
regression methods, the value of the coupling constant for the energy
of the one-dimensional Ising model. Thereafter, we will use the
two-dimensional data, but now computed at different temperatures, in
order to classify the phase of the Ising model. Below the critical
temperature, the system will be in a so-called ferromagnetic
phase. Close to the critical temperature, the final magnetization becomes smaller and smaller in absolute value
while above the critical temperature,
the net magnetization is zero. This classification case, that is the
two-dimensional Ising model, will be studied using logistic regression, a <b>random forest</b>
algorithm and deep neural networks.
<p>
You should try to program at least one of these methods yourself (choose the one you prefer).
Feel free to use the notebooks to benchmark your code. If you wish to write your own C++ or Fortran program for say a simple neural network model, please feel free to do so.
You can then benchmark your results against the above jupyter notebooks. More information can also be found at the link for the lecture notes of <a href="https://compphysics.github.io/MachineLearning/doc/web/course.html" target="_self">FYS-STK4155</a>.
<p>
We recommend that you form groups of 2-3 students and try to
collaborate on the notebooks, develop your own software and discuss
the final presentations. You can collaborate on all these topics. The
final presentation should include an overview of popular machine
learning algorithms as introduction and motivation. Thereafter you
discuss the explicit Ising model data and how you have implemented the
ML algorithms discussed here, discuss their pros and cons and try to
develop your own code for at least one of these algorithms. You are
encouraged to use the abovementioned notebooks as starting point and
guidance. The duration of your presentation should at most be 30
mins. Allow for approximately 15 mins for discussions and questions.
<h3 id="___sec2" class="anchor">Part a): Producing the data </h3>
<p>
You can use the Ising model data from the article of Mehta <em>et al.</em>, or generate your own data.
If you opt for using your own Ising model code, you need to generate \( 10000 \) energy configurations with their spin orientations after the system has reached its most likely state. These energies and their corresponding spin orientations
represent then your data.
We will use a fixed lattice of \( L\times L = 40 \times 40 \) spins in two dimensions and \( L=40 \) spins in one dimension.
Make sure the calculations have been equilibrated. For the two-dimensional system, compute the configurations
for three values of the temperature, namely \( T=0.75 \) (ordered phase), \( T=2.3 \) (near the critical point) and \( T=4.0 \) (disordered phase).
For the one-dimensional system it suffices to compute the various configurations for one temperature only, say \( T=2.0 \).
These are the data you will use to study different ML algorithms.
We generate our data with \( J=1 \).
<h3 id="___sec3" class="anchor">Part b): Estimating the coupling constant of the one-dimensional Ising model </h3>
<p>
We start with the one-dimensional Ising model and use the data we have generated with \( J=1 \). Use linear regression, Lasso and Ridge regression as described section 6 and in Notebook 4 of <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html" target="_self">Mehta *et al.*</a>. Discuss the methods and how they perform in computing the coupling constant \( J \). Give a critical analysis and discuss how to evaluate the <em>cost function</em>. You should feel free to write your own code, see also
the lecture notes of <a href="https://compphysics.github.io/MachineLearning/doc/web/course.html" target="_self">FYS-STK4155</a>, in particular te material on least square methods. You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
<h3 id="___sec4" class="anchor">Part c): Determine the phase of the two-dimensional Ising model </h3>
<p>
We switch now to binary classification methods and use logistic regression to define the phases of the Ising model.
Use described section 7 and in Notebook 6 of <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVII-logreg_ising.html" target="_self">Mehta *et al.*</a>. Discuss the methods and how they perform. Give a critical analysis and discuss how to evaluate the <em>cost function</em>. You should feel free to write your own code. Use thereafter the <em>random forests</em> algorithm to classify the same phases as done with logistic regression and discuss the pros and cons of these methods. For <em>random forests</em> you can use <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVIII-randomforests_ising.html" target="_self">notebook 9</a> of Mehta <em>et al.</em>
<p>
You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
<h3 id="___sec5" class="anchor">Part d): Classifying the Ising model phase using neural networks </h3>
<p>
We end the classification problem of the phases of the Ising model by employing the algorithm for so-called feed-forward deep neural networks (see section 9 of Mehta <em>et al.</em>). The method is described in <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CIX-DNN_ising_TFlow.html" target="_self">notebook 12</a>.
<p>
You can use tensorflow to perform these analyses. See below for instruction on how to install tensorflow.
<h3 id="___sec6" class="anchor">Part e): Summary </h3>
<p>
You should make a summary of the various methods and their pros and cons. For the final presentation, you should have at least coded one of these methods yourself and discussed the evaluation of the cost function.
<h2 id="___sec7" class="anchor">Background literature </h2>
<p>
On Machine Learning we recommend strongly the article of Mehta <em>et al.</em>
Textbooks on Machine Learning can be found at the <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Textbooks" target="_self">Github address of FYS-STK4155</a>, see in particular Marsland's text.
<ul>
<li> <a href="https://arxiv.org/abs/1803.08823" target="_self">Mehta et al, arXiv 1803.08823</a>, <em>A high-bias, low-variance introduction to Machine Learning for physicists</em>, ArXiv:1803.08823.</li>
</ul>
If you wish to read more about the Ising model and statistical physics here are three suggestions.
<ul>
<li> <a href="http://www.worldscientific.com/worldscibooks/10.1142/5660" target="_self">M. Plischke and B. Bergersen</a>, <em>Equilibrium Statistical Physics</em>, World Scientific, see chapters 5 and 6.</li>
<li> <a href="http://www.cambridge.org/no/academic/subjects/physics/computational-science-and-modelling/guide-monte-carlo-simulations-statistical-physics-4th-edition?format=HB" target="_self">D. P. Landau and K. Binder</a>, <em>A Guide to Monte Carlo Simulations in Statistical Physics</em>, Cambridge, see chapters 2,3 and 4.</li>
<li> <a href="https://global.oup.com/academic/product/monte-carlo-methods-in-statistical-physics-9780198517979?cc=no&lang=en&" target="_self">M. E. J. Newman and T. Barkema</a>, <em>Monte Carlo Methods in Statistical Physics</em>, Oxford, see chapters 3 and 4.</li>
</ul>
<h2 id="___sec8" class="anchor">Introduction to numerical projects </h2>
<p>
Here follows a brief recipe and recommendation on how to write a report for each
project.
<ul>
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
<li> Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.</li>
<li> Include the source code of your program. Comment your program properly.</li>
<li> If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.</li>
<li> Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.</li>
<li> Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.</li>
<li> Try to give an interpretation of you results in your answers to the problems.</li>
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
</ul>
<h2 id="___sec9" class="anchor">Software and needed installations </h2>
<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 tensorflow sympy pandas pillow</li>
</ol>
For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>
See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</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
<ol>
<li> brew install python3</li>
</ol>
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>
<li> sudo apt-get install python3 (or python for python2.7)</li>
</ol>
etc etc.
<p>
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
<ol>
<li> <a href="https://docs.anaconda.com/" target="_self">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="_self">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>
Popular software packages written in Python for ML are
<ul>
<li> <a href="http://scikit-learn.org/stable/" target="_self">Scikit-learn</a>,</li>
<li> <a href="https://www.tensorflow.org/" target="_self">Tensorflow</a>,</li>
<li> <a href="http://pytorch.org/" target="_self">PyTorch</a> and</li>
<li> <a href="https://keras.io/" target="_self">Keras</a>.</li>
</ul>
These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li class="active"><a href="._Project-bs000.html">1</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license
</center>
</body>
</html>
@@ -0,0 +1,364 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Project on Machine Learning">
<title>Project on Machine Learning</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Machine learning (ML) approaches to data from Ising model '
'calculations',
2,
None,
'___sec0'),
('Introduction', 3, None, '___sec1'),
('Part a): Producing the data', 3, None, '___sec2'),
('Part b): Estimating the coupling constant of the '
'one-dimensional Ising model',
3,
None,
'___sec3'),
('Part c): Determine the phase of the two-dimensional Ising '
'model',
3,
None,
'___sec4'),
('Part d): Classifying the Ising model phase using neural '
'networks',
3,
None,
'___sec5'),
('Part e): Summary', 3, None, '___sec6'),
('Background literature', 2, None, '___sec7'),
('Introduction to numerical projects', 2, None, '___sec8'),
('Software and needed installations', 2, None, '___sec9')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="Project-bs.html">Project on Machine Learning</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="#___sec0" style="font-size: 80%;"><b>Machine learning (ML) approaches to data from Ising model calculations</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec1" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Introduction</a></li>
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part a): Producing the data</a></li>
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part b): Estimating the coupling constant of the one-dimensional Ising model</a></li>
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part c): Determine the phase of the two-dimensional Ising model</a></li>
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part d): Classifying the Ising model phase using neural networks</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Part e): Summary</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;"><b>Background literature</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;"><b>Introduction to numerical projects</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;"><b>Software and needed installations</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0000"></a>
<!-- ------------------- main content ---------------------- -->
<div class="jumbotron">
<center><h1>Project on Machine Learning</h1></center> <!-- document title -->
<p>
<!-- author(s): <a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a> -->
<center>
<b><a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a></b>
</center>
<p>
<!-- institution -->
<center><b>Department of Physics, University of Oslo, Norway</b></center>
<br>
<p>
<center><h4>May 2018</h4></center> <!-- date -->
<br>
<p>
</div> <!-- end jumbotron -->
<h2 id="___sec0" class="anchor">Machine learning (ML) approaches to data from Ising model calculations </h2>
<h3 id="___sec1" class="anchor">Introduction </h3>
<p>
The aim of this project is to use an already developed Monte Carlo program for the one-dimensional and two-dimensional <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Programs/IsingModel" target="_self">Ising model</a>, in order to produce the spin configurations for a series of energies \( E_i \) (10000 in total) for a system of \( L=40 \) spins in one dimension and \( L=40\times 40 \) in two dimensions at three different temperatures.
In its simplest form the energy of the Ising model is expressed as, without an externally applied magnetic field,
$$
E=-J\sum_{< kl >}^{N}s_ks_l
$$
with
\( s_k=\pm 1 \). The quantity \( N \) represents the total number of spins and \( J \) is a coupling
constant expressing the strength of the interaction between
neighboring spins. The symbol \( < kl> \) indicates that we sum over
nearest neighbors only. We will assume that we have a ferromagnetic
ordering, viz \( J> 0 \). We will use periodic boundary conditions and
the Metropolis algorithm only. The spins take values \( -1 \) and \( +1 \) only.
<p>
We will use the Ising model to generate our training data and will focus mainly on supervised training. We will follow closely the recent article of <a href="https://arxiv.org/abs/1803.08823" target="_self">Mehta et al, arXiv 1803.08823</a>. This article stands out as an excellent review on machine learning (ML) algorithms applied to typical physics problems. The added benefit is that each figure and model presented in <a href="https://physics.bu.edu/~pankajm/MLnotebooks.html" target="_self">this article is accompanied by its jupyter notebook</a>. This means that we can start using these and compare with our own results. In case you wish to use their data for the Ising model, their data can be downloaded from the same link which lists to the jupyter notebooks. See also at the end of the project description for more information on how to install various Python packages.
<p>
With the abovementioned configurations we will determine, using first various
regression methods, the value of the coupling constant for the energy
of the one-dimensional Ising model. Thereafter, we will use the
two-dimensional data, but now computed at different temperatures, in
order to classify the phase of the Ising model. Below the critical
temperature, the system will be in a so-called ferromagnetic
phase. Close to the critical temperature, the final magnetization becomes smaller and smaller in absolute value
while above the critical temperature,
the net magnetization is zero. This classification case, that is the
two-dimensional Ising model, will be studied using logistic regression, a <b>random forest</b>
algorithm and deep neural networks.
<p>
You should try to program at least one of these methods yourself (choose the one you prefer).
Feel free to use the notebooks to benchmark your code. If you wish to write your own C++ or Fortran program for say a simple neural network model, please feel free to do so.
You can then benchmark your results against the above jupyter notebooks. More information can also be found at the link for the lecture notes of <a href="https://compphysics.github.io/MachineLearning/doc/web/course.html" target="_self">FYS-STK4155</a>.
<p>
We recommend that you form groups of 2-3 students and try to
collaborate on the notebooks, develop your own software and discuss
the final presentations. You can collaborate on all these topics. The
final presentation should include an overview of popular machine
learning algorithms as introduction and motivation. Thereafter you
discuss the explicit Ising model data and how you have implemented the
ML algorithms discussed here, discuss their pros and cons and try to
develop your own code for at least one of these algorithms. You are
encouraged to use the abovementioned notebooks as starting point and
guidance. The duration of your presentation should at most be 30
mins. Allow for approximately 15 mins for discussions and questions.
<h3 id="___sec2" class="anchor">Part a): Producing the data </h3>
<p>
You can use the Ising model data from the article of Mehta <em>et al.</em>, or generate your own data.
If you opt for using your own Ising model code, you need to generate \( 10000 \) energy configurations with their spin orientations after the system has reached its most likely state. These energies and their corresponding spin orientations
represent then your data.
We will use a fixed lattice of \( L\times L = 40 \times 40 \) spins in two dimensions and \( L=40 \) spins in one dimension.
Make sure the calculations have been equilibrated. For the two-dimensional system, compute the configurations
for three values of the temperature, namely \( T=0.75 \) (ordered phase), \( T=2.3 \) (near the critical point) and \( T=4.0 \) (disordered phase).
For the one-dimensional system it suffices to compute the various configurations for one temperature only, say \( T=2.0 \).
These are the data you will use to study different ML algorithms.
We generate our data with \( J=1 \).
<h3 id="___sec3" class="anchor">Part b): Estimating the coupling constant of the one-dimensional Ising model </h3>
<p>
We start with the one-dimensional Ising model and use the data we have generated with \( J=1 \). Use linear regression, Lasso and Ridge regression as described section 6 and in Notebook 4 of <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html" target="_self">Mehta *et al.*</a>. Discuss the methods and how they perform in computing the coupling constant \( J \). Give a critical analysis and discuss how to evaluate the <em>cost function</em>. You should feel free to write your own code, see also
the lecture notes of <a href="https://compphysics.github.io/MachineLearning/doc/web/course.html" target="_self">FYS-STK4155</a>, in particular te material on least square methods. You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
<h3 id="___sec4" class="anchor">Part c): Determine the phase of the two-dimensional Ising model </h3>
<p>
We switch now to binary classification methods and use logistic regression to define the phases of the Ising model.
Use described section 7 and in Notebook 6 of <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVII-logreg_ising.html" target="_self">Mehta *et al.*</a>. Discuss the methods and how they perform. Give a critical analysis and discuss how to evaluate the <em>cost function</em>. You should feel free to write your own code. Use thereafter the <em>random forests</em> algorithm to classify the same phases as done with logistic regression and discuss the pros and cons of these methods. For <em>random forests</em> you can use <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVIII-randomforests_ising.html" target="_self">notebook 9</a> of Mehta <em>et al.</em>
<p>
You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
<h3 id="___sec5" class="anchor">Part d): Classifying the Ising model phase using neural networks </h3>
<p>
We end the classification problem of the phases of the Ising model by employing the algorithm for so-called feed-forward deep neural networks (see section 9 of Mehta <em>et al.</em>). The method is described in <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CIX-DNN_ising_TFlow.html" target="_self">notebook 12</a>.
<p>
You can use tensorflow to perform these analyses. See below for instruction on how to install tensorflow.
<h3 id="___sec6" class="anchor">Part e): Summary </h3>
<p>
You should make a summary of the various methods and their pros and cons. For the final presentation, you should have at least coded one of these methods yourself and discussed the evaluation of the cost function.
<h2 id="___sec7" class="anchor">Background literature </h2>
<p>
On Machine Learning we recommend strongly the article of Mehta <em>et al.</em>
Textbooks on Machine Learning can be found at the <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Textbooks" target="_self">Github address of FYS-STK4155</a>, see in particular Marsland's text.
<ul>
<li> <a href="https://arxiv.org/abs/1803.08823" target="_self">Mehta et al, arXiv 1803.08823</a>, <em>A high-bias, low-variance introduction to Machine Learning for physicists</em>, ArXiv:1803.08823.</li>
</ul>
If you wish to read more about the Ising model and statistical physics here are three suggestions.
<ul>
<li> <a href="http://www.worldscientific.com/worldscibooks/10.1142/5660" target="_self">M. Plischke and B. Bergersen</a>, <em>Equilibrium Statistical Physics</em>, World Scientific, see chapters 5 and 6.</li>
<li> <a href="http://www.cambridge.org/no/academic/subjects/physics/computational-science-and-modelling/guide-monte-carlo-simulations-statistical-physics-4th-edition?format=HB" target="_self">D. P. Landau and K. Binder</a>, <em>A Guide to Monte Carlo Simulations in Statistical Physics</em>, Cambridge, see chapters 2,3 and 4.</li>
<li> <a href="https://global.oup.com/academic/product/monte-carlo-methods-in-statistical-physics-9780198517979?cc=no&lang=en&" target="_self">M. E. J. Newman and T. Barkema</a>, <em>Monte Carlo Methods in Statistical Physics</em>, Oxford, see chapters 3 and 4.</li>
</ul>
<h2 id="___sec8" class="anchor">Introduction to numerical projects </h2>
<p>
Here follows a brief recipe and recommendation on how to write a report for each
project.
<ul>
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
<li> Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.</li>
<li> Include the source code of your program. Comment your program properly.</li>
<li> If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.</li>
<li> Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.</li>
<li> Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.</li>
<li> Try to give an interpretation of you results in your answers to the problems.</li>
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
</ul>
<h2 id="___sec9" class="anchor">Software and needed installations </h2>
<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 tensorflow sympy pandas pillow</li>
</ol>
For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>
See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</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
<ol>
<li> brew install python3</li>
</ol>
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>
<li> sudo apt-get install python3 (or python for python2.7)</li>
</ol>
etc etc.
<p>
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
<ol>
<li> <a href="https://docs.anaconda.com/" target="_self">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="_self">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>
Popular software packages written in Python for ML are
<ul>
<li> <a href="http://scikit-learn.org/stable/" target="_self">Scikit-learn</a>,</li>
<li> <a href="https://www.tensorflow.org/" target="_self">Tensorflow</a>,</li>
<li> <a href="http://pytorch.org/" target="_self">PyTorch</a> and</li>
<li> <a href="https://keras.io/" target="_self">Keras</a>.</li>
</ul>
These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li class="active"><a href="._Project-bs000.html">1</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license
</center>
</body>
</html>
+305
View File
@@ -0,0 +1,305 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Project on Machine Learning">
<title>Project on Machine Learning</title>
<style type="text/css">
/* bloodish style */
body {
font-family: Helvetica, Verdana, Arial, Sans-serif;
color: #404040;
background: #ffffff;
}
h1 { font-size: 1.8em; color: #8A0808; }
h2 { font-size: 1.6em; color: #8A0808; }
h3 { font-size: 1.4em; color: #8A0808; }
h4 { color: #8A0808; }
a { color: #8A0808; text-decoration:none; }
tt { font-family: "Courier New", Courier; }
/* pre style removed because it will interfer with pygments */
p { text-indent: 0px; }
hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
p.caption { width: 80%; font-style: normal; text-align: left; }
hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa}
div { text-align: justify; text-justify: inter-word; }
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Machine learning (ML) approaches to data from Ising model '
'calculations',
2,
None,
'___sec0'),
('Introduction', 3, None, '___sec1'),
('Part a): Producing the data', 3, None, '___sec2'),
('Part b): Estimating the coupling constant of the '
'one-dimensional Ising model',
3,
None,
'___sec3'),
('Part c): Determine the phase of the two-dimensional Ising '
'model',
3,
None,
'___sec4'),
('Part d): Classifying the Ising model phase using neural '
'networks',
3,
None,
'___sec5'),
('Part e): Summary', 3, None, '___sec6'),
('Background literature', 2, None, '___sec7'),
('Introduction to numerical projects', 2, None, '___sec8'),
('Software and needed installations', 2, None, '___sec9')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "AMS" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- ------------------- main content ---------------------- -->
<center><h1>Project on Machine Learning</h1></center> <!-- document title -->
<p>
<!-- author(s): <a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_blank">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a> -->
<center>
<b><a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_blank">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a></b>
</center>
<p>
<!-- institution -->
<center><b>Department of Physics, University of Oslo, Norway</b></center>
<br>
<p>
<center><h4>May 2018</h4></center> <!-- date -->
<br>
<h2 id="___sec0">Machine learning (ML) approaches to data from Ising model calculations </h2>
<h3 id="___sec1">Introduction </h3>
<p>
The aim of this project is to use an already developed Monte Carlo program for the one-dimensional and two-dimensional <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Programs/IsingModel" target="_blank">Ising model</a>, in order to produce the spin configurations for a series of energies \( E_i \) (10000 in total) for a system of \( L=40 \) spins in one dimension and \( L=40\times 40 \) in two dimensions at three different temperatures.
In its simplest form the energy of the Ising model is expressed as, without an externally applied magnetic field,
$$
E=-J\sum_{< kl >}^{N}s_ks_l
$$
with
\( s_k=\pm 1 \). The quantity \( N \) represents the total number of spins and \( J \) is a coupling
constant expressing the strength of the interaction between
neighboring spins. The symbol \( < kl> \) indicates that we sum over
nearest neighbors only. We will assume that we have a ferromagnetic
ordering, viz \( J> 0 \). We will use periodic boundary conditions and
the Metropolis algorithm only. The spins take values \( -1 \) and \( +1 \) only.
<p>
We will use the Ising model to generate our training data and will focus mainly on supervised training. We will follow closely the recent article of <a href="https://arxiv.org/abs/1803.08823" target="_blank">Mehta et al, arXiv 1803.08823</a>. This article stands out as an excellent review on machine learning (ML) algorithms applied to typical physics problems. The added benefit is that each figure and model presented in <a href="https://physics.bu.edu/~pankajm/MLnotebooks.html" target="_blank">this article is accompanied by its jupyter notebook</a>. This means that we can start using these and compare with our own results. In case you wish to use their data for the Ising model, their data can be downloaded from the same link which lists to the jupyter notebooks. See also at the end of the project description for more information on how to install various Python packages.
<p>
With the abovementioned configurations we will determine, using first various
regression methods, the value of the coupling constant for the energy
of the one-dimensional Ising model. Thereafter, we will use the
two-dimensional data, but now computed at different temperatures, in
order to classify the phase of the Ising model. Below the critical
temperature, the system will be in a so-called ferromagnetic
phase. Close to the critical temperature, the final magnetization becomes smaller and smaller in absolute value
while above the critical temperature,
the net magnetization is zero. This classification case, that is the
two-dimensional Ising model, will be studied using logistic regression, a <b>random forest</b>
algorithm and deep neural networks.
<p>
You should try to program at least one of these methods yourself (choose the one you prefer).
Feel free to use the notebooks to benchmark your code. If you wish to write your own C++ or Fortran program for say a simple neural network model, please feel free to do so.
You can then benchmark your results against the above jupyter notebooks. More information can also be found at the link for the lecture notes of <a href="https://compphysics.github.io/MachineLearning/doc/web/course.html" target="_blank">FYS-STK4155</a>.
<p>
We recommend that you form groups of 2-3 students and try to
collaborate on the notebooks, develop your own software and discuss
the final presentations. You can collaborate on all these topics. The
final presentation should include an overview of popular machine
learning algorithms as introduction and motivation. Thereafter you
discuss the explicit Ising model data and how you have implemented the
ML algorithms discussed here, discuss their pros and cons and try to
develop your own code for at least one of these algorithms. You are
encouraged to use the abovementioned notebooks as starting point and
guidance. The duration of your presentation should at most be 30
mins. Allow for approximately 15 mins for discussions and questions.
<h3 id="___sec2">Part a): Producing the data </h3>
<p>
You can use the Ising model data from the article of Mehta <em>et al.</em>, or generate your own data.
If you opt for using your own Ising model code, you need to generate \( 10000 \) energy configurations with their spin orientations after the system has reached its most likely state. These energies and their corresponding spin orientations
represent then your data.
We will use a fixed lattice of \( L\times L = 40 \times 40 \) spins in two dimensions and \( L=40 \) spins in one dimension.
Make sure the calculations have been equilibrated. For the two-dimensional system, compute the configurations
for three values of the temperature, namely \( T=0.75 \) (ordered phase), \( T=2.3 \) (near the critical point) and \( T=4.0 \) (disordered phase).
For the one-dimensional system it suffices to compute the various configurations for one temperature only, say \( T=2.0 \).
These are the data you will use to study different ML algorithms.
We generate our data with \( J=1 \).
<h3 id="___sec3">Part b): Estimating the coupling constant of the one-dimensional Ising model </h3>
<p>
We start with the one-dimensional Ising model and use the data we have generated with \( J=1 \). Use linear regression, Lasso and Ridge regression as described section 6 and in Notebook 4 of <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html" target="_blank">Mehta *et al.*</a>. Discuss the methods and how they perform in computing the coupling constant \( J \). Give a critical analysis and discuss how to evaluate the <em>cost function</em>. You should feel free to write your own code, see also
the lecture notes of <a href="https://compphysics.github.io/MachineLearning/doc/web/course.html" target="_blank">FYS-STK4155</a>, in particular te material on least square methods. You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
<h3 id="___sec4">Part c): Determine the phase of the two-dimensional Ising model </h3>
<p>
We switch now to binary classification methods and use logistic regression to define the phases of the Ising model.
Use described section 7 and in Notebook 6 of <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVII-logreg_ising.html" target="_blank">Mehta *et al.*</a>. Discuss the methods and how they perform. Give a critical analysis and discuss how to evaluate the <em>cost function</em>. You should feel free to write your own code. Use thereafter the <em>random forests</em> algorithm to classify the same phases as done with logistic regression and discuss the pros and cons of these methods. For <em>random forests</em> you can use <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVIII-randomforests_ising.html" target="_blank">notebook 9</a> of Mehta <em>et al.</em>
<p>
You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
<h3 id="___sec5">Part d): Classifying the Ising model phase using neural networks </h3>
<p>
We end the classification problem of the phases of the Ising model by employing the algorithm for so-called feed-forward deep neural networks (see section 9 of Mehta <em>et al.</em>). The method is described in <a href="https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CIX-DNN_ising_TFlow.html" target="_blank">notebook 12</a>.
<p>
You can use tensorflow to perform these analyses. See below for instruction on how to install tensorflow.
<h3 id="___sec6">Part e): Summary </h3>
<p>
You should make a summary of the various methods and their pros and cons. For the final presentation, you should have at least coded one of these methods yourself and discussed the evaluation of the cost function.
<h2 id="___sec7">Background literature </h2>
<p>
On Machine Learning we recommend strongly the article of Mehta <em>et al.</em>
Textbooks on Machine Learning can be found at the <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Textbooks" target="_blank">Github address of FYS-STK4155</a>, see in particular Marsland's text.
<ul>
<li> <a href="https://arxiv.org/abs/1803.08823" target="_blank">Mehta et al, arXiv 1803.08823</a>, <em>A high-bias, low-variance introduction to Machine Learning for physicists</em>, ArXiv:1803.08823.</li>
</ul>
If you wish to read more about the Ising model and statistical physics here are three suggestions.
<ul>
<li> <a href="http://www.worldscientific.com/worldscibooks/10.1142/5660" target="_blank">M. Plischke and B. Bergersen</a>, <em>Equilibrium Statistical Physics</em>, World Scientific, see chapters 5 and 6.</li>
<li> <a href="http://www.cambridge.org/no/academic/subjects/physics/computational-science-and-modelling/guide-monte-carlo-simulations-statistical-physics-4th-edition?format=HB" target="_blank">D. P. Landau and K. Binder</a>, <em>A Guide to Monte Carlo Simulations in Statistical Physics</em>, Cambridge, see chapters 2,3 and 4.</li>
<li> <a href="https://global.oup.com/academic/product/monte-carlo-methods-in-statistical-physics-9780198517979?cc=no&lang=en&" target="_blank">M. E. J. Newman and T. Barkema</a>, <em>Monte Carlo Methods in Statistical Physics</em>, Oxford, see chapters 3 and 4.</li>
</ul>
<h2 id="___sec8">Introduction to numerical projects </h2>
<p>
Here follows a brief recipe and recommendation on how to write a report for each
project.
<ul>
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
<li> Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.</li>
<li> Include the source code of your program. Comment your program properly.</li>
<li> If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.</li>
<li> Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.</li>
<li> Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.</li>
<li> Try to give an interpretation of you results in your answers to the problems.</li>
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
</ul>
<h2 id="___sec9">Software and needed installations </h2>
<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 tensorflow sympy pandas pillow</li>
</ol>
For Python3, replace <b>pip</b> with <b>pip3</b>.
<p>
See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</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
<ol>
<li> brew install python3</li>
</ol>
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>
<li> sudo apt-get install python3 (or python for python2.7)</li>
</ol>
etc etc.
<p>
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
<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>
Popular software packages written in Python for ML are
<ul>
<li> <a href="http://scikit-learn.org/stable/" target="_blank">Scikit-learn</a>,</li>
<li> <a href="https://www.tensorflow.org/" target="_blank">Tensorflow</a>,</li>
<li> <a href="http://pytorch.org/" target="_blank">PyTorch</a> and</li>
<li> <a href="https://keras.io/" target="_blank">Keras</a>.</li>
</ul>
These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
<!-- ------------------- end of main content --------------- -->
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license
</center>
</body>
</html>
+348
View File
@@ -0,0 +1,348 @@
%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%%
% #ifdef PTEX2TEX_EXPLANATION
%%
%% The file follows the ptex2tex extended LaTeX format, see
%% ptex2tex: http://code.google.com/p/ptex2tex/
%%
%% Run
%% ptex2tex myfile
%% or
%% doconce ptex2tex myfile
%%
%% to turn myfile.p.tex into an ordinary LaTeX file myfile.tex.
%% (The ptex2tex program: http://code.google.com/p/ptex2tex)
%% Many preprocess options can be added to ptex2tex or doconce ptex2tex
%%
%% ptex2tex -DMINTED myfile
%% doconce ptex2tex myfile envir=minted
%%
%% ptex2tex will typeset code environments according to a global or local
%% .ptex2tex.cfg configure file. doconce ptex2tex will typeset code
%% according to options on the command line (just type doconce ptex2tex to
%% see examples). If doconce ptex2tex has envir=minted, it enables the
%% minted style without needing -DMINTED.
% #endif
% #define PREAMBLE
% #ifdef PREAMBLE
%-------------------- begin preamble ----------------------
\documentclass[%
oneside, % oneside: electronic viewing, twoside: printing
final, % draft: marks overfull hboxes, figures with paths
10pt]{article}
\listfiles % print all files needed to compile this document
\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
\usepackage[table]{xcolor}
\usepackage{bm,ltablex,microtype}
\usepackage[pdftex]{graphicx}
\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{lmodern} % Latin Modern fonts derived from Computer Modern
% Hyperlinks in PDF:
\definecolor{linkcolor}{rgb}{0,0,0.4}
\usepackage{hyperref}
\hypersetup{
breaklinks=true,
colorlinks=true,
linkcolor=linkcolor,
urlcolor=linkcolor,
citecolor=black,
filecolor=black,
%filecolor=blue,
pdfmenubar=true,
pdftoolbar=true,
bookmarksdepth=3 % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
}
%\hyperbaseurl{} % hyperlinks are relative to this root
\setcounter{tocdepth}{2} % levels in table of contents
% --- fancyhdr package for fancy headers ---
\usepackage{fancyhdr}
\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[LE,RO]{\thepage}
% Ensure copyright on titlepage (article style) and chapter pages (book style)
\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}}
% \renewcommand{\footrulewidth}{0mm}
\renewcommand{\headrulewidth}{0mm}
}
% Ensure copyright on titlepages with \thispagestyle{empty}
\fancypagestyle{empty}{
\fancyhf{}
\fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}}
\renewcommand{\footrulewidth}{0mm}
\renewcommand{\headrulewidth}{0mm}
}
\pagestyle{fancy}
% prevent orhpans and widows
\clubpenalty = 10000
\widowpenalty = 10000
% --- end of standard preamble for documents ---
% insert custom LaTeX commands...
\raggedbottom
\makeindex
\usepackage[totoc]{idxlayout} % for index in the toc
\usepackage[nottoc]{tocbibind} % for references/bibliography in the toc
%-------------------- end preamble ----------------------
\begin{document}
% matching end for #ifdef PREAMBLE
% #endif
\newcommand{\exercisesection}[1]{\subsection*{#1}}
% ------------------- main content ----------------------
% ----------------- title -------------------------
\thispagestyle{empty}
\begin{center}
{\LARGE\bf
\begin{spacing}{1.25}
Project on Machine Learning
\end{spacing}
}
\end{center}
% ----------------- author(s) -------------------------
\begin{center}
{\bf \href{{http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html}}{Data Analysis and Machine Learning FYS-STK3155/FYS4155}}
\end{center}
\begin{center}
% List of all institutions:
\centerline{{\small Department of Physics, University of Oslo, Norway}}
\end{center}
% ----------------- end author(s) -------------------------
% --- begin date ---
\begin{center}
May 2018
\end{center}
% --- end date ---
\vspace{1cm}
\subsection{Machine learning (ML) approaches to data from Ising model calculations}
\paragraph{Introduction.}
The aim of this project is to use an already developed Monte Carlo program for the one-dimensional and two-dimensional \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Programs/IsingModel}}{Ising model}, in order to produce the spin configurations for a series of energies $E_i$ (10000 in total) for a system of $L=40$ spins in one dimension and $L=40\times 40$ in two dimensions at three different temperatures.
In its simplest form the energy of the Ising model is expressed as, without an externally applied magnetic field,
\[
E=-J\sum_{< kl >}^{N}s_ks_l
\]
with
$s_k=\pm 1$. The quantity $N$ represents the total number of spins and $J$ is a coupling
constant expressing the strength of the interaction between
neighboring spins. The symbol $<kl>$ indicates that we sum over
nearest neighbors only. We will assume that we have a ferromagnetic
ordering, viz $J> 0$. We will use periodic boundary conditions and
the Metropolis algorithm only. The spins take values $-1$ and $+1$ only.
We will use the Ising model to generate our training data and will focus mainly on supervised training. We will follow closely the recent article of \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv 1803.08823}. This article stands out as an excellent review on machine learning (ML) algorithms applied to typical physics problems. The added benefit is that each figure and model presented in \href{{https://physics.bu.edu/~pankajm/MLnotebooks.html}}{this article is accompanied by its jupyter notebook}. This means that we can start using these and compare with our own results. In case you wish to use their data for the Ising model, their data can be downloaded from the same link which lists to the jupyter notebooks. See also at the end of the project description for more information on how to install various Python packages.
With the abovementioned configurations we will determine, using first various
regression methods, the value of the coupling constant for the energy
of the one-dimensional Ising model. Thereafter, we will use the
two-dimensional data, but now computed at different temperatures, in
order to classify the phase of the Ising model. Below the critical
temperature, the system will be in a so-called ferromagnetic
phase. Close to the critical temperature, the final magnetization becomes smaller and smaller in absolute value
while above the critical temperature,
the net magnetization is zero. This classification case, that is the
two-dimensional Ising model, will be studied using logistic regression, a \textbf{random forest}
algorithm and deep neural networks.
You should try to program at least one of these methods yourself (choose the one you prefer).
Feel free to use the notebooks to benchmark your code. If you wish to write your own C++ or Fortran program for say a simple neural network model, please feel free to do so.
You can then benchmark your results against the above jupyter notebooks. More information can also be found at the link for the lecture notes of \href{{https://compphysics.github.io/MachineLearning/doc/web/course.html}}{FYS-STK4155}.
We recommend that you form groups of 2-3 students and try to
collaborate on the notebooks, develop your own software and discuss
the final presentations. You can collaborate on all these topics. The
final presentation should include an overview of popular machine
learning algorithms as introduction and motivation. Thereafter you
discuss the explicit Ising model data and how you have implemented the
ML algorithms discussed here, discuss their pros and cons and try to
develop your own code for at least one of these algorithms. You are
encouraged to use the abovementioned notebooks as starting point and
guidance. The duration of your presentation should at most be 30
mins. Allow for approximately 15 mins for discussions and questions.
\paragraph{Part a): Producing the data.}
You can use the Ising model data from the article of Mehta \emph{et al.}, or generate your own data.
If you opt for using your own Ising model code, you need to generate $10000$ energy configurations with their spin orientations after the system has reached its most likely state. These energies and their corresponding spin orientations
represent then your data.
We will use a fixed lattice of $L\times L = 40 \times 40$ spins in two dimensions and $L=40$ spins in one dimension.
Make sure the calculations have been equilibrated. For the two-dimensional system, compute the configurations
for three values of the temperature, namely $T=0.75$ (ordered phase), $T=2.3$ (near the critical point) and $T=4.0$ (disordered phase).
For the one-dimensional system it suffices to compute the various configurations for one temperature only, say $T=2.0$.
These are the data you will use to study different ML algorithms.
We generate our data with $J=1$.
\paragraph{Part b): Estimating the coupling constant of the one-dimensional Ising model.}
We start with the one-dimensional Ising model and use the data we have generated with $J=1$. Use linear regression, Lasso and Ridge regression as described section 6 and in Notebook 4 of \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html}}{Mehta *et al.*}. Discuss the methods and how they perform in computing the coupling constant $J$. Give a critical analysis and discuss how to evaluate the \emph{cost function}. You should feel free to write your own code, see also
the lecture notes of \href{{https://compphysics.github.io/MachineLearning/doc/web/course.html}}{FYS-STK4155}, in particular te material on least square methods. You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
\paragraph{Part c): Determine the phase of the two-dimensional Ising model.}
We switch now to binary classification methods and use logistic regression to define the phases of the Ising model.
Use described section 7 and in Notebook 6 of \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVII-logreg_ising.html}}{Mehta *et al.*}. Discuss the methods and how they perform. Give a critical analysis and discuss how to evaluate the \emph{cost function}. You should feel free to write your own code. Use thereafter the \emph{random forests} algorithm to classify the same phases as done with logistic regression and discuss the pros and cons of these methods. For \emph{random forests} you can use \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVIII-randomforests_ising.html}}{notebook 9} of Mehta \emph{et al.}
You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
\paragraph{Part d): Classifying the Ising model phase using neural networks.}
We end the classification problem of the phases of the Ising model by employing the algorithm for so-called feed-forward deep neural networks (see section 9 of Mehta \emph{et al.}). The method is described in \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CIX-DNN_ising_TFlow.html}}{notebook 12}.
You can use tensorflow to perform these analyses. See below for instruction on how to install tensorflow.
\paragraph{Part e): Summary.}
You should make a summary of the various methods and their pros and cons. For the final presentation, you should have at least coded one of these methods yourself and discussed the evaluation of the cost function.
\subsection{Background literature}
On Machine Learning we recommend strongly the article of Mehta \emph{et al.}
Textbooks on Machine Learning can be found at the \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Textbooks}}{Github address of FYS-STK4155}, see in particular Marsland's text.
\begin{itemize}
\item \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv 1803.08823}, \emph{A high-bias, low-variance introduction to Machine Learning for physicists}, ArXiv:1803.08823.
\end{itemize}
\noindent
If you wish to read more about the Ising model and statistical physics here are three suggestions.
\begin{itemize}
\item \href{{http://www.worldscientific.com/worldscibooks/10.1142/5660}}{M. Plischke and B. Bergersen}, \emph{Equilibrium Statistical Physics}, World Scientific, see chapters 5 and 6.
\item \href{{http://www.cambridge.org/no/academic/subjects/physics/computational-science-and-modelling/guide-monte-carlo-simulations-statistical-physics-4th-edition?format=HB}}{D. P. Landau and K. Binder}, \emph{A Guide to Monte Carlo Simulations in Statistical Physics}, Cambridge, see chapters 2,3 and 4.
\item \href{{https://global.oup.com/academic/product/monte-carlo-methods-in-statistical-physics-9780198517979?cc=no&lang=en&}}{M. E. J. Newman and T. Barkema}, \emph{Monte Carlo Methods in Statistical Physics}, Oxford, see chapters 3 and 4.
\end{itemize}
\noindent
\subsection{Introduction to numerical projects}
Here follows a brief recipe and recommendation on how to write a report for each
project.
\begin{itemize}
\item Give a short description of the nature of the problem and the eventual numerical methods you have used.
\item Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.
\item Include the source code of your program. Comment your program properly.
\item If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.
\item Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.
\item Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.
\item Try to give an interpretation of you results in your answers to the problems.
\item Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.
\item Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.
\end{itemize}
\noindent
\subsection{Software and needed installations}
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
we recommend that you install the following Python packages via \textbf{pip} as
\begin{enumerate}
\item pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow
\end{enumerate}
\noindent
For Python3, replace \textbf{pip} with \textbf{pip3}.
See below for a discussion of \textbf{tensorflow} and \textbf{scikit-learn}.
For OSX users we recommend also, after having installed Xcode, to install \textbf{brew}. Brew allows
for a seamless installation of additional software via for example
\begin{enumerate}
\item brew install python3
\end{enumerate}
\noindent
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
you can use \textbf{pip} as well and simply install Python as
\begin{enumerate}
\item sudo apt-get install python3 (or python for python2.7)
\end{enumerate}
\noindent
etc etc.
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
\begin{enumerate}
\item \href{{https://docs.anaconda.com/}}{Anaconda} 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 \textbf{conda}
\item \href{{https://www.enthought.com/product/canopy/}}{Enthought canopy} is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.
\end{enumerate}
\noindent
Popular software packages written in Python for ML are
\begin{itemize}
\item \href{{http://scikit-learn.org/stable/}}{Scikit-learn},
\item \href{{https://www.tensorflow.org/}}{Tensorflow},
\item \href{{http://pytorch.org/}}{PyTorch} and
\item \href{{https://keras.io/}}{Keras}.
\end{itemize}
\noindent
These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
% ------------------- end of main content ---------------
% #ifdef PREAMBLE
\end{document}
% #endif
Binary file not shown.
+320
View File
@@ -0,0 +1,320 @@
%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%%
%-------------------- begin preamble ----------------------
\documentclass[%
oneside, % oneside: electronic viewing, twoside: printing
final, % draft: marks overfull hboxes, figures with paths
10pt]{article}
\listfiles % print all files needed to compile this document
\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
\usepackage[table]{xcolor}
\usepackage{bm,ltablex,microtype}
\usepackage[pdftex]{graphicx}
\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage{lmodern} % Latin Modern fonts derived from Computer Modern
% Hyperlinks in PDF:
\definecolor{linkcolor}{rgb}{0,0,0.4}
\usepackage{hyperref}
\hypersetup{
breaklinks=true,
colorlinks=true,
linkcolor=linkcolor,
urlcolor=linkcolor,
citecolor=black,
filecolor=black,
%filecolor=blue,
pdfmenubar=true,
pdftoolbar=true,
bookmarksdepth=3 % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
}
%\hyperbaseurl{} % hyperlinks are relative to this root
\setcounter{tocdepth}{2} % levels in table of contents
% --- fancyhdr package for fancy headers ---
\usepackage{fancyhdr}
\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[LE,RO]{\thepage}
% Ensure copyright on titlepage (article style) and chapter pages (book style)
\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}}
% \renewcommand{\footrulewidth}{0mm}
\renewcommand{\headrulewidth}{0mm}
}
% Ensure copyright on titlepages with \thispagestyle{empty}
\fancypagestyle{empty}{
\fancyhf{}
\fancyfoot[C]{{\footnotesize \copyright\ 1999-2018, "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html". Released under CC Attribution-NonCommercial 4.0 license}}
\renewcommand{\footrulewidth}{0mm}
\renewcommand{\headrulewidth}{0mm}
}
\pagestyle{fancy}
% prevent orhpans and widows
\clubpenalty = 10000
\widowpenalty = 10000
% --- end of standard preamble for documents ---
% insert custom LaTeX commands...
\raggedbottom
\makeindex
\usepackage[totoc]{idxlayout} % for index in the toc
\usepackage[nottoc]{tocbibind} % for references/bibliography in the toc
%-------------------- end preamble ----------------------
\begin{document}
% matching end for #ifdef PREAMBLE
\newcommand{\exercisesection}[1]{\subsection*{#1}}
% ------------------- main content ----------------------
% ----------------- title -------------------------
\thispagestyle{empty}
\begin{center}
{\LARGE\bf
\begin{spacing}{1.25}
Project on Machine Learning
\end{spacing}
}
\end{center}
% ----------------- author(s) -------------------------
\begin{center}
{\bf \href{{http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html}}{Data Analysis and Machine Learning FYS-STK3155/FYS4155}}
\end{center}
\begin{center}
% List of all institutions:
\centerline{{\small Department of Physics, University of Oslo, Norway}}
\end{center}
% ----------------- end author(s) -------------------------
% --- begin date ---
\begin{center}
May 2018
\end{center}
% --- end date ---
\vspace{1cm}
\subsection*{Machine learning (ML) approaches to data from Ising model calculations}
\paragraph{Introduction.}
The aim of this project is to use an already developed Monte Carlo program for the one-dimensional and two-dimensional \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Programs/IsingModel}}{Ising model}, in order to produce the spin configurations for a series of energies $E_i$ (10000 in total) for a system of $L=40$ spins in one dimension and $L=40\times 40$ in two dimensions at three different temperatures.
In its simplest form the energy of the Ising model is expressed as, without an externally applied magnetic field,
\[
E=-J\sum_{< kl >}^{N}s_ks_l
\]
with
$s_k=\pm 1$. The quantity $N$ represents the total number of spins and $J$ is a coupling
constant expressing the strength of the interaction between
neighboring spins. The symbol $<kl>$ indicates that we sum over
nearest neighbors only. We will assume that we have a ferromagnetic
ordering, viz $J> 0$. We will use periodic boundary conditions and
the Metropolis algorithm only. The spins take values $-1$ and $+1$ only.
We will use the Ising model to generate our training data and will focus mainly on supervised training. We will follow closely the recent article of \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv 1803.08823}. This article stands out as an excellent review on machine learning (ML) algorithms applied to typical physics problems. The added benefit is that each figure and model presented in \href{{https://physics.bu.edu/~pankajm/MLnotebooks.html}}{this article is accompanied by its jupyter notebook}. This means that we can start using these and compare with our own results. In case you wish to use their data for the Ising model, their data can be downloaded from the same link which lists to the jupyter notebooks. See also at the end of the project description for more information on how to install various Python packages.
With the abovementioned configurations we will determine, using first various
regression methods, the value of the coupling constant for the energy
of the one-dimensional Ising model. Thereafter, we will use the
two-dimensional data, but now computed at different temperatures, in
order to classify the phase of the Ising model. Below the critical
temperature, the system will be in a so-called ferromagnetic
phase. Close to the critical temperature, the final magnetization becomes smaller and smaller in absolute value
while above the critical temperature,
the net magnetization is zero. This classification case, that is the
two-dimensional Ising model, will be studied using logistic regression, a \textbf{random forest}
algorithm and deep neural networks.
You should try to program at least one of these methods yourself (choose the one you prefer).
Feel free to use the notebooks to benchmark your code. If you wish to write your own C++ or Fortran program for say a simple neural network model, please feel free to do so.
You can then benchmark your results against the above jupyter notebooks. More information can also be found at the link for the lecture notes of \href{{https://compphysics.github.io/MachineLearning/doc/web/course.html}}{FYS-STK4155}.
We recommend that you form groups of 2-3 students and try to
collaborate on the notebooks, develop your own software and discuss
the final presentations. You can collaborate on all these topics. The
final presentation should include an overview of popular machine
learning algorithms as introduction and motivation. Thereafter you
discuss the explicit Ising model data and how you have implemented the
ML algorithms discussed here, discuss their pros and cons and try to
develop your own code for at least one of these algorithms. You are
encouraged to use the abovementioned notebooks as starting point and
guidance. The duration of your presentation should at most be 30
mins. Allow for approximately 15 mins for discussions and questions.
\paragraph{Part a): Producing the data.}
You can use the Ising model data from the article of Mehta \emph{et al.}, or generate your own data.
If you opt for using your own Ising model code, you need to generate $10000$ energy configurations with their spin orientations after the system has reached its most likely state. These energies and their corresponding spin orientations
represent then your data.
We will use a fixed lattice of $L\times L = 40 \times 40$ spins in two dimensions and $L=40$ spins in one dimension.
Make sure the calculations have been equilibrated. For the two-dimensional system, compute the configurations
for three values of the temperature, namely $T=0.75$ (ordered phase), $T=2.3$ (near the critical point) and $T=4.0$ (disordered phase).
For the one-dimensional system it suffices to compute the various configurations for one temperature only, say $T=2.0$.
These are the data you will use to study different ML algorithms.
We generate our data with $J=1$.
\paragraph{Part b): Estimating the coupling constant of the one-dimensional Ising model.}
We start with the one-dimensional Ising model and use the data we have generated with $J=1$. Use linear regression, Lasso and Ridge regression as described section 6 and in Notebook 4 of \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html}}{Mehta *et al.*}. Discuss the methods and how they perform in computing the coupling constant $J$. Give a critical analysis and discuss how to evaluate the \emph{cost function}. You should feel free to write your own code, see also
the lecture notes of \href{{https://compphysics.github.io/MachineLearning/doc/web/course.html}}{FYS-STK4155}, in particular te material on least square methods. You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
\paragraph{Part c): Determine the phase of the two-dimensional Ising model.}
We switch now to binary classification methods and use logistic regression to define the phases of the Ising model.
Use described section 7 and in Notebook 6 of \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVII-logreg_ising.html}}{Mehta *et al.*}. Discuss the methods and how they perform. Give a critical analysis and discuss how to evaluate the \emph{cost function}. You should feel free to write your own code. Use thereafter the \emph{random forests} algorithm to classify the same phases as done with logistic regression and discuss the pros and cons of these methods. For \emph{random forests} you can use \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVIII-randomforests_ising.html}}{notebook 9} of Mehta \emph{et al.}
You can use scikit-learn to perform these analyses. See below for instruction on how to install scikit-learn.
\paragraph{Part d): Classifying the Ising model phase using neural networks.}
We end the classification problem of the phases of the Ising model by employing the algorithm for so-called feed-forward deep neural networks (see section 9 of Mehta \emph{et al.}). The method is described in \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CIX-DNN_ising_TFlow.html}}{notebook 12}.
You can use tensorflow to perform these analyses. See below for instruction on how to install tensorflow.
\paragraph{Part e): Summary.}
You should make a summary of the various methods and their pros and cons. For the final presentation, you should have at least coded one of these methods yourself and discussed the evaluation of the cost function.
\subsection*{Background literature}
On Machine Learning we recommend strongly the article of Mehta \emph{et al.}
Textbooks on Machine Learning can be found at the \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Textbooks}}{Github address of FYS-STK4155}, see in particular Marsland's text.
\begin{itemize}
\item \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv 1803.08823}, \emph{A high-bias, low-variance introduction to Machine Learning for physicists}, ArXiv:1803.08823.
\end{itemize}
\noindent
If you wish to read more about the Ising model and statistical physics here are three suggestions.
\begin{itemize}
\item \href{{http://www.worldscientific.com/worldscibooks/10.1142/5660}}{M. Plischke and B. Bergersen}, \emph{Equilibrium Statistical Physics}, World Scientific, see chapters 5 and 6.
\item \href{{http://www.cambridge.org/no/academic/subjects/physics/computational-science-and-modelling/guide-monte-carlo-simulations-statistical-physics-4th-edition?format=HB}}{D. P. Landau and K. Binder}, \emph{A Guide to Monte Carlo Simulations in Statistical Physics}, Cambridge, see chapters 2,3 and 4.
\item \href{{https://global.oup.com/academic/product/monte-carlo-methods-in-statistical-physics-9780198517979?cc=no&lang=en&}}{M. E. J. Newman and T. Barkema}, \emph{Monte Carlo Methods in Statistical Physics}, Oxford, see chapters 3 and 4.
\end{itemize}
\noindent
\subsection*{Introduction to numerical projects}
Here follows a brief recipe and recommendation on how to write a report for each
project.
\begin{itemize}
\item Give a short description of the nature of the problem and the eventual numerical methods you have used.
\item Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.
\item Include the source code of your program. Comment your program properly.
\item If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.
\item Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.
\item Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.
\item Try to give an interpretation of you results in your answers to the problems.
\item Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.
\item Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.
\end{itemize}
\noindent
\subsection*{Software and needed installations}
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
we recommend that you install the following Python packages via \textbf{pip} as
\begin{enumerate}
\item pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow
\end{enumerate}
\noindent
For Python3, replace \textbf{pip} with \textbf{pip3}.
See below for a discussion of \textbf{tensorflow} and \textbf{scikit-learn}.
For OSX users we recommend also, after having installed Xcode, to install \textbf{brew}. Brew allows
for a seamless installation of additional software via for example
\begin{enumerate}
\item brew install python3
\end{enumerate}
\noindent
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
you can use \textbf{pip} as well and simply install Python as
\begin{enumerate}
\item sudo apt-get install python3 (or python for python2.7)
\end{enumerate}
\noindent
etc etc.
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
\begin{enumerate}
\item \href{{https://docs.anaconda.com/}}{Anaconda} 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 \textbf{conda}
\item \href{{https://www.enthought.com/product/canopy/}}{Enthought canopy} is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.
\end{enumerate}
\noindent
Popular software packages written in Python for ML are
\begin{itemize}
\item \href{{http://scikit-learn.org/stable/}}{Scikit-learn},
\item \href{{https://www.tensorflow.org/}}{Tensorflow},
\item \href{{http://pytorch.org/}}{PyTorch} and
\item \href{{https://keras.io/}}{Keras}.
\end{itemize}
\noindent
These are all freely available at their respective GitHub sites. They
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing.
% ------------------- end of main content ---------------
\end{document}
File diff suppressed because it is too large Load Diff
@@ -148,39 +148,138 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>&nbsp;<br>
<center><h4>Dec 10, 2017</h4></center> <!-- date -->
<center><h4>May 11, 2018</h4></center> <!-- date -->
<br>
<p>
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2017, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
<!-- copyright --> &copy; 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
</center>
</section>
<section>
<h2 id="___sec0">What is Machine Learning? </h2>
<h2 id="___sec0">Introduction </h2>
<p>
Machine learning is the science of giving computers the ability to
learn without being explicitly programmed. The idea is that there
exist generic algorithms which can be used to find patterns in a broad
class of data sets without having to write code specifically for each
problem. The algorithm will build its own logic based on the data.
Statistics, data science and machine learning form important fields of
research in modern science. They describe how to learn and make
predictions from data, as well allowing us to extract important
correlations about physical process and the underlying laws of motion
in large data sets. The latter, big data sets, appear
frequently in essentially all disciplines, from the traditional Science,
Technology, Mathematics and Engineering fields to Life Science, Law, education research,
the Humanities and
the Social Sciences. It has become more and more common to see
research projects on big data in for example the Social
Sciences where extracting patterns from complicated survey data is one of many research directions.
Having a solid grasp of data analysis and machine learning
is thus becoming central to scientific computing in many
fields, and competences and skills within the fields of machine learning
and scientific computing are nowadays strongly requested by many
potential employers. The latter cannot be overstated, familiarity with
machine learning has almost become a prerequisite for many of the most
exciting employment opportunities, whether they are in bioinformatics,
life science, physics or finance, in the private or the public
sector. This author has had several students or met students who have
been hired recently based on their skills and competences in
scientific computing and data science, often with marginal knowledge
of machine learning.
<p>
Machine learning is a subfield of computer science, and is closely
related to computational statistics. It evolved from the study of
pattern recognition in artificial intelligence (AI) research, and has
made contributions to AI tasks like computer vision, natural language
processing and speech recognition. It has also, especially in later
years, found applications in a wide variety of other areas, including
bioinformatics, economy, physics, finance and marketing.
processing and speech recognition.
Machine learning represents the
science of giving computers the ability to learn without being
explicitly programmed. The idea is that there exist generic
algorithms which can be used to find patterns in a broad class of data
sets without having to write code specifically for each problem. The
algorithm will build its own logic based on the data.
<p>
Machine learning is an extremely rich field, in spite of its young age. The
increases we have seen during the last three decades in computational
capabilities have been followed by developments of methods and
techniques for analyzing and handling large date sets, relying heavily
on statistics, computer science and mathematics. The field is rather
new and developing rapidly. Popular software packages written in
Python for machine learning like <a href="http://scikit-learn.org/stable/" target="_blank">Scikit-learn</a>, <a href="https://www.tensorflow.org/" target="_blank">Tensorflow</a>,
<a href="http://pytorch.org/" target="_blank">PyTorch</a> and <a href="https://keras.io/" target="_blank">Keras</a>, all freely available at their respective GitHub sites,
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing. Not all the
algorithms and methods can be given a rigorous mathematical
justification, opening up thereby large rooms for experimenting
and trial and error and thereby exciting new developments.
However, a solid command of linear algebra, multivariate theory,
probability theory, statistical data analysis,
understanding errors and Monte Carlo methods are central elements in a proper understanding of many of
algorithms and methods we will discuss.
</section>
<section>
<h2 id="___sec1">Types of Machine Learning </h2>
<h2 id="___sec1">Learning outcomes </h2>
<p>
These lectures aim at giving you an overview of central aspects of
statistical data analysis as well as some of the central algorithms
used in machine learning. We will introduce a variety of central
algorithms and methods essential for studies of data analysis and
machine learning.
<p>
Hands-on projects and experimenting with data and algorithms plays a central role in
these lectures, and our hope is, through the various
projects and exercies, to expose you to fundamental
research problems in these fields, with the aim to reproduce state of
the art scientific results. You will learn to develop and
structure large codes for studying these systems, get acquainted with
computing facilities and learn to handle large scientific projects. A
good scientific and ethical conduct is emphasized throughout the
course. More specifically, you will
<ol>
<p><li> learn about basic data analysis, Bayesian statistics, Monte Carlo methods, data optimization and machine learning;</li>
<p><li> be capable of extending the acquired knowledge to other systems and cases;</li>
<p><li> Have an understanding of central algorithms used in data analysis and machine learning;</li>
<p><li> 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;</li>
<p><li> Understand methods for regression and classification;</li>
<p><li> Learn about neural network, genetic algorithms and Boltzmann machines;</li>
<p><li> 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).</li>
</ol>
<p>
There are several topics we will cover here, spanning from a
statistical data analysis and its basic concepts such expectation
values, variance, covariance, correlation functions and errors, via
well-known probability distribution functions like uniform
distribution, the binomial distribution, the Poisson distribution and
simple and multivariate normal distributions to central elements of
Bayesian statistics and modeling. We will also remind the reader about
central elements from linear algebra and standard methods based on
linear algebra used to fit functions such Cubic splines and gradient
methods for data optimization and the Singular-value decomposition and
least square methods for parameterizing data.
<p>
We will also cover Monte Carlo methods, Markov chains, well-known
algorithms for sampling stochastic events like the Metropolis-Hastings
and Gibbs sampling methods. An important aspect of all our
calculations is a proper estimation of errors. Here we will also
discuss famous resampling techniques like the blocking, bootstrapping
and jackknife methods.
<p>
The second part of the material covers several algorithms used in
machine learning.
</section>
<section>
<h2 id="___sec2">Types of Machine Learning </h2>
<p>
The approaches to machine learning are many, but are often split into two main categories.
@@ -188,7 +287,7 @@ In <em>supervised learning</em> we know the answer to a problem,
and let the computer deduce the logic behind it. On the other hand, <em>unsupervised learning</em>
is a method for finding patterns and relationship in data sets without any prior knowledge of the system.
Some authours also operate with a third category, namely <em>reinforcement learning</em>. This is a paradigm
of learning inspired by behavioural psychology, where learning is achieved by trial-and-error,
of learning inspired by behavioral psychology, where learning is achieved by trial-and-error,
solely from rewards and punishment.
<p>
@@ -203,35 +302,60 @@ Some of the most common tasks are:
<p><li> Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning.</li>
</ul>
</section>
<p>
The methods we cover have three main topics in common, irrespective of
whether we deal with supervised or unsupervised learning. The first
ingredient is normally our data set, the second is a model which is
normally a function of some parameters. The last ingredient is a
so-called <b>cost</b> function which allows us to present an estimate on
how good our model is in reproducing the data it is supposed to train.
<section>
<h2 id="___sec2">Different algorithms </h2>
In this course we will build our machine learning approach on a statistical foundation, with elements
from data analysis, stochastic processes etc before we proceed with the following machine learning algorithms
<p>
Here we will build our machine learning approach on elements of the
statistical foundation discussed above, with elements from data
analysis, stochastic processes etc. We will discuss the following
machine learning algorithms
<ol>
<p><li> Linear regression and its variants</li>
<p><li> Linear regression and its variants, in essence polynomial regression</li>
<p><li> Decision tree algorithms, from simpler to more complex ones</li>
<p><li> Nearest neighbors models</li>
<p><li> Bayesian statistics</li>
<p><li> Bayesian statistics and regression</li>
<p><li> Support vector machines and finally various variants of</li>
<p><li> Artifical neural networks</li>
<p><li> Artifical neural networks and deep learning</li>
</ol>
<p>
Before we proceed however, 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.
Before we proceed however, 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 importance of using available software.
Our emphasis here is on understanding the mathematical aspects of
different algorithms, 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
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
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
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.
</section>
<section>
<h2 id="___sec3">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.
@@ -297,7 +421,7 @@ To install <b>R</b> with Jupyter notebook <a href="https://mpacer.org/maths/r-ke
<h2 id="___sec6">Installing R, C++, cython or Julia </h2>
<p>
For the C++ affecianodas, Jupyter/IPython notebook allows you also to install C++ and run codes written in this language
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.
@@ -369,7 +493,7 @@ line = np.linspace(-<span style="color: #B452CD">3</span>,<span style="color: #B
reg = DecisionTreeRegressor(min_samples_split=<span style="color: #B452CD">3</span>).fit(x,y)
plt.plot(line, reg.predict(line), label=<span style="color: #CD5555">&quot;decision tree&quot;</span>)
regline = LinearRegression().fit(x,y)
plt.plot(line, regline.predict(line), label= <span style="color: #CD5555">&quot;Linear Rgression&quot;</span>)
plt.plot(line, regline.predict(line), label= <span style="color: #CD5555">&quot;Linear Regression&quot;</span>)
plt.show()
</pre></div>
@@ -378,7 +502,149 @@ plt.show()
<section>
<h2 id="___sec9">Predator-Prey model from ecology </h2>
<h2 id="___sec9">Simple regression model </h2>
Add info about the equations
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
xb = np.c_[np.ones((<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)), x]
theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
xnew = np.array([[<span style="color: #B452CD">0</span>],[<span style="color: #B452CD">2</span>]])
xbnew = np.c_[np.ones((<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>)), xnew]
ypredict = xbnew.dot(theta)
plt.plot(xnew, ypredict, <span style="color: #CD5555">&quot;r-&quot;</span>)
plt.plot(x, y ,<span style="color: #CD5555">&#39;ro&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>,<span style="color: #B452CD">2.0</span>,<span style="color: #B452CD">0</span>, <span style="color: #B452CD">15.0</span>])
plt.xlabel(<span style="color: #CD5555">r&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">r&#39;$y$&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Linear Regression&#39;</span>)
plt.show()
</pre></div>
</section>
<section>
<h2 id="___sec10">Simple regression model, now using <b>scikit-learn</b> </h2>
Add info about the equations
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LinearRegression
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
linreg = LinearRegression()
linreg.fit(x,y)
xnew = np.array([[<span style="color: #B452CD">0</span>],[<span style="color: #B452CD">2</span>]])
ypredict = linreg.predict(xnew)
plt.plot(xnew, ypredict, <span style="color: #CD5555">&quot;r-&quot;</span>)
plt.plot(x, y ,<span style="color: #CD5555">&#39;ro&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>,<span style="color: #B452CD">2.0</span>,<span style="color: #B452CD">0</span>, <span style="color: #B452CD">15.0</span>])
plt.xlabel(<span style="color: #CD5555">r&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">r&#39;$y$&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Random numbers &#39;</span>)
plt.show()
</pre></div>
</section>
<section>
<h2 id="___sec11">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 "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">math</span> <span style="color: #8B008B; font-weight: bold">import</span> exp, sqrt
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
xb = np.c_[np.ones((<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)), x]
theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
<span style="color: #8B008B; font-weight: bold">print</span>(theta_linreg)
theta = np.random.randn(<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>)
eta = <span style="color: #B452CD">0.1</span>
Niterations = <span style="color: #B452CD">1000</span>
m = <span style="color: #B452CD">100</span>
<span style="color: #8B008B; font-weight: bold">for</span> <span style="color: #658b00">iter</span> <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(Niterations):
gradients = <span style="color: #B452CD">2.0</span>/m*xb.T.dot(xb.dot(theta)-y)
theta -= eta*gradients
<span style="color: #8B008B; font-weight: bold">print</span>(theta)
xnew = np.array([[<span style="color: #B452CD">0</span>],[<span style="color: #B452CD">2</span>]])
xbnew = np.c_[np.ones((<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>)), xnew]
ypredict = xbnew.dot(theta)
ypredict2 = xbnew.dot(theta_linreg)
plt.plot(xnew, ypredict, <span style="color: #CD5555">&quot;r-&quot;</span>)
plt.plot(xnew, ypredict2, <span style="color: #CD5555">&quot;b-&quot;</span>)
plt.plot(x, y ,<span style="color: #CD5555">&#39;ro&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>,<span style="color: #B452CD">2.0</span>,<span style="color: #B452CD">0</span>, <span style="color: #B452CD">15.0</span>])
plt.xlabel(<span style="color: #CD5555">r&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">r&#39;$y$&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Random numbers &#39;</span>)
plt.show()
</pre></div>
</section>
<section>
<h2 id="___sec12">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 "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">math</span> <span style="color: #8B008B; font-weight: bold">import</span> exp, sqrt
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> SGDRegressor
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
xb = np.c_[np.ones((<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)), x]
theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
<span style="color: #8B008B; font-weight: bold">print</span>(theta_linreg)
sgdreg = SGDRegressor(n_iter = <span style="color: #B452CD">50</span>, penalty=<span style="color: #658b00">None</span>, eta0=<span style="color: #B452CD">0.1</span>)
sgdreg.fit(x,y.ravel())
<span style="color: #8B008B; font-weight: bold">print</span>(sgdreg.intercept_, sgdreg.coef_)
</pre></div>
</section>
<section>
<h2 id="___sec13">Polynomial regression </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>
</pre></div>
</section>
<section>
<h2 id="___sec14">Predator-Prey model from ecology </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -401,7 +667,7 @@ scientific method:
<section>
<h2 id="___sec10">Case study from Hudson bay </h2>
<h2 id="___sec15">Case study from Hudson bay </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -421,7 +687,7 @@ Here we start by
<section>
<h2 id="___sec11">Hudson bay data </h2>
<h2 id="___sec16">Hudson bay data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -467,7 +733,7 @@ One reason that this particular system has been so extensively studied is that t
<section>
<h2 id="___sec12">Plotting the data </h2>
<h2 id="___sec17">Plotting the data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -501,7 +767,7 @@ plt.show()
<section>
<h2 id="___sec13">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
<h2 id="___sec18">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 />
@@ -509,7 +775,7 @@ plt.show()
<section>
<h2 id="___sec14">Why now create a computer model for the hare and lynx populations? </h2>
<h2 id="___sec19">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>
@@ -541,7 +807,7 @@ climate and other complicating factors. How significant are these?
<section>
<h2 id="___sec15">The traditional (top-down) approach </h2>
<h2 id="___sec20">The traditional (top-down) approach </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -576,7 +842,7 @@ ODEs</em> (which cannot be solved)
<section>
<h2 id="___sec16">Basic mathematics notation </h2>
<h2 id="___sec21">Basic mathematics notation </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<ul>
@@ -593,7 +859,7 @@ ODEs</em> (which cannot be solved)
<section>
<h2 id="___sec17">Basic dynamics of the population of hares </h2>
<h2 id="___sec22">Basic dynamics of the population of hares </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -624,7 +890,7 @@ $$ \Delta H = a\Delta t H^n - b \Delta t H^nL^n$$
<section>
<h2 id="___sec18">Basic dynamics of the population of lynx </h2>
<h2 id="___sec23">Basic dynamics of the population of lynx </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -653,7 +919,7 @@ $$ \Delta L = d\Delta t H^nL^n - c\Delta t L^n$$
<section>
<h2 id="___sec19">Evolution equations </h2>
<h2 id="___sec24">Evolution equations </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -684,7 +950,7 @@ Note:
<section>
<h2 id="___sec20">Adapt the model to the Hudson Bay case </h2>
<h2 id="___sec25">Adapt the model to the Hudson Bay case </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -704,7 +970,7 @@ Note:
<section>
<h2 id="___sec21">The program </h2>
<h2 id="___sec26">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -762,7 +1028,7 @@ plt.show()
<section>
<h2 id="___sec22">The plot </h2>
<h2 id="___sec27">The plot </h2>
<p>
<br /><br /><center><p><img src="fig/Hudson_Bay_sim.png" align="bottom" width=700></p></center><br /><br />
@@ -773,7 +1039,7 @@ If we perform a least-square fitting, we can find optimal values for the paramet
<section>
<h2 id="___sec23">Linear regression in Python </h2>
<h2 id="___sec28">Linear regression in Python </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -804,7 +1070,7 @@ plt.show()
<section>
<h2 id="___sec24">Linear Least squares in R </h2>
<h2 id="___sec29">Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -836,7 +1102,7 @@ predict(linearMod,<span style="color: #00688B; font-weight: bold">data.frame</sp
<section>
<h2 id="___sec25">Non-Linear Least squares in R </h2>
<h2 id="___sec30">Non-Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -867,6 +1133,414 @@ text(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">0.5</sp
</section>
<section>
<h2 id="___sec31">Example: ecoli lab experiment </h2>
<p>
<div class="alert alert-block alert-notice alert-text-normal">
<b>Typical pattern:</b>
<p>
The population grows faster and faster. <a href="http://www.zo.utexas.edu/courses/Thoc/PopGrowth.html" target="_blank">Why? Is there an underlying (general) mechanism</a>?
</div>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<ol>
<p><li> Cells divide after \( T \) seconds on average (one generation)</li>
<p><li> \( 2N \) celles divide into twice as many new cells \( \Delta N \) in a time
interval \( \Delta t \) as \( N \) cells would: \( \Delta N \propto N \)</li>
<p><li> \( N \) cells result in twice as many new individuals \( \Delta N \) in
time \( 2\Delta t \) as in time \( \Delta t \): \( \Delta N \propto\Delta t \)</li>
<p><li> Same proportionality wrt death (repeat reasoning)</li>
<p><li> Proposed model: \( \Delta N = b\Delta t N - d\Delta tN \) for some unknown
constants \( b \) (births) and \( d \) (deaths)</li>
<p><li> Describe evolution in discrete time: \( t_n=n\Delta t \)</li>
<p><li> Program-friendly notation: \( N \) at \( t_n \) is \( N^n \)</li>
<p><li> Math model: \( N^{n+1} = N^n + r\Delta t\, N \) (with \( \ r=b-d \))</li>
<p><li> Program model: <code>N[n+1] = N[n] + r*dt*N[n]</code></li>
</ol>
</div>
</section>
<section>
<h2 id="___sec32">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
Let us solve the difference equation in as simple way as possible,
just to train some programming: \( r=1.5 \), \( N^0=1 \), \( \Delta t=0.5 \)
<p>
<!-- code=python (!bc pypro) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
t = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10</span>, <span style="color: #B452CD">21</span>) <span style="color: #228B22"># 20 intervals in [0, 10]</span>
dt = t[<span style="color: #B452CD">1</span>] - t[<span style="color: #B452CD">0</span>]
N = np.zeros(t.size)
N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">1</span>
r = <span style="color: #B452CD">0.5</span>
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, N.size-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;N[%d]=%.1f&#39;</span> % (n+<span style="color: #B452CD">1</span>, N[n+<span style="color: #B452CD">1</span>])
</pre></div>
</div>
<p>
% if FORMAT != 'ipynb':
</section>
<section>
<h2 id="___sec33">The output </h2>
<p>
<!-- code=text typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>N[1]=1.2
N[2]=1.6
N[3]=2.0
N[4]=2.4
N[5]=3.1
N[6]=3.8
N[7]=4.8
N[8]=6.0
N[9]=7.5
N[10]=9.3
N[11]=11.6
N[12]=14.6
N[13]=18.2
N[14]=22.7
N[15]=28.4
N[16]=35.5
N[17]=44.4
N[18]=55.5
N[19]=69.4
N[20]=86.7
</pre></div>
<p>
% endif
</section>
<section>
<h2 id="___sec34">Parameter estimation </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<ul>
<p><li> We do not know \( r \)</li>
<p><li> How can we estimate \( r \) from data?</li>
</ul>
<p>
We can use the difference equation with the experimental data
<p>&nbsp;<br>
$$ N^{n+1} = N^n + r\Delta t N^n$$
<p>&nbsp;<br>
Say \( N^{n+1} \) and \( N^n \) are known from data, solve wrt \( r \):
<p>&nbsp;<br>
$$ r = \frac{N^{n+1}-N^n}{N^n\Delta t} $$
<p>&nbsp;<br>
<p>
Use experimental data in the fraction, say \( t_1=600 \), \( t_2=1200 \),
\( N^1=140 \), \( N^2=250 \): \( r=0.0013 \).
</div>
</section>
<section>
<h2 id="___sec35">A program relevant for the biological problem </h2>
<p>
<!-- exact r = 0.000694 -->
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<!-- code=python (!bc pypro) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #228B22"># Estimate r</span>
data = np.loadtxt(<span style="color: #CD5555">&#39;ecoli.csv&#39;</span>, delimiter=<span style="color: #CD5555">&#39;,&#39;</span>)
t_e = data[:,<span style="color: #B452CD">0</span>]
N_e = data[:,<span style="color: #B452CD">1</span>]
i = <span style="color: #B452CD">2</span> <span style="color: #228B22"># Data point (i,i+1) used to estimate r</span>
r = (N_e[i+<span style="color: #B452CD">1</span>] - N_e[i])/(N_e[i]*(t_e[i+<span style="color: #B452CD">1</span>] - t_e[i]))
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;Estimated r=%.5f&#39;</span> % r
<span style="color: #228B22"># Can experiment with r values and see if the model can</span>
<span style="color: #228B22"># match the data better</span>
T = <span style="color: #B452CD">1200</span> <span style="color: #228B22"># cell can divide after T sec</span>
t_max = <span style="color: #B452CD">5</span>*T <span style="color: #228B22"># 5 generations in experiment</span>
t = np.linspace(<span style="color: #B452CD">0</span>, t_max, <span style="color: #B452CD">1000</span>)
dt = t[<span style="color: #B452CD">1</span>] - t[<span style="color: #B452CD">0</span>]
N = np.zeros(t.size)
N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">100</span>
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, <span style="color: #658b00">len</span>(t)-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
plt.plot(t, N, <span style="color: #CD5555">&#39;r-&#39;</span>, t_e, N_e, <span style="color: #CD5555">&#39;bo&#39;</span>)
plt.xlabel(<span style="color: #CD5555">&#39;time [s]&#39;</span>); plt.ylabel(<span style="color: #CD5555">&#39;N&#39;</span>)
plt.legend([<span style="color: #CD5555">&#39;model&#39;</span>, <span style="color: #CD5555">&#39;experiment&#39;</span>], loc=<span style="color: #CD5555">&#39;upper left&#39;</span>)
plt.show()
</pre></div>
<p>
Change <code>r</code> in the program and play around to make a better fit!
</div>
</section>
<section>
<h2 id="___sec36">Simulating financial transcations </h2>
<p>
The aim here is to simulate financial transactions among financial agents
using Monte Carlo methods. The final goal is to extract a distribution of income as function
of the income \( m \). From Pareto's work (<a href="http://www.institutcoppet.org/2012/05/08/cours-deconomie-politique-1896-de-vilfredo-pareto" target="_blank">V.&nbsp;Pareto, 1897</a>) it is known from empirical studies
that the higher end of the distribution of money follows a distribution
<p>&nbsp;<br>
$$
w_m\propto m^{-1-\alpha},
$$
<p>&nbsp;<br>
with \( \alpha\in [1,2] \). We will here follow the analysis made by <a href="http://www.sciencedirect.com/science/article/pii/S0378437104004327" target="_blank">Patriarca and collaborators</a>.
<p>
Here we will study numerically the relation between the micro-dynamic relations among financial
agents and the resulting macroscopic money distribution.
<p>
We assume we have \( N \) agents that exchange money in pairs \( (i,j) \). We assume also that all agents
start with the same amount of money \( m_0 > 0 \). At a given 'time step', we choose randomly a pair
of agents \( (i,j) \) and let a transaction take place. This means that agent \( i \)'s money \( m_i \) changes
to \( m_i' \) and similarly we have \( m_j\rightarrow m_j' \).
Money is conserved during a transaction, meaning that
<p>&nbsp;<br>
$$
\begin{equation}
m_i+m_j=m_i'+m_j'.
\tag{1}
\end{equation}
$$
<p>&nbsp;<br>
The change is done via a random reassignement (a random number) \( \epsilon \), meaning that
<p>&nbsp;<br>
$$
\begin{equation*}
m_i' = \epsilon(m_i+m_j),
\end{equation*}
$$
<p>&nbsp;<br>
leading to
<p>&nbsp;<br>
$$
\begin{equation*}
m_j'= (1-\epsilon)(m_i+m_j).
\end{equation*}
$$
<p>&nbsp;<br>
The number \( \epsilon \) is extracted from a uniform distribution.
In this simple model, no agents are left with a debt, that is \( m\ge 0 \).
Due to the conservation law above, one can show that the system relaxes toward an equilibrium
state given by a Gibbs distribution
<p>&nbsp;<br>
$$
\begin{equation*}
w_m=\beta \exp{(-\beta m)},
\end{equation*}
$$
<p>&nbsp;<br>
with
<p>&nbsp;<br>
$$
\begin{equation*}
\beta = \frac{1}{\langle m\rangle},
\end{equation*}
$$
<p>&nbsp;<br>
and \( \langle m\rangle=\sum_i m_i/N=m_0 \), the average money.
It means that after equilibrium has been reached that the majority of agents is left with a small
number of money, while the number of richest agents, those with \( m \) larger than a specific value \( m' \),
exponentially decreases with \( m' \).
<p>
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="___sec37">Simulation of Transactions </h3>
Our task is to first set up an algorithm which simulates the above transactions with an initial
amount \( m_0 \).
The challenge here is to figure out a Monte Carlo simulation based on the
above equations.
You will in particular need to make an algorithm which sets up a histogram as function of \( m \).
This histogram contains the number of times a value \( m \) is registered and represents
\( w_m\Delta m \). You will need to set up a value for the interval \( \Delta m \) (typically \( 0.01-0.05 \)).
That means you need to account for the number of times you register an income in the interval
\( m,m+\Delta m \). The number of times you register this income, represents the value that enters the histogram.
You will also need to find a criterion for when the equilibrium situation has been reached.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22">#!/usr/bin/env python</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.mlab</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">mlab</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">random</span>
<span style="color: #228B22"># initialize the rng with a seed</span>
random.seed()
<span style="color: #228B22"># Hard coding of input parameters</span>
Agents = <span style="color: #B452CD">500</span>
MCcounts = <span style="color: #B452CD">1000</span>
Transactions = <span style="color: #B452CD">100000</span>
startMoney = <span style="color: #B452CD">1.0</span>
Lambda = <span style="color: #B452CD">0.0</span>
FinancialAgents = startMoney*np.ones(Agents)
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span> (<span style="color: #B452CD">1</span>, MCcounts, <span style="color: #B452CD">1</span>):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span> (<span style="color: #B452CD">1</span>, Transactions, <span style="color: #B452CD">1</span>):
agent_i = <span style="color: #658b00">int</span>(Agents*random.random())
agent_j = <span style="color: #658b00">int</span>(Agents*random.random())
epsilon = random.random()
<span style="color: #8B008B; font-weight: bold">if</span> agent_i != agent_j:
m1 = Lambda*FinancialAgents[agent_i] + (<span style="color: #B452CD">1</span>-Lambda)*epsilon*(FinancialAgents[agent_i] + FinancialAgents[agent_j])
m2 = Lambda*FinancialAgents[agent_j] + (<span style="color: #B452CD">1</span>-Lambda)*(<span style="color: #B452CD">1</span>-epsilon)*(FinancialAgents[agent_i] + FinancialAgents[agent_j])
FinancialAgents[agent_i] = m1
FinancialAgents[agent_j] = m2
<span style="color: #228B22"># the histogram of the data</span>
n, bins, patches = plt.hist(FinancialAgents, <span style="color: #B452CD">50</span>, facecolor=<span style="color: #CD5555">&#39;green&#39;</span>)
plt.xlabel(<span style="color: #CD5555">&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">&#39;Distribution of wealth&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Money&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10</span>, <span style="color: #B452CD">0</span>, <span style="color: #B452CD">500</span>])
plt.grid(<span style="color: #658b00">True</span>)
plt.show()
</pre></div>
<p>
We can then change our model to allow for a saving criterion, meaning that the agents save
a fraction \( \lambda \) of the money they have before the transaction is made. The final distribution will then no longer be given by Gibbs distribution. It could also include a taxation on financial transactions.
<p>
The conservation law of Eq. <a href="#mjx-eqn-1">(1)</a> holds, but the money to be shared in a transaction between
agent \( i \) and agent \( j \) is now \( (1-\lambda)(m_i+m_j) \). This means that we have
<p>&nbsp;<br>
$$
\begin{equation*}
m_i' = \lambda m_i+\epsilon(1-\lambda)(m_i+m_j),
\end{equation*}
$$
<p>&nbsp;<br>
and
<p>&nbsp;<br>
$$
\begin{equation*}
m_j' = \lambda m_j+(1-\epsilon)(1-\lambda)(m_i+m_j),
\end{equation*}
$$
<p>&nbsp;<br>
which can be written as
<p>&nbsp;<br>
$$
\begin{equation*}
m_i'=m_i+\delta m
\end{equation*}
$$
<p>&nbsp;<br>
and
<p>&nbsp;<br>
$$
\begin{equation*}
m_j'=m_j-\delta m,
\end{equation*}
$$
<p>&nbsp;<br>
with
<p>&nbsp;<br>
$$
\begin{equation*}
\delta m=(1-\lambda)(\epsilon m_j-(1-\epsilon)m_i),
\end{equation*}
$$
<p>&nbsp;<br>
showing how money is conserved during a transaction.
Select values of \( \lambda =0.25,0.5 \) and \( \lambda=0.9 \) and try to extract the corresponding
equilibrium distributions and compare these with the Gibbs distribution. Comment your results.
Extract a parametrization of the above curves, see for example <a href="http://www.sciencedirect.com/science/article/pii/S0378437104004327" target="_blank">Patriarca and collaborators</a> and see if you can parametrize the high-end tails of the distributions in terms of power laws. Comment your results.
<p>
In the studies above the agents were selected randomly, irrespective of whether we allowed for
saving or not during a transaction. What is often observed is that various agents tend to make preferences for for whom to interact with. We will now study the evolution of the distribution of wealth \( w_m \) by assuming that there is a likelihood
<p>&nbsp;<br>
$$
p_{ij} \propto \vert m_i-m_j\vert^{-\alpha},
$$
<p>&nbsp;<br>
for an interaction between agents \( i \) and \( j \) with respective wealths \( m_i \) and \( m_j \). The parameter \( \alpha > 0 \). For \( \alpha=0 \) we recover our model from part 5a).
Perform the same analysis as previously with \( N=500 \) as well as with \( N=1000 \) agents and study the distribution of wealth for \( \alpha =0.5 \), \( \alpha =1.0 \), \( \alpha =1.5 \) and \( \alpha =2.0 \).
You should try to reproduce Figure 1 of <a href="http://www.sciencedirect.com/science/article/pii/S0378437114006967" target="_blank">Goswami and Sen</a>.
Extract the tail of the distribution and see if it follows a Pareto distribution
<p>&nbsp;<br>
$$
w_m\propto m^{-1-\alpha}.
$$
<p>&nbsp;<br>
What happens if \( \alpha \gg 1 \)?
<p>
Perform the analysis with and without a saving \( \lambda \) on each transaction and comment your results.
We add to the previous probability the possibility that two agents who interact have performed similar transactions earlier. That is, in addition to being financially close, we assume that the likelihood for interacting increases if two agents have interacted earlier.
We add this feature by modifying the previous likelihood to
<p>&nbsp;<br>
$$
p_{ij} \propto \vert m_i-m_j\vert^{-\alpha}\left(c_{ij}+1\right)^{\gamma},
$$
<p>&nbsp;<br>
where \( c_{ij} \) represents the number of previous interactions that have taken place between \( i \) and \( j \). The factor \( 1 \) is added in order to ensure that if they have not interacted earlier they can still interact. Perform similar studies as above with \( N=1000 \), \( \alpha=1.0 \) and \( \alpha=2.0 \) using \( \gamma = 0.0, 1.0, 2.0, 3.0 \) and \( 4.0 \). Plot the wealth distributions for these cases and try to extract eventual power law tails with and without a saving \( \lambda \) in each transaction. Comment your results and compare them with figures 5 and 6 of <a href="http://www.sciencedirect.com/science/article/pii/S0378437114006967" target="_blank">Goswami and Sen</a>.
</section>
</div> <!-- class="slides" -->
</div> <!-- class="reveal" -->
@@ -60,9 +60,9 @@ div { text-align: justify; text-justify: inter-word; }
<!-- tocinfo
{'highest level': 2,
'sections': [('What is Machine Learning?', 2, None, '___sec0'),
('Types of Machine Learning', 2, None, '___sec1'),
('Different algorithms', 2, None, '___sec2'),
'sections': [('Introduction', 2, None, '___sec0'),
('Learning outcomes', 2, None, '___sec1'),
('Types of Machine Learning', 2, None, '___sec2'),
('Software and needed installations', 2, None, '___sec3'),
('Python installers', 2, None, '___sec4'),
('Installing R, C++, cython or Julia', 2, None, '___sec5'),
@@ -72,33 +72,57 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec7'),
('Representing data, more examples', 2, None, '___sec8'),
('Predator-Prey model from ecology', 2, None, '___sec9'),
('Case study from Hudson bay', 2, None, '___sec10'),
('Hudson bay data', 2, None, '___sec11'),
('Plotting the data', 2, None, '___sec12'),
('Simple regression model', 2, None, '___sec9'),
('Simple regression model, now using _scikit-learn_',
2,
None,
'___sec10'),
('Simple regression model with gradient descent',
2,
None,
'___sec11'),
('Simple regression model with stochastic gradient descent',
2,
None,
'___sec12'),
('Polynomial regression', 2, None, '___sec13'),
('Predator-Prey model from ecology', 2, None, '___sec14'),
('Case study from Hudson bay', 2, None, '___sec15'),
('Hudson bay data', 2, None, '___sec16'),
('Plotting the data', 2, None, '___sec17'),
('Hares and lynx in Hudson bay from 1900 to 1920',
2,
None,
'___sec13'),
'___sec18'),
('Why now create a computer model for the hare and lynx '
'populations?',
2,
None,
'___sec14'),
('The traditional (top-down) approach', 2, None, '___sec15'),
('Basic mathematics notation', 2, None, '___sec16'),
'___sec19'),
('The traditional (top-down) approach', 2, None, '___sec20'),
('Basic mathematics notation', 2, None, '___sec21'),
('Basic dynamics of the population of hares',
2,
None,
'___sec17'),
('Basic dynamics of the population of lynx', 2, None, '___sec18'),
('Evolution equations', 2, None, '___sec19'),
('Adapt the model to the Hudson Bay case', 2, None, '___sec20'),
('The program', 2, None, '___sec21'),
('The plot', 2, None, '___sec22'),
('Linear regression in Python', 2, None, '___sec23'),
('Linear Least squares in R', 2, None, '___sec24'),
('Non-Linear Least squares in R', 2, None, '___sec25')]}
'___sec22'),
('Basic dynamics of the population of lynx', 2, None, '___sec23'),
('Evolution equations', 2, None, '___sec24'),
('Adapt the model to the Hudson Bay case', 2, None, '___sec25'),
('The program', 2, None, '___sec26'),
('The plot', 2, None, '___sec27'),
('Linear regression in Python', 2, None, '___sec28'),
('Linear Least squares in R', 2, None, '___sec29'),
('Non-Linear Least squares in R', 2, None, '___sec30'),
('Example: ecoli lab experiment', 2, None, '___sec31'),
('The program', 2, None, '___sec32'),
('The output', 2, None, '___sec33'),
('Parameter estimation', 2, None, '___sec34'),
('A program relevant for the biological problem',
2,
None,
'___sec35'),
('Simulating financial transcations', 2, None, '___sec36'),
('Simulation of Transactions', 3, None, '___sec37')]}
end of tocinfo -->
<body>
@@ -140,33 +164,131 @@ 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>Dec 10, 2017</h4></center> <!-- date -->
<center><h4>May 11, 2018</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec0">What is Machine Learning? </h2>
<h2 id="___sec0">Introduction </h2>
<p>
Machine learning is the science of giving computers the ability to
learn without being explicitly programmed. The idea is that there
exist generic algorithms which can be used to find patterns in a broad
class of data sets without having to write code specifically for each
problem. The algorithm will build its own logic based on the data.
Statistics, data science and machine learning form important fields of
research in modern science. They describe how to learn and make
predictions from data, as well allowing us to extract important
correlations about physical process and the underlying laws of motion
in large data sets. The latter, big data sets, appear
frequently in essentially all disciplines, from the traditional Science,
Technology, Mathematics and Engineering fields to Life Science, Law, education research,
the Humanities and
the Social Sciences. It has become more and more common to see
research projects on big data in for example the Social
Sciences where extracting patterns from complicated survey data is one of many research directions.
Having a solid grasp of data analysis and machine learning
is thus becoming central to scientific computing in many
fields, and competences and skills within the fields of machine learning
and scientific computing are nowadays strongly requested by many
potential employers. The latter cannot be overstated, familiarity with
machine learning has almost become a prerequisite for many of the most
exciting employment opportunities, whether they are in bioinformatics,
life science, physics or finance, in the private or the public
sector. This author has had several students or met students who have
been hired recently based on their skills and competences in
scientific computing and data science, often with marginal knowledge
of machine learning.
<p>
Machine learning is a subfield of computer science, and is closely
related to computational statistics. It evolved from the study of
pattern recognition in artificial intelligence (AI) research, and has
made contributions to AI tasks like computer vision, natural language
processing and speech recognition. It has also, especially in later
years, found applications in a wide variety of other areas, including
bioinformatics, economy, physics, finance and marketing.
processing and speech recognition.
Machine learning represents the
science of giving computers the ability to learn without being
explicitly programmed. The idea is that there exist generic
algorithms which can be used to find patterns in a broad class of data
sets without having to write code specifically for each problem. The
algorithm will build its own logic based on the data.
<p>
Machine learning is an extremely rich field, in spite of its young age. The
increases we have seen during the last three decades in computational
capabilities have been followed by developments of methods and
techniques for analyzing and handling large date sets, relying heavily
on statistics, computer science and mathematics. The field is rather
new and developing rapidly. Popular software packages written in
Python for machine learning like <a href="http://scikit-learn.org/stable/" target="_blank">Scikit-learn</a>, <a href="https://www.tensorflow.org/" target="_blank">Tensorflow</a>,
<a href="http://pytorch.org/" target="_blank">PyTorch</a> and <a href="https://keras.io/" target="_blank">Keras</a>, all freely available at their respective GitHub sites,
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing. Not all the
algorithms and methods can be given a rigorous mathematical
justification, opening up thereby large rooms for experimenting
and trial and error and thereby exciting new developments.
However, a solid command of linear algebra, multivariate theory,
probability theory, statistical data analysis,
understanding errors and Monte Carlo methods are central elements in a proper understanding of many of
algorithms and methods we will discuss.
<p>
<!-- !split -->
<h2 id="___sec1">Learning outcomes </h2>
<p>
These lectures aim at giving you an overview of central aspects of
statistical data analysis as well as some of the central algorithms
used in machine learning. We will introduce a variety of central
algorithms and methods essential for studies of data analysis and
machine learning.
<p>
Hands-on projects and experimenting with data and algorithms plays a central role in
these lectures, and our hope is, through the various
projects and exercies, to expose you to fundamental
research problems in these fields, with the aim to reproduce state of
the art scientific results. You will learn to develop and
structure large codes for studying these systems, get acquainted with
computing facilities and learn to handle large scientific projects. A
good scientific and ethical conduct is emphasized throughout the
course. More specifically, you will
<ol>
<li> learn about basic data analysis, Bayesian statistics, Monte Carlo methods, data optimization and machine learning;</li>
<li> be capable of extending the acquired knowledge to other systems and cases;</li>
<li> Have an understanding of central algorithms used in data analysis and machine learning;</li>
<li> 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;</li>
<li> Understand methods for regression and classification;</li>
<li> Learn about neural network, genetic algorithms and Boltzmann machines;</li>
<li> 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).</li>
</ol>
There are several topics we will cover here, spanning from a
statistical data analysis and its basic concepts such expectation
values, variance, covariance, correlation functions and errors, via
well-known probability distribution functions like uniform
distribution, the binomial distribution, the Poisson distribution and
simple and multivariate normal distributions to central elements of
Bayesian statistics and modeling. We will also remind the reader about
central elements from linear algebra and standard methods based on
linear algebra used to fit functions such Cubic splines and gradient
methods for data optimization and the Singular-value decomposition and
least square methods for parameterizing data.
<p>
We will also cover Monte Carlo methods, Markov chains, well-known
algorithms for sampling stochastic events like the Metropolis-Hastings
and Gibbs sampling methods. An important aspect of all our
calculations is a proper estimation of errors. Here we will also
discuss famous resampling techniques like the blocking, bootstrapping
and jackknife methods.
<p>
The second part of the material covers several algorithms used in
machine learning.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec1">Types of Machine Learning </h2>
<h2 id="___sec2">Types of Machine Learning </h2>
<p>
The approaches to machine learning are many, but are often split into two main categories.
@@ -174,7 +296,7 @@ In <em>supervised learning</em> we know the answer to a problem,
and let the computer deduce the logic behind it. On the other hand, <em>unsupervised learning</em>
is a method for finding patterns and relationship in data sets without any prior knowledge of the system.
Some authours also operate with a third category, namely <em>reinforcement learning</em>. This is a paradigm
of learning inspired by behavioural psychology, where learning is achieved by trial-and-error,
of learning inspired by behavioral psychology, where learning is achieved by trial-and-error,
solely from rewards and punishment.
<p>
@@ -187,32 +309,57 @@ Some of the most common tasks are:
<li> Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning.</li>
</ul>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec2">Different algorithms </h2>
In this course we will build our machine learning approach on a statistical foundation, with elements
from data analysis, stochastic processes etc before we proceed with the following machine learning algorithms
<ol>
<li> Linear regression and its variants</li>
<li> Decision tree algorithms, from simpler to more complex ones</li>
<li> Nearest neighbors models</li>
<li> Bayesian statistics</li>
<li> Support vector machines and finally various variants of</li>
<li> Artifical neural networks</li>
</ol>
Before we proceed however, 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.
The methods we cover have three main topics in common, irrespective of
whether we deal with supervised or unsupervised learning. The first
ingredient is normally our data set, the second is a model which is
normally a function of some parameters. The last ingredient is a
so-called <b>cost</b> function which allows us to present an estimate on
how good our model is in reproducing the data it is supposed to train.
<p>
Our emphasis here is on understanding the mathematical aspects of different algorithms, however, where possible
we will emphasize the importance of using available software.
Here we will build our machine learning approach on elements of the
statistical foundation discussed above, with elements from data
analysis, stochastic processes etc. We will discuss the following
machine learning algorithms
<ol>
<li> Linear regression and its variants, in essence polynomial regression</li>
<li> Decision tree algorithms, from simpler to more complex ones</li>
<li> Nearest neighbors models</li>
<li> Bayesian statistics and regression</li>
<li> Support vector machines and finally various variants of</li>
<li> Artifical neural networks and deep learning</li>
</ol>
Before we proceed however, 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
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
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
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
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.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec3">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.
@@ -274,7 +421,7 @@ To install <b>R</b> with Jupyter notebook <a href="https://mpacer.org/maths/r-ke
<h2 id="___sec6">Installing R, C++, cython or Julia </h2>
<p>
For the C++ affecianodas, Jupyter/IPython notebook allows you also to install C++ and run codes written in this language
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.
@@ -349,7 +496,7 @@ line = np.linspace(-<span style="color: #B452CD">3</span>,<span style="color: #B
reg = DecisionTreeRegressor(min_samples_split=<span style="color: #B452CD">3</span>).fit(x,y)
plt.plot(line, reg.predict(line), label=<span style="color: #CD5555">&quot;decision tree&quot;</span>)
regline = LinearRegression().fit(x,y)
plt.plot(line, regline.predict(line), label= <span style="color: #CD5555">&quot;Linear Rgression&quot;</span>)
plt.plot(line, regline.predict(line), label= <span style="color: #CD5555">&quot;Linear Regression&quot;</span>)
plt.show()
</pre></div>
@@ -359,7 +506,144 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">Predator-Prey model from ecology </h2>
<h2 id="___sec9">Simple regression model </h2>
Add info about the equations
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
xb = np.c_[np.ones((<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)), x]
theta = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
xnew = np.array([[<span style="color: #B452CD">0</span>],[<span style="color: #B452CD">2</span>]])
xbnew = np.c_[np.ones((<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>)), xnew]
ypredict = xbnew.dot(theta)
plt.plot(xnew, ypredict, <span style="color: #CD5555">&quot;r-&quot;</span>)
plt.plot(x, y ,<span style="color: #CD5555">&#39;ro&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>,<span style="color: #B452CD">2.0</span>,<span style="color: #B452CD">0</span>, <span style="color: #B452CD">15.0</span>])
plt.xlabel(<span style="color: #CD5555">r&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">r&#39;$y$&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Linear Regression&#39;</span>)
plt.show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Simple regression model, now using <b>scikit-learn</b> </h2>
Add info about the equations
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LinearRegression
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
linreg = LinearRegression()
linreg.fit(x,y)
xnew = np.array([[<span style="color: #B452CD">0</span>],[<span style="color: #B452CD">2</span>]])
ypredict = linreg.predict(xnew)
plt.plot(xnew, ypredict, <span style="color: #CD5555">&quot;r-&quot;</span>)
plt.plot(x, y ,<span style="color: #CD5555">&#39;ro&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>,<span style="color: #B452CD">2.0</span>,<span style="color: #B452CD">0</span>, <span style="color: #B452CD">15.0</span>])
plt.xlabel(<span style="color: #CD5555">r&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">r&#39;$y$&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Random numbers &#39;</span>)
plt.show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">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 "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">math</span> <span style="color: #8B008B; font-weight: bold">import</span> exp, sqrt
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
xb = np.c_[np.ones((<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)), x]
theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
<span style="color: #8B008B; font-weight: bold">print</span>(theta_linreg)
theta = np.random.randn(<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>)
eta = <span style="color: #B452CD">0.1</span>
Niterations = <span style="color: #B452CD">1000</span>
m = <span style="color: #B452CD">100</span>
<span style="color: #8B008B; font-weight: bold">for</span> <span style="color: #658b00">iter</span> <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(Niterations):
gradients = <span style="color: #B452CD">2.0</span>/m*xb.T.dot(xb.dot(theta)-y)
theta -= eta*gradients
<span style="color: #8B008B; font-weight: bold">print</span>(theta)
xnew = np.array([[<span style="color: #B452CD">0</span>],[<span style="color: #B452CD">2</span>]])
xbnew = np.c_[np.ones((<span style="color: #B452CD">2</span>,<span style="color: #B452CD">1</span>)), xnew]
ypredict = xbnew.dot(theta)
ypredict2 = xbnew.dot(theta_linreg)
plt.plot(xnew, ypredict, <span style="color: #CD5555">&quot;r-&quot;</span>)
plt.plot(xnew, ypredict2, <span style="color: #CD5555">&quot;b-&quot;</span>)
plt.plot(x, y ,<span style="color: #CD5555">&#39;ro&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>,<span style="color: #B452CD">2.0</span>,<span style="color: #B452CD">0</span>, <span style="color: #B452CD">15.0</span>])
plt.xlabel(<span style="color: #CD5555">r&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">r&#39;$y$&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Random numbers &#39;</span>)
plt.show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">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 "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># Importing various packages</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">math</span> <span style="color: #8B008B; font-weight: bold">import</span> exp, sqrt
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">random</span> <span style="color: #8B008B; font-weight: bold">import</span> random, seed
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> SGDRegressor
x = <span style="color: #B452CD">2</span>*np.random.rand(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.randn(<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)
xb = np.c_[np.ones((<span style="color: #B452CD">100</span>,<span style="color: #B452CD">1</span>)), x]
theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
<span style="color: #8B008B; font-weight: bold">print</span>(theta_linreg)
sgdreg = SGDRegressor(n_iter = <span style="color: #B452CD">50</span>, penalty=<span style="color: #658b00">None</span>, eta0=<span style="color: #B452CD">0.1</span>)
sgdreg.fit(x,y.ravel())
<span style="color: #8B008B; font-weight: bold">print</span>(sgdreg.intercept_, sgdreg.coef_)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Polynomial regression </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">Predator-Prey model from ecology </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -383,7 +667,7 @@ scientific method:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Case study from Hudson bay </h2>
<h2 id="___sec15">Case study from Hudson bay </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -404,7 +688,7 @@ Here we start by
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">Hudson bay data </h2>
<h2 id="___sec16">Hudson bay data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -453,7 +737,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="___sec12">Plotting the data </h2>
<h2 id="___sec17">Plotting the data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -489,7 +773,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
<h2 id="___sec18">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 />
@@ -497,7 +781,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">Why now create a computer model for the hare and lynx populations? </h2>
<h2 id="___sec19">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>
@@ -530,7 +814,7 @@ climate and other complicating factors. How significant are these?
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec15">The traditional (top-down) approach </h2>
<h2 id="___sec20">The traditional (top-down) approach </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -563,7 +847,7 @@ ODEs</em> (which cannot be solved)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec16">Basic mathematics notation </h2>
<h2 id="___sec21">Basic mathematics notation </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -583,7 +867,7 @@ ODEs</em> (which cannot be solved)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">Basic dynamics of the population of hares </h2>
<h2 id="___sec22">Basic dynamics of the population of hares </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -611,7 +895,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="___sec18">Basic dynamics of the population of lynx </h2>
<h2 id="___sec23">Basic dynamics of the population of lynx </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -640,7 +924,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="___sec19">Evolution equations </h2>
<h2 id="___sec24">Evolution equations </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -669,7 +953,7 @@ Note:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec20">Adapt the model to the Hudson Bay case </h2>
<h2 id="___sec25">Adapt the model to the Hudson Bay case </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -692,7 +976,7 @@ Note:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec21">The program </h2>
<h2 id="___sec26">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -752,7 +1036,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec22">The plot </h2>
<h2 id="___sec27">The plot </h2>
<p>
<br /><br /><center><p><img src="fig/Hudson_Bay_sim.png" align="bottom" width=700></p></center><br /><br />
@@ -763,7 +1047,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="___sec23">Linear regression in Python </h2>
<h2 id="___sec28">Linear regression in Python </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -796,7 +1080,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec24">Linear Least squares in R </h2>
<h2 id="___sec29">Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -830,7 +1114,7 @@ predict(linearMod,<span style="color: #00688B; font-weight: bold">data.frame</sp
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec25">Non-Linear Least squares in R </h2>
<h2 id="___sec30">Non-Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -862,12 +1146,392 @@ text(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">0.5</sp
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec31">Example: ecoli lab experiment </h2>
<p>
<div class="alert alert-block alert-notice alert-text-normal">
<b>Typical pattern:</b>
<p>
The population grows faster and faster. <a href="http://www.zo.utexas.edu/courses/Thoc/PopGrowth.html" target="_blank">Why? Is there an underlying (general) mechanism</a>?
</div>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<ol>
<li> Cells divide after \( T \) seconds on average (one generation)</li>
<li> \( 2N \) celles divide into twice as many new cells \( \Delta N \) in a time
interval \( \Delta t \) as \( N \) cells would: \( \Delta N \propto N \)</li>
<li> \( N \) cells result in twice as many new individuals \( \Delta N \) in
time \( 2\Delta t \) as in time \( \Delta t \): \( \Delta N \propto\Delta t \)</li>
<li> Same proportionality wrt death (repeat reasoning)</li>
<li> Proposed model: \( \Delta N = b\Delta t N - d\Delta tN \) for some unknown
constants \( b \) (births) and \( d \) (deaths)</li>
<li> Describe evolution in discrete time: \( t_n=n\Delta t \)</li>
<li> Program-friendly notation: \( N \) at \( t_n \) is \( N^n \)</li>
<li> Math model: \( N^{n+1} = N^n + r\Delta t\, N \) (with \( \ r=b-d \))</li>
<li> Program model: <code>N[n+1] = N[n] + r*dt*N[n]</code></li>
</ol>
</div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec32">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
Let us solve the difference equation in as simple way as possible,
just to train some programming: \( r=1.5 \), \( N^0=1 \), \( \Delta t=0.5 \)
<p>
<!-- code=python (!bc pypro) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
t = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10</span>, <span style="color: #B452CD">21</span>) <span style="color: #228B22"># 20 intervals in [0, 10]</span>
dt = t[<span style="color: #B452CD">1</span>] - t[<span style="color: #B452CD">0</span>]
N = np.zeros(t.size)
N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">1</span>
r = <span style="color: #B452CD">0.5</span>
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, N.size-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;N[%d]=%.1f&#39;</span> % (n+<span style="color: #B452CD">1</span>, N[n+<span style="color: #B452CD">1</span>])
</pre></div>
</div>
<p>
% if FORMAT != 'ipynb':
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec33">The output </h2>
<p>
<!-- code=text typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>N[1]=1.2
N[2]=1.6
N[3]=2.0
N[4]=2.4
N[5]=3.1
N[6]=3.8
N[7]=4.8
N[8]=6.0
N[9]=7.5
N[10]=9.3
N[11]=11.6
N[12]=14.6
N[13]=18.2
N[14]=22.7
N[15]=28.4
N[16]=35.5
N[17]=44.4
N[18]=55.5
N[19]=69.4
N[20]=86.7
</pre></div>
<p>
% endif
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec34">Parameter estimation </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<ul>
<li> We do not know \( r \)</li>
<li> How can we estimate \( r \) from data?</li>
</ul>
We can use the difference equation with the experimental data
$$ N^{n+1} = N^n + r\Delta t N^n$$
Say \( N^{n+1} \) and \( N^n \) are known from data, solve wrt \( r \):
$$ r = \frac{N^{n+1}-N^n}{N^n\Delta t} $$
<p>
Use experimental data in the fraction, say \( t_1=600 \), \( t_2=1200 \),
\( N^1=140 \), \( N^2=250 \): \( r=0.0013 \).
</div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec35">A program relevant for the biological problem </h2>
<p>
<!-- exact r = 0.000694 -->
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<p>
<!-- code=python (!bc pypro) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eee8d5"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #228B22"># Estimate r</span>
data = np.loadtxt(<span style="color: #CD5555">&#39;ecoli.csv&#39;</span>, delimiter=<span style="color: #CD5555">&#39;,&#39;</span>)
t_e = data[:,<span style="color: #B452CD">0</span>]
N_e = data[:,<span style="color: #B452CD">1</span>]
i = <span style="color: #B452CD">2</span> <span style="color: #228B22"># Data point (i,i+1) used to estimate r</span>
r = (N_e[i+<span style="color: #B452CD">1</span>] - N_e[i])/(N_e[i]*(t_e[i+<span style="color: #B452CD">1</span>] - t_e[i]))
<span style="color: #8B008B; font-weight: bold">print</span> <span style="color: #CD5555">&#39;Estimated r=%.5f&#39;</span> % r
<span style="color: #228B22"># Can experiment with r values and see if the model can</span>
<span style="color: #228B22"># match the data better</span>
T = <span style="color: #B452CD">1200</span> <span style="color: #228B22"># cell can divide after T sec</span>
t_max = <span style="color: #B452CD">5</span>*T <span style="color: #228B22"># 5 generations in experiment</span>
t = np.linspace(<span style="color: #B452CD">0</span>, t_max, <span style="color: #B452CD">1000</span>)
dt = t[<span style="color: #B452CD">1</span>] - t[<span style="color: #B452CD">0</span>]
N = np.zeros(t.size)
N[<span style="color: #B452CD">0</span>] = <span style="color: #B452CD">100</span>
<span style="color: #8B008B; font-weight: bold">for</span> n <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>, <span style="color: #658b00">len</span>(t)-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>):
N[n+<span style="color: #B452CD">1</span>] = N[n] + r*dt*N[n]
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
plt.plot(t, N, <span style="color: #CD5555">&#39;r-&#39;</span>, t_e, N_e, <span style="color: #CD5555">&#39;bo&#39;</span>)
plt.xlabel(<span style="color: #CD5555">&#39;time [s]&#39;</span>); plt.ylabel(<span style="color: #CD5555">&#39;N&#39;</span>)
plt.legend([<span style="color: #CD5555">&#39;model&#39;</span>, <span style="color: #CD5555">&#39;experiment&#39;</span>], loc=<span style="color: #CD5555">&#39;upper left&#39;</span>)
plt.show()
</pre></div>
<p>
Change <code>r</code> in the program and play around to make a better fit!
</div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec36">Simulating financial transcations </h2>
<p>
The aim here is to simulate financial transactions among financial agents
using Monte Carlo methods. The final goal is to extract a distribution of income as function
of the income \( m \). From Pareto's work (<a href="http://www.institutcoppet.org/2012/05/08/cours-deconomie-politique-1896-de-vilfredo-pareto" target="_blank">V.&nbsp;Pareto, 1897</a>) it is known from empirical studies
that the higher end of the distribution of money follows a distribution
$$
w_m\propto m^{-1-\alpha},
$$
with \( \alpha\in [1,2] \). We will here follow the analysis made by <a href="http://www.sciencedirect.com/science/article/pii/S0378437104004327" target="_blank">Patriarca and collaborators</a>.
<p>
Here we will study numerically the relation between the micro-dynamic relations among financial
agents and the resulting macroscopic money distribution.
<p>
We assume we have \( N \) agents that exchange money in pairs \( (i,j) \). We assume also that all agents
start with the same amount of money \( m_0 > 0 \). At a given 'time step', we choose randomly a pair
of agents \( (i,j) \) and let a transaction take place. This means that agent \( i \)'s money \( m_i \) changes
to \( m_i' \) and similarly we have \( m_j\rightarrow m_j' \).
Money is conserved during a transaction, meaning that
$$
\begin{equation}
m_i+m_j=m_i'+m_j'.
\label{eq:conserve}
\end{equation}
$$
The change is done via a random reassignement (a random number) \( \epsilon \), meaning that
$$
\begin{equation*}
m_i' = \epsilon(m_i+m_j),
\end{equation*}
$$
leading to
$$
\begin{equation*}
m_j'= (1-\epsilon)(m_i+m_j).
\end{equation*}
$$
The number \( \epsilon \) is extracted from a uniform distribution.
In this simple model, no agents are left with a debt, that is \( m\ge 0 \).
Due to the conservation law above, one can show that the system relaxes toward an equilibrium
state given by a Gibbs distribution
$$
\begin{equation*}
w_m=\beta \exp{(-\beta m)},
\end{equation*}
$$
with
$$
\begin{equation*}
\beta = \frac{1}{\langle m\rangle},
\end{equation*}
$$
and \( \langle m\rangle=\sum_i m_i/N=m_0 \), the average money.
It means that after equilibrium has been reached that the majority of agents is left with a small
number of money, while the number of richest agents, those with \( m \) larger than a specific value \( m' \),
exponentially decreases with \( m' \).
<p>
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="___sec37">Simulation of Transactions </h3>
Our task is to first set up an algorithm which simulates the above transactions with an initial
amount \( m_0 \).
The challenge here is to figure out a Monte Carlo simulation based on the
above equations.
You will in particular need to make an algorithm which sets up a histogram as function of \( m \).
This histogram contains the number of times a value \( m \) is registered and represents
\( w_m\Delta m \). You will need to set up a value for the interval \( \Delta m \) (typically \( 0.01-0.05 \)).
That means you need to account for the number of times you register an income in the interval
\( m,m+\Delta m \). The number of times you register this income, represents the value that enters the histogram.
You will also need to find a criterion for when the equilibrium situation has been reached.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22">#!/usr/bin/env python</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.mlab</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">mlab</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">random</span>
<span style="color: #228B22"># initialize the rng with a seed</span>
random.seed()
<span style="color: #228B22"># Hard coding of input parameters</span>
Agents = <span style="color: #B452CD">500</span>
MCcounts = <span style="color: #B452CD">1000</span>
Transactions = <span style="color: #B452CD">100000</span>
startMoney = <span style="color: #B452CD">1.0</span>
Lambda = <span style="color: #B452CD">0.0</span>
FinancialAgents = startMoney*np.ones(Agents)
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span> (<span style="color: #B452CD">1</span>, MCcounts, <span style="color: #B452CD">1</span>):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span> (<span style="color: #B452CD">1</span>, Transactions, <span style="color: #B452CD">1</span>):
agent_i = <span style="color: #658b00">int</span>(Agents*random.random())
agent_j = <span style="color: #658b00">int</span>(Agents*random.random())
epsilon = random.random()
<span style="color: #8B008B; font-weight: bold">if</span> agent_i != agent_j:
m1 = Lambda*FinancialAgents[agent_i] + (<span style="color: #B452CD">1</span>-Lambda)*epsilon*(FinancialAgents[agent_i] + FinancialAgents[agent_j])
m2 = Lambda*FinancialAgents[agent_j] + (<span style="color: #B452CD">1</span>-Lambda)*(<span style="color: #B452CD">1</span>-epsilon)*(FinancialAgents[agent_i] + FinancialAgents[agent_j])
FinancialAgents[agent_i] = m1
FinancialAgents[agent_j] = m2
<span style="color: #228B22"># the histogram of the data</span>
n, bins, patches = plt.hist(FinancialAgents, <span style="color: #B452CD">50</span>, facecolor=<span style="color: #CD5555">&#39;green&#39;</span>)
plt.xlabel(<span style="color: #CD5555">&#39;$x$&#39;</span>)
plt.ylabel(<span style="color: #CD5555">&#39;Distribution of wealth&#39;</span>)
plt.title(<span style="color: #CD5555">r&#39;Money&#39;</span>)
plt.axis([<span style="color: #B452CD">0</span>, <span style="color: #B452CD">10</span>, <span style="color: #B452CD">0</span>, <span style="color: #B452CD">500</span>])
plt.grid(<span style="color: #658b00">True</span>)
plt.show()
</pre></div>
<p>
We can then change our model to allow for a saving criterion, meaning that the agents save
a fraction \( \lambda \) of the money they have before the transaction is made. The final distribution will then no longer be given by Gibbs distribution. It could also include a taxation on financial transactions.
<p>
The conservation law of Eq. \eqref{eq:conserve} holds, but the money to be shared in a transaction between
agent \( i \) and agent \( j \) is now \( (1-\lambda)(m_i+m_j) \). This means that we have
$$
\begin{equation*}
m_i' = \lambda m_i+\epsilon(1-\lambda)(m_i+m_j),
\end{equation*}
$$
and
$$
\begin{equation*}
m_j' = \lambda m_j+(1-\epsilon)(1-\lambda)(m_i+m_j),
\end{equation*}
$$
which can be written as
$$
\begin{equation*}
m_i'=m_i+\delta m
\end{equation*}
$$
and
$$
\begin{equation*}
m_j'=m_j-\delta m,
\end{equation*}
$$
with
$$
\begin{equation*}
\delta m=(1-\lambda)(\epsilon m_j-(1-\epsilon)m_i),
\end{equation*}
$$
showing how money is conserved during a transaction.
Select values of \( \lambda =0.25,0.5 \) and \( \lambda=0.9 \) and try to extract the corresponding
equilibrium distributions and compare these with the Gibbs distribution. Comment your results.
Extract a parametrization of the above curves, see for example <a href="http://www.sciencedirect.com/science/article/pii/S0378437104004327" target="_blank">Patriarca and collaborators</a> and see if you can parametrize the high-end tails of the distributions in terms of power laws. Comment your results.
<p>
In the studies above the agents were selected randomly, irrespective of whether we allowed for
saving or not during a transaction. What is often observed is that various agents tend to make preferences for for whom to interact with. We will now study the evolution of the distribution of wealth \( w_m \) by assuming that there is a likelihood
$$
p_{ij} \propto \vert m_i-m_j\vert^{-\alpha},
$$
for an interaction between agents \( i \) and \( j \) with respective wealths \( m_i \) and \( m_j \). The parameter \( \alpha > 0 \). For \( \alpha=0 \) we recover our model from part 5a).
Perform the same analysis as previously with \( N=500 \) as well as with \( N=1000 \) agents and study the distribution of wealth for \( \alpha =0.5 \), \( \alpha =1.0 \), \( \alpha =1.5 \) and \( \alpha =2.0 \).
You should try to reproduce Figure 1 of <a href="http://www.sciencedirect.com/science/article/pii/S0378437114006967" target="_blank">Goswami and Sen</a>.
Extract the tail of the distribution and see if it follows a Pareto distribution
$$
w_m\propto m^{-1-\alpha}.
$$
What happens if \( \alpha \gg 1 \)?
<p>
Perform the analysis with and without a saving \( \lambda \) on each transaction and comment your results.
We add to the previous probability the possibility that two agents who interact have performed similar transactions earlier. That is, in addition to being financially close, we assume that the likelihood for interacting increases if two agents have interacted earlier.
We add this feature by modifying the previous likelihood to
$$
p_{ij} \propto \vert m_i-m_j\vert^{-\alpha}\left(c_{ij}+1\right)^{\gamma},
$$
where \( c_{ij} \) represents the number of previous interactions that have taken place between \( i \) and \( j \). The factor \( 1 \) is added in order to ensure that if they have not interacted earlier they can still interact. Perform similar studies as above with \( N=1000 \), \( \alpha=1.0 \) and \( \alpha=2.0 \) using \( \gamma = 0.0, 1.0, 2.0, 3.0 \) and \( 4.0 \). Plot the wealth distributions for these cases and try to extract eventual power law tails with and without a saving \( \lambda \) in each transaction. Comment your results and compare them with figures 5 and 6 of <a href="http://www.sciencedirect.com/science/article/pii/S0378437114006967" target="_blank">Goswami and Sen</a>.
<!-- ------------------- end of main content --------------- -->
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2017, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
<!-- copyright --> &copy; 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
</center>
+735 -71
View File
@@ -65,9 +65,9 @@ div { text-align: justify; text-justify: inter-word; }
<!-- tocinfo
{'highest level': 2,
'sections': [('What is Machine Learning?', 2, None, '___sec0'),
('Types of Machine Learning', 2, None, '___sec1'),
('Different algorithms', 2, None, '___sec2'),
'sections': [('Introduction', 2, None, '___sec0'),
('Learning outcomes', 2, None, '___sec1'),
('Types of Machine Learning', 2, None, '___sec2'),
('Software and needed installations', 2, None, '___sec3'),
('Python installers', 2, None, '___sec4'),
('Installing R, C++, cython or Julia', 2, None, '___sec5'),
@@ -77,33 +77,57 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec7'),
('Representing data, more examples', 2, None, '___sec8'),
('Predator-Prey model from ecology', 2, None, '___sec9'),
('Case study from Hudson bay', 2, None, '___sec10'),
('Hudson bay data', 2, None, '___sec11'),
('Plotting the data', 2, None, '___sec12'),
('Simple regression model', 2, None, '___sec9'),
('Simple regression model, now using _scikit-learn_',
2,
None,
'___sec10'),
('Simple regression model with gradient descent',
2,
None,
'___sec11'),
('Simple regression model with stochastic gradient descent',
2,
None,
'___sec12'),
('Polynomial regression', 2, None, '___sec13'),
('Predator-Prey model from ecology', 2, None, '___sec14'),
('Case study from Hudson bay', 2, None, '___sec15'),
('Hudson bay data', 2, None, '___sec16'),
('Plotting the data', 2, None, '___sec17'),
('Hares and lynx in Hudson bay from 1900 to 1920',
2,
None,
'___sec13'),
'___sec18'),
('Why now create a computer model for the hare and lynx '
'populations?',
2,
None,
'___sec14'),
('The traditional (top-down) approach', 2, None, '___sec15'),
('Basic mathematics notation', 2, None, '___sec16'),
'___sec19'),
('The traditional (top-down) approach', 2, None, '___sec20'),
('Basic mathematics notation', 2, None, '___sec21'),
('Basic dynamics of the population of hares',
2,
None,
'___sec17'),
('Basic dynamics of the population of lynx', 2, None, '___sec18'),
('Evolution equations', 2, None, '___sec19'),
('Adapt the model to the Hudson Bay case', 2, None, '___sec20'),
('The program', 2, None, '___sec21'),
('The plot', 2, None, '___sec22'),
('Linear regression in Python', 2, None, '___sec23'),
('Linear Least squares in R', 2, None, '___sec24'),
('Non-Linear Least squares in R', 2, None, '___sec25')]}
'___sec22'),
('Basic dynamics of the population of lynx', 2, None, '___sec23'),
('Evolution equations', 2, None, '___sec24'),
('Adapt the model to the Hudson Bay case', 2, None, '___sec25'),
('The program', 2, None, '___sec26'),
('The plot', 2, None, '___sec27'),
('Linear regression in Python', 2, None, '___sec28'),
('Linear Least squares in R', 2, None, '___sec29'),
('Non-Linear Least squares in R', 2, None, '___sec30'),
('Example: ecoli lab experiment', 2, None, '___sec31'),
('The program', 2, None, '___sec32'),
('The output', 2, None, '___sec33'),
('Parameter estimation', 2, None, '___sec34'),
('A program relevant for the biological problem',
2,
None,
'___sec35'),
('Simulating financial transcations', 2, None, '___sec36'),
('Simulation of Transactions', 3, None, '___sec37')]}
end of tocinfo -->
<body>
@@ -145,33 +169,131 @@ 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>Dec 10, 2017</h4></center> <!-- date -->
<center><h4>May 11, 2018</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec0">What is Machine Learning? </h2>
<h2 id="___sec0">Introduction </h2>
<p>
Machine learning is the science of giving computers the ability to
learn without being explicitly programmed. The idea is that there
exist generic algorithms which can be used to find patterns in a broad
class of data sets without having to write code specifically for each
problem. The algorithm will build its own logic based on the data.
Statistics, data science and machine learning form important fields of
research in modern science. They describe how to learn and make
predictions from data, as well allowing us to extract important
correlations about physical process and the underlying laws of motion
in large data sets. The latter, big data sets, appear
frequently in essentially all disciplines, from the traditional Science,
Technology, Mathematics and Engineering fields to Life Science, Law, education research,
the Humanities and
the Social Sciences. It has become more and more common to see
research projects on big data in for example the Social
Sciences where extracting patterns from complicated survey data is one of many research directions.
Having a solid grasp of data analysis and machine learning
is thus becoming central to scientific computing in many
fields, and competences and skills within the fields of machine learning
and scientific computing are nowadays strongly requested by many
potential employers. The latter cannot be overstated, familiarity with
machine learning has almost become a prerequisite for many of the most
exciting employment opportunities, whether they are in bioinformatics,
life science, physics or finance, in the private or the public
sector. This author has had several students or met students who have
been hired recently based on their skills and competences in
scientific computing and data science, often with marginal knowledge
of machine learning.
<p>
Machine learning is a subfield of computer science, and is closely
related to computational statistics. It evolved from the study of
pattern recognition in artificial intelligence (AI) research, and has
made contributions to AI tasks like computer vision, natural language
processing and speech recognition. It has also, especially in later
years, found applications in a wide variety of other areas, including
bioinformatics, economy, physics, finance and marketing.
processing and speech recognition.
Machine learning represents the
science of giving computers the ability to learn without being
explicitly programmed. The idea is that there exist generic
algorithms which can be used to find patterns in a broad class of data
sets without having to write code specifically for each problem. The
algorithm will build its own logic based on the data.
<p>
Machine learning is an extremely rich field, in spite of its young age. The
increases we have seen during the last three decades in computational
capabilities have been followed by developments of methods and
techniques for analyzing and handling large date sets, relying heavily
on statistics, computer science and mathematics. The field is rather
new and developing rapidly. Popular software packages written in
Python for machine learning like <a href="http://scikit-learn.org/stable/" target="_blank">Scikit-learn</a>, <a href="https://www.tensorflow.org/" target="_blank">Tensorflow</a>,
<a href="http://pytorch.org/" target="_blank">PyTorch</a> and <a href="https://keras.io/" target="_blank">Keras</a>, all freely available at their respective GitHub sites,
encompass communities of developers in the thousands or more. And the number
of code developers and contributors keeps increasing. Not all the
algorithms and methods can be given a rigorous mathematical
justification, opening up thereby large rooms for experimenting
and trial and error and thereby exciting new developments.
However, a solid command of linear algebra, multivariate theory,
probability theory, statistical data analysis,
understanding errors and Monte Carlo methods are central elements in a proper understanding of many of
algorithms and methods we will discuss.
<p>
<!-- !split -->
<h2 id="___sec1">Learning outcomes </h2>
<p>
These lectures aim at giving you an overview of central aspects of
statistical data analysis as well as some of the central algorithms
used in machine learning. We will introduce a variety of central
algorithms and methods essential for studies of data analysis and
machine learning.
<p>
Hands-on projects and experimenting with data and algorithms plays a central role in
these lectures, and our hope is, through the various
projects and exercies, to expose you to fundamental
research problems in these fields, with the aim to reproduce state of
the art scientific results. You will learn to develop and
structure large codes for studying these systems, get acquainted with
computing facilities and learn to handle large scientific projects. A
good scientific and ethical conduct is emphasized throughout the
course. More specifically, you will
<ol>
<li> learn about basic data analysis, Bayesian statistics, Monte Carlo methods, data optimization and machine learning;</li>
<li> be capable of extending the acquired knowledge to other systems and cases;</li>
<li> Have an understanding of central algorithms used in data analysis and machine learning;</li>
<li> 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;</li>
<li> Understand methods for regression and classification;</li>
<li> Learn about neural network, genetic algorithms and Boltzmann machines;</li>
<li> 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).</li>
</ol>
There are several topics we will cover here, spanning from a
statistical data analysis and its basic concepts such expectation
values, variance, covariance, correlation functions and errors, via
well-known probability distribution functions like uniform
distribution, the binomial distribution, the Poisson distribution and
simple and multivariate normal distributions to central elements of
Bayesian statistics and modeling. We will also remind the reader about
central elements from linear algebra and standard methods based on
linear algebra used to fit functions such Cubic splines and gradient
methods for data optimization and the Singular-value decomposition and
least square methods for parameterizing data.
<p>
We will also cover Monte Carlo methods, Markov chains, well-known
algorithms for sampling stochastic events like the Metropolis-Hastings
and Gibbs sampling methods. An important aspect of all our
calculations is a proper estimation of errors. Here we will also
discuss famous resampling techniques like the blocking, bootstrapping
and jackknife methods.
<p>
The second part of the material covers several algorithms used in
machine learning.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec1">Types of Machine Learning </h2>
<h2 id="___sec2">Types of Machine Learning </h2>
<p>
The approaches to machine learning are many, but are often split into two main categories.
@@ -179,7 +301,7 @@ In <em>supervised learning</em> we know the answer to a problem,
and let the computer deduce the logic behind it. On the other hand, <em>unsupervised learning</em>
is a method for finding patterns and relationship in data sets without any prior knowledge of the system.
Some authours also operate with a third category, namely <em>reinforcement learning</em>. This is a paradigm
of learning inspired by behavioural psychology, where learning is achieved by trial-and-error,
of learning inspired by behavioral psychology, where learning is achieved by trial-and-error,
solely from rewards and punishment.
<p>
@@ -192,32 +314,57 @@ Some of the most common tasks are:
<li> Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning.</li>
</ul>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec2">Different algorithms </h2>
In this course we will build our machine learning approach on a statistical foundation, with elements
from data analysis, stochastic processes etc before we proceed with the following machine learning algorithms
<ol>
<li> Linear regression and its variants</li>
<li> Decision tree algorithms, from simpler to more complex ones</li>
<li> Nearest neighbors models</li>
<li> Bayesian statistics</li>
<li> Support vector machines and finally various variants of</li>
<li> Artifical neural networks</li>
</ol>
Before we proceed however, 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.
The methods we cover have three main topics in common, irrespective of
whether we deal with supervised or unsupervised learning. The first
ingredient is normally our data set, the second is a model which is
normally a function of some parameters. The last ingredient is a
so-called <b>cost</b> function which allows us to present an estimate on
how good our model is in reproducing the data it is supposed to train.
<p>
Our emphasis here is on understanding the mathematical aspects of different algorithms, however, where possible
we will emphasize the importance of using available software.
Here we will build our machine learning approach on elements of the
statistical foundation discussed above, with elements from data
analysis, stochastic processes etc. We will discuss the following
machine learning algorithms
<ol>
<li> Linear regression and its variants, in essence polynomial regression</li>
<li> Decision tree algorithms, from simpler to more complex ones</li>
<li> Nearest neighbors models</li>
<li> Bayesian statistics and regression</li>
<li> Support vector machines and finally various variants of</li>
<li> Artifical neural networks and deep learning</li>
</ol>
Before we proceed however, 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
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
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
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
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.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec3">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.
@@ -279,7 +426,7 @@ To install <b>R</b> with Jupyter notebook <a href="https://mpacer.org/maths/r-ke
<h2 id="___sec6">Installing R, C++, cython or Julia </h2>
<p>
For the C++ affecianodas, Jupyter/IPython notebook allows you also to install C++ and run codes written in this language
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.
@@ -354,7 +501,7 @@ line <span style="color: #666666">=</span> np<span style="color: #666666">.</spa
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 Rgression&quot;</span>)
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>
@@ -364,7 +511,144 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">Predator-Prey model from ecology </h2>
<h2 id="___sec9">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="___sec10">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="___sec11">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="___sec12">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="___sec13">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="___sec14">Predator-Prey model from ecology </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -388,7 +672,7 @@ scientific method:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Case study from Hudson bay </h2>
<h2 id="___sec15">Case study from Hudson bay </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -409,7 +693,7 @@ Here we start by
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">Hudson bay data </h2>
<h2 id="___sec16">Hudson bay data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -458,7 +742,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="___sec12">Plotting the data </h2>
<h2 id="___sec17">Plotting the data </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -494,7 +778,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Hares and lynx in Hudson bay from 1900 to 1920 </h2>
<h2 id="___sec18">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 />
@@ -502,7 +786,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">Why now create a computer model for the hare and lynx populations? </h2>
<h2 id="___sec19">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>
@@ -535,7 +819,7 @@ climate and other complicating factors. How significant are these?
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec15">The traditional (top-down) approach </h2>
<h2 id="___sec20">The traditional (top-down) approach </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -568,7 +852,7 @@ ODEs</em> (which cannot be solved)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec16">Basic mathematics notation </h2>
<h2 id="___sec21">Basic mathematics notation </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -588,7 +872,7 @@ ODEs</em> (which cannot be solved)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">Basic dynamics of the population of hares </h2>
<h2 id="___sec22">Basic dynamics of the population of hares </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -616,7 +900,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="___sec18">Basic dynamics of the population of lynx </h2>
<h2 id="___sec23">Basic dynamics of the population of lynx </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -645,7 +929,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="___sec19">Evolution equations </h2>
<h2 id="___sec24">Evolution equations </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -674,7 +958,7 @@ Note:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec20">Adapt the model to the Hudson Bay case </h2>
<h2 id="___sec25">Adapt the model to the Hudson Bay case </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -697,7 +981,7 @@ Note:
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec21">The program </h2>
<h2 id="___sec26">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
@@ -757,7 +1041,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec22">The plot </h2>
<h2 id="___sec27">The plot </h2>
<p>
<br /><br /><center><p><img src="fig/Hudson_Bay_sim.png" align="bottom" width=700></p></center><br /><br />
@@ -768,7 +1052,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="___sec23">Linear regression in Python </h2>
<h2 id="___sec28">Linear regression in Python </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -801,7 +1085,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec24">Linear Least squares in R </h2>
<h2 id="___sec29">Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -835,7 +1119,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="___sec25">Non-Linear Least squares in R </h2>
<h2 id="___sec30">Non-Linear Least squares in R </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -867,12 +1151,392 @@ 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="___sec31">Example: ecoli lab experiment </h2>
<p>
<div class="alert alert-block alert-notice alert-text-normal">
<b>Typical pattern:</b>
<p>
The population grows faster and faster. <a href="http://www.zo.utexas.edu/courses/Thoc/PopGrowth.html" target="_blank">Why? Is there an underlying (general) mechanism</a>?
</div>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<ol>
<li> Cells divide after \( T \) seconds on average (one generation)</li>
<li> \( 2N \) celles divide into twice as many new cells \( \Delta N \) in a time
interval \( \Delta t \) as \( N \) cells would: \( \Delta N \propto N \)</li>
<li> \( N \) cells result in twice as many new individuals \( \Delta N \) in
time \( 2\Delta t \) as in time \( \Delta t \): \( \Delta N \propto\Delta t \)</li>
<li> Same proportionality wrt death (repeat reasoning)</li>
<li> Proposed model: \( \Delta N = b\Delta t N - d\Delta tN \) for some unknown
constants \( b \) (births) and \( d \) (deaths)</li>
<li> Describe evolution in discrete time: \( t_n=n\Delta t \)</li>
<li> Program-friendly notation: \( N \) at \( t_n \) is \( N^n \)</li>
<li> Math model: \( N^{n+1} = N^n + r\Delta t\, N \) (with \( \ r=b-d \))</li>
<li> Program model: <code>N[n+1] = N[n] + r*dt*N[n]</code></li>
</ol>
</div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec32">The program </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
Let us solve the difference equation in as simple way as possible,
just to train some programming: \( r=1.5 \), \( N^0=1 \), \( \Delta t=0.5 \)
<p>
<!-- code=python (!bc pypro) 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>
t <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">10</span>, <span style="color: #666666">21</span>) <span style="color: #408080; font-style: italic"># 20 intervals in [0, 10]</span>
dt <span style="color: #666666">=</span> t[<span style="color: #666666">1</span>] <span style="color: #666666">-</span> t[<span style="color: #666666">0</span>]
N <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(t<span style="color: #666666">.</span>size)
N[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> <span style="color: #666666">1</span>
r <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>
<span style="color: #008000; font-weight: bold">for</span> n <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">0</span>, N<span style="color: #666666">.</span>size<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>):
N[n<span style="color: #666666">+1</span>] <span style="color: #666666">=</span> N[n] <span style="color: #666666">+</span> r<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>N[n]
<span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">&#39;N[</span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121">]=</span><span style="color: #BB6688; font-weight: bold">%.1f</span><span style="color: #BA2121">&#39;</span> <span style="color: #666666">%</span> (n<span style="color: #666666">+1</span>, N[n<span style="color: #666666">+1</span>])
</pre></div>
</div>
<p>
% if FORMAT != 'ipynb':
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec33">The output </h2>
<p>
<!-- code=text typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>N[1]=1.2
N[2]=1.6
N[3]=2.0
N[4]=2.4
N[5]=3.1
N[6]=3.8
N[7]=4.8
N[8]=6.0
N[9]=7.5
N[10]=9.3
N[11]=11.6
N[12]=14.6
N[13]=18.2
N[14]=22.7
N[15]=28.4
N[16]=35.5
N[17]=44.4
N[18]=55.5
N[19]=69.4
N[20]=86.7
</pre></div>
<p>
% endif
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec34">Parameter estimation </h2>
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<ul>
<li> We do not know \( r \)</li>
<li> How can we estimate \( r \) from data?</li>
</ul>
We can use the difference equation with the experimental data
$$ N^{n+1} = N^n + r\Delta t N^n$$
Say \( N^{n+1} \) and \( N^n \) are known from data, solve wrt \( r \):
$$ r = \frac{N^{n+1}-N^n}{N^n\Delta t} $$
<p>
Use experimental data in the fraction, say \( t_1=600 \), \( t_2=1200 \),
\( N^1=140 \), \( N^2=250 \): \( r=0.0013 \).
</div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec35">A program relevant for the biological problem </h2>
<p>
<!-- exact r = 0.000694 -->
<p>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
<p>
<!-- code=python (!bc pypro) 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: #408080; font-style: italic"># Estimate r</span>
data <span style="color: #666666">=</span> np<span style="color: #666666">.</span>loadtxt(<span style="color: #BA2121">&#39;ecoli.csv&#39;</span>, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">&#39;,&#39;</span>)
t_e <span style="color: #666666">=</span> data[:,<span style="color: #666666">0</span>]
N_e <span style="color: #666666">=</span> data[:,<span style="color: #666666">1</span>]
i <span style="color: #666666">=</span> <span style="color: #666666">2</span> <span style="color: #408080; font-style: italic"># Data point (i,i+1) used to estimate r</span>
r <span style="color: #666666">=</span> (N_e[i<span style="color: #666666">+1</span>] <span style="color: #666666">-</span> N_e[i])<span style="color: #666666">/</span>(N_e[i]<span style="color: #666666">*</span>(t_e[i<span style="color: #666666">+1</span>] <span style="color: #666666">-</span> t_e[i]))
<span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">&#39;Estimated r=</span><span style="color: #BB6688; font-weight: bold">%.5f</span><span style="color: #BA2121">&#39;</span> <span style="color: #666666">%</span> r
<span style="color: #408080; font-style: italic"># Can experiment with r values and see if the model can</span>
<span style="color: #408080; font-style: italic"># match the data better</span>
T <span style="color: #666666">=</span> <span style="color: #666666">1200</span> <span style="color: #408080; font-style: italic"># cell can divide after T sec</span>
t_max <span style="color: #666666">=</span> <span style="color: #666666">5*</span>T <span style="color: #408080; font-style: italic"># 5 generations in experiment</span>
t <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, t_max, <span style="color: #666666">1000</span>)
dt <span style="color: #666666">=</span> t[<span style="color: #666666">1</span>] <span style="color: #666666">-</span> t[<span style="color: #666666">0</span>]
N <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(t<span style="color: #666666">.</span>size)
N[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> <span style="color: #666666">100</span>
<span style="color: #008000; font-weight: bold">for</span> n <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">0</span>, <span style="color: #008000">len</span>(t)<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>):
N[n<span style="color: #666666">+1</span>] <span style="color: #666666">=</span> N[n] <span style="color: #666666">+</span> r<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>N[n]
<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>
plt<span style="color: #666666">.</span>plot(t, N, <span style="color: #BA2121">&#39;r-&#39;</span>, t_e, N_e, <span style="color: #BA2121">&#39;bo&#39;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;time [s]&#39;</span>); plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;N&#39;</span>)
plt<span style="color: #666666">.</span>legend([<span style="color: #BA2121">&#39;model&#39;</span>, <span style="color: #BA2121">&#39;experiment&#39;</span>], loc<span style="color: #666666">=</span><span style="color: #BA2121">&#39;upper left&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
Change <code>r</code> in the program and play around to make a better fit!
</div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec36">Simulating financial transcations </h2>
<p>
The aim here is to simulate financial transactions among financial agents
using Monte Carlo methods. The final goal is to extract a distribution of income as function
of the income \( m \). From Pareto's work (<a href="http://www.institutcoppet.org/2012/05/08/cours-deconomie-politique-1896-de-vilfredo-pareto" target="_blank">V.&nbsp;Pareto, 1897</a>) it is known from empirical studies
that the higher end of the distribution of money follows a distribution
$$
w_m\propto m^{-1-\alpha},
$$
with \( \alpha\in [1,2] \). We will here follow the analysis made by <a href="http://www.sciencedirect.com/science/article/pii/S0378437104004327" target="_blank">Patriarca and collaborators</a>.
<p>
Here we will study numerically the relation between the micro-dynamic relations among financial
agents and the resulting macroscopic money distribution.
<p>
We assume we have \( N \) agents that exchange money in pairs \( (i,j) \). We assume also that all agents
start with the same amount of money \( m_0 > 0 \). At a given 'time step', we choose randomly a pair
of agents \( (i,j) \) and let a transaction take place. This means that agent \( i \)'s money \( m_i \) changes
to \( m_i' \) and similarly we have \( m_j\rightarrow m_j' \).
Money is conserved during a transaction, meaning that
$$
\begin{equation}
m_i+m_j=m_i'+m_j'.
\label{eq:conserve}
\end{equation}
$$
The change is done via a random reassignement (a random number) \( \epsilon \), meaning that
$$
\begin{equation*}
m_i' = \epsilon(m_i+m_j),
\end{equation*}
$$
leading to
$$
\begin{equation*}
m_j'= (1-\epsilon)(m_i+m_j).
\end{equation*}
$$
The number \( \epsilon \) is extracted from a uniform distribution.
In this simple model, no agents are left with a debt, that is \( m\ge 0 \).
Due to the conservation law above, one can show that the system relaxes toward an equilibrium
state given by a Gibbs distribution
$$
\begin{equation*}
w_m=\beta \exp{(-\beta m)},
\end{equation*}
$$
with
$$
\begin{equation*}
\beta = \frac{1}{\langle m\rangle},
\end{equation*}
$$
and \( \langle m\rangle=\sum_i m_i/N=m_0 \), the average money.
It means that after equilibrium has been reached that the majority of agents is left with a small
number of money, while the number of richest agents, those with \( m \) larger than a specific value \( m' \),
exponentially decreases with \( m' \).
<p>
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="___sec37">Simulation of Transactions </h3>
Our task is to first set up an algorithm which simulates the above transactions with an initial
amount \( m_0 \).
The challenge here is to figure out a Monte Carlo simulation based on the
above equations.
You will in particular need to make an algorithm which sets up a histogram as function of \( m \).
This histogram contains the number of times a value \( m \) is registered and represents
\( w_m\Delta m \). You will need to set up a value for the interval \( \Delta m \) (typically \( 0.01-0.05 \)).
That means you need to account for the number of times you register an income in the interval
\( m,m+\Delta m \). The number of times you register this income, represents the value that enters the histogram.
You will also need to find a criterion for when the equilibrium situation has been reached.
<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">#!/usr/bin/env python</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.mlab</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">mlab</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">import</span> <span style="color: #0000FF; font-weight: bold">random</span>
<span style="color: #408080; font-style: italic"># initialize the rng with a seed</span>
random<span style="color: #666666">.</span>seed()
<span style="color: #408080; font-style: italic"># Hard coding of input parameters</span>
Agents <span style="color: #666666">=</span> <span style="color: #666666">500</span>
MCcounts <span style="color: #666666">=</span> <span style="color: #666666">1000</span>
Transactions <span style="color: #666666">=</span> <span style="color: #666666">100000</span>
startMoney <span style="color: #666666">=</span> <span style="color: #666666">1.0</span>
Lambda <span style="color: #666666">=</span> <span style="color: #666666">0.0</span>
FinancialAgents <span style="color: #666666">=</span> startMoney<span style="color: #666666">*</span>np<span style="color: #666666">.</span>ones(Agents)
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span> (<span style="color: #666666">1</span>, MCcounts, <span style="color: #666666">1</span>):
<span style="color: #008000; font-weight: bold">for</span> j <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span> (<span style="color: #666666">1</span>, Transactions, <span style="color: #666666">1</span>):
agent_i <span style="color: #666666">=</span> <span style="color: #008000">int</span>(Agents<span style="color: #666666">*</span>random<span style="color: #666666">.</span>random())
agent_j <span style="color: #666666">=</span> <span style="color: #008000">int</span>(Agents<span style="color: #666666">*</span>random<span style="color: #666666">.</span>random())
epsilon <span style="color: #666666">=</span> random<span style="color: #666666">.</span>random()
<span style="color: #008000; font-weight: bold">if</span> agent_i <span style="color: #666666">!=</span> agent_j:
m1 <span style="color: #666666">=</span> Lambda<span style="color: #666666">*</span>FinancialAgents[agent_i] <span style="color: #666666">+</span> (<span style="color: #666666">1-</span>Lambda)<span style="color: #666666">*</span>epsilon<span style="color: #666666">*</span>(FinancialAgents[agent_i] <span style="color: #666666">+</span> FinancialAgents[agent_j])
m2 <span style="color: #666666">=</span> Lambda<span style="color: #666666">*</span>FinancialAgents[agent_j] <span style="color: #666666">+</span> (<span style="color: #666666">1-</span>Lambda)<span style="color: #666666">*</span>(<span style="color: #666666">1-</span>epsilon)<span style="color: #666666">*</span>(FinancialAgents[agent_i] <span style="color: #666666">+</span> FinancialAgents[agent_j])
FinancialAgents[agent_i] <span style="color: #666666">=</span> m1
FinancialAgents[agent_j] <span style="color: #666666">=</span> m2
<span style="color: #408080; font-style: italic"># the histogram of the data</span>
n, bins, patches <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>hist(FinancialAgents, <span style="color: #666666">50</span>, facecolor<span style="color: #666666">=</span><span style="color: #BA2121">&#39;green&#39;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;$x$&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Distribution of wealth&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&#39;Money&#39;</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">0</span>, <span style="color: #666666">10</span>, <span style="color: #666666">0</span>, <span style="color: #666666">500</span>])
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
We can then change our model to allow for a saving criterion, meaning that the agents save
a fraction \( \lambda \) of the money they have before the transaction is made. The final distribution will then no longer be given by Gibbs distribution. It could also include a taxation on financial transactions.
<p>
The conservation law of Eq. \eqref{eq:conserve} holds, but the money to be shared in a transaction between
agent \( i \) and agent \( j \) is now \( (1-\lambda)(m_i+m_j) \). This means that we have
$$
\begin{equation*}
m_i' = \lambda m_i+\epsilon(1-\lambda)(m_i+m_j),
\end{equation*}
$$
and
$$
\begin{equation*}
m_j' = \lambda m_j+(1-\epsilon)(1-\lambda)(m_i+m_j),
\end{equation*}
$$
which can be written as
$$
\begin{equation*}
m_i'=m_i+\delta m
\end{equation*}
$$
and
$$
\begin{equation*}
m_j'=m_j-\delta m,
\end{equation*}
$$
with
$$
\begin{equation*}
\delta m=(1-\lambda)(\epsilon m_j-(1-\epsilon)m_i),
\end{equation*}
$$
showing how money is conserved during a transaction.
Select values of \( \lambda =0.25,0.5 \) and \( \lambda=0.9 \) and try to extract the corresponding
equilibrium distributions and compare these with the Gibbs distribution. Comment your results.
Extract a parametrization of the above curves, see for example <a href="http://www.sciencedirect.com/science/article/pii/S0378437104004327" target="_blank">Patriarca and collaborators</a> and see if you can parametrize the high-end tails of the distributions in terms of power laws. Comment your results.
<p>
In the studies above the agents were selected randomly, irrespective of whether we allowed for
saving or not during a transaction. What is often observed is that various agents tend to make preferences for for whom to interact with. We will now study the evolution of the distribution of wealth \( w_m \) by assuming that there is a likelihood
$$
p_{ij} \propto \vert m_i-m_j\vert^{-\alpha},
$$
for an interaction between agents \( i \) and \( j \) with respective wealths \( m_i \) and \( m_j \). The parameter \( \alpha > 0 \). For \( \alpha=0 \) we recover our model from part 5a).
Perform the same analysis as previously with \( N=500 \) as well as with \( N=1000 \) agents and study the distribution of wealth for \( \alpha =0.5 \), \( \alpha =1.0 \), \( \alpha =1.5 \) and \( \alpha =2.0 \).
You should try to reproduce Figure 1 of <a href="http://www.sciencedirect.com/science/article/pii/S0378437114006967" target="_blank">Goswami and Sen</a>.
Extract the tail of the distribution and see if it follows a Pareto distribution
$$
w_m\propto m^{-1-\alpha}.
$$
What happens if \( \alpha \gg 1 \)?
<p>
Perform the analysis with and without a saving \( \lambda \) on each transaction and comment your results.
We add to the previous probability the possibility that two agents who interact have performed similar transactions earlier. That is, in addition to being financially close, we assume that the likelihood for interacting increases if two agents have interacted earlier.
We add this feature by modifying the previous likelihood to
$$
p_{ij} \propto \vert m_i-m_j\vert^{-\alpha}\left(c_{ij}+1\right)^{\gamma},
$$
where \( c_{ij} \) represents the number of previous interactions that have taken place between \( i \) and \( j \). The factor \( 1 \) is added in order to ensure that if they have not interacted earlier they can still interact. Perform similar studies as above with \( N=1000 \), \( \alpha=1.0 \) and \( \alpha=2.0 \) using \( \gamma = 0.0, 1.0, 2.0, 3.0 \) and \( 4.0 \). Plot the wealth distributions for these cases and try to extract eventual power law tails with and without a saving \( \lambda \) in each transaction. Comment your results and compare them with figures 5 and 6 of <a href="http://www.sciencedirect.com/science/article/pii/S0378437114006967" target="_blank">Goswami and Sen</a>.
<!-- ------------------- end of main content --------------- -->
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2017, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
<!-- copyright --> &copy; 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
</center>
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
+33 -11
View File
@@ -73,13 +73,30 @@ Automatically generated HTML file from DocOnce source
2,
None,
'___sec10'),
('The $\\chi^2$ function', 2, None, '___sec11'),
('The $\\chi^2$ function', 2, None, '___sec12'),
('The $\\chi^2$ function', 2, None, '___sec13'),
('Simple regression model', 2, None, '___sec11'),
('Simple regression model, now using _scikit-learn_',
2,
None,
'___sec12'),
('Correlations and the quality of our results',
2,
None,
'___sec13'),
('The $\\chi^2$ function', 2, None, '___sec14'),
('The $\\chi^2$ function', 2, None, '___sec15'),
('The $\\chi^2$ function', 2, None, '___sec16'),
('The singular value decompostion', 2, None, '___sec17')]}
('The $\\chi^2$ function', 2, None, '___sec17'),
('The $\\chi^2$ function', 2, None, '___sec18'),
('The $\\chi^2$ function', 2, None, '___sec19'),
('Simple regression model with gradient descent',
2,
None,
'___sec20'),
('Simple regression model with stochastic gradient descent',
2,
None,
'___sec21'),
('The singular value decompostion', 2, None, '___sec22')]}
end of tocinfo -->
<body>
@@ -128,13 +145,18 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._Regression-bs009.html#___sec8" style="font-size: 80%;">Interpretations and optimizing our parameters</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs010.html#___sec9" style="font-size: 80%;">Interpretations and optimizing our parameters</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs011.html#___sec10" style="font-size: 80%;">Interpretations and optimizing our parameters</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs012.html#___sec11" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs013.html#___sec12" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs014.html#___sec13" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs012.html#___sec11" style="font-size: 80%;">Simple regression model</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs013.html#___sec12" style="font-size: 80%;">Simple regression model, now using <b>scikit-learn</b></a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs014.html#___sec13" style="font-size: 80%;">Correlations and the quality of our results</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs015.html#___sec14" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs016.html#___sec15" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs017.html#___sec16" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs018.html#___sec17" style="font-size: 80%;">The singular value decompostion</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs018.html#___sec17" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs019.html#___sec18" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs020.html#___sec19" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs021.html#___sec20" style="font-size: 80%;">Simple regression model with gradient descent</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs022.html#___sec21" style="font-size: 80%;">Simple regression model with stochastic gradient descent</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs023.html#___sec22" style="font-size: 80%;">The singular value decompostion</a></li>
</ul>
</li>
@@ -169,7 +191,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>Nov 26, 2017</h4></center> <!-- date -->
<center><h4>May 14, 2018</h4></center> <!-- date -->
<br>
<p>
@@ -193,7 +215,7 @@ MathJax.Hub.Config({
<li><a href="._Regression-bs008.html">9</a></li>
<li><a href="._Regression-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._Regression-bs018.html">19</a></li>
<li><a href="._Regression-bs023.html">24</a></li>
<li><a href="._Regression-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -211,7 +233,7 @@ MathJax.Hub.Config({
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2017, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
<!-- copyright --> &copy; 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
</center>
@@ -73,13 +73,30 @@ Automatically generated HTML file from DocOnce source
2,
None,
'___sec10'),
('The $\\chi^2$ function', 2, None, '___sec11'),
('The $\\chi^2$ function', 2, None, '___sec12'),
('The $\\chi^2$ function', 2, None, '___sec13'),
('Simple regression model', 2, None, '___sec11'),
('Simple regression model, now using _scikit-learn_',
2,
None,
'___sec12'),
('Correlations and the quality of our results',
2,
None,
'___sec13'),
('The $\\chi^2$ function', 2, None, '___sec14'),
('The $\\chi^2$ function', 2, None, '___sec15'),
('The $\\chi^2$ function', 2, None, '___sec16'),
('The singular value decompostion', 2, None, '___sec17')]}
('The $\\chi^2$ function', 2, None, '___sec17'),
('The $\\chi^2$ function', 2, None, '___sec18'),
('The $\\chi^2$ function', 2, None, '___sec19'),
('Simple regression model with gradient descent',
2,
None,
'___sec20'),
('Simple regression model with stochastic gradient descent',
2,
None,
'___sec21'),
('The singular value decompostion', 2, None, '___sec22')]}
end of tocinfo -->
<body>
@@ -128,13 +145,18 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._Regression-bs009.html#___sec8" style="font-size: 80%;">Interpretations and optimizing our parameters</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs010.html#___sec9" style="font-size: 80%;">Interpretations and optimizing our parameters</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs011.html#___sec10" style="font-size: 80%;">Interpretations and optimizing our parameters</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs012.html#___sec11" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs013.html#___sec12" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs014.html#___sec13" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs012.html#___sec11" style="font-size: 80%;">Simple regression model</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs013.html#___sec12" style="font-size: 80%;">Simple regression model, now using <b>scikit-learn</b></a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs014.html#___sec13" style="font-size: 80%;">Correlations and the quality of our results</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs015.html#___sec14" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs016.html#___sec15" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs017.html#___sec16" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs018.html#___sec17" style="font-size: 80%;">The singular value decompostion</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs018.html#___sec17" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs019.html#___sec18" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs020.html#___sec19" style="font-size: 80%;">The \( \chi^2 \) function</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs021.html#___sec20" style="font-size: 80%;">Simple regression model with gradient descent</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs022.html#___sec21" style="font-size: 80%;">Simple regression model with stochastic gradient descent</a></li>
<!-- navigation toc: --> <li><a href="._Regression-bs023.html#___sec22" style="font-size: 80%;">The singular value decompostion</a></li>
</ul>
</li>
@@ -190,7 +212,7 @@ A regression model aims at finding a likelihood function \( p(y\vert \hat{x}) \)
<li><a href="._Regression-bs009.html">10</a></li>
<li><a href="._Regression-bs010.html">11</a></li>
<li><a href="">...</a></li>
<li><a href="._Regression-bs018.html">19</a></li>
<li><a href="._Regression-bs023.html">24</a></li>
<li><a href="._Regression-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->

Some files were not shown because too many files have changed in this diff Show More