Giter Site home page Giter Site logo

Comments (7)

poypoyan avatar poypoyan commented on July 28, 2024

Apologies for late reply. Will look into this. Again, thanks!

from edhsmm.

poypoyan avatar poypoyan commented on July 28, 2024

Good day,

The work-around for the error is to set the type of np.empty in all_obs like this: np.empty((...), dtype=np.int64).

Now to perform multiple observation, "insert" first all observations into one big ndarray, and then specify the "boundaries" between observations in a list of lengths. This is the same to how it's being done for hmmlearn (at least at the time I implemented edhsmm).

Here is the modified code that works.

import sys

import numpy as np
from edhsmm.hsmm_multinom import MultinomialHSMM

# modified initial parameters for MultinomialHSMM
def init_true_model(hsmm_class):
    hsmm_class.pi = np.array([2 / 3, 1 / 3, 0 / 3])
    hsmm_class.dur = np.array([
        [0.1, 0.005, 0.005, 0.89],
        [0.1, 0.005, 0.89, 0.005],
        [0.1, 0.89, 0.005, 0.005]
    ])
    hsmm_class.tmat = np.array([
        [0.0, 0.5, 0.5],
        [0.3, 0.0, 0.7],
        [0.4, 0.6, 0.0]
    ])
    hsmm_class.emit = np.array([
        [0.8, 0.1, 0.1],
        [0.1, 0.8, 0.1],
        [0.1, 0.1, 0.8]
    ])   # shape should be (n_states, n_symbols)

# modified initial parameters for MultinomialHSMM
def init_middle_model(hsmm_class):
    hsmm_class.pi = np.array([1/3, 1/3, 1/3])
    hsmm_class.dur = np.array([
        [0.25, 0.25, 0.25, 0.25],
        [0.25, 0.25, 0.25, 0.25],
        [0.25, 0.25, 0.25, 0.25]
    ])
    hsmm_class.tmat = np.array([
        [0.0, 0.5, 0.5],
        [0.5, 0.0, 0.5],
        [0.5, 0.5, 0.0]
    ])
    hsmm_class.emit = np.array([
        [1/3, 1/3, 1/3],
        [1/3, 1/3, 1/3],
        [1/3, 1/3, 1/3]
    ])   # shape should be (n_states, n_symbols)


# initialize HSMM
squirrel_true_model = MultinomialHSMM(n_states = 3, n_durations = 4)
init_true_model(squirrel_true_model)

rng_seed = 12345
n_samples = 100
n_obs = 10
n_var = 1

# to use multiple observers: "insert" all observations to `all_obs`
# then specify the "boundaries" between observations in `all_len`
all_obs = np.empty((n_samples * n_obs, n_var), dtype=np.int64)   # work-around
all_len = []
ctr_len = 0
for i in range(n_obs):
    sample_len, sample, _ = squirrel_true_model.sample(n_samples=n_samples, random_state=rng_seed)
    all_obs[ctr_len: ctr_len + sample_len, :] = sample
    all_len += [sample_len]
    ctr_len += sample_len
    rng_seed += 1

squirrel_middle_model = MultinomialHSMM(n_states = 3, n_durations = 4)
init_middle_model(squirrel_true_model)
squirrel_middle_model.fit(all_obs)

# display learned parameters for T
print("Start Probabilities: [T]\n", squirrel_middle_model.pi, "\n")
print("Transition Matrix: [T]\n", squirrel_middle_model.tmat, "\n")
print("Durations: [T]\n", squirrel_middle_model.dur, "\n")
print("Emission Probabilities: [T]\n", squirrel_middle_model.emit, "\n")

Thanks!
poypoyan

from edhsmm.

rtrepos avatar rtrepos commented on July 28, 2024

Hi poypoyan,
Thank you for your proposal and your availability ! I will try it very soon.
Best
Ronan

from edhsmm.

rtrepos avatar rtrepos commented on July 28, 2024

Hi poypoyan,
Finally, i found time to test it and It works! So thank you again for your help.
I am nevertheless surprised that initial probabilities are quite difficult to estimate.
For example in this setup with many samples (500) where all is well estimated except initial probabilities. If you have any idea, I would be pleased to hear that. Otherwise you can close this thread.
Best
Ronan


import sys

import numpy as np
from edhsmm.hsmm_multinom import MultinomialHSMM

# modified initial parameters for MultinomialHSMM
def init_true_model(hsmm_class):
    hsmm_class.pi = np.array([2 / 3, 1 / 3, 0 / 3])
    hsmm_class.dur = np.array([
        [0.1, 0.005, 0.005, 0.89],
        [0.1, 0.005, 0.89, 0.005],
        [0.1, 0.89, 0.005, 0.005]
    ])
    hsmm_class.tmat = np.array([
        [0.0, 0.5, 0.5],
        [0.3, 0.0, 0.7],
        [0.4, 0.6, 0.0]
    ])
    hsmm_class.emit = np.array([
        [0.8, 0.1, 0.1],
        [0.1, 0.8, 0.1],
        [0.1, 0.1, 0.8]
    ])   # shape should be (n_states, n_symbols)

# modified initial parameters for MultinomialHSMM
def init_middle_model(hsmm_class):
    hsmm_class.pi = np.array([1/3, 1/3, 1/3])
    hsmm_class.dur = np.array([
        [0.25, 0.25, 0.25, 0.25],
        [0.25, 0.25, 0.25, 0.25],
        [0.25, 0.25, 0.25, 0.25]
    ])
    hsmm_class.tmat = np.array([
        [0.0, 0.5, 0.5],
        [0.5, 0.0, 0.5],
        [0.5, 0.5, 0.0]
    ])
    hsmm_class.emit = np.array([
        [1/3, 1/3, 1/3],
        [1/3, 1/3, 1/3],
        [1/3, 1/3, 1/3]
    ])   # shape should be (n_states, n_symbols)


# initialize HSMM
squirrel_true_model = MultinomialHSMM(n_states = 3, n_durations = 4)
init_true_model(squirrel_true_model)

rng_seed = 123598
n_samples = 500
n_obs = 300
n_var = 1

# to use multiple observers: "insert" all observations to `all_obs`
# then specify the "boundaries" between observations in `all_len`
all_obs = np.empty((n_samples * n_obs, n_var), dtype=np.int64)   # work-around
all_len = []
ctr_len = 0
for i in range(n_obs):
    sample_len, sample, _ = squirrel_true_model.sample(n_samples=n_samples, random_state=rng_seed)
    all_obs[ctr_len: ctr_len + sample_len, :] = sample
    all_len += [sample_len]
    ctr_len += sample_len
    rng_seed += 1

squirrel_middle_model = MultinomialHSMM(n_states = 3, n_durations = 4)
init_middle_model(squirrel_true_model)
squirrel_middle_model.fit(all_obs)

# display learned parameters for T
print("Start Probabilities: [T]\n", squirrel_middle_model.pi, "\n")
print("Transition Matrix: [T]\n", squirrel_middle_model.tmat, "\n")
print("Durations: [T]\n", squirrel_middle_model.dur, "\n")
print("Emission Probabilities: [T]\n", squirrel_middle_model.emit, "\n")

from edhsmm.

poypoyan avatar poypoyan commented on July 28, 2024

Hello.

I forgot a few things:

  1. For multiple observation, the "boundaries" (all_len) should be included in fit():
squirrel_middle_model.fit(all_obs, all_len)
  1. Regarding random_state: I am thinking of just having it in the HSMM object, as in
squirrel_true_model = MultinomialHSMM(n_states = 3, n_durations = 4, random_state = 42)

hence removing it in sample(). What do you think?

I'll investigate the issue with initial probabilities. I'll make a new issue, and close this issue.

Thanks!
poypoyan

from edhsmm.

rtrepos avatar rtrepos commented on July 28, 2024

Hello,
I am sorry to have taken so much time to answer you, I was not available recently.
Thanks again for your help and to answer your questions :

  • I have put all_len when calling the fit process, surprisingly, the results are not really better but that's ok.
  • For the random_state, I think you use the np.random.default_rng which is a global rng (is this really the case ?) then my advice would be to not include it as an argument of the MultinomialHSMM. Because, internally you will modify this global rng and a user could be surprised to see the differences if he uses it for another purpose in the same script. Even more, I would remove it from the sample function and let the user set for example in front of his script : np.random.default_rng(random_state)

Best
Ronan

from edhsmm.

poypoyan avatar poypoyan commented on July 28, 2024

Hello.

I cannot get myself to work on this repo anymore 😓. Because of this, I am in the process of archiving this repo. I am sorry that I cannot work on resolving this issue. Thank you for your interest and understanding.

poypoyan

from edhsmm.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.