Create a plot of $\sin(2x)$ and $\sin(x)$ from $0$ to $2\pi$ with the following properties:
In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from math import pi
import seaborn
seaborn.set_context("talk")
x = np.linspace(0, 2*pi, 250)
y1 = np.sin(x)
y2 = np.sin(2*x)
plt.xlim(0, 2*pi)
plt.xlabel('$x$')
plt.ylabel('$sine$ $wave$')
plt.plot(x, y1, label='$sin(x)$')
lines1 = plt.plot(x, y2, label='$sin(2x)$')
# A backslace, \, allows you to wrap a line
plt.vlines(x=pi, ymin=-1.5, ymax=1.5, label='$x=\pi$', color=lines1[0].get_color())
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.title(u'Me Gusta el Pitón!!!')
plt.legend(loc='lower left')
plt.show()
Make sure you have downloaded the Excel spreadsheet for this week's homework. 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.
for
loop, calculate the sample mean of Power of the fuel cell.mean
function, calculate the sample mean of the Power of the fuel cell.for
loop to calculate the standard deviation of the Power of the fuel cell.var
function, calculate the standard deviation of the Power of the fuel cell.
In [2]:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set_context("poster")
#load the fuel cell data
fuel_data = pd.read_excel('fuel_cell.xlsx')
#show the columns
for i in fuel_data.columns:
print '\'{}\''.format(i)
In [4]:
#This is how you access data
print fuel_data['Time']
In [5]:
#Ex2.1
sum=0
power=fuel_data['Power']
totalnumber=len(power)
for i in power:
sum+=i
print sum/totalnumber, sum/676
In [6]:
#Ex 2.2
mu=np.mean(power)
print mu
In [8]:
# Ex 2.3
import math
sume=0
for i in power:
sume+=(i-mu)**2
print math.sqrt(sume/(totalnumber-1))
print math.sqrt(np.var(power, ddof=1))
In [8]:
#Ex 2.4
var= np.var(power)
print math.sqrt(var)
2.3 divides by N and 2.4 divides by $(N-1)$
Write what quantity the question asks for symbolically, write the equation you need to compute symbolically (if necessary) and compute your answer in Python.
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?
What is the probability you return to your homework after exactly 5 videos?
What is the expected number of videos you will watch? You may use any method to compute this.
In [9]:
n=1
p=0.25
P=(1-p)**(n-1)*p
print P
In [10]:
n=5
p=0.25
P=(1-p)**(n-1)*p
print P
$E(N=n)=\displaystyle\sum\limits_{n}^N nP(N=n)$.
For geometric we know $E[n] = 1 / p$.
In [11]:
#Print the analytical expected value.
p=0.25
E=1/p
print E
#Compute using numpy
n = np.arange(1,100)
exp_n = n * (1 - p)**(n-1) * p
E = np.sum(exp_n)
print E
Answer the following problems in Python
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.for
loop. It should be very close to 1.for
loop from 4.3 and print the number of videos when the sum is 0.95. What does that represent?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.
In [12]:
import numpy as np
some_ints = np.arange(1,101) ** 2
print some_ints
In [13]:
#4.2
sqsum=0 # sum of the squared integers
for i in some_ints:
sqsum += i
if(sqsum > 1000):
break
print '{} was an integer that gives a sum greater than 1000. The sum finished with a value of {}'.format(i, sqsum)
In [14]:
#4.3
#create domain
n=np.arange(1, 101)
#set parameters
p=0.25
#initialize sum
Psum = 0
for ni in n:
Psum += (1-p)**(ni-1)*p #add to Psum
print "{:e}is the probability of watching {} or less videos.".format(Psum, ni)
In [15]:
#4.4
#create domain
n=np.arange(1, 101)
#set parameters
p=0.25
#initialize sum
Psum = 0
for ni in n:
Psum += (1-p)**(ni-1)*p #add to Psum
if (Psum>0.95):
break
print "When {} videos are watched, the probability is greater than 0.95" .format(ni)
In [16]:
def geometric_trial(p, confidence=0.05):
#Takes in a probability of success and confidence level
#Returns the maxmium number of trials which will occur according
#to the confidence level.
n = 1
psum = 0
while psum < 1 - confidence:
psum += (1 - p) ** (n - 1) * p
n += 1
return n - 1 #Note: Since we added 1 before our check in the while loop, we must subtract one
print geometric_trial(0.25)
print geometric_trial(0.25, .1)
In [17]:
from math import pi, sqrt
dx = 0.000025
T = np.arange(-5, 1, dx)
sigma = 2.5
mu = 5
prob = np.sum( 1 / (sigma * sqrt(2 * pi) ) * np.exp(-(T - mu)**2 / (2 * sigma ** 2)) ) * dx
print "P(-5 < x < 1)=", prob
Write what quantity the question asks for symbolically, write the equation you need to compute symbolically and compute your answer in Python.
In [18]:
from math import exp, factorial
p=0.001# probability of the laugh
N=500 # number of tweets
x=3 # number of times one laughs
mu = N * p
#P(x=3)?
P3=exp(-mu)*(mu)**x / factorial(x)
print "The probability of laughing {} times is {:%}" .format(x, P3)
In [19]:
Psum = exp(-mu)*(mu)**1 / factorial(1) + exp(-mu)*(mu)**2 / factorial(2)
print "The probability of laughing less than three time is: {:%}" .format(Psum)
In [20]:
Pmore=(1-Psum-P3)
print "The probability of laughing more than three times is: {:%}" .format(Pmore)
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.
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.5 you might want to try it with a for loop.
To better understand your dating agenda, make a plot of the probability of the number of second dates.
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.
$P(x;N,p) = {N \choose x} p^{x}(1−p)^{N−x}$
$p=0.10$ - probability of the second date
$N=8$ number of dates per first week
$P(x=3)= {8 \choose 3} 0.1^{3}(1−0.1)^{8−3}$
In [21]:
from scipy.special import comb
pbnm=0.1
N=8
x=3
P3=comb(N,x)*pbnm**x*(1-pbnm)**(N-x)
print "The probability of going to {} second dates is {:%}".format(x, P3)
In [22]:
dates=np.arange(4, 9)
pbnm=0.1
N=8
P_3_more = 0
for di in dates:
P_3_more += comb(N,di)*pbnm**di*(1-pbnm)**(N-di)
print "The probability of having more than three dates is", P_3_more
In [23]:
dates=np.arange(0, 9)
P3more=comb(N, dates)*pbnm**dates*(1-pbnm)**(N-dates)
plt.plot(dates, P3more, 'yo-')
plt.title("Second Dates")
plt.show()
In [24]:
dates=np.arange(0, 9)
En = 0
for di in dates:
En += di * comb(N,di)*pbnm**di*(1-pbnm)**(N-di)
analytical= N * pbnm
print "The numerical {} and analytical, {} expected values are the same!" .format(En, analytical)
In [24]: