def apply_t_test(samples, population_mean, alpha=0.05, difference=1.0, desc=""):
    """
    Applies a t-test, considering test power also.
    :param samples: Samples from simulation.
    :param population_mean: Mean on the test dataset.
    :param alpha: Type 1 risk: Rejecting null when null is true.
    :param difference: Expected difference.
    :return: None
    """
    t_stat, two_tail_prob = stats.ttest_1samp(samples, population_mean)
    logger.info(desc + ": Two-Sample T-test: t-statistic " + str(t_stat) + " p-value " + str(two_tail_prob))

    sample_mean = np.mean(samples)
    sample_size = len(samples)
    sample_std = np.std(samples)
    df = sample_size - 1

    # FYI: The examples on Discrete-Event Simulation by Jerry Banks were replicated using the following python code.
    threshold = stats.t.ppf(1 - alpha / 2, df)
    logger.info(desc + ": Critical value of t: " + str(threshold) + " for a level of significance (alpha) " + str(
        alpha) + " and degrees of freedom " + str(df))

    null_hypothesis = "The mean of the sample (" + str(sample_mean) + ") is equal to the population mean ( " + str(
        population_mean) + ")"

    reject_null = None
    if abs(t_stat) > threshold:
        reject_null = True
        logger.info(desc + ": We REJECT the null hypothesis: " + null_hypothesis)
    else:
        reject_null = False
        logger.info(desc + ": We CANNOT REJECT the null hypothesis " + null_hypothesis)

    effect_size = difference / sample_std
    logger.info(desc + ": Effect size for a difference of " + str(difference) + ": " + str(effect_size))

    test_power = power.tt_solve_power(effect_size=effect_size, alpha=alpha, nobs=sample_size)
    logger.info(desc + ": Test power: " + str(test_power))

    return reject_null, test_power
Esempio n. 2
0
    ttest_pow = TTestPower()
    print('\nroundtrip - root with respect to all variables')
    print('\n       calculated, desired')

    nobs_p = ttest_pow.solve_power(effect_size=effect_size, nobs=None, alpha=alpha, power=power)
    print('nobs  ', nobs_p)
    print('effect', ttest_pow.solve_power(effect_size=None, nobs=nobs_p, alpha=alpha, power=power), effect_size)

    print('alpha ', ttest_pow.solve_power(effect_size=effect_size, nobs=nobs_p, alpha=None, power=power), alpha)
    print('power  ', ttest_pow.solve_power(effect_size=effect_size, nobs=nobs_p, alpha=alpha, power=None), power)

    print('\nroundtrip - root with respect to all variables')
    print('\n       calculated, desired')

    print('nobs  ', tt_solve_power(effect_size=effect_size, nobs=None, alpha=alpha, power=power), nobs_p)
    print('effect', tt_solve_power(effect_size=None, nobs=nobs_p, alpha=alpha, power=power), effect_size)

    print('alpha ', tt_solve_power(effect_size=effect_size, nobs=nobs_p, alpha=None, power=power), alpha)
    print('power  ', tt_solve_power(effect_size=effect_size, nobs=nobs_p, alpha=alpha, power=None), power)

    print('\none sided')
    nobs_p1 = tt_solve_power(effect_size=effect_size, nobs=None, alpha=alpha, power=power, alternative='larger')
    print('nobs  ', nobs_p1)
    print('effect', tt_solve_power(effect_size=None, nobs=nobs_p1, alpha=alpha, power=power, alternative='larger'), effect_size)
    print('alpha ', tt_solve_power(effect_size=effect_size, nobs=nobs_p1, alpha=None, power=power, alternative='larger'), alpha)
    print('power  ', tt_solve_power(effect_size=effect_size, nobs=nobs_p1, alpha=alpha, power=None, alternative='larger'), power)

    #start_ttp = dict(effect_size=0.01, nobs1=10., alpha=0.15, power=0.6)

    ttind_solve_power = TTestIndPower().solve_power
Esempio n. 3
0
#Calculate effect size over a range of hypothetical save percentages compared to average
Effect_Size = (np.arange(93, 100.25, 0.25) - SV_Average) / SV_Deviation

#Set alpha to be constant at 0.05
alpha = 0.05

#Calculate sample size required to achieve specified power over the range of effect sizes calculated above
Sample_80 = []
Sample_85 = []
Sample_90 = []
Sample_95 = []
for effect in Effect_Size:
    Sample_80.append(
        tt_solve_power(effect_size=effect,
                       nobs=None,
                       alpha=alpha,
                       power=0.8,
                       alternative='two-sided'))
    Sample_85.append(
        tt_solve_power(effect_size=effect,
                       nobs=None,
                       alpha=alpha,
                       power=0.85,
                       alternative='two-sided'))
    Sample_90.append(
        tt_solve_power(effect_size=effect,
                       nobs=None,
                       alpha=alpha,
                       power=0.9,
                       alternative='two-sided'))
    Sample_95.append(
Esempio n. 4
0
                              alpha=None,
                              power=power), alpha)
    print(
        'power  ',
        ttest_pow.solve_power(effect_size=effect_size,
                              nobs=nobs_p,
                              alpha=alpha,
                              power=None), power)

    print('\nroundtrip - root with respect to all variables')
    print('\n       calculated, desired')

    print(
        'nobs  ',
        tt_solve_power(effect_size=effect_size,
                       nobs=None,
                       alpha=alpha,
                       power=power), nobs_p)
    print(
        'effect',
        tt_solve_power(effect_size=None, nobs=nobs_p, alpha=alpha,
                       power=power), effect_size)

    print(
        'alpha ',
        tt_solve_power(effect_size=effect_size,
                       nobs=nobs_p,
                       alpha=None,
                       power=power), alpha)
    print(
        'power  ',
        tt_solve_power(effect_size=effect_size,