<jupyter_start><jupyter_text>This is an implication of the Central Limit Theorem.If the sample has no sample errors or response errors, then we can use the sample to infer things about the population.The confidence interval is where we think the population parameter lies, based on a confidence level.  Finding the CI Given:a sample of the population$ \mu $ : the population parameter we want to know about, which is unknown$ n $ : the size of the sample$ \bar{x} $ : the sample statisticconfidence between 0 and 1$ \alpha $ : 1-confidence<jupyter_code>sample = c(1,2,4,5,6,9,12,23,37)
alpha = 0.05
n = length(sample)
sample_statistic = mean(sample)
sample_statistic<jupyter_output><empty_output><jupyter_text>When you know the $ \sigma $ of the populationWhen you know the population's standard deviation, then first, use $ \frac{\alpha}{2} $ to find Z from the normal distribution.$ \bar{x} \pm Z \frac{\sigma}{\sqrt{n}} $<jupyter_code>standard_deviation = NaN # This must be defined, otherwise, use the T distribution
error = standard_deviation/sqrt(n)
Z = qnorm(1-alpha/2)

CI_lower = sample_statistic - Z*error
CI_upper = sample_statistic + Z*error

CI_lower
CI_upper
sprintf("We can say with %spc confidence, assuming the sample is random and is a normal distribution, the population parameter is between %s and %s", (1-alpha)*100, CI_lower, CI_upper)<jupyter_output><empty_output><jupyter_text>When you don't know $ \sigma $, or if the sample is smaller than 120, you should instead use the sample's standard deviationUse $ \frac{\alpha}{2} $ and the $ df $ to find the T from the student distribution$ \bar{x} \pm T \frac{S}{\sqrt{n}} $ Above a sample size of about 120, the Student (T) and Normal distributions are almost the same<jupyter_code>variance = var(sample)
standard_deviation = sqrt(variance) # Is a sample SD by default
error = standard_deviation/sqrt(n)
degrees_of_freedom = n-1
T = qt(1-alpha/2, degrees_of_freedom)

CI_lower = sample_statistic - 1.89457860509001*error
CI_upper = sample_statistic + 1.89457860509001*error

CI_lower
CI_upper
sprintf("We can say with %spc confidence, assuming the sample is random and is a normal distribution, the population parameter is between %s and %s", (1-alpha)*100, CI_lower, CI_upper)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09306409130816505, 'fraction_numerical': 0.03028972783143108, 'mean_word_length': 5.437853107344632, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '595039', 'license': 'MIT', 'n_tokens_mistral': 749, 'n_tokens_neox': 693, 'n_words': 271, 'path': 'statistics/confidence_intervals.ipynb', 'repo_name': 'JasonThomasData/algorithms', 'size': '2278'}