-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics_and_probability.py
148 lines (115 loc) · 4.48 KB
/
statistics_and_probability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# statistics_and_probability.py
##############################################################################
# Imports #
##############################################################################
from statistics import mean, median, stdev, variance, mode, StatisticsError
import math
from typing import List, Union
def calculate_mean(numbers: List[Union[int, float]]) -> float | None:
"""
Calculate the mean of a list of numbers.
Parameters:
numbers (list): List of numbers.
Returns:
float: The mean of the numbers. Returns None if input is not a non-empty list.
"""
if not isinstance(numbers, list) or not numbers:
print("Error: Input must be a non-empty list.")
return None
return mean(numbers)
def calculate_median(numbers: List[Union[int, float]]) -> float | None:
"""
Calculate the median of a list of numbers.
Parameters:
numbers (list): List of numbers.
Returns:
float: The median of the numbers. Returns None if input is not a non-empty list.
"""
if not isinstance(numbers, list) or not numbers:
print("Error: Input must be a non-empty list.")
return None
return median(numbers)
def calculate_stdev(numbers: List[Union[int, float]]) -> float | None:
"""
Calculate the standard deviation of a list of numbers.
Parameters:
numbers (list): List of numbers.
Returns:
float: The standard deviation of the numbers. Returns None if input is not a non-empty list.
"""
if not isinstance(numbers, list) or not numbers:
print("Error: Input must be a non-empty list.")
return None
return stdev(numbers)
def calculate_variance(numbers: List[Union[int, float]]) -> float | None:
"""
Calculate the variance of a list of numbers.
Parameters:
numbers (list): List of numbers.
Returns:
float: The variance of the numbers. Returns None if input is not a non-empty list.
"""
if not isinstance(numbers, list) or not numbers:
print("Error: Input must be a non-empty list.")
return None
return variance(numbers)
def calculate_mode(numbers: List[Union[int, float]]) -> float | None:
"""
Calculate the mode of a list of numbers.
Parameters:
numbers (list): List of numbers.
Returns:
float: The mode of the numbers. Returns None if input is not a non-empty list or if the list does not have a mode.
"""
if not isinstance(numbers, list) or not numbers:
print("Error: Input must be a non-empty list.")
return None
try:
return mode(numbers)
except StatisticsError:
print("Error: The list does not have a mode.")
return None
def probability(event_outcomes: int, sample_space: int) -> float | None:
"""
Calculate the probability of an event.
The probability of an event is calculated as the ratio of the number of outcomes in which the
event occurs to the total number of outcomes in the sample space.
Parameters:
event_outcomes (int): Number of outcomes in which the event occurs.
sample_space (int): Total number of outcomes in the sample space.
Returns:
float: The probability of the event. Returns None if sample_space is zero.
"""
try:
return event_outcomes / sample_space
except ZeroDivisionError:
print("Error: The sample space cannot be zero.")
return None
def combinations(n: int, r: int) -> float | None:
"""
Calculate the number of combinations of n items taken r at a time.
Parameters:
n (int): Total number of items.
r (int): Number of items to choose.
Returns:
int: The number of combinations or None if invalid parameters.
"""
try:
return math.factorial(n) / (math.factorial(r) * math.factorial(n - r))
except ValueError:
print("Error: n and r should be non-negative integers, and n should be greater than or equal to r.")
return None
def permutations(n: int, r: int) -> float | None:
"""
Calculate the number of permutations of n items taken r at a time.
Parameters:
n (int): Total number of items.
r (int): Number of items to choose.
Returns:
int: The number of permutations or None if invalid parameters.
"""
try:
return math.factorial(n) / math.factorial(n - r)
except ValueError:
print("Error: n and r should be non-negative integers, and n should be greater than or equal to r.")
return None