Answer the following problems in Python
for
loop, sum integers from 1 to 100 and break
if your sum is greater than 200. Print the integer which causes your sum to exceed 200.while
loop, a break statement, and your function from the previous question. You must use python tutor on this problem to receive full credit
In [18]:
import numpy as np
ints = np.arange(1,21)
pows = 2**ints
print(pows)
print(pows[9], pows[19])
In [19]:
sum = 0
for i in range(1, 100):
sum += i
if sum > 200:
print(i, sum)
break
In [20]:
from scipy.special import factorial
def fxn(n, k):
'''Computes the number of permutations of n objects in a k-length sequence.
Args:
n: The number of objects
k: The sequence length
Retunrs:
The number of permutations of fixed length.
'''
return factorial(n) / factorial(n - k)
In [24]:
cats = 1
while fxn(n=30, k=cats) < 10**6:
cats += 1
print(cats)
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 25%. Define the sample space and define success to reflect a geometric distribution.
What's the probability that you will return after watching exactly one video?
What's the probability that you will return after watching exactly two videos?
Your friend wants to know when you will get back to homework. You explain the sample space is unbounded, but you guarantee there is a 99% chance you will return to your homework after how many videos? You must use python tutor on your loop to receive full credit.
What is the expected number of videos you will watch? You may use any method to compute this.
In [25]:
0.75 * 0.25
Out[25]:
In [28]:
gsum = 0
n = 0
p = 0.75
while gsum < 0.99:
n += 1
gsum += (1 - p)**(n - 1) * p
print(n, gsum)
You will return after watching the 4th video
The per-capita (expected) income in the US is \$27,500 and its standard deviation is \$15,000. Assuming the normal distribution, answer the following questions. You may use scipy stats for all of these but you must use Z values.
In [56]:
from scipy import stats as ss
mu = 27500
sig = 15000
Z = (0 - mu) / sig
print(ss.norm.cdf(Z))
The assumption is OK, only 3% of our probability is in "impossible" values of negative numbers
In [57]:
Z = (67000 - mu) / sig
print(1 - ss.norm.cdf(Z))
The probability is 0.4%. It appears this is a bad model since income is much more spread than this.
In [64]:
ss.norm.ppf(0.99, scale=sig, loc=mu)
Out[64]:
The top 1% of earners is anyone above $62,395
We're going to write a program to discover how much to save in our retirement accounts. You are going to start with $P$ dollars, your principal. You invest in low-cost index funds which have an expected return of 5% (your principal is 105% of last years value). To live after your retirement, you withdraw \$30,000 per year. Complete the following tasks:
Write a function which takes in a principal, $P$, maturation rate of $r$, and withdrawl amount of $W$. It returns the amount of money remaining. Withdrawl occurs before maturation. Using your function, with a principal of \$250,000 and the numbers given above, how much money remains after 1 year? Call this function annual.
Write a new function called terminator which takes in $P$, $r$ and $W$. It should use your annual function and a for loop. It returns the number of years before your principal is gone and you have no more retirement money. Since you're using a for loop, you should have some upper bound. Let's use 50 for that upper bound. To test your method, you should get 11 years using the numbers from part 1.
Make a graph of principal vs number of years. You should use a for loop, not numpy, since your functions aren't built for numpy arrays. Your principals should run from \$100,000 to \$500,000.
Make your terminator (for this part only) have an upper bonud for 1000 years. You are a vampire. Make a plot from \$100,000 to \$750,000. How much money do you need in retirement as a vampire?
In [7]:
def annual(P, r=0.05, W=30000):
'''Computes the change in principal after one year
Args:
P: The principal - amount of money at the beginning of the year
r: The rate of return from principal
W: The amount withdrawn
Returns:
The new principal'''
P -= W
P *= (r + 1)
return P
def terminator(P, r=0.05, W=30000, upper_limit=50):
'''Finds the number of years before the principal is exhausted.
Args:
P: The principal - amount of money at the beginning of the year
r: The rate of return from principal
W: The amount withdrawn
upper_limit: The maximum iterations before giving up.
Returns:
The new principal'''
for i in range(upper_limit):
if(P < 0):
break
P = annual(P, r, W)
return i
terminator(250000)
Out[7]:
In [13]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
ps = [i / 1000. for i in range(10**5, int(5 * 10**5), 100)]
ys = [terminator(p * 1000) for p in ps]
plt.plot(ps, ys)
plt.xlabel('Thousands of Dollars')
plt.ylabel('Years')
plt.show()
In [16]:
ps = [i / 1000. for i in range(10**5, int(7.5 * 10**5), 100)]
ys = [terminator(p * 1000, upper_limit=1000) for p in ps]
plt.plot(ps, ys)
plt.show()
Rewrite your annual function to sample random numbers. Youur investment rate of return (maturation rate) should be sampled from a normal distribution with standard deviation 0.03 and mean 0.05. Your withdrawl should come from a normal distribution with mean \$30,000 and standard deviation \$10,000. Call your new annual, s_annual and your new terminator s_terminator. Answer the following questions:
Previously you calculated you can live for 11 years off of a principal of \$250,000. Using your new terminator and annual functions, make a histogram for how many years you can live off of \$250,000. Use 1000 samples.
Create a function which takes in a principal and a target number of years for the principal to last. It should return what fraction of terminator runs succeed in lasting that many years. For example, a principal of \$250,000 should have about 55%-60% success with 10 years.
Using any method you would like (e.g., trial and error or plotting), what should your principal be to ensure retirement for 25 years in 95% of the samples?
In [33]:
from scipy import stats as ss
def s_annual(P, r=0.05, W=30000, sig_r=0.03, sig_W=10000):
'''Computes the change in principal after one year with stochastic
Args:
P: The principal - amount of money at the beginning of the year
r: The rate of return from principal
W: The amount withdrawn
Returns:
The new principal'''
P -= ss.norm.rvs(size=1,scale=sig_W, loc=W)
P *= (ss.norm.rvs(size=1, scale=sig_r, loc=r) + 1)
return P
def s_terminator(P, r=0.05, W=30000, upper_limit=50):
for i in range(upper_limit):
if(P < 0):
break
P = s_annual(P, r, W)
return i
samples = []
for i in range(1000):
samples.append(s_terminator(2.5 * 10**5))
In [35]:
plt.hist(samples)
plt.show()
In [38]:
def s_threshold(P, y):
'''Returns the fraction of times the principal P lasts longer than y'''
success = 0
for i in range(1000):
if s_terminator(P) > y:
success += 1
return success / 1000
s_threshold(2.5 * 10**5, 10)
Out[38]:
In [42]:
p = np.linspace(1 * 10**5, 10 * 10**5, 200)
for pi in p:
if s_threshold(pi, 25) > 0.95:
print(pi)
break
In [1]:
import random
In [2]:
import scipy.stats
In [3]:
scipy.stats.geom?
In [ ]: