Esempio n. 1
0
def test_bivariate_normal_likelihood_probability_bounds(x, y):
    sample_x_mean_whole = estimators.sample_mean(x)
    sample_x_var_whole = estimators.sample_variance(x)
    sample_y_mean_whole = estimators.sample_mean(y)
    sample_y_var_whole = estimators.sample_variance(y)
    sample_xy_cov_whole = estimators.sample_covariance(x, y)
    data = [[sample_x, y[i]] for i, sample_x in enumerate(x)]
    params = [
        sample_x_mean_whole, sample_y_mean_whole, sample_x_var_whole,
        sample_y_var_whole, sample_xy_cov_whole
    ]
    likelihood_whole = math.exp(
        estimators.bivariate_normal_likelihood_function(params, data))

    sample_x_mean_truncated = estimators.sample_mean(x)
    sample_x_var_truncated = estimators.sample_variance(x)
    sample_y_mean_truncated = estimators.sample_mean(y)
    sample_y_var_truncated = estimators.sample_variance(y)
    sample_xy_cov_truncated = estimators.sample_covariance(x, y)
    data = [[sample_x, y[i]] for i, sample_x in enumerate(x[:-2])]
    params = [
        sample_x_mean_truncated, sample_y_mean_truncated,
        sample_x_var_truncated, sample_y_var_truncated, sample_xy_cov_truncated
    ]
    likelihood_truncated = math.exp(
        estimators.bivariate_normal_likelihood_function(params, data))

    # NOTE: see note in previous test.
    assert (likelihood_truncated > likelihood_whole)
Esempio n. 2
0
def test_rolling_recursive_mean(first_x, second_x):
    lost_obs, new_obs = first_x[0], second_x[-1]
    n = len(first_x)
    actual_previous_mean = estimators.sample_mean(first_x)
    actual_next_mean = estimators.sample_mean(second_x)
    recursive_mean = estimators.recursive_rolling_mean(actual_previous_mean,
                                                       new_obs, lost_obs, n)
    assert (settings.is_within_tolerance(
        lambda: recursive_mean - actual_next_mean))
Esempio n. 3
0
def test_univariate_normal_likelihood_probability_bounds(x):
    sample_mean = estimators.sample_mean(x)
    sample_variance = estimators.sample_variance(x)
    likelihood = math.exp(
        estimators.univariate_normal_likelihood_function(
            [sample_mean, sample_variance], x))
    assert (likelihood > 0 and likelihood < 1)
Esempio n. 4
0
def test_univariate_normal_likelihood_monotonicity(x):
    """
    Assume the samples are populations, then sample mean is population mean. The sample mean of the 
    truncated sample is distributed around the sample mean of the population. In other words, observations 
    from the population and the truncated sample have the same distribution. Therefore, by the monotonicity
    of probability distributions, the probability of the truncated sample should be less than the probability
    of the entire population.
    """
    sample_mean = estimators.sample_mean(x)
    sample_variance = estimators.sample_variance(x)
    likelihood_whole = math.exp(
        estimators.univariate_normal_likelihood_function(
            [sample_mean, sample_variance], x))

    likelihood_truncated = math.exp(
        estimators.univariate_normal_likelihood_function(
            [sample_mean, sample_variance], x[:-2]))

    # NOTE:
    #   Proposition
    #       the event of the whole sample is a subset of the event of the truncated sample
    #
    #   Notes
    #       observing the sequence (1, 2, 3) is a subset of observing the sequence (1, 2)
    #                                                                         e.g. (1, 2, 3)
    #                                                                              (1, 2, 75),
    #                                                                              (1, 2, -6), etc.
    #
    #       is this true? isn't observing two random elements from a sample a different type of thing than
    #       observing three random elements? or can we say, the probability the third element is something
    #       from the sample is 1, so the probability of the sequence (1, 2) is equal to the probability
    #       (1, 2, x) where x represents all possible values of the third element from the population?
    #
    #       does the equivalence of probability imply these two events are the same event? is the probability
    #       of all possible future events embedded in the probability of a sequence, if it is understood
    #       the sequence is generated identical random draws from the same population?
    assert (likelihood_truncated > likelihood_whole)
Esempio n. 5
0
def test_mean(x, mu):
    mean = estimators.sample_mean(x=x)
    assert (settings.is_within_tolerance(lambda: mean - mu))
Esempio n. 6
0
def test_mean_missing_samples(x):
    with pytest.raises(Exception) as sample_error:
        estimators.sample_mean(x=x)
    assert sample_error.type == ValueError
Esempio n. 7
0
def test_mean_null_sample(x):
    with pytest.raises(Exception) as sample_error:
        estimators.sample_mean(x=x)
    assert sample_error.type == SampleSizeError