jmschrei / pomegranate

Fast, flexible and easy to use probabilistic modelling in Python.
http://pomegranate.readthedocs.org/en/latest/
MIT License
3.37k stars 590 forks source link

ZeroDivisionError if NormalDistribution has sigma = 0 #573

Closed komodovaran closed 4 years ago

komodovaran commented 5 years ago

This no longer works in 0.11.0 (works in 0.10.0 though):

import pomegranate as pg; pg.NormalDistribution(1, 0)

Traceback: File "pomegranate/distributions/NormalDistribution.pyx", line 37, in pomegranate.distributions.NormalDistribution.NormalDistribution.__init__ ZeroDivisionError: float division by zero

jmschrei commented 5 years ago

I did change that. Do you have a reason to want a zero variance?

On Thu, Mar 28, 2019 at 4:55 AM Komodovaran notifications@github.com wrote:

This no longer works in 0.11.0 (works in 0.10.0 though):

import pomegranate as pg; pg.NormalDistribution(1, 0)

Traceback: File "pomegranate/distributions/NormalDistribution.pyx", line 37, in pomegranate.distributions.NormalDistribution.NormalDistribution.init ZeroDivisionError: float division by zero

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jmschrei/pomegranate/issues/573, or mute the thread https://github.com/notifications/unsubscribe-auth/ADvEEBOSjSsAu3NU8x4J3Pos6ftzwN3Oks5vbIOTgaJpZM4cPnkY .

komodovaran commented 5 years ago

Generating noise-free hidden states has its uses, e.g.

import pomegranate as pg
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows = 3)
ax = ax.ravel()

for a, s in zip(ax, (0, 0.1, 0.5)):
    state_means = [1, 2]
    n = len(state_means)

    dists = [pg.NormalDistribution(m, s) for m in state_means]
    p = 0.10

    tmat = np.empty([n, n])
    tmat.fill(p)
    np.fill_diagonal(tmat, 1 - p)

    model = pg.HiddenMarkovModel.from_matrix(tmat, distributions=dists, starts=(0, 1))
    model.bake()

    a.plot(np.array(model.sample(200)))

plt.show()

Now I can add noise to the ground truth and see how different statistical approaches behave, for a Markov Chain system like this.

fig

jmschrei commented 5 years ago

That’s true. Would it be possible for your purposes to just set the variance to be absurdly small, like 1e-16? Or do you need exact values?

On Thu, Mar 28, 2019 at 9:58 AM Komodovaran notifications@github.com wrote:

Generating noise-free hidden states has its uses, e.g.

import pomegranate as pg import numpy as np import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows = 3) ax = ax.ravel()

for a, s in zip(ax, (0, 0.1, 0.5)): state_means = [1, 2] n = len(state_means)

dists = [pg.NormalDistribution(m, s) for m in state_means]
p = 0.10

tmat = np.empty([n, n])
tmat.fill(p)
np.fill_diagonal(tmat, 1 - p)

model = pg.HiddenMarkovModel.from_matrix(tmat, distributions=dists, starts=(0, 1))
model.bake()

a.plot(np.array(model.sample(200)))

plt.show()

[image: fig] https://user-images.githubusercontent.com/20357875/55163322-e5c7c000-5169-11e9-8fce-74a6ac0c0446.png

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/jmschrei/pomegranate/issues/573#issuecomment-477606399, or mute the thread https://github.com/notifications/unsubscribe-auth/ADvEEHoI5YBt5Nibi3k6j2kqwbgNqxmhks5vbMqhgaJpZM4cPnkY .

komodovaran commented 5 years ago

Good solution, hadn't thought of that actually. Rounding easily takes care of that then.

jkleckner commented 5 years ago

@Komodovaran Did you try that with the code above? It hung for me (haven't tried to debug).

komodovaran commented 5 years ago

Now is actually the first time I'm trying to run the code... and you're right. It hangs.