0% found this document useful (0 votes)
3 views2 pages

Statistics_HW2_code

The document contains Python code for two problems involving statistical analysis using NumPy and Matplotlib. The first problem generates uniform random variables and compares empirical and theoretical distributions, while the second problem analyzes a normal distribution to calculate various probabilities and thresholds. Key results include probabilities for specific conditions and values for which certain probabilities are met.

Uploaded by

luojingwen0505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Statistics_HW2_code

The document contains Python code for two problems involving statistical analysis using NumPy and Matplotlib. The first problem generates uniform random variables and compares empirical and theoretical distributions, while the second problem analyzes a normal distribution to calculate various probabilities and thresholds. Key results include probabilities for specific conditions and values for which certain probabilities are met.

Uploaded by

luojingwen0505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In [8]: # Chapter 2, problem 7

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(0, 1, 10000)
y = np.random.uniform(0, 1, 10000)
z = np.linspace(0, 1, 10000)
z_empirical = np.where(x<y,x,y)

def f_z(z):
if 0 <= z <= 1:
return 2*(1-z)
else:
return 0
z_theoretical = [f_z(i) for i in z]

count, bins, ignored = plt.hist(z_empirical, 100, density=True, label='E


mpirical')
plt.plot(z, z_theoretical, label='Theoretical')
plt.legend(loc='upper right')
plt.show()

In [9]: # chapter 2, problem 18


import numpy as np
import matplotlib.pyplot as plt

mu = 3
sigma = 4
x = np.random.normal(mu, sigma, 10000)

cnt = 0
for e in x:
if e < 7:
cnt += 1
p = cnt/x.size
print('a) probability of X < 7 is ', p)

cnt = 0
for e in x:
if e > -2:
cnt += 1
p = cnt/x.size
print('b) probability of X > -2 is ', p)

v = np.linspace(9, 10, num=1000)


min_err = 1
sol = mu
for ev in v:
cnt = 0
for ex in x:
if ex > ev:
cnt += 1
p = cnt/x.size
if (abs(p-0.05) < min_err):
min_err = abs(p-0.05)
sol = ev
print('c) x value for probability of X > x being 0.05 is ', sol)

cnt = 0
for e in x:
if (e > 0 and e < 4):
cnt += 1
p = cnt/x.size
print('d) probability of 0 < X < 4 is ', p)

v = np.linspace(9, 10, num=1000)


min_err = 1
sol = mu
for ev in v:
cnt = 0
for ex in x:
if abs(ex) > abs(ev):
cnt += 1
p = cnt/x.size
if (abs(p-0.05) < min_err):
min_err = abs(p-0.05)
sol = ev
print('e) x value for probability of |X| > |x| being 0.05 is ', sol)

a) probability of X < 7 is 0.838


b) probability of X > -2 is 0.8961
c) x value for probability of X > x being 0.05 is 9.602602602602602
d) probability of 0 < X < 4 is 0.3696
e) x value for probability of |X| > |x| being 0.05 is 9.635635635635635

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy