Example #1
0
    def byConfidenceInterval(self) -> ConfidenceIntervalResults:
        """
        Classifies individual observations as data, lower outliers, and
        upper outliers. Leverages a definition of confidence interval
        that extends one standard deviation of the upper data above,
        and one standard deviation of the lower data below. All other
        data are outliers.
        """
        global_mean: Rational = Moment.mean(self.data)

        upper, lower = ops.splitList(self.data.data,
                                     lambda obs: obs <= global_mean)
        upper_std_dev: Rational = Moment.std_dev(Vector(upper))
        lower_std_dev: Rational = Moment.std_dev(Vector(lower))
        np_upper = np.std(upper)
        np_lower = np.std(lower)

        upper_outliers, upper_data = ops.splitList(
            upper, lambda obs: obs <= global_mean + upper_std_dev)
        lower_outliers, lower_data = ops.splitList(
            lower, lambda obs: obs >= global_mean - lower_std_dev)

        return ConfidenceIntervalResults(global_mean, upper_std_dev,
                                         lower_std_dev,
                                         upper_data + lower_data,
                                         Vector(lower_outliers).sort().data,
                                         Vector(upper_outliers).sort().data)
Example #2
0
def test_gaussian() -> None:
    truth: np.array = np.random.normal(0, 1, size=100)
    wide: np.array = np.random.normal(0, 3, size=100)
    biased: np.array = np.random.normal(6, 1, size=100)

    to_ll: Callable[[List[Rational]], Callable[
        [Rational], Rational]] = lambda data: Likelihood.gaussian(
            Moment.mean(Vector(data)), Moment.std_dev(Vector(data)))

    truth_ll: Callable[[Rational], Rational] = to_ll(truth)
    wide_ll: Callable[[Rational], Rational] = to_ll(wide)
    biased_ll: Callable[[Rational], Rational] = to_ll(biased)

    draw_truth_from_truth: Rational = Likelihood.log_likelihood(
        truth, truth_ll)
    draw_wide_from_truth: Rational = Likelihood.log_likelihood(wide, truth_ll)
    draw_biased_from_truth: Rational = Likelihood.log_likelihood(
        biased, truth_ll)

    draw_truth_from_wide: Rational = Likelihood.log_likelihood(truth, wide_ll)
    draw_wide_from_wide: Rational = Likelihood.log_likelihood(wide, wide_ll)
    draw_biased_from_wide: Rational = Likelihood.log_likelihood(
        biased, wide_ll)

    draw_truth_from_biased: Rational = Likelihood.log_likelihood(
        truth, biased_ll)
    draw_wide_from_biased: Rational = Likelihood.log_likelihood(
        wide, biased_ll)
    draw_biased_from_biased: Rational = Likelihood.log_likelihood(
        biased, biased_ll)

    assert draw_truth_from_truth > draw_wide_from_truth
    assert draw_truth_from_truth > draw_biased_from_truth

    assert draw_wide_from_wide < draw_truth_from_wide  # Note that draws from the middle of wide will have higher likelihood
    assert draw_wide_from_wide > draw_biased_from_wide

    assert draw_biased_from_biased > draw_truth_from_biased
    assert draw_biased_from_biased > draw_wide_from_biased
Example #3
0
def test_std_dev() -> None:
    assert withinTol(Moment.std_dev(vn01), np.std(n01)) == True
Example #4
0
def test_variance() -> None:
    test: float = Moment.variance(vn01)
    truth: float = np.var(n01)
    assert withinTol(Moment.variance(vn01), np.var(n01)) == True
Example #5
0
def test_trimmed_mean() -> None:
    p: int = 10
    assert Moment.trimmed_mean(vsimple, 1) == 2
    assert withinTol(Moment.trimmed_mean(vn01, p), np.mean(n01[p:-p]))
Example #6
0
def test_mean() -> None:
    assert Moment.mean(vsimple) == 2.25
    test: float = Moment.mean(vn01)
    truth: float = np.mean(n01)
    assert withinTol(Moment.mean(vn01), np.mean(n01)) == True
Example #7
0
 def to_gaussian(data: List[Rational]) -> Callable[[Rational], Rational]:
     return Likelihood.gaussian(Moment.mean(Vector(data)),
                                Moment.std_dev(Vector(data)))