import pandas as pd

df = pd.read_csv('data/human_body_temperature.csv')

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

"""
Explanation: What is the True Normal Human Body Temperature?
Background
The mean normal body temperature was held to be 37$^{\circ}$C or 98.6$^{\circ}$F for more than 120 years since it was first conceptualized and reported by Carl Wunderlich in a famous 1868 book. But, is this value statistically correct?
<div class="span5 alert alert-info">
<h3>Exercises</h3>

<p>In this exercise, you will analyze a dataset of human body temperatures and employ the concepts of hypothesis testing, confidence intervals, and statistical significance.</p>

<p>Answer the following questions <b>in this notebook below and submit to your Github account</b>.</p> 

<ol>
<li>  Is the distribution of body temperatures normal? 
    <ul>
    <li> Although this is not a requirement for CLT to hold (read CLT carefully), it gives us some peace of mind that the population may also be normally distributed if we assume that this sample is representative of the population.
    </ul>
<li>  Is the sample size large? Are the observations independent?
    <ul>
    <li> Remember that this is a condition for the CLT, and hence the statistical tests we are using, to apply.
    </ul>
<li>  Is the true population mean really 98.6 degrees F?
    <ul>
    <li> Would you use a one-sample or two-sample test? Why?
    <li> In this situation, is it appropriate to use the $t$ or $z$ statistic? 
    <li> Now try using the other test. How is the result be different? Why?
    </ul>
<li>  At what temperature should we consider someone's temperature to be "abnormal"?
    <ul>
    <li> Start by computing the margin of error and confidence interval.
    </ul>
<li>  Is there a significant difference between males and females in normal temperature?
    <ul>
    <li> What test did you use and why?
    <li> Write a story with your conclusion in the context of the original problem.
    </ul>
</ol>

You can include written notes in notebook cells using Markdown: 
   - In the control panel at the top, choose Cell > Cell Type > Markdown
   - Markdown syntax: http://nestacms.com/docs/creating-content/markdown-cheat-sheet

#### Resources

+ Information and data sources: http://www.amstat.org/publications/jse/datasets/normtemp.txt, http://www.amstat.org/publications/jse/jse_data_archive.htm
+ Markdown syntax: http://nestacms.com/docs/creating-content/markdown-cheat-sheet

****
</div>
End of explanation
"""


sns.distplot(df['temperature'])

"""
Explanation: Is the distribution of body temperatures normal?
End of explanation
"""


df.shape

df.head()

"""
Explanation: From the bar chart we can say that the distrubition resembles normal.
Is the sample size large? Are the observations independent?
End of explanation
"""


stddev_sd = df.temperature.std() / np.sqrt(130)
sample_mean = df.temperature.mean()

z_statistic = (sample_mean - 98.6) / stddev_sd
(len(df), sample_mean, sample_stddev, z_statistic)

"""
Explanation: Is the true population mean really 98.6 degrees F?
End of explanation
"""


other = pd.Series(np.random.normal(98.6, df.temperature.std(), 130))
other_mean = other.mean()
pooled_stddev = np.sqrt(sample_stddev * sample_stddev/130 + other.var()/130)
(other_mean, pooled_stddev, other_mean - sample_mean)

"""
Explanation: As I'm comparing a sample mean to a fixed value (assumed population mean of 98.6F), I'm using one-sample test. In the situation we have n >> 30 thus it's appropriate to use z-statistic. There is a distance of 5.45 std dev between sample mean and the assumed population mean, therefore it gives us very high confidence (>99.9%) that 98.6F is not the true population mean. In order to make a two-sample test I'm going to generate a second sample based on the assumed population mean and sample standard deviation.
End of explanation
"""


z_statistic = (other_mean - sample_mean) / pooled_stddev
z_statistic

"""
Explanation: I assume there is no difference between two sample means (H0: sample_mean - other_mean = 0). I'm going to show 99% confidence, that H0 is not true and that there is in reality a difference between those two means. For that, the distance between means has to be >= 2.58 (z-value for 0.995 - two-sided test).
End of explanation
"""


(sample_mean - 1.96*sample_stddev, sample_mean + 1.96*sample_stddev, 1.96*sample_stddev)

"""
Explanation: At what temperature should we consider someone's temperature to be "abnormal"?
End of explanation
"""


males = df.temperature[df.gender=='M']
females = df.temperature[df.gender=='F']
(males.size, females.size)

males_mean = males.mean()
females_mean = females.mean()
males_std = males.std()
females_std = females.std()
(males_mean, females_mean)

(males_mean - females_mean) / sample_stddev

"""
Explanation: Margin of error is 0.126F and the confidence interval (98.12F, 98.38F) which means any temperature below 98.12F or above 98.38F would be considered abnormal.
Is there a significant difference between males and females in normal temperature?
End of explanation
"""


