Learn Probalility and Statistics with Handson, including Permutations and Combinations, Random Variables, Discreate and Continuous Distribution, Naive Bayes Theorem, Hypothesis testing, Chi-Squared Test.
Lab 2 : Welcome to Probability and Statistics -2
Combinations with Factorial - Hands-on
Solution:
from itertools import combinations
from itertools import permutations
import numpy as np
import math
def comb_perm(arr):
#Write your code here
'''
Input: arr : numpy array
Return : no_of_comb,no_of_perm : Integer
'''
'''
Task 1:
Given an array, you should find the number of
combinations and permutations when taken 2 elements
at a time without a replacement that can be formed from
the array of elements.
arr = [A,B,C,D]
'''
no_of_comb= len(list(combinations(arr ,2)))
no_of_perm= len(list(permutations(arr, 2) ))
# print( no_of_comb , no_of_perm )
return no_of_comb,no_of_perm
if __name__=='__main__':
array1=[]
n=int(input())
for i in range(n):
array1.append(input())
narray1=np.array(array1)
print(comb_perm(narray1))
Lab 3: Welcome to Probability and Statistics -3
Statistics - Hands-on
Solution:
import math
def dancers():
'''
output: ans : Integer
'''
#Write your code here
#Assign your value to variable ans
'''
Task 1:
The teacher wants a group to be formed for the
upcoming dance competition. She wants a group of 5
dancers consisting of 3 boys and 2 girls. In how many
ways can a group of 5 dancers be formed by selecting
3 boys out of 6 and 2 girls out of 5? Help her out!
Write a function that returns the number of ways
a group of 5 dancers consisting of 3 boys and 2
girls can be formed.
'''
# {6!} / {3!(6-3)!} # NcR! - 6C3!
# {5!} / {2!(5-2)!} # NcR! - 5C2!
s = math.factorial(6)
Th = math.factorial(3)
min1 = math.factorial(6-3)
c1 = s/ (Th*min1)
f = math.factorial(5)
Tw = math.factorial(2)
min2 = math.factorial(5-2)
c2 = f/ (Tw*min2)
ans= c1*c2
return int(ans)
if __name__=='__main__':
print(dancers())
Lab 4: Welcome to Probability and Statistics -4
Mutually Exclusive Events
Solution:
from scipy import stats
def binomial():
'''
output: ans : Float
'''
#Write your code here
#Assign the probability value to the variable ans
#Round off to 2 decimal places
# n: the total number of trials
# r: a list of integers from 0 to n, inclusive.
# p: the probability that the outcome of a single experiment will be a success.
# pmf(r,n, p)
ans = 1 - round(stats.binom.pmf(0,4,0.6),2)
return ans
if __name__=='__main__':
print(binomial())
Lab 5: Welcome to Probability and Statistics -5
Non-mutually Exclusive Events
Solution:
from scipy import stats
def poisson():
'''
output: ans : Float
'''
'''
The average number of bouquets sold by a flower shop is 10 per day.
What is the probability that exactly 15 bouquets will be sold tomorrow? Use Poisson Distribution.
'''
#Write your code here
#Assign the probability value to the variable ans
#Round off to 2 decimal places
ans= stats.poisson.pmf(15 , 10)
return round(ans,2)
if __name__=='__main__':
print(poisson())
Lab 6: Welcome to Probability and Statistics -6
Binomial - Hands-on
Solution:
from scipy import stats
def spinner():
'''
output: ans : Float
'''
#Write your code here
#Assign the probability value to the variable ans
# Round off to 2 decimal places
# P(AUB) = P(A) + P(B) //For Mutually Exclusive
P_Seoul = 1/4
P_Paris = 1/4
after_spinning = P_Seoul + P_Paris
ans= round(after_spinning, 2)
return ans
if __name__=='__main__':
print(spinner())
Lab 7: Welcome to Probability and Statistics -7 Poisson - Hands-on
Poisson
Solution:
from scipy import stats
def accident():
'''
output: ans : Float
'''
""" Task:
On New Year's Eve, the probability of a person having a car
accident is 0.09. The probability of a person driving while
intoxicated is 0.32 and the probability of a person having a car
accident while intoxicated is 0.15.
What is the probability of a person driving while intoxicated or
having a car accident?
"""
# Write your code here
# P(A U B) = P(A) + P(B) – P( A∩B)
probability_Accident = 0.09 # 1. P(accident) = 0.09
probability_intoxicated = 0.32 # 2. P(intoxicated) = 0.32
probability_accident_and_intoxicated = 0.15 # 3. P(accident and intoxicated) = 0.15
ans = probability_Accident+ probability_intoxicated - probability_accident_and_intoxicated
#Assign the probability value to the variable ans. Round off to 2 decimal places
return round(ans, 2)
if __name__=='__main__':
print(accident())
Lab 8: Welcome to Probability and Statistics -8
Chi-squared Test - Hands-on
Solution:
from scipy.stats import chi2_contingency
from scipy.stats import chi2
def chi_test():
'''
Output
1. stat: Float
2. dof : Integer
3. p_val: Float
4. res: String
'''
# Task 1 :
# Declare a 2D array with the values mentioned in the contingency table of marital status by education.
material_status = [[18,36,21,9,6], [12,36,45,36,21], [6,9,9,3,3], [3,9,9,6,3]]
# Task 2: Calculate the values of the following:
# Chi-Square Statistic
# Degree of Freedom
# P value
stat,p_val, dof, res = chi2_contingency(material_status)
# print( chi2_contingency(arr))
# Task 3: Assume the alpha value to be 0.05
prob_success = 0.95
alpha_error = 1.0 - prob_success # 0.05
critical = chi2.ppf(prob_success, dof)
# print(critical)
# Task 4 : Compare the P value with alpha and decide whether or not to reject the null hypothesis.
# If Rejected assign the string "Reject the Null Hypothesis" to res variable
# Else assign the string "Failed to reject the Null Hypothesis" to res variable
# Hint: Use chi2_contingency() of scipy package.
if p_val <= alpha_error:
res='Reject the Null Hypothesis'
else:
res = 'Failed to reject the Null Hypothesis'
#Note 5: Round off the Float values to 2 decimal places.
stat = round(stat,2)
dof = round(dof,2)
p_val = round(p_val,2)
return stat,dof,p_val,res
if __name__=='__main__':
print(chi_test())
Lab 1: Welcome to Probability and Statistics -1
Probability and Statistics -1
Solution:
import numpy as np
from scipy import stats
import statistics
def measures(arr):
sample = arr
#Write your code here
'''
Input: arr : numpy array
Return : mean,median,std_deviation,variance,mode,iqr : float
Note:
1. Assign the values to designated variables
2. Round off to 2 decimal places
'''
# Task 1:
# Calculate Mean value for the given parameter 'data'.
mean = np.mean(sample)
# Task 2:
# Calculate Median value for the given parameter 'data'.
median = np.median(sample)
# Task 3:
# Calculate Mode value for the given parameter 'data'.
mode = statistics.mode(sample)
# Task 4:
# Calcuate 25th and 75th percentile value for given parameter `data` and return as a numpy array.
variance = statistics.variance(sample)
# Task 5:
# Calcuate Inter quartile range value for given parameter `data`
iqr = stats.iqr(sample, interpolation='lower')
std_deviation= statistics.stdev(sample)
return mean,median,std_deviation,variance,mode,iqr
if __name__=='__main__':
array1=[]
n=int(input())
for i in range(n):
array1.append(float(input()))
narray1=np.array(array1)
print(measures(narray1))