Exp 5-6-7-8
Exp 5-6-7-8
Program:
import numpy as np
import matplotlib.pyplot as
plt def estimate_coef(x, y):
# number of
observations/points n =
np.size(x)
# mean of x and y vector
m_x = np.mean(x)
m_y = np.mean(y)
# calculating cross-deviation and deviation about x
SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x
# calculating regression
coefficients b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x
return (b_0, b_1)
def plot_regression_line(x, y, b):
# plotting the actual points as scatter plot
plt.scatter(x, y, color = "m",
marker = "o", s =
30) # predicted response
vector y_pred = b[0] + b[1]*x
# plotting the regression line
plt.plot(x, y_pred, color =
"g") # putting labels
plt.xlabel('x')
plt.ylabel('y')
# function to show plot
plt.show()
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
# estimating
coefficients b =
estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0],
b[1])) # plotting regression line
plot_regression_line(x, y, b)
if name == " main ":
main()
Output:
Estimated coefficients:
b_0 = -0.0586206896552
b_1 = 1.45747126437
Z-TEST
Program:
# imports
import math
import numpy as np
from numpy.random import randn
from statsmodels.stats.weightstats import ztest
# Generate a random array of 50 numbers having mean 110
and sd 15
# similar to the IQ scores data we assume
above mean_iq = 110
sd_iq = 15/math.sqrt(50)
alpha = 0.05
null_mean =100
data =
sd_iq*randn(50)+mean_iq #
print mean and sd
print('mean=%.2f stdv=%.2f' % (np.mean(data), np.std(data)))
# now we perform the test. In this function, we passed
data, in the value parameter
# we passed mean value in the null hypothesis, in
alternative hypothesis we check whether the
# mean is larger
ztest_Score,p_value=ztest(data,value=null_mean,alternative='
la rger')
# the function outputs a p_value and z-score corresponding
to that value, we compare the
# p-value with alpha, if it is greater than alpha then
we do not null hypothesis
# else we reject it.
if(p_value < alpha):
print("Reject Null
Hypothesis")
else:
print("Fail to Reject NUll Hypothesis")
Output:
Reject Null Hypothesis
Experiment No: 7
T-TEST
Program:
# Importing the required libraries and
packages import numpy as np
from scipy import stats
# Defining two random
distributions # Sample Size
N = 10
# Gaussian distributed data with mean = 2 and var
= 1 x = np.random.randn(N) + 2
# Gaussian distributed data with mean = 0 and var
= 1 y = np.random.randn(N)
# Calculating the Standard Deviation
# Calculating the variance to get the standard
deviation var_x = x.var(ddof = 1)
var_y = y.var(ddof =
1) # Standard
Deviation
SD = np.sqrt((var_x + var_y) / 2)
print("Standard Deviation =",
SD) # Calculating the T-
Statistics
tval = (x.mean() - y.mean()) / (SD * np.sqrt(2 /
N)) # Comparing with the critical T-Value
# Degrees of freedom
dof = 2 * N - 2
# p-value after comparison with the T-
Statistics pval = 1 - stats.t.cdf( tval, df
= dof) print("t = " + str(tval))
print("p = " + str(2 * pval))
## Cross Checking using the internal function from SciPy
Packa ge
tval2, pval2 = stats.ttest_ind(x, y)
print("t = " + str(tval2))
print("p = " + str(pval2))
Output:
Standard Deviation =
0.7642398582227466 t =
4.87688162540348
p = 0.0001212767169695983
t = 4.876881625403479
p = 0.00012127671696957205
Experiment No: 8
ANOVA
Program:
# Installing the
package
install.packages("dplyr
") # Loading the package
library(dplyr)
# Variance in mean within group and between group
boxplot(mtcars$disp~factor(mtcars$gear),
xlab = "gear", ylab = "disp")
# Step 1: Setup Null Hypothesis and Alternate
Hypothesis # H0 = mu = mu01 = mu02 (There is no
difference
# between average displacement for different
gear) # H1 = Not all means are equal
# Step 2: Calculate test statistics using aov function
mtcars_aov <- aov(mtcars$disp~factor(mtcars$gear))
summary(mtcars_aov)
# Step 3: Calculate F-Critical Value
# For 0.05 Significant value, critical value = alpha =
0.05 # Step 4: Compare test statistics with F-Critical
value
# and conclude test p <alpha, Reject Null Hypothesis
Output: