Skip to content

Commit 2d4362d

Browse files
Eliminated redundant file, and renamed solution S6_3_1_normalDistribution.py
1 parent 23bc459 commit 2d4362d

File tree

2 files changed

+66
-82
lines changed

2 files changed

+66
-82
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,73 @@
1-
'''Solutions to 6.3.1 Examples of Normal Distributions'''
1+
"""Solution to the Examples in Section 6.3.1"""
22

3-
# author: Thomas Haslwanter, date: Oct-2015
3+
# author: Thomas Haslwanter
4+
# date: Sept-2019
45

5-
# Import standard packages
6+
# Import the required packages
67
import numpy as np
78
from scipy import stats
89

9-
# Example 1
10-
nd = stats.norm(175, 6)
11-
p = nd.cdf(184) - nd.cdf(183)
12-
print('The probability that a randomly selected man is 183 cm tall is {0:.1f}% .'.format(p*100))
1310

14-
# Example 2
11+
def man_183():
12+
"""If cans are assumed to have a standard deviation of 4 g, what does the average
13+
weight need to be in order to ensure that 99 % of all cans have a weight of at least
14+
250 g?
1515
16-
# Example 3
16+
To answer that question, we calculate the probability that the size is
17+
between 183.0 and 184.0 cm."""
18+
19+
# Define the distribution
20+
mean = 175
21+
sd = 6
22+
nd = stats.norm(mean, sd)
23+
24+
# Calculate and show the probability
25+
prob_183 = 100*(nd.cdf(184) - nd.cdf(183))
26+
print(f'The probability that a man is exactly 183 cm tall is {prob_183:4.1f} %')
27+
28+
# Note that here I use "f-strings", which only have become available in Python 3.7.
29+
# For Python 3.6 or earlier, you have to use:
30+
# print('The probability that a man is exactly 183 cm tall is {0:4.1f} %'.format(prob_183))
31+
32+
33+
def weight_cans():
34+
"""If cans are assumed to have a standard deviation of 4 g, what does the average
35+
weight need to be in order to ensure that 99 % of all cans have a weight of at least
36+
250 g? """
37+
38+
# Define the parameters
39+
sd = 4
40+
lower_lim = 250
41+
nd = stats.norm()
42+
43+
# Calculate the average weight, and show the result
44+
mean = lower_lim + sd * nd.ppf(0.99)
45+
print(f'If the average can weighs {mean:4.1f} g and the standard ' +
46+
f'deviation is {sd} g, then 99% will weigh above {lower_lim} g.')
47+
48+
49+
def small_men():
50+
""" If cans are assumed to have a standard deviation of 4 g, what does the average
51+
weight need to be in order to ensure that 99 % of all cans have a weight of at least
52+
250 g?
53+
"""
54+
55+
# Define the parameters
56+
male = {'avg':175, 'sd':6}
57+
female = {'avg':168, 'sd':3}
58+
59+
# Calculate the distribution for the difference between a randomly selected man, and a randomly selected woman
60+
diff = {'avg':male['avg'] - female['avg'], 'sd': np.sqrt(male['sd']**2 + female['sd']**2)}
61+
nd = stats.norm(diff['avg'], diff['sd'])
62+
p_small_male = nd.cdf(0)
63+
64+
# Show the likelyhood that the man is smaller
65+
print(f'The probability that a randomly selected man is shorter than a randomly selected woman is {p_small_male*100:4.1f}%.')
66+
67+
68+
if __name__ == '__main__':
69+
"""Run the individual solutions"""
70+
man_183()
71+
weight_cans()
72+
small_men()
73+

ISP/Exercise_Solutions/S6_Questions631.py

-73
This file was deleted.

0 commit comments

Comments
 (0)