Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
518 changes: 518 additions & 0 deletions Exams/Final/Final_answers.ipynb

Large diffs are not rendered by default.

596 changes: 596 additions & 0 deletions Exams/Mid-term/Exam_Answers.ipynb

Large diffs are not rendered by default.

1,047 changes: 1,047 additions & 0 deletions Labs/Lab-1/Copy_of_Lab_1.ipynb

Large diffs are not rendered by default.

1,101 changes: 1,101 additions & 0 deletions Labs/Lab-2/Lab_2_Solutions.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Labs/Lab-3/Lab-3-Solutions.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Labs/Lab-4/Lab-4-Solutions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nbformat":4,"nbformat_minor":0,"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.8.1"},"colab":{"name":"Lab-4-Solutions.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-4/Lab-4.ipynb","timestamp":1583511217256}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"tF8VIBcwn1iH","colab_type":"text"},"source":["## Lab 4\n","\n","[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-4/Lab-4.ipynb)\n","\n","In this lab we will become familiar with distributions, histograms, and functional programming. \n","\n","\n","### Uniform Distribution\n","Lets start with generating some fake random data. You can get a random number between 0 and 1 using the python random module as follow:"]},{"cell_type":"code","metadata":{"id":"yEQxT5NGn1iL","colab_type":"code","outputId":"c066dae8-852f-470a-eb42-ec1f21e1ef63","colab":{"base_uri":"https://localhost:8080/","height":34},"executionInfo":{"status":"ok","timestamp":1583511245546,"user_tz":360,"elapsed":525,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["import random\n","x=random.random()\n","print(\"The Value of x is\", x)"],"execution_count":1,"outputs":[{"output_type":"stream","text":["The Value of x is 0.4049658004556974\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"RTW5hUCrn1iQ","colab_type":"text"},"source":["Everytime you call random, you will get a new number.\n","\n","*Exercise 1:* Using random, write a function `generate_uniform(N, mymin, mymax)`, that returns a python list containing N random numbers between specified minimum and maximum value. Note that you may want to quickly work out on paper how to turn numbers between 0 and 1 to between other values. "]},{"cell_type":"code","metadata":{"id":"TGEvDfVUn1iR","colab_type":"code","colab":{}},"source":["# Skeleton\n","def generate_uniform(N,x_min,x_max):\n"," out = []\n"," for i in range (0,N):\n"," x=random.randint(x_min,x_max)\n"," out.append(x)\n"," return out"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"RsfIGDdAn1iT","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":102},"outputId":"6da67292-61f4-4941-8b3d-a78060821736","executionInfo":{"status":"ok","timestamp":1583511495011,"user_tz":360,"elapsed":326,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["# Test your solution here\n","data=generate_uniform(1000,-10,10)\n","print (\"Data Type:\", type(data))\n","print (\"Data Length:\", len(data))\n","if len(data)>0: \n"," print (\"Type of Data Contents:\", type(data[0]))\n"," print (\"Data Minimum:\", min(data))\n"," print (\"Data Maximum:\", max(data))"],"execution_count":4,"outputs":[{"output_type":"stream","text":["Data Type: <class 'list'>\n","Data Length: 1000\n","Type of Data Contents: <class 'int'>\n","Data Minimum: -10\n","Data Maximum: 10\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"KX-GmK5In1iW","colab_type":"text"},"source":["*Exercise 2a:* \n","Write a function that computes the mean of values in a list."]},{"cell_type":"code","metadata":{"id":"YkSzFEkvn1iX","colab_type":"code","colab":{}},"source":["# Skeleton\n","def mean(Data):\n"," m=0.\n"," m=sum(Data)/len(Data)\n"," \n"," return m"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"HAxcYUUdn1ib","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"d8b554c7-2100-4e52-803d-7bf913a42c1a","executionInfo":{"status":"ok","timestamp":1583511667561,"user_tz":360,"elapsed":337,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["# Test your solution here\n","print (\"Mean of Data:\", mean(data))"],"execution_count":6,"outputs":[{"output_type":"stream","text":["Mean of Data: 0.033\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"M3kUsJGWn1ie","colab_type":"text"},"source":["*Exercise 2b:* \n","Write a function that computes the variance of values in a list."]},{"cell_type":"code","metadata":{"id":"P1solaF7n1if","colab_type":"code","colab":{}},"source":["# Skeleton\n","import statistics\n","def variance(Data):\n"," m=0.\n"," m=statistics.variance(data)\n","\n"," return m"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"G1XBcmjan1ih","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"aa7b8001-105c-4d8a-e064-99990a400fe9","executionInfo":{"status":"ok","timestamp":1583511985131,"user_tz":360,"elapsed":228,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["# Test your solution here\n","print (\"Variance of Data:\", variance(data))"],"execution_count":12,"outputs":[{"output_type":"stream","text":["Variance of Data: 35.51542642642642\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"IirsOIVWn1ik","colab_type":"text"},"source":["## Histogramming"]},{"cell_type":"markdown","metadata":{"id":"JjsvPalGn1il","colab_type":"text"},"source":["*Exercise 3:* Write a function that bins the data so that you can create a histogram. An example of how to implement histogramming is the following logic:\n","\n","* User inputs a list of values `x` and optionally `n_bins` which defaults to 10.\n","* If not supplied, find the minimum and maximum (`x_min`,`x_max`) of the values in x.\n","* Determine the bin size (`bin_size`) by dividing the range of the function by the number of bins.\n","* Create an empty list of zeros of size `n_bins`, call it `hist`.\n","* Loop over the values in `x`\n"," * Loop over the values in `hist` with index `i`:\n"," * If x is between `x_min+i*bin_size` and `x_min+(i+1)*bin_size`, increment `hist[i].` \n"," * For efficiency, try to use continue to goto the next bin and data point.\n","* Return `hist` and the list corresponding of the bin edges (i.e. of `x_min+i*bin_size`). "]},{"cell_type":"code","metadata":{"id":"xDa5AtIDn1im","colab_type":"code","colab":{}},"source":["# Solution\n","def histogram(x,n_bins=10,x_min=None,x_max=None):\n"," \n","\n","\n"," return hist,bin_edges"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"1P5HnmTIn1ip","colab_type":"code","colab":{}},"source":["# Test your solution here\n","h,b=histogram(data,100)\n","print(h)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"erSLdAecn1it","colab_type":"text"},"source":["*Exercise 4:* Write a function that uses the histogram function in the previous exercise to create a text-based \"graph\". For example the output could look like the following:\n","```\n","[ 0, 1] : ######\n","[ 1, 2] : #####\n","[ 2, 3] : ######\n","[ 3, 4] : ####\n","[ 4, 5] : ####\n","[ 5, 6] : ######\n","[ 6, 7] : #####\n","[ 7, 8] : ######\n","[ 8, 9] : ####\n","[ 9, 10] : #####\n","```\n","\n","Where each line corresponds to a bin and the number of `#`'s are proportional to the value of the data in the bin. "]},{"cell_type":"code","metadata":{"id":"3DP7KMqRn1iu","colab_type":"code","colab":{}},"source":["# Solution\n","def draw_histogram(x,n_bins,x_min=None,x_max=None,character=\"#\",max_character_per_line=20):\n"," ### BEGIN SOLUTION\n","\n"," # Fill in your solution here \n"," \n"," ### END SOLUTION\n","\n"," return hist,bin_edges"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"zkCVWu0nn1iw","colab_type":"code","colab":{}},"source":["# Test your solution here\n","h,b=histogram(data,20)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"mNxJ1cVNn1iy","colab_type":"text"},"source":["## Functional Programming\n","\n","*Exercise 5:* Write a function the applies a booling function (that returns true/false) to every element in data, and return a list of indices of elements where the result was true. Use this function to find the indices of entries greater than 0.5. "]},{"cell_type":"code","metadata":{"id":"o6LPTfm6n1i0","colab_type":"code","colab":{}},"source":["def where(mylist,myfunc):\n"," out= []\n"," \n"," \n"," \n"," return out"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"w2PiMQS6n1i2","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"XKlshea7n1i6","colab_type":"text"},"source":["*Exercise 6:* The inrange(mymin,mymax) function below returns a function that tests if it's input is between the specified values. Write corresponding functions that test:\n","* Even\n","* Odd\n","* Greater than\n","* Less than\n","* Equal\n","* Divisible by"]},{"cell_type":"code","metadata":{"id":"PgXgF8uKn1i6","colab_type":"code","colab":{}},"source":["def in_range(mymin,mymax):\n"," def testrange(x):\n"," return x<mymax and x>=mymin\n"," return testrange\n","\n","# Examples:\n","F1=inrange(0,10)\n","F2=inrange(10,20)\n","\n","# Test of in_range\n","print (F1(0), F1(1), F1(10), F1(15), F1(20))\n","print (F2(0), F2(1), F2(10), F2(15), F2(20))\n","\n","print (\"Number of Entries passing F1:\", len(where(data,F1)))\n","print (\"Number of Entries passing F2:\", len(where(data,F2)))"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"Q_HjcyH7n1i9","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"s3PLinaDn1i_","colab_type":"code","colab":{}},"source":["# Test your solution"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"ZhkwG7n1n1jC","colab_type":"text"},"source":["*Exercise 7:* Repeat the previous exercise using `lambda` and the built-in python functions sum and map instead of your solution above. "]},{"cell_type":"code","metadata":{"id":"jVIM_dcQn1jD","colab_type":"code","colab":{}},"source":["### BEGIN SOLUTION\n","\n"," # Fill in your solution here \n"," \n","### END SOLUTION"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"sV-gUy3Hn1jF","colab_type":"text"},"source":["## Monte Carlo\n","\n","*Exercise 7:* Write a \"generator\" function called `generate_function(func,x_min,x_max,N)`, that instead of generating a flat distribution, generates a distribution with functional form coded in `func`. Note that `func` will always be > 0. \n","\n","Use the test function below and your histogramming functions above to demonstrate that your generator is working properly.\n","\n","Hint: A simple, but slow, solution is to a draw random number test_x within the specified range and another number p between the min and max of the function (which you will have to determine). If p<=function(test_x), then place test_x on the output. If not, repeat the process, drawing two new numbers. Repeat until you have the specified number of generated numbers, N. For this problem, it's OK to determine the min and max by numerically sampling the function. "]},{"cell_type":"code","metadata":{"id":"uodRGGvSn1jG","colab_type":"code","colab":{}},"source":["def generate_function(func,x_min,x_max,N=1000):\n"," out = list()\n"," ### BEGIN SOLUTION\n","\n"," # Fill in your solution here \n"," \n"," ### END SOLUTION\n"," \n"," return out"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"3AcyVd4Dn1jI","colab_type":"code","colab":{}},"source":["# A test function\n","def test_func(x,a=1,b=1):\n"," return abs(a*x+b)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"8Dxz8u-3n1jK","colab_type":"text"},"source":["*Exercise 8:* Use your function to generate 1000 numbers that are normal distributed, using the `gaussian` function below. Confirm the mean and variance of the data is close to the mean and variance you specify when building the Gaussian. Histogram the data. "]},{"cell_type":"code","metadata":{"id":"imbkswPZn1jL","colab_type":"code","colab":{}},"source":["import math\n","\n","def gaussian(mean, sigma):\n"," def f(x):\n"," return math.exp(-((x-mean)**2)/(2*sigma**2))/math.sqrt(math.pi*sigma)\n"," return f\n","\n","# Example Instantiation\n","g1=gaussian(0,1)\n","g2=gaussian(10,3)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"x5ASGq12n1jN","colab_type":"text"},"source":["*Exercise 9:* Combine your `generate_function`, `where`, and `in_range` functions above to create an integrate function. Use your integrate function to show that approximately 68% of Normal distribution is within one variance."]},{"cell_type":"code","metadata":{"id":"_gLjho7Vn1jO","colab_type":"code","colab":{}},"source":["def integrate(func, x_min, x_max, n_points=1000):\n"," \n"," return integral"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"jHUyEJogn1jQ","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]}
Loading