{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nSampling from HMM\n-----------------\n\nThis script shows how to sample points from a Hiden Markov Model (HMM):\nwe use a 4-components with specified mean and covariance.\n\nThe plot show the sequence of observations generated with the transitions\nbetween them. We can see that, as specified by our transition matrix,\nthere are no transition between component 1 and 3.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom hmmlearn import hmm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Prepare parameters for a 4-components HMM\nInitial population probability\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "startprob = np.array([0.6, 0.3, 0.1, 0.0])\n# The transition matrix, note that there are no transitions possible\n# between component 1 and 3\ntransmat = np.array([[0.7, 0.2, 0.0, 0.1],\n                     [0.3, 0.5, 0.2, 0.0],\n                     [0.0, 0.3, 0.5, 0.2],\n                     [0.2, 0.0, 0.2, 0.6]])\n# The means of each component\nmeans = np.array([[0.0,  0.0],\n                  [0.0, 11.0],\n                  [9.0, 10.0],\n                  [11.0, -1.0]])\n# The covariance of each component\ncovars = .5 * np.tile(np.identity(2), (4, 1, 1))\n\n# Build an HMM instance and set parameters\nmodel = hmm.GaussianHMM(n_components=4, covariance_type=\"full\")\n\n# Instead of fitting it from the data, we directly set the estimated\n# parameters, the means and covariance of the components\nmodel.startprob_ = startprob\nmodel.transmat_ = transmat\nmodel.means_ = means\nmodel.covars_ = covars"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate samples\nX, Z = model.sample(500)\n\n# Plot the sampled data\nplt.plot(X[:, 0], X[:, 1], \".-\", label=\"observations\", ms=6,\n         mfc=\"orange\", alpha=0.7)\n\n# Indicate the component numbers\nfor i, m in enumerate(means):\n    plt.text(m[0], m[1], 'Component %i' % (i + 1),\n             size=17, horizontalalignment='center',\n             bbox=dict(alpha=.7, facecolor='w'))\nplt.legend(loc='best')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}