コード例 #1
0
 def test_getAdaptiveRangeMulti(self):
     measurements = [
         value.ValueUncertainty(),
         value.ValueUncertainty(2.0, 0.5)
     ]
     dist = distributions.Distribution(measurements=measurements)
     result = distributions.distributions_ops.adaptive_range(dist, 10.0)
コード例 #2
0
 def test_overlap(self):
     d1 = distributions.Distribution(
         measurements=[value.ValueUncertainty()])
     d2 = distributions.Distribution(
         measurements=[value.ValueUncertainty()])
     self.assertAlmostEqual(distributions.distributions_ops.overlap(d1, d2),
                            1.0)
コード例 #3
0
    def rotate(self, rotation_angle, origin=None):
        """ Rotates the position value by the specified angle using a standard
            2D rotation matrix formulation. If an origin Position2D instance is
            not specified the rotation will occur around the origin. Also, if
            an origin is specified, the uncertainty in that origin value will
            be propagated through to the uncertainty of the rotated result.

        :param rotation_angle:
        :param origin: (optional)
        :return:
        """

        if origin is None:
            origin = self.__class__(
                value.ValueUncertainty(0, 0),
                value.ValueUncertainty(0, 0)
            )

        a = rotation_angle.radians
        x = self.x.raw - origin.x.raw
        y = self.y.raw - origin.y.raw

        self.x.update(
            x * math.cos(a) - y*math.sin(a) + origin.x.raw,
            ops.sqrt_sum_of_squares(self.x.uncertainty, origin.x.uncertainty)
        )
        self.y.update(
            y * math.cos(a) + x*math.sin(a) + origin.y.raw,
            ops.sqrt_sum_of_squares(self.y.uncertainty, origin.y.uncertainty)
        )

        return self
コード例 #4
0
def weighted(*values):
    """
    Calculates the uncertainty weighted average of the provided values,
    where each value is a ValueUncertainty instance. For mathematical
    formulation of the weighted average see "An Introduction to Error
    Analysis, 2nd Edition" by John R. Taylor, Chapter 7.2.

    :param values:
    :return:
    """

    if not values:
        return value.ValueUncertainty()

    if isinstance(values[0], (list, tuple)):
        values = values[0]
        if not values:
            return value.ValueUncertainty()

    wxs = 0.0
    ws = 0.0
    for v in values:
        w = 1.0 / (v.uncertainty * v.uncertainty)
        wxs += w * v.value
        ws += w

    ave = wxs / ws
    unc = 1.0 / math.sqrt(ws)

    return value.ValueUncertainty(value=ave, uncertainty=unc)
コード例 #5
0
    def __init__(self, x=None, y=None):
        if x is None:
            x = value.ValueUncertainty()
        if y is None:
            y = value.ValueUncertainty()

        self.x = x
        self.y = y
コード例 #6
0
    def test_angleBetween(self):
        p1 = value2D.Point2D(value.ValueUncertainty(2.0, 0.1),
                             value.ValueUncertainty(0.0, 0.1))
        p2 = value2D.Point2D(value.ValueUncertainty(0.0, 0.1),
                             value.ValueUncertainty(2.0, 0.1))

        a = p1.angle_between(p2)
        self.assertAlmostEquals(a.degrees, 90.0, 1)
コード例 #7
0
    def test_compareAgainstGaussian3(self):
        d1 = distributions.Distribution(measurements=[value.ValueUncertainty()])
        d2 = distributions.Distribution(measurements=[
            value.ValueUncertainty(5.0),
            value.ValueUncertainty(8.0),
            value.ValueUncertainty(10.0, 2.0)
        ])

        self.assertGreaterEqual(distributions.distributions_ops.overlap(d1, d2), 0.0)
        self.assertLess(distributions.distributions_ops.overlap(d1, d2), 0.06)
コード例 #8
0
    def test_doubleDensityOverlap(self):
        """ Two overlapping measurement values should have a total probability
            of unity
        """
        x_values = np.linspace(-10.0, 10.0, 100)
        measurements = [value.ValueUncertainty(), value.ValueUncertainty()]

        dist = distributions.Distribution(measurements=measurements)
        area = get_area_under_curve(x_values, dist.probabilities_at(x_values))
        self.assertAlmostEqual(area, 1.0, 3)
コード例 #9
0
    def test_weightedAverage(self):
        """ doc... """
        values = [
            value.ValueUncertainty(11.0, 1.0),
            value.ValueUncertainty(12.0, 1.0),
            value.ValueUncertainty(10.0, 3.0)
        ]

        result = mean.weighted(*values)

        self.assertEqual(result.value, 11.4, 'Value Match')
        self.assertEqual(result.uncertainty, 0.7, 'Value Match')
コード例 #10
0
    def test_arithmetic(self):
        """
        This test is from a problem in Taylor's Error Analysis 3.9 (p. 68)
        """
        l = value.ValueUncertainty(92.95, 0.1)
        T = value.ValueUncertainty(1.936, 0.004)

        g = 4.0 * (math.pi**2) * l / (T**2)

        self.assertIsInstance(g, value.ValueUncertainty)
        self.assertAlmostEqual(g.value, 979.0)
        self.assertAlmostEqual(g.uncertainty, 4.0)
コード例 #11
0
def create_point(x=0.0, y=0.0, x_unc=0.001, y_unc=0.001):
    """

    :param x:
    :param y:
    :param x_unc:
    :param y_unc:
    :return:
    """

    return Point2D(x=value.ValueUncertainty(x, x_unc),
                   y=value.ValueUncertainty(y, y_unc))
コード例 #12
0
    def test_simple_arithmetic(self):
        """
        A very simple error propagation test example
        """

        width = value.ValueUncertainty(11, 0.4)
        length = value.ValueUncertainty(8, 0.3)

        area = length * width

        print(area.label)
        self.assertAlmostEqual(area.value, 88)
        self.assertAlmostEqual(area.uncertainty, 5)
コード例 #13
0
    def test_doubleDensityOffset(self):
        """ Two measurements with different values and uncertainties should
            still result in a total probability of unity
        """

        x_values = np.linspace(-10.0, 25.0, 100)
        measurements = [
            value.ValueUncertainty(),
            value.ValueUncertainty(2.0, 2.0)
        ]

        dist = distributions.Distribution(measurements=measurements)
        area = get_area_under_curve(x_values, dist.probabilities_at(x_values))
        self.assertAlmostEqual(area, 1.0, 3)
コード例 #14
0
    def test_toValueUncertainty(self):
        """test_toValueUncertainty doc..."""
        v = value.ValueUncertainty(math.pi, 0.00456)
        self.assertEqual(v.value, 3.142, 'Values do not match %s' % v.label)
        self.assertEqual(v.uncertainty, 0.005,
                         'Uncertainties do not match %s' % v.label)

        v = value.ValueUncertainty(100.0 * math.pi, 42.0)
        self.assertEqual(v.value, 310.0, 'Values do not match %s' % v.label)
        self.assertEqual(v.uncertainty, 40.0,
                         'Uncertainties do not match %s' % v.label)

        v = value.ValueUncertainty(0.001 * math.pi, 0.000975)
        self.assertEqual(v.value, 0.003, 'Values do not match %s' % v.label)
        self.assertEqual(v.uncertainty, 0.001,
                         'Uncertainties do not match %s' % v.label)
コード例 #15
0
    def test_tukey_box(self):
        measurements = []
        while len(measurements) < 6:
            measurements.append(value.ValueUncertainty())

        dist = distributions.Distribution(measurements=measurements)
        unweighted = boxes.unweighted_tukey(dist)
        weighted = boxes.weighted_tukey(dist)
コード例 #16
0
def unweighted(*values):
    """

    :param values:
    :return:
    """

    if not values:
        return value.ValueUncertainty()

    if isinstance(values[0], (list, tuple)):
        values = values[0]
        if not values:
            return value.ValueUncertainty()

    values = [v.value for v in values]
    return value.ValueUncertainty(value=float(np.mean(values)),
                                  uncertainty=float(np.std(values)))
コード例 #17
0
    def test_rotate(self):
        tests = [(90.0, 0.0, 1.0), (-90.0, 0.0, -1.0), (180.0, -1.0, 0.0),
                 (-180.0, -1.0, 0.0), (270.0, 0.0, -1.0), (-270.0, 0.0, 1.0),
                 (360.0, 1.0, 0.0), (-360.0, 1.0, 0.0),
                 (45.0, HALF_SQRT_2, HALF_SQRT_2),
                 (-45.0, HALF_SQRT_2, -HALF_SQRT_2),
                 (315.0, HALF_SQRT_2, -HALF_SQRT_2),
                 (-315.0, HALF_SQRT_2, HALF_SQRT_2), (30.0, HALF_SQRT_3, 0.5),
                 (-30.0, HALF_SQRT_3, -0.5), (330.0, HALF_SQRT_3, -0.5),
                 (-330.0, HALF_SQRT_3, 0.5)]

        for test in tests:
            radius = random.uniform(0.001, 1000.0)
            p = value2D.Point2D(value.ValueUncertainty(radius, 0.25),
                                value.ValueUncertainty(0.0, 0.25))

            p.rotate(angle.Angle(degrees=test[0]))
            self.assertAlmostEqual(p.x.raw, radius * test[1], 2)
            self.assertAlmostEqual(p.y.raw, radius * test[2], 2)
コード例 #18
0
def weighted_mean_and_deviation(*values):
    """
    Returns the mean and standard deviation of a weighted set of values.
    For further info see:
        http://stats.stackexchange.com/questions/6534/
            how-do-i-calculate-a-weighted-standard-deviation-in-excel

    :param values:
    :return:
    """

    if not values:
        return value.ValueUncertainty()

    if isinstance(values[0], (list, tuple)):
        values = values[0]
        if not values:
            return value.ValueUncertainty()

    if len(values) == 1:
        return values[0].clone()

    wxs = 0.0
    ws = 0.0
    weights = []

    for v in values:
        w = 1.0 / (v.uncertainty * v.uncertainty)
        weights.append(w)
        wxs += w * v.value
        ws += w

    ave = wxs / ws
    dev = 0.0
    N = len(values)
    for i in range(N):
        dev += weights[i] * (values[i].value - ave)**2

    denom = ws * (N - 1.0) / N
    dev = math.sqrt(dev / denom)

    return value.ValueUncertainty(value=ave, uncertainty=dev)
コード例 #19
0
    def test_roundingIssue(self):
        """
        There was a rounding issue that I wanted to check to see was correct.
        This confirms that as strange as it looks, it is correct.
        """

        x1 = value.ValueUncertainty(1.3125, 0.010050373127401788)
        y1 = value.ValueUncertainty(0.2, 0.010050373127401788)
        x2 = value.ValueUncertainty(1.3125, 0.08749999999999997)
        y2 = value.ValueUncertainty(0.0, 0.010050373127401788)

        a = x2 - x1
        a_squared = a**2
        b = y2 - y1
        b_squared = b**2
        summed = a_squared + b_squared

        result = summed**0.5

        print(result)
コード例 #20
0
    def test_weighted_mad(self):
        """
        """

        delta = 1.8

        measurements = []
        for index in range(10):
            measurements.append(value.ValueUncertainty(delta, 1.1))
            measurements.append(value.ValueUncertainty(-delta, 1.1))

        dist = distributions.Distribution(measurements)

        median = distributions.distributions_ops.percentile(dist, 0.5)
        mad = distributions.distributions_ops.weighted_median_average_deviation(
            dist)

        self.assertAlmostEqual(median, 0, places=1)
        self.assertAlmostEqual(mad,
                               delta,
                               delta=0.5,
                               msg='Median: {}'.format(median))
コード例 #21
0
 def value(self):
     return value.ValueUncertainty(self.radians, self.uncertainty)
コード例 #22
0
 def value_degrees(self):
     return value.ValueUncertainty(self.degrees, self.uncertainty_degrees)
コード例 #23
0
 def test_getAdaptiveRange(self):
     dist = distributions.Distribution(
         measurements=[value.ValueUncertainty()])
     result = distributions.distributions_ops.adaptive_range(dist, 10.0)
コード例 #24
0
 def test_compareAgainstGaussian2(self):
     d1 = distributions.Distribution(
         measurements=[value.ValueUncertainty()])
     d2 = distributions.Distribution(
         measurements=[value.ValueUncertainty(uncertainty=0.5)])
     self.assertLess(distributions.distributions_ops.overlap(d1, d2), 0.7)