Ejemplo n.º 1
0
    def gaussian_misclassification(this: List[Rational],
                                   that: List[Rational]) -> Rational:
        """ 
        Calculates the sample means and standard deviations of this and that, and then uses
        them to calculate the log-likelihood of drawing this from that and vice versa. The
        Results are then averaged together to provide a relative measure of the likelihood of
        misclassification. Lower values indicate lower probability of misclassification.

        Since likelihood scales with the number of observations, the same number of observations
        are used from both this and that.
        """
        this_g: Callable[[Rational], Rational] = Likelihood.to_gaussian(this)
        that_g: Callable[[Rational], Rational] = Likelihood.to_gaussian(that)

        # permuting in case we get a sorted list
        zipped: List[Tuple[Rational, Rational]] = ops.zipLists(
            np.random.permutation(this), np.random.permutation(that))

        def tuple_ll_f(
                agg: Tuple[Rational, Rational],
                next_val: Tuple[Rational,
                                Rational]) -> Tuple[Rational, Rational]:
            """ 
            Combines likelihoods of drawing the next value of this or that from the other distribution with
            the running likelihood total.
            """
            this_agg, that_agg = agg
            this_next, that_next = next_val
            return (this_agg + that_g(this_next), that_agg + this_g(that_next))

        zipped_ll: Tuple[Rational,
                         Rational] = ops.foldSeq(zipped, (0, 0), tuple_ll_f)
        this_ll, that_ll = zipped_ll

        return (this_ll + that_ll) / 2
Ejemplo n.º 2
0
 def log_likelihood(data: Sequence[Rational],
                    dist_ll: Callable[[Rational], Rational]) -> Rational:
     """ 
     Given a log-likelihood function and data, the function returns the aggregate 
     log-likelihood over the entire data sequence.
     """
     return ops.foldSeq(data, 0, lambda out, next: out + dist_ll(next))
Ejemplo n.º 3
0
 def covariance(this: Vector, that: Vector) -> Rational:
     if this.length != that.length:
         raise ValueError("Input vectors must be of equivalent length")
     zipped: Iterator[Tuple[Rational, Rational]] = zip(this.data, that.data)
     dev_product: Callable[[Rational, Rational], Rational] = lambda s, o: (s - Moment.mean(this)) * (o - Moment.mean(that))
     sum_dev_prods: Rational = ops.foldSeq(zipped, 0, lambda out, next: out + dev_product(next[0], next[1]))
     return sum_dev_prods / (this.length - 1)
Ejemplo n.º 4
0
    def conditional_prop(self, predicate: Callable[[Rational],
                                                   bool]) -> Rational:
        def f(counts: Tuple[Rational, Rational],
              next_val: Rational) -> Tuple[Rational, Rational]:
            old_total, old_passed = counts
            return (old_total + 1,
                    old_passed + 1 if predicate(next_val) else old_passed)

        total, passed = ops.foldSeq(self.data, (0, 0), f)
        return passed / total
Ejemplo n.º 5
0
 def higherOrderMoment(data: Vector, order: int) -> Rational:
     sum_exp_dev: Rational = ops.foldSeq(data, 0, lambda out, next: out + (next - Moment.mean(data))**order)
     raw_moment: Rational = sum_exp_dev / (data.length - 1)
     return raw_moment / (Moment.std_dev(data)**order)
Ejemplo n.º 6
0
 def variance(data: Vector) -> Rational:
     mu: Rational = Moment.mean(data)
     sum_squares: Rational = ops.foldSeq(data, 0, lambda out, xi: out + (xi - mu)**2)
     return sum_squares / (data.length - 1)