{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Homework 4\n", "====\n", "####CHE 116: Numerical Methods and Statistics\n", "Prof. Andrew White\n", "\n", "Version 1.3 (2/9/2015)\n", "\n", "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Plotting Practice (6 Points)\n", "====\n", "\n", "Create a plot of $\\sin(2x)$ and $\\sin(x)$ from $0$ to $2\\pi$ with the following properties:\n", "\n", "1. In Seaborn in the \"Poster\" context\n", "1. LaTeX equations labeling the two curves\n", "2. A Vertical line at $\\pi$ that is the color of the first sine wave, $\\sin(2x)$\n", "4. A legend\n", "5. An $x$ and $y$ label\n", "6. A title that says \"I love Python\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Working with Data (5 Points)\n", "====\n", "Make sure you have downloaded the Excel spreadsheet for this week's homework and it is the same folder as this worksheet. The data in the spreadsheet is from a 4-plate hydrogen fuel cell. The cell below loads the data. Run the cell and then write your own python code in subsequent cells.\n", "\n", "1. Using a `for` loop, calculate the sample mean of Power of the fuel cell.\n", "2. Using the numpy `mean` function, calculate the sample mean of the Power of the fuel cell.\n", "3. Use a `for` loop to calculate the standard deviation of the Power of the fuel cell.\n", "4. Using the numpy `var` function, calculate the standard deviation of the Power of the fuel cell. \n", "5. Your answers between 2.3 and 2.4 should be slightly different. Why?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'Time'\n", "'Voltage'\n", "'Current'\n", "'Power'\n", "'Efficiency'\n", "'Resistance'\n", "'Control Sig'\n", "'Fan %'\n", "'H2 In'\n", "'H2 Out'\n", "'Cell 1'\n", "'Cell 2'\n", "'Cell 3'\n", "'Cell 4'\n", "'RH %'\n", "'Temperature'\n" ] } ], "source": [ "%matplotlib inline\n", "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import seaborn\n", "seaborn.set_context(\"poster\")\n", "\n", "#load the fuel cell data\n", "fuel_data = pd.read_excel('fuel_cell.xlsx')\n", "\n", "#show the columns\n", "for i in fuel_data.columns:\n", " print '\\'{}\\''.format(i)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 3691.651\n", "1 3693.891\n", "2 3696.715\n", "3 3699.508\n", "4 3702.460\n", "5 3705.275\n", "6 3707.923\n", "7 3710.187\n", "8 3712.795\n", "9 3715.428\n", "10 3718.260\n", "11 3721.035\n", "12 3723.707\n", "13 3725.963\n", "14 3728.203\n", "...\n", "662 5496.243\n", "663 5499.492\n", "664 5502.652\n", "665 5505.819\n", "666 5508.987\n", "667 5512.171\n", "668 5514.691\n", "669 5516.948\n", "670 5519.284\n", "671 5522.412\n", "672 5525.595\n", "673 5528.739\n", "674 5531.987\n", "675 5535.188\n", "676 5538.324\n", "Name: Time, Length: 677, dtype: float64\n" ] } ], "source": [ "#This is how you access data\n", "print fuel_data['Time']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Watching Youtube with the Geometric Distribution (3 Points)\n", "====\n", "\n", "Write what quantity the question asks for symbolically, write the equation you need to compute symbolically (if necessary) and compute your answer in Python. \n", "\n", "1. You accidentally open youtube while doing your homework. After watching one video, you find another interesting video you must watch with probability 75%. What is the probability you return to your homework after 1 video?\n", "\n", "2. What is the probability you return to your homework after exactly 5 videos?\n", "\n", "3. What is the expected number of videos you will watch? You may use any method to compute this." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Python Practice (10 Points)\n", "====\n", "Answer the following problems in Python\n", "\n", "1. Create an array containing the integers from 1 to 100 squared. Note: do not include 0.\n", "2. Using a `for` loop, sum the squared integers and `break` if your sum is greater than 1000. Print the integer which causes your sum to exceed 1000.\n", "3. Take the geometric distribution from Problem 3 and sum the probability of from 1 to 100 videos using a `for` loop. It should be 1 out to many decimal places.\n", "4. Add a break to your `for` loop from 4.3 and print the number of videos when the sum is greater 0.95. What does that represent?\n", "5. [3 Points] Turn your `for` loop into `while` loop, so that you do not need to guess how big your array will need to be and turn it into a function. Your function should take two arguments: the probability of success and the confidence level (default 5%) for a geometric distribution. It should return a number of trials for which there is $>=$ 95% probability of success before reaching that trial number. *Hint: Test your function with problem 4.4*\n", "7. [3 Points] Given a normal distribution with $\\mu=5$, $\\sigma = 2.5$, what is $P(-5 < x < 1)$?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Laughing at Tweets with Poisson Distribution (3 Points)\n", "====\n", "Write what quantity the question asks for symbolically, write the equation you need to compute symbolically and compute your answer in Python.\n", "\n", "1. The probability of laughing at a tweet is 0.1%. If you browse 500 tweets, what's the probability that you laugh three times?\n", "2. What is the probability you laugh less than three times?\n", "3. What is the probability you laugh more than three times? *Hint: The three questions' answers should sum to 1.*\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. Weekend Dating with Binomial Distribution (4 Points)\n", "====\n", "\n", "1. The probability that you go on a second date is 10%. If you go on 8 first dates this weekend, what's the probability you'll have 3 second dates next weekend? State the distribution, its parameters and the question being asked symbolically and answer the problem in Python.\n", "\n", "2. What is the probability of having more than 3 second dates for next weekend? State the question being asked symbolically and answer the problem in Python. *Hint: consider using the `np.sum` function to make it easy, but to answer 6.4 you might want to try it with a for loop*.\n", "\n", "3. To better understand your dating agenda, make a plot of the probability of the number of second dates. It should span the entire sample space.\n", "\n", "4. Caclulate the expected number of second dates with the parameters above using a `for` loop. *Do not just use the expected value equation for the binomial distribution*.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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": 1 }