State which hypothesis best matches the scenario and justify your answer
Choose and peform hypothesis tests for the following examples. Report your p-values!!
The number of yellow skittles is on average 8 per bag. Your bag has 11. Is this significantly different?
You normally drink 8 oz of water with a standard deviation of 2 oz. Today you drank 12 oz. Is this significantly different?
Ting says your cellphone bill will be about \$50 or lower if you switch. You switch and find your bills a are \$48, \$ 53, \$67, \$44. Was their claim correct?
You are testing the efficacy of a drug that causes weight loss. You have 15 patients try the drug. Their weights in pounds before are [178,189,136,134,172,171,178,179,154,159,167,149,125,180,135] and after are [174,178,139,133,166,165,172,180,141,145,176,151,108,174,131]. Is the drug effective?
You now add a control group which is given a placebo. Their before and after weights are [211,175,151,160,163,137,169,145,171,177,148,145,173,174,167] and [200,171,146,162,178,134,183,148,164,166,141,142,166,167,142]. Is your conclusion the same? Why or why not?
import scipy.stats as ss
#Poisson
1 - ss.poisson.cdf(10, 8)
0.18411420744145335
Z = (12 - 8) / 2
2 * ss.norm.cdf(-Z)
0.04550026389635839
bills = [45, 53, 67, 44]
bm = np.mean(bills)
bs = np.std(bills, ddof=1)
T = (bm - 50) / (bs / np.sqrt(len(bills)))
2 * ss.t.cdf(-T, df=len(bills) - 1)
0.42348057022481395
0.7004718483539545
before = [178,189,136,134,172,171,178,179,154,159,167,149,125,180,135]
after = [174,178,139,133,166,165,172,180,141,145,176,151,108,174,131]
ss.wilcoxon(before, after)
WilcoxonResult(statistic=19.5, pvalue=0.02112008758907893)
There is not a significant different in weight loss between the placebo or drug group, so the drug is no more effective than a placebo.
beforec = [211,175,151,160,163,137,169,145,171,177,148,145,173,174,167]
afterc = [200,171,146,162,178,134,183,148,164,166,141,142,166,167,142]
changec = [b - a for b,a in zip(beforec,afterc)]
change = [b - a for b,a in zip(before, after)]
ss.ranksums(change, changec)
RanksumsResult(statistic=0.14517323726225953, pvalue=0.8845740956336082)
Solve the following problems using optimization. Report your answer in Markdown!
$x = 1.17$
import scipy.optimize as opt
opt.root(lambda x: np.tan(x) - x**2 - 1, x0=1)
fjac: array([[-1.]]) fun: array([-2.07389661e-13]) message: 'The solution converged.' nfev: 11 qtf: array([-1.50393884e-08]) r: array([-4.29069632]) status: 1 success: True x: array([1.17209362])
$x = 4$
opt.minimize(lambda x: (x - 4)**2, x0=0)
fun: 1.6517868451916196e-16 hess_inv: array([[0.5]]) jac: array([-1.08032108e-08]) message: 'Optimization terminated successfully.' nfev: 12 nit: 3 njev: 4 status: 0 success: True x: array([3.99999999])
This is non-convex
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(-4, 4, 500)
plt.plot(x, x**2 * np.sin(x)**2)
[<matplotlib.lines.Line2D at 0x7fd4061b50b8>]
Two solutions (ok to report just 1) $x,y = 4.47, 1.97$; $x,y = 4, -2$
def sys(v):
x = v[0]
y = v[1]
return [x**2 - y - 18, np.sqrt(x) + y**2 - 6]
opt.root(sys, x0 = [1,-1])
fjac: array([[-0.99930056, -0.0373951 ], [ 0.0373951 , -0.99930056]]) fun: array([-3.19744231e-14, -3.10862447e-14]) message: 'The solution converged.' nfev: 13 qtf: array([-8.24822349e-10, -7.51151959e-10]) r: array([-8.05791962, 1.12720016, 3.94005999]) status: 1 success: True x: array([ 4., -2.])