예제 #1
0
def test_distributions_exponential_random_sample():
	d = ExponentialDistribution(7)

	x = numpy.array([0.03586, 0.292267, 0.033083, 0.358359, 0.095748])

	assert_array_almost_equal(d.sample(5, random_state=5), x)
	assert_raises(AssertionError, assert_array_almost_equal, d.sample(5), x)
예제 #2
0
def test_distributions_exponential_random_sample():
	d = ExponentialDistribution(7)

	x = numpy.array([0.03586, 0.292267, 0.033083, 0.358359, 0.095748])

	assert_array_almost_equal(d.sample(5, random_state=5), x)
	assert_raises(AssertionError, assert_array_almost_equal, d.sample(5), x)
예제 #3
0
def test_distributions_independent_random_sample():
	d = IndependentComponentsDistribution([NormalDistribution(5, 2),
										   UniformDistribution(0, 10),
										   ExponentialDistribution(7),
										   LogNormalDistribution(0, 0.4)])

	x = numpy.array([[5.882455, 2.219932, 0.03586 , 1.193024],
					 [4.33826 , 8.707323, 0.292267, 0.876036],
					 [9.861542, 2.067192, 0.033083, 2.644041]])

	assert_array_almost_equal(d.sample(3, random_state=5), x)
	assert_raises(AssertionError, assert_array_almost_equal, d.sample(5), x)
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_exponential():
    d = ExponentialDistribution(3)
    assert_equal(round(d.log_probability(8), 4), -22.9014)

    d.fit([2.7, 2.9, 3.8, 1.9, 2.7, 1.6, 1.3, 1.0, 1.9])
    assert_equal(round(d.parameters[0], 4), 0.4545)

    d = ExponentialDistribution(4)
    assert_not_equal(round(d.log_probability(8), 4), -22.9014)

    d.summarize([2.7, 2.9, 3.8])
    d.summarize([1.9, 2.7, 1.6])
    d.summarize([1.3, 1.0, 1.9])
    d.from_summaries()

    assert_equal(round(d.parameters[0], 4), 0.4545)

    e = Distribution.from_json(d.to_json())
    assert_equal(e.name, "ExponentialDistribution")
    assert_equal(round(e.parameters[0], 4), 0.4545)

    f = pickle.loads(pickle.dumps(e))
    assert_equal(f.name, "ExponentialDistribution")
    assert_equal(round(f.parameters[0], 4), 0.4545)
예제 #6
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)
예제 #7
0
def getMixtureModelCutOff(samples,alpha,mu,sigma):
    mixture_m = GeneralMixtureModel([ ExponentialDistribution(alpha), NormalDistribution(mu,sigma)] )
    model = mixture_m.fit(samples.reshape(-1,1))
    pred_alpha = model.distributions[0].parameters[0]
    return expon.ppf(0.95,0,1/pred_alpha)
예제 #8
0
def test_exponential():
	d = ExponentialDistribution(3)
	assert_equal(round(d.log_probability(8), 4), -22.9014)

	d.fit([2.7, 2.9, 3.8, 1.9, 2.7, 1.6, 1.3, 1.0, 1.9])
	assert_equal(round(d.parameters[0], 4), 0.4545)

	d = ExponentialDistribution(4)
	assert_not_equal(round(d.log_probability(8), 4), -22.9014)

	d.summarize([2.7, 2.9, 3.8])
	d.summarize([1.9, 2.7, 1.6])
	d.summarize([1.3, 1.0, 1.9])
	d.from_summaries()

	assert_equal(round(d.parameters[0], 4), 0.4545)

	e = Distribution.from_json(d.to_json())
	assert_equal(e.name, "ExponentialDistribution")
	assert_equal(round(e.parameters[0], 4), 0.4545)

	f = pickle.loads(pickle.dumps(e))
	assert_equal(f.name, "ExponentialDistribution")
	assert_equal(round(f.parameters[0], 4), 0.4545)
예제 #9
0
파일: main.py 프로젝트: tsameti/dbns
from pomegranate import (
    NaiveBayes,
    NormalDistribution,
    UniformDistribution,
    ExponentialDistribution,
    GeneralMixtureModel,
    MultivariateGaussianDistribution,
    BernoulliDistribution,
)
import pandas as pd
import numpy as np

X = pd.DataFrame({"A": [1, 0, 1, 0, 1], "B": [1, 1, 1, 1, 0]})

x = BernoulliDistribution(0.4)

vals = []
[vals.append(x.sample()) for i in range(1000)]

model = NaiveBayes([
    NormalDistribution(5, 2),
    UniformDistribution(0, 10),
    ExponentialDistribution(1.0)
])
model.predict(np.array([[10]]))

model = GeneralMixtureModel.from_samples(MultivariateGaussianDistribution,
                                         n_components=3,
                                         X=X)