import numpy as np
import pandas as pd
from scipy import stats
import matplotlib as mpl
%matplotlib inline

import seaborn as sns
sns.set(color_codes=True)

cars = pd.read_csv("cars_v1.csv", encoding="ISO-8859-1")

mileage_is_null = cars.Mileage.isnull()

mileage_is_null

cars = cars.ix[~mileage_is_null]

cars.head()

"""
Explanation: Hypothesis Testing
(We are covering, what is referred to as, frequentist method of Hypothesis testing)
We would like to know if the effects we see in the sample(observed data) are likely to occur in the population. 
The way classical hypothesis testing works is by conducting a statistical test to answer the following question:

Given the sample and an effect, what is the probability of seeing that effect just by chance?

Here are the steps on how we would do this

Compute test statistic
Define null hypothesis
Compute p-value
Interpret the result

If p-value is very low(most often than now, below 0.05), the effect is considered statistically significant. That means that effect is unlikely to have occured by chance. The inference? The effect is likely to be seen in the population too. 
This process is very similar to the proof by contradiction paradigm. We first assume that the effect is false. That's the null hypothesis. Next step is to compute the probability of obtaining that effect (the p-value). If p-value is very low(<0.05 as a rule of thumb), we reject the null hypothesis.
End of explanation
"""


cars_Automatic = cars[cars.GearType==' Automatic'].copy().reset_index()
cars_Manual = cars[cars.GearType==' Manual'].copy().reset_index()

print("Average Mileage of Automatic cars:", cars_Automatic.Mileage.mean())
print("Average Mileage of Manual cars:", cars_Manual.Mileage.mean())

#Mean and standard deviation of Manual Cars
print("Mean of Manual Cars:", cars_Manual.Mileage.mean())
print("Standard Deviation of Manual Cars:", cars_Manual.Mileage.std())

"""
Explanation: Is the average mileage of Automatic cars significantly different from the average mileage of Manual cars ?
End of explanation
"""


#Mean and standard deviation of Automatic Cars

#Confidence interval on the mean of manual cars
stats.norm.interval(0.95, loc=cars_Manual.Mileage.mean(), scale = cars_Manual.Mileage.std()/np.sqrt(len(cars_Manual)))

"""
Explanation: Exercise
End of explanation
"""


#Confidence interval on the mean of automatic cars


"""
Explanation: Exercise
End of explanation
"""


print("Effect size:", cars_Manual.Mileage.mean() - cars_Automatic.Mileage.mean())

"""
Explanation: Effect Size
End of explanation
"""


stats.ttest_ind(cars_Manual.Mileage, cars_Automatic.Mileage, equal_var=True)

"""
Explanation: Null Hypothesis: Mean prices aren't significantly different
Perform t-test and determine the p-value.
End of explanation
"""


stats.shapiro(cars_Manual.Mileage)

stats.shapiro(cars_Automatic.Mileage)

"""
Explanation: p-value is the probability that the effective size was by chance. And here, p-value is almost 0.
Conclusion: The mileage difference is significant. 
Assumption of t-test
One assumption is that the data used came from a normal distribution. 
<br>
There's a Shapiro-Wilk test to test for normality. If p-value is less than 0.05, then there's a low chance that the distribution is normal.
End of explanation
"""


stats.chisquare(Observed, Expected)

"""
Explanation: Errrrrr.....
A/B testing
Comparing two versions to check which one performs better. Eg: Show to people two variants for the same webpage that they want to see and find which one provides better conversion rate (or the relevant metric). wiki
<h2> Something to think about: Which of these give smaller p-values ? </h2>


Smaller effect size
Smaller standard error
Smaller sample size
Higher variance

Answer: 
Chi-square tests
Chi-Square tests are used when the data are frequencies, rather than numerical score/price.
The following two tests make use of chi-square statistic

chi-square test for goodness of fit
chi-square test for independence

Chi-square test is a non-parametric test. They do not require assumptions about population parameters and they do not test hypotheses about population parameters.
<h2> Chi-Square test for goodness fit </h2>

$$ \chi^2 = \sum (O - E)^2/E $$

O is observed frequency
E is expected frequency
$ \chi $ is the chi-square statistic
End of explanation
"""


