mathgenerator.statistics

  1import random
  2import math
  3
  4
  5def combinations(max_lengthgth=20):
  6    """Combinations of Objects
  7
  8    | Ex. Problem | Ex. Solution |
  9    | --- | --- |
 10    | Find the number of combinations from $19$ objects picked $6$ at a time. | $27132$ |
 11    """
 12    a = random.randint(10, max_lengthgth)
 13    b = random.randint(0, 9)
 14
 15    solution = int(math.factorial(
 16        a) / (math.factorial(b) * math.factorial(a - b)))
 17
 18    problem = f"Find the number of combinations from ${a}$ objects picked ${b}$ at a time."
 19    return problem, f'${solution}$'
 20
 21
 22def conditional_probability():
 23    """Conditional Probability
 24
 25    | Ex. Problem | Ex. Solution |
 26    | --- | --- |
 27    | Someone tested positive for a nasty disease which only $1.18$% of the population have. Test sensitivity (true positive) is equal to $SN=98.73$% whereas test specificity (true negative) $SP=99.99$%. What is the probability that this guy really has that disease? | $99.16$% |
 28    """
 29    P_disease = round(2. * random.random(), 2)
 30    true_positive = round(random.random() + float(random.randint(90, 99)), 2)
 31    true_negative = round(random.random() + float(random.randint(90, 99)), 2)
 32
 33    def BayesFormula(P_disease, true_positive, true_negative):
 34        P_notDisease = 100. - P_disease
 35        false_positive = 100. - true_negative
 36        P_plus = (P_disease) * (true_positive) + \
 37            (P_notDisease) * (false_positive)
 38        P_disease_plus = ((true_positive) * (100 * P_disease)) / P_plus
 39
 40        return P_disease_plus
 41
 42    answer = round(BayesFormula(P_disease, true_positive, true_negative), 2)
 43
 44    problem = "Someone tested positive for a nasty disease which only ${0:.2f}$% of the population have. " \
 45        "Test sensitivity (true positive) is equal to $SN={1:.2f}$% whereas test specificity (true negative) $SP={2:.2f}$%. " \
 46        "What is the probability that this guy really has that disease?".format(
 47            P_disease, true_positive, true_negative)
 48    solution = f'${answer}$%'
 49    return problem, solution
 50
 51
 52def confidence_interval():
 53    """Confidence interval For sample S
 54
 55    | Ex. Problem | Ex. Solution |
 56    | --- | --- |
 57    | The confidence interval for sample $[234, 223, 210, 203, 258, 299, 281, 208, 278, 252, 295, 245, 280, 235, 219, 297, 214, 267, 212, 256, 232, 221]$ with $99$% confidence is | $(263.31, 229.33)$ |
 58    """
 59    n = random.randint(20, 40)
 60    j = random.randint(0, 3)
 61
 62    lst = random.sample(range(200, 300), n)
 63    lst_per = [80, 90, 95, 99]
 64    lst_t = [1.282, 1.645, 1.960, 2.576]
 65
 66    mean = 0
 67    sd = 0
 68
 69    for i in lst:
 70        count = i + mean
 71        mean = count
 72
 73    mean = mean / n
 74
 75    for i in lst:
 76        x = (i - mean)**2 + sd
 77        sd = x
 78
 79    sd = sd / n
 80    standard_error = lst_t[j] * math.sqrt(sd / n)
 81    upper = round(mean + standard_error, 2)
 82    lower = round(mean - standard_error, 2)
 83
 84    problem = 'The confidence interval for sample ${}$ with ${}$% confidence is'.format(
 85        [x for x in lst], lst_per[j])
 86    solution = f'$({upper}, {lower})$'
 87    return problem, solution
 88
 89
 90def data_summary(number_values=15, min_val=5, max_val=50):
 91    """Mean, Standard Deviation and Variance
 92
 93    | Ex. Problem | Ex. Solution |
 94    | --- | --- |
 95    | Find the mean,standard deviation and variance for the data $9, 29, 46, 27, 46, 15, 10, 44, 19, 33, 38, 7, 34, 28, 8$ | The Mean is $26.2$, Standard Deviation is $186.29$, Variance is $13.65$ |
 96    """
 97    random_list = []
 98
 99    for i in range(number_values):
100        n = random.randint(min_val, max_val)
101        random_list.append(n)
102
103    a = sum(random_list)
104    mean = round(a / number_values, 2)
105
106    var = 0
107    for i in range(number_values):
108        var += (random_list[i] - mean)**2
109
110    standardDeviation = round(var / number_values, 2)
111    variance = round((var / number_values) ** 0.5, 2)
112
113    problem = f"Find the mean,standard deviation and variance for the data ${', '.join(map(str, random_list))}$"
114    solution = f"The Mean is ${mean}$, Standard Deviation is ${standardDeviation}$, Variance is ${variance}$"
115    return problem, solution
116
117
118def dice_sum_probability(max_dice=3):
119    r"""Probability of a certain sum appearing on faces of dice
120
121    | Ex. Problem | Ex. Solution |
122    | --- | --- |
123    | If $2$ dice are rolled at the same time, the probability of getting a sum of $5 =$ | $\frac{4}{36}$ |
124    """
125    a = random.randint(1, max_dice)
126    b = random.randint(a, 6 * a)
127
128    count = 0
129    for i in [1, 2, 3, 4, 5, 6]:
130        if a == 1:
131            if i == b:
132                count = count + 1
133        elif a == 2:
134            for j in [1, 2, 3, 4, 5, 6]:
135                if i + j == b:
136                    count = count + 1
137        elif a == 3:
138            for j in [1, 2, 3, 4, 5, 6]:
139                for k in [1, 2, 3, 4, 5, 6]:
140                    if i + j + k == b:
141                        count = count + 1
142
143    problem = f"If ${a}$ dice are rolled at the same time, the probability of getting a sum of ${b} =$"
144    solution = rf"\frac{{{count}}}{{{6**a}}}"
145    return problem, solution
146
147
148def mean_median(max_length=10):
149    """Mean and Median
150
151    | Ex. Problem | Ex. Solution |
152    | --- | --- |
153    | Given the series of numbers $[4, 19, 21, 22, 43, 44, 60, 81, 87, 92]$. Find the arithmatic mean and median of the series | Arithmetic mean of the series is $47.3$ and arithmetic median of this series is $43.5$ |
154    """
155    randomlist = random.sample(range(1, 99), max_length)
156    total = 0
157    for n in randomlist:
158        total = total + n
159    mean = total / 10
160    randomlist.sort()
161    median = (randomlist[4] + randomlist[5]) / 2
162
163    problem = f"Given the series of numbers ${randomlist}$. Find the arithmatic mean and median of the series"
164    solution = f"Arithmetic mean of the series is ${mean}$ and arithmetic median of this series is ${median}$"
165    return problem, solution
166
167
168def permutation(max_lengthgth=20):
169    """Permutations
170
171    | Ex. Problem | Ex. Solution |
172    | --- | --- |
173    | Number of Permutations from $18$ objects picked $5$ at a time is: | $1028160$ |
174    """
175    a = random.randint(10, max_lengthgth)
176    b = random.randint(0, 9)
177    solution = int(math.factorial(a) / (math.factorial(a - b)))
178
179    problem = f"Number of Permutations from ${a}$ objects picked ${b}$ at a time is: "
180    return problem, f"${solution}$"
def combinations(max_lengthgth=20):
 6def combinations(max_lengthgth=20):
 7    """Combinations of Objects
 8
 9    | Ex. Problem | Ex. Solution |
10    | --- | --- |
11    | Find the number of combinations from $19$ objects picked $6$ at a time. | $27132$ |
12    """
13    a = random.randint(10, max_lengthgth)
14    b = random.randint(0, 9)
15
16    solution = int(math.factorial(
17        a) / (math.factorial(b) * math.factorial(a - b)))
18
19    problem = f"Find the number of combinations from ${a}$ objects picked ${b}$ at a time."
20    return problem, f'${solution}$'

Combinations of Objects

Ex. Problem Ex. Solution
Find the number of combinations from $19$ objects picked $6$ at a time. $27132$
def conditional_probability():
23def conditional_probability():
24    """Conditional Probability
25
26    | Ex. Problem | Ex. Solution |
27    | --- | --- |
28    | Someone tested positive for a nasty disease which only $1.18$% of the population have. Test sensitivity (true positive) is equal to $SN=98.73$% whereas test specificity (true negative) $SP=99.99$%. What is the probability that this guy really has that disease? | $99.16$% |
29    """
30    P_disease = round(2. * random.random(), 2)
31    true_positive = round(random.random() + float(random.randint(90, 99)), 2)
32    true_negative = round(random.random() + float(random.randint(90, 99)), 2)
33
34    def BayesFormula(P_disease, true_positive, true_negative):
35        P_notDisease = 100. - P_disease
36        false_positive = 100. - true_negative
37        P_plus = (P_disease) * (true_positive) + \
38            (P_notDisease) * (false_positive)
39        P_disease_plus = ((true_positive) * (100 * P_disease)) / P_plus
40
41        return P_disease_plus
42
43    answer = round(BayesFormula(P_disease, true_positive, true_negative), 2)
44
45    problem = "Someone tested positive for a nasty disease which only ${0:.2f}$% of the population have. " \
46        "Test sensitivity (true positive) is equal to $SN={1:.2f}$% whereas test specificity (true negative) $SP={2:.2f}$%. " \
47        "What is the probability that this guy really has that disease?".format(
48            P_disease, true_positive, true_negative)
49    solution = f'${answer}$%'
50    return problem, solution

Conditional Probability

Ex. Problem Ex. Solution
Someone tested positive for a nasty disease which only $1.18$% of the population have. Test sensitivity (true positive) is equal to $SN=98.73$% whereas test specificity (true negative) $SP=99.99$%. What is the probability that this guy really has that disease? $99.16$%
def confidence_interval():
53def confidence_interval():
54    """Confidence interval For sample S
55
56    | Ex. Problem | Ex. Solution |
57    | --- | --- |
58    | The confidence interval for sample $[234, 223, 210, 203, 258, 299, 281, 208, 278, 252, 295, 245, 280, 235, 219, 297, 214, 267, 212, 256, 232, 221]$ with $99$% confidence is | $(263.31, 229.33)$ |
59    """
60    n = random.randint(20, 40)
61    j = random.randint(0, 3)
62
63    lst = random.sample(range(200, 300), n)
64    lst_per = [80, 90, 95, 99]
65    lst_t = [1.282, 1.645, 1.960, 2.576]
66
67    mean = 0
68    sd = 0
69
70    for i in lst:
71        count = i + mean
72        mean = count
73
74    mean = mean / n
75
76    for i in lst:
77        x = (i - mean)**2 + sd
78        sd = x
79
80    sd = sd / n
81    standard_error = lst_t[j] * math.sqrt(sd / n)
82    upper = round(mean + standard_error, 2)
83    lower = round(mean - standard_error, 2)
84
85    problem = 'The confidence interval for sample ${}$ with ${}$% confidence is'.format(
86        [x for x in lst], lst_per[j])
87    solution = f'$({upper}, {lower})$'
88    return problem, solution

Confidence interval For sample S

Ex. Problem Ex. Solution
The confidence interval for sample $[234, 223, 210, 203, 258, 299, 281, 208, 278, 252, 295, 245, 280, 235, 219, 297, 214, 267, 212, 256, 232, 221]$ with $99$% confidence is $(263.31, 229.33)$
def data_summary(number_values=15, min_val=5, max_val=50):
 91def data_summary(number_values=15, min_val=5, max_val=50):
 92    """Mean, Standard Deviation and Variance
 93
 94    | Ex. Problem | Ex. Solution |
 95    | --- | --- |
 96    | Find the mean,standard deviation and variance for the data $9, 29, 46, 27, 46, 15, 10, 44, 19, 33, 38, 7, 34, 28, 8$ | The Mean is $26.2$, Standard Deviation is $186.29$, Variance is $13.65$ |
 97    """
 98    random_list = []
 99
100    for i in range(number_values):
101        n = random.randint(min_val, max_val)
102        random_list.append(n)
103
104    a = sum(random_list)
105    mean = round(a / number_values, 2)
106
107    var = 0
108    for i in range(number_values):
109        var += (random_list[i] - mean)**2
110
111    standardDeviation = round(var / number_values, 2)
112    variance = round((var / number_values) ** 0.5, 2)
113
114    problem = f"Find the mean,standard deviation and variance for the data ${', '.join(map(str, random_list))}$"
115    solution = f"The Mean is ${mean}$, Standard Deviation is ${standardDeviation}$, Variance is ${variance}$"
116    return problem, solution

Mean, Standard Deviation and Variance

Ex. Problem Ex. Solution
Find the mean,standard deviation and variance for the data $9, 29, 46, 27, 46, 15, 10, 44, 19, 33, 38, 7, 34, 28, 8$ The Mean is $26.2$, Standard Deviation is $186.29$, Variance is $13.65$
def dice_sum_probability(max_dice=3):
119def dice_sum_probability(max_dice=3):
120    r"""Probability of a certain sum appearing on faces of dice
121
122    | Ex. Problem | Ex. Solution |
123    | --- | --- |
124    | If $2$ dice are rolled at the same time, the probability of getting a sum of $5 =$ | $\frac{4}{36}$ |
125    """
126    a = random.randint(1, max_dice)
127    b = random.randint(a, 6 * a)
128
129    count = 0
130    for i in [1, 2, 3, 4, 5, 6]:
131        if a == 1:
132            if i == b:
133                count = count + 1
134        elif a == 2:
135            for j in [1, 2, 3, 4, 5, 6]:
136                if i + j == b:
137                    count = count + 1
138        elif a == 3:
139            for j in [1, 2, 3, 4, 5, 6]:
140                for k in [1, 2, 3, 4, 5, 6]:
141                    if i + j + k == b:
142                        count = count + 1
143
144    problem = f"If ${a}$ dice are rolled at the same time, the probability of getting a sum of ${b} =$"
145    solution = rf"\frac{{{count}}}{{{6**a}}}"
146    return problem, solution

Probability of a certain sum appearing on faces of dice

Ex. Problem Ex. Solution
If $2$ dice are rolled at the same time, the probability of getting a sum of $5 =$ $\frac{4}{36}$
def mean_median(max_length=10):
149def mean_median(max_length=10):
150    """Mean and Median
151
152    | Ex. Problem | Ex. Solution |
153    | --- | --- |
154    | Given the series of numbers $[4, 19, 21, 22, 43, 44, 60, 81, 87, 92]$. Find the arithmatic mean and median of the series | Arithmetic mean of the series is $47.3$ and arithmetic median of this series is $43.5$ |
155    """
156    randomlist = random.sample(range(1, 99), max_length)
157    total = 0
158    for n in randomlist:
159        total = total + n
160    mean = total / 10
161    randomlist.sort()
162    median = (randomlist[4] + randomlist[5]) / 2
163
164    problem = f"Given the series of numbers ${randomlist}$. Find the arithmatic mean and median of the series"
165    solution = f"Arithmetic mean of the series is ${mean}$ and arithmetic median of this series is ${median}$"
166    return problem, solution

Mean and Median

Ex. Problem Ex. Solution
Given the series of numbers $[4, 19, 21, 22, 43, 44, 60, 81, 87, 92]$. Find the arithmatic mean and median of the series Arithmetic mean of the series is $47.3$ and arithmetic median of this series is $43.5$
def permutation(max_lengthgth=20):
169def permutation(max_lengthgth=20):
170    """Permutations
171
172    | Ex. Problem | Ex. Solution |
173    | --- | --- |
174    | Number of Permutations from $18$ objects picked $5$ at a time is: | $1028160$ |
175    """
176    a = random.randint(10, max_lengthgth)
177    b = random.randint(0, 9)
178    solution = int(math.factorial(a) / (math.factorial(a - b)))
179
180    problem = f"Number of Permutations from ${a}$ objects picked ${b}$ at a time is: "
181    return problem, f"${solution}$"

Permutations

Ex. Problem Ex. Solution
Number of Permutations from $18$ objects picked $5$ at a time is: $1028160$