Skip to main content

A tool for doing hypothesis testing

Project description

import sys
import os
sys.path.append(os.path.abspath(os.path.join('..')))


Description

A package to run hypothesis testing for one and two samples.


One Sample Hypothesis Testing

from hypothesis_testing_tool.compute_stats.one_sample_statistics import OneSampleTest

Let's see an example of how you can use the OneSampleTest class.

import random

# Set the seed for reproducibility
seed_value = 42
random.seed(seed_value)

random_sample = [random.gauss(mu = 5, sigma = 1) for _ in range(1000)]
t_test = OneSampleTest(
        data = random_sample,
        null_population_mean = 3.5
        ).t_test_results

print(f"p-value: {t_test.pvalue:.2f}")
print(f"t-statistic: {t_test.statistic:.2f}")
p-value: 0.00
t-statistic: 45.96

The default is with alternative = "two-sided", but that can change to a one tail test.

t_test_less = OneSampleTest(
        data = random_sample,
        null_population_mean = 3.5,
        alternative = "less"
        ).t_test_results

print(f"p-value: {t_test_less.pvalue:.2f}")
print(f"t-statistic: {t_test_less.statistic:.2f}")
p-value: 1.00
t-statistic: 45.96
t_test_greater = OneSampleTest(
        data = random_sample,
        null_population_mean = 3.5,
        alternative = "greater"
        ).t_test_results

print(f"p-value: {t_test_greater.pvalue:.2f}")
print(f"t-statistic: {t_test_greater.statistic:.2f}")
p-value: 0.00
t-statistic: 45.96

You can also compute the confidence interval (default = 95%) for the mean, using the calculate_ci method.

The calculate_ci method takes an optional argument alpha to adjust to 99% CI (alpha = 0.01) or any other.

confidence_interval = OneSampleTest(
        data = random_sample,
        null_population_mean = 3.5
        ).calculate_ci()
confidence_interval
{'lower_bound': 4.918459428969473,
 'point_estimate': 4.9817306915192265,
 'upper_bound': 5.04500195406898,
 'null_population_mean': 3.5}

Finally, you can create a plot with the CI and save it to a local path.

from hypothesis_testing_tool.presentation.create_plots import create_one_sample_hypothesis_plot
create_one_sample_hypothesis_plot(
    path_to_save_plot = "artifacts/one_sample_plot.png",
    ci_dict = confidence_interval,
    width = 10,
    height = 5
)
(<Figure size 1000x500 with 1 Axes>,
 <Axes: title={'center': '95% Confidence Interval (CI) for the Mean of One Sample'}, xlabel='Values'>)

In the plot above the 95% confidence interval includes 3.5, so we do not reject the null hypothesis.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypothesis_testing_tool-0.1.2.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

hypothesis_testing_tool-0.1.2-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page