{"id":19544205,"url":"https://github.com/sunset1995/hmm","last_synced_at":"2025-04-26T17:32:54.678Z","repository":{"id":94182620,"uuid":"82918240","full_name":"sunset1995/HMM","owner":"sunset1995","description":"Implement Discrete Hidden Markov Model.","archived":false,"fork":false,"pushed_at":"2017-03-21T02:02:00.000Z","size":199,"stargazers_count":4,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-04T17:02:22.037Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sunset1995.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-23T10:57:41.000Z","updated_at":"2022-06-20T14:18:16.000Z","dependencies_parsed_at":"2023-03-13T17:04:33.493Z","dependency_job_id":null,"html_url":"https://github.com/sunset1995/HMM","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunset1995%2FHMM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunset1995%2FHMM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunset1995%2FHMM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunset1995%2FHMM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunset1995","download_url":"https://codeload.github.com/sunset1995/HMM/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251026582,"owners_count":21525002,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T03:25:45.357Z","updated_at":"2025-04-26T17:32:54.672Z","avatar_url":"https://github.com/sunset1995.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HMM\n\nImplement Discrete Hidden Markov Model.\n\n\n## Quick Start\nRun `pip3 install -r requirements.txt` to install the dependency.  \nCopy or create a soft link to the directory `hmm` to the diretory you work on.  \nHere is an quick example:\n```\nfrom hmm.hmm import DiscreteHMM\n\n# Setting model's initial parameter\nA = (\n\t(0.1, 0.1, 0.8),\n\t(0.1, 0.1, 0.8),\n\t(0.1, 0.1, 0.8), )\nB = (\n\t(0.1, 0.9),\n\t(0.2, 0.8),\n\t(0.3, 0.7), )\npi = (1/3, 1/3, 1/3)\n\n# Create an model\nhmm = DiscreteHMM(3, 2, A=A, B=B, pi=pi)\n\n# Train the model to a local maximal\n# which better describes the given observations than original model\ntrain_obs_seq = (0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0)\nhmm.train(train_obs_seq, verbose=1)\n\n# Show the model\nhmm.show_model()\n\n# Check whether each row sum to 1.0\nprint('checksum:', hmm.check_model())\nprint('=' * 20)\n\n# Predict the probabilit distribution of the hidden state given a observation sequence\nprediction = hmm.given((1, 0, 1, 0, 1, 0, 1))\nprint('Current hidden state:', prediction['forward'])\nprint('Most likely path    :', prediction['viterby'])\nprint('=' * 20)\n\n# Continuing with previous given sequence and making prediction\nfor obs in (1, 0, 0, 1):\n\tprediction = hmm.given_more((obs, ))\n\tprint('Current hidden state:', prediction['forward'])\n\tprint('Most likely path    :', prediction['viterby'])\n\tprint('=' * 20)\n```\n\n\n## Examples\n- [whether forecast](./examples/weather_forecast/)\n- [forex](./examples/forex/)\n\n\n## Documents\n\n### DiscreteHMM(N, M, A, B, pi)\n- Counstructor of the hmm model\n- N: _required_, _int_. Number of hidden states\n- M: _required_, _int_. Number of observation symbol\n- A: _optional_, _tuple of tuple_. State transition probability\n\t- Randomly generate one if not given\n\t- If the dimension is not `N by N` or each row is not sum to 1.0, Assertion error will be raised\n- B: _optional_, _tuple of tuple_. Emmision probability\n\t- Randomly generate one if not given\n\t- If the dimension is not `N by M` or each row is not sum to 1.0, Assertion error will be raised\n- pi: _optional_, _tuple_. Initial state probability\n\t- Randomly generate one if not given\n\t- If the dimension is not `1 by N` or the only one row is not sum to 1.0, Assertion error will be raised\n\nAfter creating a model, you can use it to make some prediction (see [weather forecast viterby example](./examples/weather_forecast/viterbi.py)) or using some observation sequences to train it (see [weather forecast train example](./examples/weather_forecast/train.py)).  \n\n### DiscreteHMM.train(obs_seq, itnum, eps, verbose):\n- Using the given `obs_seq` to train the model\n- Each member in `obs_seq` should between `0` and `M-1`\n- obs_seq: _required_, _tuple_. The given observation sequence\n- itnum: _optional_, _int_. Maximum # of iterations. Default is `100`\n- eps: _optional_, _float_. Stop if the sum of difference of each entries (called delta) between two iteration is less than `eps`. Default is `0.01`\n- verbose: _optional_, _int_ `0` shows nothing, `1` shows iteration count and delta, `2` show model after each iteration. Default is `0`\n\nCall `train` multiple times with different `obs_seq` or `train` after some prediction is available.  \n\n### DiscreteHMM.given(obs_seq)\n- Start with `obs_seq`, coculate the hidden state probability distribution and viterby path given the sequence.\n- Each member in `obs_seq` should between `0` and `M-1`\n- obs_seq: _required_, _tuple_. The given observation sequence\n- Return a `dict`\n\t- `forward`: a numpy array. Probability of each hidden state.\n\t- `viterby`: a list. The viterby path which is the most likely path generating the `obs_seq`\n\n### DiscreteHMM.given_more(obs_seq)\n- Like `given` but the result will base on previous given `obs_seq`\n- You can calling `given_more` without calling `given`. Then the first time calling `given_more` is equal to `given`\n- If you want to make model forget all previous `obs_seq`, call `given`\n\n### DiscreteHMM.show_model()\n- Show the model\n\n### DiscreteHMM.check_model()\n- Return whether each row sum to 1.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunset1995%2Fhmm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunset1995%2Fhmm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunset1995%2Fhmm/lists"}