List if a distribution is continuous or discrete, its support, and an example of a random variable which follows the distribution. 1 Bonus point per problem for identifying a rv related to chemical engineering.
1.1 Discrete, $[1,\infty)$, the number of collisions until two molecules react
1.2 Discrete, $[0, N]$ or $[0,\infty]$, the number of accidents at a refinery
1.3 Discrete, $[0, N]$, the number of failures of a metal at a certain temperature
1.4 Continuous, $(0,\infty)$, the amount of time between between incoming crude oil shipments
1.5 Discrete, $\{0,1\}$, the open/close state of a valve
Using this sentence: "The quick brown fox jumps over the lazy dog", Create slices of the array to answer the following questions. Answer in Python
In [2]:
string='The quick brown fox jumps over the lazy dog'
print 'The first element is "{}"' .format(string[0])
In [3]:
print 'The sentence without the the last element is: ', string[:-1]
In [4]:
print string[0:5]
In [5]:
s=len(string)
print s
print '{:s}'.format(string[0:(s/2)])
k=len(string[0:((s)/2)])
print k
In [6]:
s=len(string)
print s
print string[((s)/2):]
l=len(string[((s)/2):])
print l
In [4]:
print string[3::3]
for
loop, sum all the elements in the array and print the sum. Check your answer using the numpy sum
function.from scipy.special import factorial
. Note: the word lambda
is a reserved word in python, meaning you cannot use it for a variable name. You can tell if a variable is reserved because it is a different color/font than other variables in ipython. Also, when applying mathematical functions to numpy arrays, you must use the numpy versions, for example numpy.cos
instead of math.cos
for
loop and the array from Question 3.3, compute the expected value of the exponential distribution. To evaluate the integral, use $\int f(x)dx = \sum_i f(x_i) \Delta x$, where $\Delta x$ is the spacing between your points and $f(x_i)$ is the function evaluated at point $x_i$. As you know, it should be $1/3$.sum
function from numpy, compute the expected value without a for
loop.
In [8]:
import numpy as np
x = np.arange(0,30, 0.02)
print x
In [9]:
xsum = 0
for element in x:
xsum += element
print xsum, np.sum(x)
In [10]:
from scipy.special import factorial
lamb = 3.0
y = np.exp(-lamb * x) * lamb
print y
In [11]:
expected_value = 0
for element in x:
expected_value += element * np.exp(-lamb * element) * lamb * 0.02
print expected_value, 1 / 3.
In [12]:
expected_value = np.sum(x * y * 0.02)
print expected_value, 1. / 3
In [16]:
#create the sample space
Q = np.arange(2,13)
#probability of a roll
P_n = (6. - abs(Q - 7)) / 36
#sum of n * P_n to get E[n]
expected_value = np.sum(Q * P_n)
#sum of n^2 * P_n to get E[n^2]
expected_value_2 = np.sum(Q**2 * P_n)
#variance via E[n^2] - E[n]^2
variance = expected_value_2 - expected_value ** 2
print 'E[x] = {}, Var(x) = {}'.format(expected_value, variance)
In [9]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn
seaborn.set_context("talk")
x = np.linspace(0, 4, 250)
y = 2.0 * np.exp(-2.0 * x)
plt.plot(x,y)
plt.show()
In [10]:
n=np.arange(1, 9)
p=0.4
pgeo=(1-p)**(n-1)*p
plt.plot(n, pgeo, 'yo-')
plt.show()
4.3 Plot the binomial distribution with $N=10$, $p=0.25$.
In [11]:
from scipy.special import comb
p = 0.25
N = 10
h=np.arange(N+1)
L=comb(N, h)
pbi=L*p**h*(1-p)**(N-h)
plt.plot(h, pbi, 'ro-')
plt.show()
In [15]:
N = 150
p = 0.5
h=np.arange(N+1)
L=comb(N, h)
pbi=L*p**h*(1-p)**(N-h)
plt.plot(h, pbi, 'go-')
plt.show()
In [ ]: