{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Singular Spectrum Analysis\n\n\nThis example shows how you can decompose a time series into several time series\nusing :class:`pyts.decomposition.SingularSpectrumAnalysis`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom pyts.decomposition import SingularSpectrumAnalysis\n\n# Parameters\nn_samples, n_timestamps = 100, 48\n\n# Toy dataset\nrng = np.random.RandomState(41)\nX = rng.randn(n_samples, n_timestamps)\n\n# We decompose the time series into three subseries\nwindow_size = 15\ngroups = [np.arange(i, i + 5) for i in range(0, 11, 5)]\n\n# Singular Spectrum Analysis\nssa = SingularSpectrumAnalysis(window_size=15, groups=groups)\nX_ssa = ssa.fit_transform(X)\n\n# Show the results for the first time series and its subseries\nplt.figure(figsize=(16, 6))\n\nax1 = plt.subplot(121)\nax1.plot(X[0], 'o-', label='Original')\nax1.legend(loc='best', fontsize=14)\n\nax2 = plt.subplot(122)\nfor i in range(len(groups)):\n    ax2.plot(X_ssa[0, i], 'o--', label='SSA {0}'.format(i + 1))\nax2.legend(loc='best', fontsize=14)\n\nplt.suptitle('Singular Spectrum Analysis', fontsize=20)\n\nplt.tight_layout()\nplt.subplots_adjust(top=0.88)\nplt.show()\n\n# The first subseries consists of the trend of the original time series.\n# The second and third subseries consist of noise."
      ]
    }
  ],
  "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.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}