def test_independent():
    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         ExponentialDistribution(2)])

    assert_equal(round(d.log_probability((4, 1)), 4), -3.0439)
    assert_equal(round(d.log_probability((100, 0.001)), 4), -1129.0459)

    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         ExponentialDistribution(2)],
        weights=[18., 1.])

    assert_equal(round(d.log_probability((4, 1)), 4), -32.5744)
    assert_equal(round(d.log_probability((100, 0.001)), 4), -20334.5764)

    d.fit([(5, 1), (5.2, 1.7), (4.7, 1.9), (4.9, 2.4), (4.5, 1.2)])

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.86)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 0.2417)
    assert_equal(round(d.parameters[0][1].parameters[0], 4), 0.6098)

    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         UniformDistribution(0, 10)])
    d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0), (3, 0), (4, 0), (5, 0),
           (2, 20)],
          inertia=0.5)

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0), (3, 0), (4, 0), (5, 0),
           (2, 20)],
          inertia=0.75)

    assert_not_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_not_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_not_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_not_equal(d.parameters[0][1].parameters[1], 15)

    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         UniformDistribution(0, 10)])

    d.summarize([(0, 0), (5, 0), (3, 0)])
    d.summarize([(5, -5), (7, 0)])
    d.summarize([(3, 0), (4, 0), (5, 0), (2, 20)])
    d.from_summaries(inertia=0.5)

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    d.freeze()
    d.fit([(1, 7), (7, 2), (2, 4), (2, 4), (1, 4)])

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    e = Distribution.from_json(d.to_json())
    assert_equal(e.name, "IndependentComponentsDistribution")

    assert_equal(round(e.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(e.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(e.parameters[0][1].parameters[0], -2.5)
    assert_equal(e.parameters[0][1].parameters[1], 15)

    f = pickle.loads(pickle.dumps(e))
    assert_equal(e.name, "IndependentComponentsDistribution")

    assert_equal(round(f.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(f.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(f.parameters[0][1].parameters[0], -2.5)
    assert_equal(f.parameters[0][1].parameters[1], 15)

    X = numpy.array([[0.5, 0.2, 0.7], [0.3, 0.1, 0.9], [0.4, 0.3, 0.8],
                     [0.3, 0.3, 0.9], [0.3, 0.2, 0.6], [0.5, 0.2, 0.8]])

    d = IndependentComponentsDistribution.from_samples(
        X, distributions=NormalDistribution)
    assert_almost_equal(d.parameters[0][0].parameters[0], 0.38333, 4)
    assert_almost_equal(d.parameters[0][0].parameters[1], 0.08975, 4)
    assert_almost_equal(d.parameters[0][1].parameters[0], 0.21666, 4)
    assert_almost_equal(d.parameters[0][1].parameters[1], 0.06872, 4)
    assert_almost_equal(d.parameters[0][2].parameters[0], 0.78333, 4)
    assert_almost_equal(d.parameters[0][2].parameters[1], 0.10672, 4)

    d = IndependentComponentsDistribution.from_samples(
        X, distributions=ExponentialDistribution)
    assert_almost_equal(d.parameters[0][0].parameters[0], 2.6087, 4)
    assert_almost_equal(d.parameters[0][1].parameters[0], 4.6154, 4)
    assert_almost_equal(d.parameters[0][2].parameters[0], 1.2766, 4)

    d = IndependentComponentsDistribution.from_samples(X,
                                                       distributions=[
                                                           NormalDistribution,
                                                           NormalDistribution,
                                                           NormalDistribution
                                                       ])
    assert_almost_equal(d.parameters[0][0].parameters[0], 0.38333, 4)
    assert_almost_equal(d.parameters[0][0].parameters[1], 0.08975, 4)
    assert_almost_equal(d.parameters[0][1].parameters[0], 0.21666, 4)
    assert_almost_equal(d.parameters[0][1].parameters[1], 0.06872, 4)
    assert_almost_equal(d.parameters[0][2].parameters[0], 0.78333, 4)
    assert_almost_equal(d.parameters[0][2].parameters[1], 0.10672, 4)

    d = IndependentComponentsDistribution.from_samples(
        X,
        distributions=[
            NormalDistribution, LogNormalDistribution, ExponentialDistribution
        ])
    assert_almost_equal(d.parameters[0][0].parameters[0], 0.38333, 4)
    assert_almost_equal(d.parameters[0][0].parameters[1], 0.08975, 4)
    assert_almost_equal(d.parameters[0][1].parameters[0], -1.5898, 4)
    assert_almost_equal(d.parameters[0][1].parameters[1], 0.36673, 4)
    assert_almost_equal(d.parameters[0][2].parameters[0], 1.27660, 4)
def test_independent():
	d = IndependentComponentsDistribution(
		[NormalDistribution(5, 2), ExponentialDistribution(2)])

	assert_equal(round(d.log_probability((4, 1)), 4), -3.0439)
	assert_equal(round(d.log_probability((100, 0.001)), 4), -1129.0459)

	d = IndependentComponentsDistribution([NormalDistribution(5, 2),
										   ExponentialDistribution(2)],
										  weights=[18., 1.])

	assert_equal(round(d.log_probability((4, 1)), 4), -32.5744)
	assert_equal(round(d.log_probability((100, 0.001)), 4), -20334.5764)

	d.fit([(5, 1), (5.2, 1.7), (4.7, 1.9), (4.9, 2.4), (4.5, 1.2)])

	assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.86)
	assert_equal(round(d.parameters[0][0].parameters[1], 4), 0.2417)
	assert_equal(round(d.parameters[0][1].parameters[0], 4), 0.6098)

	d = IndependentComponentsDistribution([NormalDistribution(5, 2),
										   UniformDistribution(0, 10)])
	d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0),
		   (3, 0), (4, 0), (5, 0), (2, 20)], inertia=0.5)

	assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
	assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

	assert_equal(d.parameters[0][1].parameters[0], -2.5)
	assert_equal(d.parameters[0][1].parameters[1], 15)

	d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0),
		   (3, 0), (4, 0), (5, 0), (2, 20)], inertia=0.75)

	assert_not_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
	assert_not_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

	assert_not_equal(d.parameters[0][1].parameters[0], -2.5)
	assert_not_equal(d.parameters[0][1].parameters[1], 15)

	d = IndependentComponentsDistribution([NormalDistribution(5, 2),
										   UniformDistribution(0, 10)])

	d.summarize([(0, 0), (5, 0), (3, 0)])
	d.summarize([(5, -5), (7, 0)])
	d.summarize([(3, 0), (4, 0), (5, 0), (2, 20)])
	d.from_summaries(inertia=0.5)

	assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
	assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

	assert_equal(d.parameters[0][1].parameters[0], -2.5)
	assert_equal(d.parameters[0][1].parameters[1], 15)

	d.freeze()
	d.fit([(1, 7), (7, 2), (2, 4), (2, 4), (1, 4)])

	assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
	assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

	assert_equal(d.parameters[0][1].parameters[0], -2.5)
	assert_equal(d.parameters[0][1].parameters[1], 15)

	e = Distribution.from_json(d.to_json())
	assert_equal(e.name, "IndependentComponentsDistribution")

	assert_equal(round(e.parameters[0][0].parameters[0], 4), 4.3889)
	assert_equal(round(e.parameters[0][0].parameters[1], 4), 1.9655)

	assert_equal(e.parameters[0][1].parameters[0], -2.5)
	assert_equal(e.parameters[0][1].parameters[1], 15)

	f = pickle.loads(pickle.dumps(e))
	assert_equal(e.name, "IndependentComponentsDistribution")

	assert_equal(round(f.parameters[0][0].parameters[0], 4), 4.3889)
	assert_equal(round(f.parameters[0][0].parameters[1], 4), 1.9655)

	assert_equal(f.parameters[0][1].parameters[0], -2.5)
	assert_equal(f.parameters[0][1].parameters[1], 15)

	X = numpy.array([[0.5, 0.2, 0.7],
		          [0.3, 0.1, 0.9],
		          [0.4, 0.3, 0.8],
		          [0.3, 0.3, 0.9],
		          [0.3, 0.2, 0.6],
		          [0.5, 0.2, 0.8]])

	d = IndependentComponentsDistribution.from_samples(X,
		distributions=NormalDistribution)
	assert_almost_equal(d.parameters[0][0].parameters[0], 0.38333, 4)
	assert_almost_equal(d.parameters[0][0].parameters[1], 0.08975, 4)
	assert_almost_equal(d.parameters[0][1].parameters[0], 0.21666, 4)
	assert_almost_equal(d.parameters[0][1].parameters[1], 0.06872, 4)
	assert_almost_equal(d.parameters[0][2].parameters[0], 0.78333, 4)
	assert_almost_equal(d.parameters[0][2].parameters[1], 0.10672, 4)

	d = IndependentComponentsDistribution.from_samples(X,
		distributions=ExponentialDistribution)
	assert_almost_equal(d.parameters[0][0].parameters[0], 2.6087, 4)
	assert_almost_equal(d.parameters[0][1].parameters[0], 4.6154, 4)
	assert_almost_equal(d.parameters[0][2].parameters[0], 1.2766, 4)

	d = IndependentComponentsDistribution.from_samples(X,
		distributions=[NormalDistribution, NormalDistribution, NormalDistribution])
	assert_almost_equal(d.parameters[0][0].parameters[0], 0.38333, 4)
	assert_almost_equal(d.parameters[0][0].parameters[1], 0.08975, 4)
	assert_almost_equal(d.parameters[0][1].parameters[0], 0.21666, 4)
	assert_almost_equal(d.parameters[0][1].parameters[1], 0.06872, 4)
	assert_almost_equal(d.parameters[0][2].parameters[0], 0.78333, 4)
	assert_almost_equal(d.parameters[0][2].parameters[1], 0.10672, 4)

	d = IndependentComponentsDistribution.from_samples(X,
		distributions=[NormalDistribution, LogNormalDistribution, ExponentialDistribution])
	assert_almost_equal(d.parameters[0][0].parameters[0], 0.38333, 4)
	assert_almost_equal(d.parameters[0][0].parameters[1], 0.08975, 4)
	assert_almost_equal(d.parameters[0][1].parameters[0], -1.5898, 4)
	assert_almost_equal(d.parameters[0][1].parameters[1], 0.36673, 4)
	assert_almost_equal(d.parameters[0][2].parameters[0], 1.27660, 4)
Exemple #3
0
def test_independent():
    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         ExponentialDistribution(2)])

    assert_equal(round(d.log_probability((4, 1)), 4), -3.0439)
    assert_equal(round(d.log_probability((100, 0.001)), 4), -1129.0459)

    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         ExponentialDistribution(2)],
        weights=[18., 1.])

    assert_equal(round(d.log_probability((4, 1)), 4), -0.1536)
    assert_equal(round(d.log_probability((100, 0.001)), 4), -1126.1556)

    d.fit([(5, 1), (5.2, 1.7), (4.7, 1.9), (4.9, 2.4), (4.5, 1.2)])

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.86)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 0.2417)
    assert_equal(round(d.parameters[0][1].parameters[0], 4), 0.6098)

    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         UniformDistribution(0, 10)])
    d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0), (3, 0), (4, 0), (5, 0),
           (2, 20)],
          inertia=0.5)

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0), (3, 0), (4, 0), (5, 0),
           (2, 20)],
          inertia=0.75)

    assert_not_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_not_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_not_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_not_equal(d.parameters[0][1].parameters[1], 15)

    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2),
         UniformDistribution(0, 10)])

    d.summarize([(0, 0), (5, 0), (3, 0)])
    d.summarize([(5, -5), (7, 0)])
    d.summarize([(3, 0), (4, 0), (5, 0), (2, 20)])
    d.from_summaries(inertia=0.5)

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    d.freeze()
    d.fit([(1, 7), (7, 2), (2, 4), (2, 4), (1, 4)])

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    e = Distribution.from_json(d.to_json())
    assert_equal(e.name, "IndependentComponentsDistribution")

    assert_equal(round(e.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(e.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(e.parameters[0][1].parameters[0], -2.5)
    assert_equal(e.parameters[0][1].parameters[1], 15)

    f = pickle.loads(pickle.dumps(e))
    assert_equal(e.name, "IndependentComponentsDistribution")

    assert_equal(round(f.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(f.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(f.parameters[0][1].parameters[0], -2.5)
    assert_equal(f.parameters[0][1].parameters[1], 15)
def test_independent():
    d = IndependentComponentsDistribution(
        [NormalDistribution(5, 2), ExponentialDistribution(2)])

    assert_equal(round(d.log_probability((4, 1)), 4), -3.0439)
    assert_equal(round(d.log_probability((100, 0.001)), 4), -1129.0459)

    d = IndependentComponentsDistribution([NormalDistribution(5, 2),
                                           ExponentialDistribution(2)],
                                          weights=[18., 1.])

    assert_equal(round(d.log_probability((4, 1)), 4), -0.1536)
    assert_equal(round(d.log_probability((100, 0.001)), 4), -1126.1556)

    d.fit([(5, 1), (5.2, 1.7), (4.7, 1.9), (4.9, 2.4), (4.5, 1.2)])

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.86)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 0.2417)
    assert_equal(round(d.parameters[0][1].parameters[0], 4), 0.6098)

    d = IndependentComponentsDistribution([NormalDistribution(5, 2),
                                           UniformDistribution(0, 10)])
    d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0),
           (3, 0), (4, 0), (5, 0), (2, 20)], inertia=0.5)

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    d.fit([(0, 0), (5, 0), (3, 0), (5, -5), (7, 0),
           (3, 0), (4, 0), (5, 0), (2, 20)], inertia=0.75)

    assert_not_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_not_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_not_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_not_equal(d.parameters[0][1].parameters[1], 15)

    d = IndependentComponentsDistribution([NormalDistribution(5, 2),
                                           UniformDistribution(0, 10)])

    d.summarize([(0, 0), (5, 0), (3, 0)])
    d.summarize([(5, -5), (7, 0)])
    d.summarize([(3, 0), (4, 0), (5, 0), (2, 20)])
    d.from_summaries(inertia=0.5)

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    d.freeze()
    d.fit([(1, 7), (7, 2), (2, 4), (2, 4), (1, 4)])

    assert_equal(round(d.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(d.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(d.parameters[0][1].parameters[0], -2.5)
    assert_equal(d.parameters[0][1].parameters[1], 15)

    e = Distribution.from_json(d.to_json())
    assert_equal(e.name, "IndependentComponentsDistribution")

    assert_equal(round(e.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(e.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(e.parameters[0][1].parameters[0], -2.5)
    assert_equal(e.parameters[0][1].parameters[1], 15)

    f = pickle.loads(pickle.dumps(e))
    assert_equal(e.name, "IndependentComponentsDistribution")

    assert_equal(round(f.parameters[0][0].parameters[0], 4), 4.3889)
    assert_equal(round(f.parameters[0][0].parameters[1], 4), 1.9655)

    assert_equal(f.parameters[0][1].parameters[0], -2.5)
    assert_equal(f.parameters[0][1].parameters[1], 15)