

<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="lang:clipboard.copy" content="Copy to clipboard">
  <meta name="lang:clipboard.copied" content="Copied to clipboard">
  <meta name="lang:search.language" content="en">
  <meta name="lang:search.pipeline.stopwords" content="True">
  <meta name="lang:search.pipeline.trimmer" content="True">
  <meta name="lang:search.result.none" content="No matching documents">
  <meta name="lang:search.result.one" content="1 matching document">
  <meta name="lang:search.result.other" content="# matching documents">
  <meta name="lang:search.tokenizer" content="[\s\-]+">

  
    <link href="https://fonts.gstatic.com/" rel="preconnect" crossorigin>
    <link href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,500,700|Roboto:300,400,400i,700&display=fallback" rel="stylesheet">

    <style>
      body,
      input {
        font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif
      }

      code,
      kbd,
      pre {
        font-family: "Roboto Mono", "Courier New", Courier, monospace
      }
    </style>
  

  <link rel="stylesheet" href="../../../../_static/stylesheets/application.css"/>
  <link rel="stylesheet" href="../../../../_static/stylesheets/application-palette.css"/>
  <link rel="stylesheet" href="../../../../_static/stylesheets/application-fixes.css"/>
  
  <link rel="stylesheet" href="../../../../_static/fonts/material-icons.css"/>
  
  <meta name="theme-color" content="#3f51b5">
  <script src="../../../../_static/javascripts/modernizr.js"></script>
  
  
  
    <title>statsmodels.tsa.statespace.representation &#8212; statsmodels</title>
  <link rel="icon" type="image/png" sizes="32x32" href="../../../../_static/icons/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="../../../../_static/icons/favicon-16x16.png">
  <link rel="manifest" href="../../../../_static/icons/site.webmanifest">
  <link rel="mask-icon" href="../../../../_static/icons/safari-pinned-tab.svg" color="#919191">
  <meta name="msapplication-TileColor" content="#2b5797">
  <meta name="msapplication-config" content="../../../../_static/icons/browserconfig.xml">
  <link rel="stylesheet" href="../../../../_static/stylesheets/examples.css">
  <link rel="stylesheet" href="../../../../_static/stylesheets/deprecation.css">
    <link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/material.css" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/plot_directive.css" />
    <script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
    <link rel="shortcut icon" href="../../../../_static/favicon.ico"/>
    <link rel="author" title="About these documents" href="../../../../about.html" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
  
   

  </head>
  <body dir=ltr
        data-md-color-primary=indigo data-md-color-accent=blue>
  
  <svg class="md-svg">
    <defs data-children-count="0">
      
      <svg xmlns="http://www.w3.org/2000/svg" width="416" height="448" viewBox="0 0 416 448" id="__github"><path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19T128 352t-18.125-8.5-10.75-19T96 304t3.125-20.5 10.75-19T128 256t18.125 8.5 10.75 19T160 304zm160 0q0 10-3.125 20.5t-10.75 19T288 352t-18.125-8.5-10.75-19T256 304t3.125-20.5 10.75-19T288 256t18.125 8.5 10.75 19T320 304zm40 0q0-30-17.25-51T296 232q-10.25 0-48.75 5.25Q229.5 240 208 240t-39.25-2.75Q130.75 232 120 232q-29.5 0-46.75 21T56 304q0 22 8 38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0 37.25-1.75t35-7.375 30.5-15 20.25-25.75T360 304zm56-44q0 51.75-15.25 82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5T212 416q-19.5 0-35.5-.75t-36.875-3.125-38.125-7.5-34.25-12.875T37 371.5t-21.5-28.75Q0 312 0 260q0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25 30.875Q171.5 96 212 96q37 0 70 8 26.25-20.5 46.75-30.25T376 64q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34 99.5z"/></svg>
      
    </defs>
  </svg>
  
  <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer">
  <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search">
  <label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
  <a href="#_modules/statsmodels/tsa/statespace/representation" tabindex="1" class="md-skip"> Skip to content </a>
  <header class="md-header" data-md-component="header">
  <nav class="md-header-nav md-grid">
    <div class="md-flex navheader">
      <div class="md-flex__cell md-flex__cell--shrink">
        <a href="../../../../index.html" title="statsmodels"
           class="md-header-nav__button md-logo">
          
              <img src="../../../../_static/statsmodels-logo-v2-bw.svg" height="26"
                   alt="statsmodels logo">
          
        </a>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
      </div>
      <div class="md-flex__cell md-flex__cell--stretch">
        <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
          <span class="md-header-nav__topic">statsmodels v0.13.0</span>
          <span class="md-header-nav__topic"> statsmodels.tsa.statespace.representation </span>
        </div>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        <label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
        
<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" action="../../../../search.html" method="get" name="search">
      <input type="text" class="md-search__input" name="q" placeholder="Search"
             autocapitalize="off" autocomplete="off" spellcheck="false"
             data-md-component="query" data-md-state="active">
      <label class="md-icon md-search__icon" for="__search"></label>
      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        &#xE5CD;
      </button>
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="result">
          <div class="md-search-result__meta">
            Type to start searching
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>

      </div>
      
        <div class="md-flex__cell md-flex__cell--shrink">
          <div class="md-header-nav__source">
            <a href="https://github.com/statsmodels/statsmodels" title="Go to repository" class="md-source" data-md-source="github">

    <div class="md-source__icon">
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24" width="28" height="28">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>
  
  <div class="md-source__repository">
    statsmodels
  </div>
</a>
          </div>
        </div>
      
      
  
  <script src="../../../../_static/javascripts/version_dropdown.js"></script>
  <script>
    var json_loc = "../../../../../versions-v2.json",
        target_loc = "../../../../../",
        text = "Versions";
    $( document ).ready( add_version_dropdown(json_loc, target_loc, text));
  </script>
  

    </div>
  </nav>
</header>

  
  <div class="md-container">
    
    
    
  <nav class="md-tabs" data-md-component="tabs">
    <div class="md-tabs__inner md-grid">
      <ul class="md-tabs__list">
          <li class="md-tabs__item"><a href="../../../index.html" class="md-tabs__link">Module code</a></li>
      </ul>
    </div>
  </nav>
    <main class="md-main">
      <div class="md-main__inner md-grid" data-md-component="container">
        
          <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
            <div class="md-sidebar__scrollwrap">
              <div class="md-sidebar__inner">
                <nav class="md-nav md-nav--primary" data-md-level="0">
  <label class="md-nav__title md-nav__title--site" for="__drawer">
    <a href="../../../../index.html" title="statsmodels" class="md-nav__button md-logo">
      
        <img src="../../../../_static/statsmodels-logo-v2-bw.svg" alt=" logo" width="48" height="48">
      
    </a>
    <a href="../../../../index.html"
       title="statsmodels">statsmodels v0.13.0</a>
  </label>
    <div class="md-nav__source">
      <a href="https://github.com/statsmodels/statsmodels" title="Go to repository" class="md-source" data-md-source="github">

    <div class="md-source__icon">
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24" width="28" height="28">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>
  
  <div class="md-source__repository">
    statsmodels
  </div>
</a>
    </div>
  
  

  
  <ul class="md-nav__list">
    <li class="md-nav__item">
    
    
      <a href="../../../../install.html" class="md-nav__link">Installing statsmodels</a>
      
    
    </li>
    <li class="md-nav__item">
    
    
      <a href="../../../../gettingstarted.html" class="md-nav__link">Getting started</a>
      
    
    </li>
    <li class="md-nav__item">
    
    
      <a href="../../../../user-guide.html" class="md-nav__link">User Guide</a>
      
    
    </li>
    <li class="md-nav__item">
    
    
      <a href="../../../../examples/index.html" class="md-nav__link">Examples</a>
      
    
    </li>
    <li class="md-nav__item">
    
    
      <a href="../../../../api.html" class="md-nav__link">API Reference</a>
      
    
    </li>
    <li class="md-nav__item">
    
    
      <a href="../../../../about.html" class="md-nav__link">About statsmodels</a>
      
    
    </li>
    <li class="md-nav__item">
    
    
      <a href="../../../../dev/index.html" class="md-nav__link">Developer Page</a>
      
    
    </li>
    <li class="md-nav__item">
    
    
      <a href="../../../../release/index.html" class="md-nav__link">Release Notes</a>
      
    
    </li>
  </ul>
  

</nav>
              </div>
            </div>
          </div>
          <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
            <div class="md-sidebar__scrollwrap">
              <div class="md-sidebar__inner">
                
<nav class="md-nav md-nav--secondary">
  <ul class="md-nav__list" data-md-scrollfix="">
    

<li id="searchbox" class="md-nav__item"></li>

  </ul>
</nav>
              </div>
            </div>
          </div>
        
        <div class="md-content">
          <article class="md-content__inner md-typeset" role="main">
            
  <h1 id="modules-statsmodels-tsa-statespace-representation--page-root">Source code for statsmodels.tsa.statespace.representation</h1><div class="highlight"><pre>
<span></span><span class="sd">"""</span>
<span class="sd">State Space Representation</span>

<span class="sd">Author: Chad Fulton</span>
<span class="sd">License: Simplified-BSD</span>
<span class="sd">"""</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">.tools</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">find_best_blas_type</span><span class="p">,</span> <span class="n">validate_matrix_shape</span><span class="p">,</span> <span class="n">validate_vector_shape</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">.initialization</span> <span class="kn">import</span> <span class="n">Initialization</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">tools</span>


<span class="k">class</span> <span class="nc">OptionWrapper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mask_attribute</span><span class="p">,</span> <span class="n">mask_value</span><span class="p">):</span>
        <span class="c1"># Name of the class-level bitmask attribute</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mask_attribute</span> <span class="o">=</span> <span class="n">mask_attribute</span>
        <span class="c1"># Value of this option</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mask_value</span> <span class="o">=</span> <span class="n">mask_value</span>

    <span class="k">def</span> <span class="fm">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">objtype</span><span class="p">):</span>
        <span class="c1"># Return True / False based on whether the bit is set in the bitmask</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask_attribute</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&amp;</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask_value</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">mask_attribute_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask_attribute</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">bool</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">mask_attribute_value</span> <span class="o">|</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask_value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">mask_attribute_value</span> <span class="o">&amp;</span> <span class="o">~</span><span class="bp">self</span><span class="o">.</span><span class="n">mask_value</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask_attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">MatrixWrapper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">attribute</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attribute</span> <span class="o">=</span> <span class="n">attribute</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_attribute</span> <span class="o">=</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">attribute</span>

    <span class="k">def</span> <span class="fm">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">objtype</span><span class="p">):</span>
        <span class="n">matrix</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_attribute</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="c1"># # Remove last dimension if the array is not actually time-varying</span>
        <span class="c1"># if matrix is not None and matrix.shape[-1] == 1:</span>
        <span class="c1">#     return np.squeeze(matrix, -1)</span>
        <span class="k">return</span> <span class="n">matrix</span>

    <span class="k">def</span> <span class="fm">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s2">"F"</span><span class="p">)</span>
        <span class="n">shape</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">shapes</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">attribute</span><span class="p">]</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_set_matrix</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">shape</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_set_vector</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">shape</span><span class="p">)</span>

        <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">shapes</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">attribute</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">shape</span>

    <span class="k">def</span> <span class="nf">_set_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">shape</span><span class="p">):</span>
        <span class="c1"># Expand 1-dimensional array if possible</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span>
                <span class="n">value</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:]</span>

        <span class="c1"># Enforce that the matrix is appropriate size</span>
        <span class="n">validate_matrix_shape</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">obj</span><span class="o">.</span><span class="n">nobs</span>
        <span class="p">)</span>

        <span class="c1"># Expand time-invariant matrix</span>
        <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">value</span><span class="p">[:,</span> <span class="p">:,</span> <span class="kc">None</span><span class="p">],</span> <span class="n">order</span><span class="o">=</span><span class="s2">"F"</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">_set_vector</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">shape</span><span class="p">):</span>
        <span class="c1"># Enforce that the vector has appropriate length</span>
        <span class="n">validate_vector_shape</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">obj</span><span class="o">.</span><span class="n">nobs</span>
        <span class="p">)</span>

        <span class="c1"># Expand the time-invariant vector</span>
        <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">value</span><span class="p">[:,</span> <span class="kc">None</span><span class="p">],</span> <span class="n">order</span><span class="o">=</span><span class="s2">"F"</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">value</span>


<div class="viewcode-block" id="Representation"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.html#statsmodels.tsa.statespace.kalman_filter.Representation">[docs]</a><span class="k">class</span> <span class="nc">Representation</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    State space representation of a time series process</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    k_endog : {array_like, int}</span>
<span class="sd">        The observed time-series process :math:`y` if array like or the</span>
<span class="sd">        number of variables in the process if an integer.</span>
<span class="sd">    k_states : int</span>
<span class="sd">        The dimension of the unobserved state process.</span>
<span class="sd">    k_posdef : int, optional</span>
<span class="sd">        The dimension of a guaranteed positive definite covariance matrix</span>
<span class="sd">        describing the shocks in the measurement equation. Must be less than</span>
<span class="sd">        or equal to `k_states`. Default is `k_states`.</span>
<span class="sd">    initial_variance : float, optional</span>
<span class="sd">        Initial variance used when approximate diffuse initialization is</span>
<span class="sd">        specified. Default is 1e6.</span>
<span class="sd">    initialization : Initialization object or str, optional</span>
<span class="sd">        Initialization method for the initial state. If a string, must be one</span>
<span class="sd">        of {'diffuse', 'approximate_diffuse', 'stationary', 'known'}.</span>
<span class="sd">    initial_state : array_like, optional</span>
<span class="sd">        If `initialization='known'` is used, the mean of the initial state's</span>
<span class="sd">        distribution.</span>
<span class="sd">    initial_state_cov : array_like, optional</span>
<span class="sd">        If `initialization='known'` is used, the covariance matrix of the</span>
<span class="sd">        initial state's distribution.</span>
<span class="sd">    nobs : int, optional</span>
<span class="sd">        If an endogenous vector is not given (i.e. `k_endog` is an integer),</span>
<span class="sd">        the number of observations can optionally be specified. If not</span>
<span class="sd">        specified, they will be set to zero until data is bound to the model.</span>
<span class="sd">    dtype : np.dtype, optional</span>
<span class="sd">        If an endogenous vector is not given (i.e. `k_endog` is an integer),</span>
<span class="sd">        the default datatype of the state space matrices can optionally be</span>
<span class="sd">        specified. Default is `np.float64`.</span>
<span class="sd">    design : array_like, optional</span>
<span class="sd">        The design matrix, :math:`Z`. Default is set to zeros.</span>
<span class="sd">    obs_intercept : array_like, optional</span>
<span class="sd">        The intercept for the observation equation, :math:`d`. Default is set</span>
<span class="sd">        to zeros.</span>
<span class="sd">    obs_cov : array_like, optional</span>
<span class="sd">        The covariance matrix for the observation equation :math:`H`. Default</span>
<span class="sd">        is set to zeros.</span>
<span class="sd">    transition : array_like, optional</span>
<span class="sd">        The transition matrix, :math:`T`. Default is set to zeros.</span>
<span class="sd">    state_intercept : array_like, optional</span>
<span class="sd">        The intercept for the transition equation, :math:`c`. Default is set to</span>
<span class="sd">        zeros.</span>
<span class="sd">    selection : array_like, optional</span>
<span class="sd">        The selection matrix, :math:`R`. Default is set to zeros.</span>
<span class="sd">    state_cov : array_like, optional</span>
<span class="sd">        The covariance matrix for the state equation :math:`Q`. Default is set</span>
<span class="sd">        to zeros.</span>
<span class="sd">    **kwargs</span>
<span class="sd">        Additional keyword arguments. Not used directly. It is present to</span>
<span class="sd">        improve compatibility with subclasses, so that they can use `**kwargs`</span>
<span class="sd">        to specify any default state space matrices (e.g. `design`) without</span>
<span class="sd">        having to clean out any other keyword arguments they might have been</span>
<span class="sd">        passed.</span>

<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    nobs : int</span>
<span class="sd">        The number of observations.</span>
<span class="sd">    k_endog : int</span>
<span class="sd">        The dimension of the observation series.</span>
<span class="sd">    k_states : int</span>
<span class="sd">        The dimension of the unobserved state process.</span>
<span class="sd">    k_posdef : int</span>
<span class="sd">        The dimension of a guaranteed positive</span>
<span class="sd">        definite covariance matrix describing</span>
<span class="sd">        the shocks in the measurement equation.</span>
<span class="sd">    shapes : dictionary of name:tuple</span>
<span class="sd">        A dictionary recording the initial shapes</span>
<span class="sd">        of each of the representation matrices as</span>
<span class="sd">        tuples.</span>
<span class="sd">    initialization : str</span>
<span class="sd">        Kalman filter initialization method. Default is unset.</span>
<span class="sd">    initial_variance : float</span>
<span class="sd">        Initial variance for approximate diffuse</span>
<span class="sd">        initialization. Default is 1e6.</span>

<span class="sd">    Notes</span>
<span class="sd">    -----</span>
<span class="sd">    A general state space model is of the form</span>

<span class="sd">    .. math::</span>

<span class="sd">        y_t &amp; = Z_t \alpha_t + d_t + \varepsilon_t \\</span>
<span class="sd">        \alpha_t &amp; = T_t \alpha_{t-1} + c_t + R_t \eta_t \\</span>

<span class="sd">    where :math:`y_t` refers to the observation vector at time :math:`t`,</span>
<span class="sd">    :math:`\alpha_t` refers to the (unobserved) state vector at time</span>
<span class="sd">    :math:`t`, and where the irregular components are defined as</span>

<span class="sd">    .. math::</span>

<span class="sd">        \varepsilon_t \sim N(0, H_t) \\</span>
<span class="sd">        \eta_t \sim N(0, Q_t) \\</span>

<span class="sd">    The remaining variables (:math:`Z_t, d_t, H_t, T_t, c_t, R_t, Q_t`) in the</span>
<span class="sd">    equations are matrices describing the process. Their variable names and</span>
<span class="sd">    dimensions are as follows</span>

<span class="sd">    Z : `design`          :math:`(k\_endog \times k\_states \times nobs)`</span>

<span class="sd">    d : `obs_intercept`   :math:`(k\_endog \times nobs)`</span>

<span class="sd">    H : `obs_cov`         :math:`(k\_endog \times k\_endog \times nobs)`</span>

<span class="sd">    T : `transition`      :math:`(k\_states \times k\_states \times nobs)`</span>

<span class="sd">    c : `state_intercept` :math:`(k\_states \times nobs)`</span>

<span class="sd">    R : `selection`       :math:`(k\_states \times k\_posdef \times nobs)`</span>

<span class="sd">    Q : `state_cov`       :math:`(k\_posdef \times k\_posdef \times nobs)`</span>

<span class="sd">    In the case that one of the matrices is time-invariant (so that, for</span>
<span class="sd">    example, :math:`Z_t = Z_{t+1} ~ \forall ~ t`), its last dimension may</span>
<span class="sd">    be of size :math:`1` rather than size `nobs`.</span>

<span class="sd">    References</span>
<span class="sd">    ----------</span>
<span class="sd">    .. [*] Durbin, James, and Siem Jan Koopman. 2012.</span>
<span class="sd">       Time Series Analysis by State Space Methods: Second Edition.</span>
<span class="sd">       Oxford University Press.</span>
<span class="sd">    """</span>

    <span class="n">endog</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) The observation vector, alias for `obs`.</span>
<span class="sd">    """</span>
    <span class="n">design</span> <span class="o">=</span> <span class="n">MatrixWrapper</span><span class="p">(</span><span class="s1">'design'</span><span class="p">,</span> <span class="s1">'design'</span><span class="p">)</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) Design matrix: :math:`Z~(k\_endog \times k\_states \times nobs)`</span>
<span class="sd">    """</span>
    <span class="n">obs_intercept</span> <span class="o">=</span> <span class="n">MatrixWrapper</span><span class="p">(</span><span class="s1">'observation intercept'</span><span class="p">,</span> <span class="s1">'obs_intercept'</span><span class="p">)</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) Observation intercept: :math:`d~(k\_endog \times nobs)`</span>
<span class="sd">    """</span>
    <span class="n">obs_cov</span> <span class="o">=</span> <span class="n">MatrixWrapper</span><span class="p">(</span><span class="s1">'observation covariance matrix'</span><span class="p">,</span> <span class="s1">'obs_cov'</span><span class="p">)</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) Observation covariance matrix:</span>
<span class="sd">    :math:`H~(k\_endog \times k\_endog \times nobs)`</span>
<span class="sd">    """</span>
    <span class="n">transition</span> <span class="o">=</span> <span class="n">MatrixWrapper</span><span class="p">(</span><span class="s1">'transition'</span><span class="p">,</span> <span class="s1">'transition'</span><span class="p">)</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) Transition matrix:</span>
<span class="sd">    :math:`T~(k\_states \times k\_states \times nobs)`</span>
<span class="sd">    """</span>
    <span class="n">state_intercept</span> <span class="o">=</span> <span class="n">MatrixWrapper</span><span class="p">(</span><span class="s1">'state intercept'</span><span class="p">,</span> <span class="s1">'state_intercept'</span><span class="p">)</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) State intercept: :math:`c~(k\_states \times nobs)`</span>
<span class="sd">    """</span>
    <span class="n">selection</span> <span class="o">=</span> <span class="n">MatrixWrapper</span><span class="p">(</span><span class="s1">'selection'</span><span class="p">,</span> <span class="s1">'selection'</span><span class="p">)</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) Selection matrix:</span>
<span class="sd">    :math:`R~(k\_states \times k\_posdef \times nobs)`</span>
<span class="sd">    """</span>
    <span class="n">state_cov</span> <span class="o">=</span> <span class="n">MatrixWrapper</span><span class="p">(</span><span class="s1">'state covariance matrix'</span><span class="p">,</span> <span class="s1">'state_cov'</span><span class="p">)</span>
    <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">    (array) State covariance matrix:</span>
<span class="sd">    :math:`Q~(k\_posdef \times k\_posdef \times nobs)`</span>
<span class="sd">    """</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k_endog</span><span class="p">,</span> <span class="n">k_states</span><span class="p">,</span> <span class="n">k_posdef</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">initial_variance</span><span class="o">=</span><span class="mf">1e6</span><span class="p">,</span> <span class="n">nobs</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">,</span>
                 <span class="n">design</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">obs_intercept</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">obs_cov</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">transition</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">state_intercept</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">selection</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">state_cov</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">statespace_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># Check if k_endog is actually the endog array</span>
        <span class="n">endog</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">k_endog</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
            <span class="n">endog</span> <span class="o">=</span> <span class="n">k_endog</span>
            <span class="c1"># If so, assume that it is either column-ordered and in wide format</span>
            <span class="c1"># or row-ordered and in long format</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">endog</span><span class="o">.</span><span class="n">flags</span><span class="p">[</span><span class="s1">'C_CONTIGUOUS'</span><span class="p">]</span> <span class="ow">and</span>
                    <span class="p">(</span><span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">nobs</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)):</span>
                <span class="n">endog</span> <span class="o">=</span> <span class="n">endog</span><span class="o">.</span><span class="n">T</span>
            <span class="n">k_endog</span> <span class="o">=</span> <span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c1"># Endogenous array, dimensions, dtype</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span> <span class="o">=</span> <span class="n">k_endog</span>
        <span class="k">if</span> <span class="n">k_endog</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Number of endogenous variables in statespace'</span>
                             <span class="s1">' model must be a positive number.'</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span> <span class="o">=</span> <span class="n">nobs</span>

        <span class="c1"># Get dimensions from transition equation</span>
        <span class="k">if</span> <span class="n">k_states</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Number of states in statespace model must be a'</span>
                             <span class="s1">' positive number.'</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span> <span class="o">=</span> <span class="n">k_states</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_posdef</span> <span class="o">=</span> <span class="n">k_posdef</span> <span class="k">if</span> <span class="n">k_posdef</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">k_states</span>

        <span class="c1"># Make sure k_posdef &lt;= k_states</span>
        <span class="c1"># TODO: we could technically allow k_posdef &gt; k_states, but the Cython</span>
        <span class="c1"># code needs to be more thoroughly checked to avoid seg faults.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_posdef</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Dimension of state innovation `k_posdef` cannot'</span>
                             <span class="s1">' be larger than the dimension of the state.'</span><span class="p">)</span>

        <span class="c1"># Bind endog, if it was given</span>
        <span class="k">if</span> <span class="n">endog</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>

        <span class="c1"># Record the shapes of all of our matrices</span>
        <span class="c1"># Note: these are time-invariant shapes; in practice the last dimension</span>
        <span class="c1"># may also be `self.nobs` for any or all of these.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">'obs'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span><span class="p">),</span>
            <span class="s1">'design'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
            <span class="s1">'obs_intercept'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
            <span class="s1">'obs_cov'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
            <span class="s1">'transition'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
            <span class="s1">'state_intercept'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
            <span class="s1">'selection'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_posdef</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
            <span class="s1">'state_cov'</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_posdef</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_posdef</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
        <span class="p">}</span>

        <span class="c1"># Representation matrices</span>
        <span class="c1"># These matrices are only used in the Python object as containers,</span>
        <span class="c1"># which will be copied to the appropriate _statespace object if a</span>
        <span class="c1"># filter is called.</span>
        <span class="n">scope</span> <span class="o">=</span> <span class="nb">locals</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">shape</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">'obs'</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="c1"># Create the initial storage array for each matrix</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">name</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s2">"F"</span><span class="p">))</span>

            <span class="c1"># If we were given an initial value for the matrix, set it</span>
            <span class="c1"># (notice it is being set via the descriptor)</span>
            <span class="k">if</span> <span class="n">scope</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">scope</span><span class="p">[</span><span class="n">name</span><span class="p">])</span>

        <span class="c1"># Options</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial_variance</span> <span class="o">=</span> <span class="n">initial_variance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prefix_statespace_map</span> <span class="o">=</span> <span class="p">(</span><span class="n">statespace_classes</span>
                                      <span class="k">if</span> <span class="n">statespace_classes</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
                                      <span class="k">else</span> <span class="n">tools</span><span class="o">.</span><span class="n">prefix_statespace_map</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span>

        <span class="c1"># State-space initialization data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'initialization'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">basic_inits</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'diffuse'</span><span class="p">,</span> <span class="s1">'approximate_diffuse'</span><span class="p">,</span> <span class="s1">'stationary'</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span> <span class="ow">in</span> <span class="n">basic_inits</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">initialization</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span> <span class="o">==</span> <span class="s1">'known'</span><span class="p">:</span>
            <span class="k">if</span> <span class="s1">'constant'</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">constant</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'constant'</span><span class="p">]</span>
            <span class="k">elif</span> <span class="s1">'initial_state'</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="c1"># TODO deprecation warning</span>
                <span class="n">constant</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'initial_state'</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Initial state must be provided when "known"'</span>
                                 <span class="s1">' is the specified initialization method.'</span><span class="p">)</span>
            <span class="k">if</span> <span class="s1">'stationary_cov'</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">stationary_cov</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'stationary_cov'</span><span class="p">]</span>
            <span class="k">elif</span> <span class="s1">'initial_state_cov'</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="c1"># TODO deprecation warning</span>
                <span class="n">stationary_cov</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'initial_state_cov'</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Initial state covariance matrix must be'</span>
                                 <span class="s1">' provided when "known" is the specified'</span>
                                 <span class="s1">' initialization method.'</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="s1">'known'</span><span class="p">,</span> <span class="n">constant</span><span class="o">=</span><span class="n">constant</span><span class="p">,</span>
                            <span class="n">stationary_cov</span><span class="o">=</span><span class="n">stationary_cov</span><span class="p">)</span>
        <span class="k">elif</span> <span class="p">(</span><span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">initialization</span><span class="p">,</span> <span class="n">Initialization</span><span class="p">)</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"Invalid state space initialization method."</span><span class="p">)</span>

        <span class="c1"># Matrix representations storage</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># Setup the underlying statespace object storage</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># Caches</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_time_invariant</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="n">_type</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="c1"># If only a string is given then we must be getting an entire matrix</span>
        <span class="k">if</span> <span class="n">_type</span> <span class="ow">is</span> <span class="nb">str</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'"</span><span class="si">%s</span><span class="s1">" is an invalid state space matrix name'</span>
                                 <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
            <span class="n">matrix</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">key</span><span class="p">)</span>

            <span class="c1"># See note on time-varying arrays, below</span>
            <span class="k">if</span> <span class="n">matrix</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">matrix</span><span class="p">[(</span><span class="nb">slice</span><span class="p">(</span><span class="kc">None</span><span class="p">),)</span><span class="o">*</span><span class="p">(</span><span class="n">matrix</span><span class="o">.</span><span class="n">ndim</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="mi">0</span><span class="p">,)]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">matrix</span>
        <span class="c1"># Otherwise if we have a tuple, we want a slice of a matrix</span>
        <span class="k">elif</span> <span class="n">_type</span> <span class="ow">is</span> <span class="nb">tuple</span><span class="p">:</span>
            <span class="n">name</span><span class="p">,</span> <span class="n">slice_</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'"</span><span class="si">%s</span><span class="s1">" is an invalid state space matrix name'</span>
                                 <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

            <span class="n">matrix</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>

            <span class="c1"># Since the model can support time-varying arrays, but often we</span>
            <span class="c1"># will instead have time-invariant arrays, we want to allow setting</span>
            <span class="c1"># a matrix slice like mod['transition',0,:] even though technically</span>
            <span class="c1"># it should be mod['transition',0,:,0]. Thus if the array in</span>
            <span class="c1"># question is time-invariant but the last slice was excluded,</span>
            <span class="c1"># add it in as a zero.</span>
            <span class="k">if</span> <span class="n">matrix</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">slice_</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">matrix</span><span class="o">.</span><span class="n">ndim</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">slice_</span> <span class="o">=</span> <span class="n">slice_</span> <span class="o">+</span> <span class="p">(</span><span class="mi">0</span><span class="p">,)</span>

            <span class="k">return</span> <span class="n">matrix</span><span class="p">[</span><span class="n">slice_</span><span class="p">]</span>
        <span class="c1"># Otherwise, we have only a single slice index, but it is not a string</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'First index must the name of a valid state space'</span>
                             <span class="s1">' matrix.'</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">_type</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="c1"># If only a string is given then we must be setting an entire matrix</span>
        <span class="k">if</span> <span class="n">_type</span> <span class="ow">is</span> <span class="nb">str</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'"</span><span class="si">%s</span><span class="s1">" is an invalid state space matrix name'</span>
                                 <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="c1"># If it's a tuple (with a string as the first element) then we must be</span>
        <span class="c1"># setting a slice of a matrix</span>
        <span class="k">elif</span> <span class="n">_type</span> <span class="ow">is</span> <span class="nb">tuple</span><span class="p">:</span>
            <span class="n">name</span><span class="p">,</span> <span class="n">slice_</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'"</span><span class="si">%s</span><span class="s1">" is an invalid state space matrix name'</span>
                                 <span class="o">%</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

            <span class="c1"># Change the dtype of the corresponding matrix</span>
            <span class="n">dtype</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">dtype</span>
            <span class="n">matrix</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>
            <span class="n">valid_types</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'f'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'F'</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">matrix</span><span class="o">.</span><span class="n">dtype</span> <span class="o">==</span> <span class="n">dtype</span> <span class="ow">and</span> <span class="n">dtype</span><span class="o">.</span><span class="n">char</span> <span class="ow">in</span> <span class="n">valid_types</span><span class="p">:</span>
                <span class="n">matrix</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">real</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="p">)</span>

            <span class="c1"># Since the model can support time-varying arrays, but often we</span>
            <span class="c1"># will instead have time-invariant arrays, we want to allow setting</span>
            <span class="c1"># a matrix slice like mod['transition',0,:] even though technically</span>
            <span class="c1"># it should be mod['transition',0,:,0]. Thus if the array in</span>
            <span class="c1"># question is time-invariant but the last slice was excluded,</span>
            <span class="c1"># add it in as a zero.</span>
            <span class="k">if</span> <span class="n">matrix</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">slice_</span><span class="p">)</span> <span class="o">==</span> <span class="n">matrix</span><span class="o">.</span><span class="n">ndim</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">slice_</span> <span class="o">=</span> <span class="n">slice_</span> <span class="o">+</span> <span class="p">(</span><span class="mi">0</span><span class="p">,)</span>

            <span class="c1"># Set the new value</span>
            <span class="n">matrix</span><span class="p">[</span><span class="n">slice_</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">matrix</span><span class="p">)</span>
        <span class="c1"># Otherwise we got a single non-string key, (e.g. mod[:]), which is</span>
        <span class="c1"># invalid</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'First index must the name of a valid state space'</span>
                             <span class="s1">' matrix.'</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_clone_kwargs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">endog</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Construct keyword arguments for cloning a state space model</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        endog : array_like</span>
<span class="sd">            An observed time-series process :math:`y`.</span>
<span class="sd">        **kwargs</span>
<span class="sd">            Keyword arguments to pass to the new state space representation</span>
<span class="sd">            model constructor. Those that are not specified are copied from</span>
<span class="sd">            the specification of the current state space model.</span>
<span class="sd">        """</span>

        <span class="c1"># We always need the base dimensions, but they cannot change from</span>
        <span class="c1"># the base model when cloning (the idea is: if these need to change,</span>
        <span class="c1"># need to make a new instance manually, since it's not really cloning).</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">'nobs'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">'k_endog'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">'k_states'</span><span class="p">,</span> <span class="s1">'k_posdef'</span><span class="p">]:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span> <span class="ow">or</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">!=</span> <span class="n">val</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Cannot change the dimension of </span><span class="si">%s</span><span class="s1"> when'</span>
                                 <span class="s1">' cloning.'</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>

        <span class="c1"># Get defaults for time-invariant system matrices, if not otherwise</span>
        <span class="c1"># provided</span>
        <span class="c1"># Time-varying matrices must be replaced.</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">'obs'</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">mat</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">mat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'The `</span><span class="si">%s</span><span class="s1">` matrix is time-varying. Cloning'</span>
                                     <span class="s1">' this model requires specifying an'</span>
                                     <span class="s1">' updated matrix.'</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">mat</span>

        <span class="c1"># Default is to use the same initialization</span>
        <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">'initialization'</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">kwargs</span>

<div class="viewcode-block" id="Representation.clone"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.clone.html#statsmodels.tsa.statespace.kalman_filter.Representation.clone">[docs]</a>    <span class="k">def</span> <span class="nf">clone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">endog</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Clone a state space representation while overriding some elements</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        endog : array_like</span>
<span class="sd">            An observed time-series process :math:`y`.</span>
<span class="sd">        **kwargs</span>
<span class="sd">            Keyword arguments to pass to the new state space representation</span>
<span class="sd">            model constructor. Those that are not specified are copied from</span>
<span class="sd">            the specification of the current state space model.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Representation</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        If some system matrices are time-varying, then new time-varying</span>
<span class="sd">        matrices *must* be provided.</span>
<span class="sd">        """</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_clone_kwargs</span><span class="p">(</span><span class="n">endog</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">mod</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">mod</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">mod</span></div>

<div class="viewcode-block" id="Representation.extend"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.extend.html#statsmodels.tsa.statespace.kalman_filter.Representation.extend">[docs]</a>    <span class="k">def</span> <span class="nf">extend</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">endog</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Extend the current state space model, or a specific (time) subset</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        endog : array_like</span>
<span class="sd">            An observed time-series process :math:`y`.</span>
<span class="sd">        start : int, optional</span>
<span class="sd">            The first period of a time-varying state space model to include in</span>
<span class="sd">            the new model. Has no effect if the state space model is</span>
<span class="sd">            time-invariant. Default is the initial period.</span>
<span class="sd">        end : int, optional</span>
<span class="sd">            The last period of a time-varying state space model to include in</span>
<span class="sd">            the new model. Has no effect if the state space model is</span>
<span class="sd">            time-invariant. Default is the final period.</span>
<span class="sd">        **kwargs</span>
<span class="sd">            Keyword arguments to pass to the new state space representation</span>
<span class="sd">            model constructor. Those that are not specified are copied from</span>
<span class="sd">            the specification of the current state space model.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Representation</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        This method does not allow replacing a time-varying system matrix with</span>
<span class="sd">        a time-invariant one (or vice-versa). If that is required, use `clone`.</span>
<span class="sd">        """</span>
        <span class="n">endog</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">atleast_1d</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">endog</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">endog</span> <span class="o">=</span> <span class="n">endog</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span>
        <span class="n">nobs</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">end</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">end</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span>

        <span class="k">if</span> <span class="n">start</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">start</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span> <span class="o">+</span> <span class="n">start</span>
        <span class="k">if</span> <span class="n">end</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">end</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span> <span class="o">+</span> <span class="n">end</span>
        <span class="k">if</span> <span class="n">start</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'The `start` argument of the extension within the'</span>
                             <span class="s1">' base model cannot be after the end of the'</span>
                             <span class="s1">' base model.'</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">end</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'The `end` argument of the extension within the'</span>
                             <span class="s1">' base model cannot be after the end of the'</span>
                             <span class="s1">' base model.'</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">start</span> <span class="o">&gt;</span> <span class="n">end</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'The `start` argument of the extension within the'</span>
                             <span class="s1">' base model cannot be after the `end` argument.'</span><span class="p">)</span>

        <span class="c1"># Note: if start == end or if end &lt; self.nobs, then we're just cloning</span>
        <span class="c1"># (no extension)</span>
        <span class="n">endog</span> <span class="o">=</span> <span class="n">tools</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">endog</span><span class="p">[:,</span> <span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">endog</span><span class="p">])</span>

        <span class="c1"># Extend any time-varying arrays</span>
        <span class="n">error_ti</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'Model has time-invariant </span><span class="si">%s</span><span class="s1"> matrix, so cannot provide'</span>
                    <span class="s1">' an extended matrix.'</span><span class="p">)</span>
        <span class="n">error_tv</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'Model has time-varying </span><span class="si">%s</span><span class="s1"> matrix, so an updated'</span>
                    <span class="s1">' time-varying matrix for the extension period'</span>
                    <span class="s1">' is required.'</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">shape</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">'obs'</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">mat</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

            <span class="c1"># If we were *not* given an extended value for this matrix...</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="c1"># If this is a time-varying matrix in the existing model</span>
                <span class="k">if</span> <span class="n">mat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="c1"># If we have an extension period, then raise an error</span>
                    <span class="c1"># because we should have been given an extended value</span>
                    <span class="k">if</span> <span class="n">end</span> <span class="o">+</span> <span class="n">nobs</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">error_tv</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
                    <span class="c1"># If we do not have an extension period, then set the new</span>
                    <span class="c1"># time-varying matrix to be the portion of the existing</span>
                    <span class="c1"># time-varying matrix that corresponds to the period of</span>
                    <span class="c1"># interest</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">kwargs</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">mat</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">start</span><span class="p">:</span><span class="n">end</span> <span class="o">+</span> <span class="n">nobs</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">nobs</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Extension is being performed within-sample'</span>
                                 <span class="s1">' so cannot provide an extended matrix'</span><span class="p">)</span>
            <span class="c1"># If we were given an extended value for this matrix</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># TODO: Need to add a check for ndim, and if the matrix has</span>
                <span class="c1"># one fewer dimensions than the existing matrix, add a new axis</span>

                <span class="c1"># If this is a time-invariant matrix in the existing model,</span>
                <span class="c1"># raise an error</span>
                <span class="k">if</span> <span class="n">mat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">error_ti</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

                <span class="c1"># Otherwise, validate the shape of the given extended value</span>
                <span class="c1"># Note: we do not validate the number of observations here</span>
                <span class="c1"># (so we pass in updated_mat.shape[-1] as the nobs argument</span>
                <span class="c1"># in the validate_* calls); instead, we check below that we</span>
                <span class="c1"># at least `nobs` values were passed in and then only take the</span>
                <span class="c1"># first of them as required. This can be useful when e.g. the</span>
                <span class="c1"># end user knows the extension values up to some maximum</span>
                <span class="c1"># endpoint, but does not know what the calling methods may</span>
                <span class="c1"># specifically require.</span>
                <span class="n">updated_mat</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">kwargs</span><span class="p">[</span><span class="n">name</span><span class="p">])</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                    <span class="n">validate_vector_shape</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">updated_mat</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                          <span class="n">updated_mat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">validate_matrix_shape</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">updated_mat</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                          <span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">updated_mat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>

                <span class="k">if</span> <span class="n">updated_mat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nobs</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">error_tv</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">updated_mat</span> <span class="o">=</span> <span class="n">updated_mat</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="p">:</span><span class="n">nobs</span><span class="p">]</span>

                <span class="c1"># Concatenate to get the new time-varying matrix</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">c_</span><span class="p">[</span><span class="n">mat</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">],</span> <span class="n">updated_mat</span><span class="p">]</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">endog</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="Representation.diff_endog"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.diff_endog.html#statsmodels.tsa.statespace.kalman_filter.Representation.diff_endog">[docs]</a>    <span class="k">def</span> <span class="nf">diff_endog</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_endog</span><span class="p">,</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">):</span>
        <span class="c1"># TODO: move this function to tools?</span>
        <span class="n">endog</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">T</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_endog</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">endog</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Given data (length </span><span class="si">%d</span><span class="s1">) is too short to diff'</span>
                             <span class="s1">' against model data (length </span><span class="si">%d</span><span class="s1">).'</span>
                             <span class="o">%</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">new_endog</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">endog</span><span class="p">)))</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_endog</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">endog</span><span class="p">):</span>
            <span class="n">nobs_append</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_endog</span><span class="p">)</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>
            <span class="n">endog</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">c_</span><span class="p">[</span><span class="n">endog</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">new_endog</span><span class="p">[</span><span class="o">-</span><span class="n">nobs_append</span><span class="p">:]</span><span class="o">.</span><span class="n">T</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]</span><span class="o">.</span><span class="n">T</span>

        <span class="n">new_nan</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">new_endog</span><span class="p">)</span>
        <span class="n">existing_nan</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>
        <span class="n">diff</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">new_endog</span> <span class="o">-</span> <span class="n">endog</span><span class="p">)</span>
        <span class="n">diff</span><span class="p">[</span><span class="n">new_nan</span> <span class="o">^</span> <span class="n">existing_nan</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span>
        <span class="n">diff</span><span class="p">[</span><span class="n">new_nan</span> <span class="o">&amp;</span> <span class="n">existing_nan</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>

        <span class="n">is_revision</span> <span class="o">=</span> <span class="p">(</span><span class="n">diff</span> <span class="o">&gt;</span> <span class="n">tolerance</span><span class="p">)</span>
        <span class="n">is_new</span> <span class="o">=</span> <span class="n">existing_nan</span> <span class="o">&amp;</span> <span class="o">~</span><span class="n">new_nan</span>
        <span class="n">is_revision</span><span class="p">[</span><span class="n">is_new</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="n">revision_ix</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">is_revision</span><span class="p">)))</span>
        <span class="n">new_ix</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">is_new</span><span class="p">)))</span>

        <span class="k">return</span> <span class="n">revision_ix</span><span class="p">,</span> <span class="n">new_ix</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        (str) BLAS prefix of currently active representation matrices</span>
<span class="sd">        """</span>
        <span class="n">arrays</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_design</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_obs_intercept</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_obs_cov</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_transition</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_state_intercept</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_selection</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_state_cov</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">endog</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">arrays</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">endog</span><span class="p">,)</span> <span class="o">+</span> <span class="n">arrays</span>
        <span class="k">return</span> <span class="n">find_best_blas_type</span><span class="p">(</span><span class="n">arrays</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">dtype</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        (dtype) Datatype of currently active representation matrices</span>
<span class="sd">        """</span>
        <span class="k">return</span> <span class="n">tools</span><span class="o">.</span><span class="n">prefix_dtype_map</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">time_invariant</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        (bool) Whether or not currently active representation matrices are</span>
<span class="sd">        time-invariant</span>
<span class="sd">        """</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_time_invariant</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_design</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_obs_intercept</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_obs_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_transition</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_state_intercept</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_selection</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_state_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_time_invariant</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_statespace</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span>
        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">obs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sa">r</span><span class="sd">"""</span>
<span class="sd">        (array) Observation vector: :math:`y~(k\_endog \times nobs)`</span>
<span class="sd">        """</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">endog</span>

<div class="viewcode-block" id="Representation.bind"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.bind.html#statsmodels.tsa.statespace.kalman_filter.Representation.bind">[docs]</a>    <span class="k">def</span> <span class="nf">bind</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">endog</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Bind data to the statespace representation</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        endog : ndarray</span>
<span class="sd">            Endogenous data to bind to the model. Must be column-ordered</span>
<span class="sd">            ndarray with shape (`k_endog`, `nobs`) or row-ordered ndarray with</span>
<span class="sd">            shape (`nobs`, `k_endog`).</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        The strict requirements arise because the underlying statespace and</span>
<span class="sd">        Kalman filtering classes require Fortran-ordered arrays in the wide</span>
<span class="sd">        format (shaped (`k_endog`, `nobs`)), and this structure is setup to</span>
<span class="sd">        prevent copying arrays in memory.</span>

<span class="sd">        By default, numpy arrays are row (C)-ordered and most time series are</span>
<span class="sd">        represented in the long format (with time on the 0-th axis). In this</span>
<span class="sd">        case, no copying or re-ordering needs to be performed, instead the</span>
<span class="sd">        array can simply be transposed to get it in the right order and shape.</span>

<span class="sd">        Although this class (Representation) has stringent `bind` requirements,</span>
<span class="sd">        it is assumed that it will rarely be used directly.</span>
<span class="sd">        """</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">endog</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"Invalid endogenous array; must be an ndarray."</span><span class="p">)</span>

        <span class="c1"># Make sure we have a 2-dimensional array</span>
        <span class="c1"># Note: reshaping a 1-dim array into a 2-dim array by changing the</span>
        <span class="c1">#       shape tuple always results in a row (C)-ordered array, so it</span>
        <span class="c1">#       must be shaped (nobs, k_endog)</span>
        <span class="k">if</span> <span class="n">endog</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># In the case of nobs x 0 arrays</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">endog</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
            <span class="c1"># In the case of k_endog x 0 arrays</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">endog</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">endog</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Invalid endogenous array provided; must be'</span>
                             <span class="s1">' 2-dimensional.'</span><span class="p">)</span>

        <span class="c1"># Check for valid column-ordered arrays</span>
        <span class="k">if</span> <span class="n">endog</span><span class="o">.</span><span class="n">flags</span><span class="p">[</span><span class="s1">'F_CONTIGUOUS'</span><span class="p">]</span> <span class="ow">and</span> <span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="c1"># Check for valid row-ordered arrays, and transpose them to be the</span>
        <span class="c1"># correct column-ordered array</span>
        <span class="k">elif</span> <span class="n">endog</span><span class="o">.</span><span class="n">flags</span><span class="p">[</span><span class="s1">'C_CONTIGUOUS'</span><span class="p">]</span> <span class="ow">and</span> <span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span><span class="p">:</span>
            <span class="n">endog</span> <span class="o">=</span> <span class="n">endog</span><span class="o">.</span><span class="n">T</span>
        <span class="c1"># Invalid column-ordered arrays</span>
        <span class="k">elif</span> <span class="n">endog</span><span class="o">.</span><span class="n">flags</span><span class="p">[</span><span class="s1">'F_CONTIGUOUS'</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Invalid endogenous array; column-ordered'</span>
                             <span class="s1">' arrays must have first axis shape of'</span>
                             <span class="s1">' `k_endog`.'</span><span class="p">)</span>
        <span class="c1"># Invalid row-ordered arrays</span>
        <span class="k">elif</span> <span class="n">endog</span><span class="o">.</span><span class="n">flags</span><span class="p">[</span><span class="s1">'C_CONTIGUOUS'</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Invalid endogenous array; row-ordered'</span>
                             <span class="s1">' arrays must have last axis shape of'</span>
                             <span class="s1">' `k_endog`.'</span><span class="p">)</span>
        <span class="c1"># Non-contiguous arrays</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Invalid endogenous array; must be ordered in'</span>
                             <span class="s1">' contiguous memory.'</span><span class="p">)</span>

        <span class="c1"># We may still have a non-fortran contiguous array, so double-check</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">endog</span><span class="o">.</span><span class="n">flags</span><span class="p">[</span><span class="s1">'F_CONTIGUOUS'</span><span class="p">]:</span>
            <span class="n">endog</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asfortranarray</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>

        <span class="c1"># Set a flag for complex data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_complex_endog</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">iscomplexobj</span><span class="p">(</span><span class="n">endog</span><span class="p">)</span>

        <span class="c1"># Set the data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">endog</span> <span class="o">=</span> <span class="n">endog</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

        <span class="c1"># Reset shapes</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'shapes'</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="p">[</span><span class="s1">'obs'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">shape</span></div>

<div class="viewcode-block" id="Representation.initialize"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.initialize.html#statsmodels.tsa.statespace.kalman_filter.Representation.initialize">[docs]</a>    <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initialization</span><span class="p">,</span> <span class="n">approximate_diffuse_variance</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">constant</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stationary_cov</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">"""Create an Initialization object if necessary"""</span>
        <span class="k">if</span> <span class="n">initialization</span> <span class="o">==</span> <span class="s1">'known'</span><span class="p">:</span>
            <span class="n">initialization</span> <span class="o">=</span> <span class="n">Initialization</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="s1">'known'</span><span class="p">,</span>
                                            <span class="n">constant</span><span class="o">=</span><span class="n">constant</span><span class="p">,</span>
                                            <span class="n">stationary_cov</span><span class="o">=</span><span class="n">stationary_cov</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">initialization</span> <span class="o">==</span> <span class="s1">'approximate_diffuse'</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">approximate_diffuse_variance</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">approximate_diffuse_variance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_variance</span>
            <span class="n">initialization</span> <span class="o">=</span> <span class="n">Initialization</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="s1">'approximate_diffuse'</span><span class="p">,</span>
                <span class="n">approximate_diffuse_variance</span><span class="o">=</span><span class="n">approximate_diffuse_variance</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">initialization</span> <span class="o">==</span> <span class="s1">'stationary'</span><span class="p">:</span>
            <span class="n">initialization</span> <span class="o">=</span> <span class="n">Initialization</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="s1">'stationary'</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">initialization</span> <span class="o">==</span> <span class="s1">'diffuse'</span><span class="p">:</span>
            <span class="n">initialization</span> <span class="o">=</span> <span class="n">Initialization</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="s1">'diffuse'</span><span class="p">)</span>

        <span class="c1"># We must have an initialization object at this point</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">initialization</span><span class="p">,</span> <span class="n">Initialization</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"Invalid state space initialization method."</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span> <span class="o">=</span> <span class="n">initialization</span></div>

<div class="viewcode-block" id="Representation.initialize_known"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.initialize_known.html#statsmodels.tsa.statespace.kalman_filter.Representation.initialize_known">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_known</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">constant</span><span class="p">,</span> <span class="n">stationary_cov</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Initialize the statespace model with known distribution for initial</span>
<span class="sd">        state.</span>

<span class="sd">        These values are assumed to be known with certainty or else</span>
<span class="sd">        filled with parameters during, for example, maximum likelihood</span>
<span class="sd">        estimation.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        constant : array_like</span>
<span class="sd">            Known mean of the initial state vector.</span>
<span class="sd">        stationary_cov : array_like</span>
<span class="sd">            Known covariance matrix of the initial state vector.</span>
<span class="sd">        """</span>
        <span class="n">constant</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">constant</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s2">"F"</span><span class="p">)</span>
        <span class="n">stationary_cov</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">stationary_cov</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s2">"F"</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">constant</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Invalid dimensions for constant state vector.'</span>
                             <span class="s1">' Requires shape (</span><span class="si">%d</span><span class="s1">,), got </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span>
                             <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">constant</span><span class="o">.</span><span class="n">shape</span><span class="p">)))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">stationary_cov</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'Invalid dimensions for stationary covariance'</span>
                             <span class="s1">' matrix. Requires shape (</span><span class="si">%d</span><span class="s1">,</span><span class="si">%d</span><span class="s1">), got </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span>
                             <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span><span class="p">,</span>
                              <span class="nb">str</span><span class="p">(</span><span class="n">stationary_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">)))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="s1">'known'</span><span class="p">,</span> <span class="n">constant</span><span class="o">=</span><span class="n">constant</span><span class="p">,</span>
                        <span class="n">stationary_cov</span><span class="o">=</span><span class="n">stationary_cov</span><span class="p">)</span></div>

<div class="viewcode-block" id="Representation.initialize_approximate_diffuse"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.initialize_approximate_diffuse.html#statsmodels.tsa.statespace.kalman_filter.Representation.initialize_approximate_diffuse">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_approximate_diffuse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variance</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Initialize the statespace model with approximate diffuse values.</span>

<span class="sd">        Rather than following the exact diffuse treatment (which is developed</span>
<span class="sd">        for the case that the variance becomes infinitely large), this assigns</span>
<span class="sd">        an arbitrary large number for the variance.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variance : float, optional</span>
<span class="sd">            The variance for approximating diffuse initial conditions. Default</span>
<span class="sd">            is 1e6.</span>
<span class="sd">        """</span>
        <span class="k">if</span> <span class="n">variance</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">variance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_variance</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="s1">'approximate_diffuse'</span><span class="p">,</span>
                        <span class="n">approximate_diffuse_variance</span><span class="o">=</span><span class="n">variance</span><span class="p">)</span></div>

<div class="viewcode-block" id="Representation.initialize_stationary"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.initialize_stationary.html#statsmodels.tsa.statespace.kalman_filter.Representation.initialize_stationary">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_stationary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Initialize the statespace model as stationary.</span>
<span class="sd">        """</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="s1">'stationary'</span><span class="p">)</span></div>

<div class="viewcode-block" id="Representation.initialize_diffuse"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.Representation.initialize_diffuse.html#statsmodels.tsa.statespace.kalman_filter.Representation.initialize_diffuse">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_diffuse</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">"""</span>
<span class="sd">        Initialize the statespace model as stationary.</span>
<span class="sd">        """</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="s1">'diffuse'</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_initialize_representation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span>
        <span class="n">dtype</span> <span class="o">=</span> <span class="n">tools</span><span class="o">.</span><span class="n">prefix_dtype_map</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span>

        <span class="c1"># If the dtype-specific representation matrices do not exist, create</span>
        <span class="c1"># them</span>
        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">:</span>
            <span class="c1"># Copy the statespace representation matrices</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">matrix</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">matrix</span> <span class="o">==</span> <span class="s1">'obs'</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="n">matrix</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">obs</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Note: this always makes a copy</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="n">matrix</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">matrix</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="p">)</span>
                    <span class="p">)</span>
        <span class="c1"># If they do exist, update them</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">matrix</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="n">existing</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="n">matrix</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">matrix</span> <span class="o">==</span> <span class="s1">'obs'</span><span class="p">:</span>
                    <span class="c1"># existing[:] = self.obs.astype(dtype)</span>
                    <span class="k">pass</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">new</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="n">matrix</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">existing</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="n">new</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span>
                        <span class="n">existing</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">new</span><span class="p">[:]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="n">matrix</span><span class="p">]</span> <span class="o">=</span> <span class="n">new</span>

        <span class="c1"># Determine if we need to (re-)create the _statespace models</span>
        <span class="c1"># (if time-varying matrices changed)</span>
        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">:</span>
            <span class="n">ss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span>
            <span class="n">create</span> <span class="o">=</span> <span class="p">(</span>
                <span class="ow">not</span> <span class="n">ss</span><span class="o">.</span><span class="n">obs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="n">ss</span><span class="o">.</span><span class="n">design</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">design</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="n">ss</span><span class="o">.</span><span class="n">obs_intercept</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_intercept</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="n">ss</span><span class="o">.</span><span class="n">obs_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="n">ss</span><span class="o">.</span><span class="n">transition</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">transition</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="p">(</span><span class="n">ss</span><span class="o">.</span><span class="n">state_intercept</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">state_intercept</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="n">ss</span><span class="o">.</span><span class="n">selection</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">selection</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="ow">or</span>
                <span class="ow">not</span> <span class="n">ss</span><span class="o">.</span><span class="n">state_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_cov</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">create</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="c1"># (re-)create if necessary</span>
        <span class="k">if</span> <span class="n">create</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">prefix</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">:</span>
                <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span>

            <span class="c1"># Setup the base statespace object</span>
            <span class="bp">cls</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefix_statespace_map</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span> <span class="o">=</span> <span class="bp">cls</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'obs'</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'design'</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'obs_intercept'</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'obs_cov'</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'transition'</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'state_intercept'</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'selection'</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_representations</span><span class="p">[</span><span class="n">prefix</span><span class="p">][</span><span class="s1">'state_cov'</span><span class="p">]</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">dtype</span><span class="p">,</span> <span class="n">create</span>

    <span class="k">def</span> <span class="nf">_initialize_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">complex_step</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="c1"># TODO once the transition to using the Initialization objects is</span>
        <span class="c1"># complete, this should be moved entirely to the _{{prefix}}Statespace</span>
        <span class="c1"># object.</span>
        <span class="k">if</span> <span class="n">prefix</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span>

        <span class="c1"># (Re-)initialize the statespace model</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">initialization</span><span class="p">,</span> <span class="n">Initialization</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span><span class="o">.</span><span class="n">initialized</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">'Initialization is incomplete.'</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">initialization</span><span class="p">,</span>
                                                 <span class="n">complex_step</span><span class="o">=</span><span class="n">complex_step</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">'Statespace model not initialized.'</span><span class="p">)</span></div>


<div class="viewcode-block" id="FrozenRepresentation"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.FrozenRepresentation.html#statsmodels.tsa.statespace.kalman_filter.FrozenRepresentation">[docs]</a><span class="k">class</span> <span class="nc">FrozenRepresentation</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">"""</span>
<span class="sd">    Frozen Statespace Model</span>

<span class="sd">    Takes a snapshot of a Statespace model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model : Representation</span>
<span class="sd">        A Statespace representation</span>

<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    nobs : int</span>
<span class="sd">        Number of observations.</span>
<span class="sd">    k_endog : int</span>
<span class="sd">        The dimension of the observation series.</span>
<span class="sd">    k_states : int</span>
<span class="sd">        The dimension of the unobserved state process.</span>
<span class="sd">    k_posdef : int</span>
<span class="sd">        The dimension of a guaranteed positive definite</span>
<span class="sd">        covariance matrix describing the shocks in the</span>
<span class="sd">        measurement equation.</span>
<span class="sd">    dtype : dtype</span>
<span class="sd">        Datatype of representation matrices</span>
<span class="sd">    prefix : str</span>
<span class="sd">        BLAS prefix of representation matrices</span>
<span class="sd">    shapes : dictionary of name:tuple</span>
<span class="sd">        A dictionary recording the shapes of each of</span>
<span class="sd">        the representation matrices as tuples.</span>
<span class="sd">    endog : ndarray</span>
<span class="sd">        The observation vector.</span>
<span class="sd">    design : ndarray</span>
<span class="sd">        The design matrix, :math:`Z`.</span>
<span class="sd">    obs_intercept : ndarray</span>
<span class="sd">        The intercept for the observation equation, :math:`d`.</span>
<span class="sd">    obs_cov : ndarray</span>
<span class="sd">        The covariance matrix for the observation equation :math:`H`.</span>
<span class="sd">    transition : ndarray</span>
<span class="sd">        The transition matrix, :math:`T`.</span>
<span class="sd">    state_intercept : ndarray</span>
<span class="sd">        The intercept for the transition equation, :math:`c`.</span>
<span class="sd">    selection : ndarray</span>
<span class="sd">        The selection matrix, :math:`R`.</span>
<span class="sd">    state_cov : ndarray</span>
<span class="sd">        The covariance matrix for the state equation :math:`Q`.</span>
<span class="sd">    missing : array of bool</span>
<span class="sd">        An array of the same size as `endog`, filled</span>
<span class="sd">        with boolean values that are True if the</span>
<span class="sd">        corresponding entry in `endog` is NaN and False</span>
<span class="sd">        otherwise.</span>
<span class="sd">    nmissing : array of int</span>
<span class="sd">        An array of size `nobs`, where the ith entry</span>
<span class="sd">        is the number (between 0 and `k_endog`) of NaNs in</span>
<span class="sd">        the ith row of the `endog` array.</span>
<span class="sd">    time_invariant : bool</span>
<span class="sd">        Whether or not the representation matrices are time-invariant</span>
<span class="sd">    initialization : Initialization object</span>
<span class="sd">        Kalman filter initialization method.</span>
<span class="sd">    initial_state : array_like</span>
<span class="sd">        The state vector used to initialize the Kalamn filter.</span>
<span class="sd">    initial_state_cov : array_like</span>
<span class="sd">        The state covariance matrix used to initialize the Kalamn filter.</span>
<span class="sd">    """</span>
    <span class="n">_model_attributes</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">'model'</span><span class="p">,</span> <span class="s1">'prefix'</span><span class="p">,</span> <span class="s1">'dtype'</span><span class="p">,</span> <span class="s1">'nobs'</span><span class="p">,</span> <span class="s1">'k_endog'</span><span class="p">,</span> <span class="s1">'k_states'</span><span class="p">,</span>
        <span class="s1">'k_posdef'</span><span class="p">,</span> <span class="s1">'time_invariant'</span><span class="p">,</span> <span class="s1">'endog'</span><span class="p">,</span> <span class="s1">'design'</span><span class="p">,</span> <span class="s1">'obs_intercept'</span><span class="p">,</span>
        <span class="s1">'obs_cov'</span><span class="p">,</span> <span class="s1">'transition'</span><span class="p">,</span> <span class="s1">'state_intercept'</span><span class="p">,</span> <span class="s1">'selection'</span><span class="p">,</span>
        <span class="s1">'state_cov'</span><span class="p">,</span> <span class="s1">'missing'</span><span class="p">,</span> <span class="s1">'nmissing'</span><span class="p">,</span> <span class="s1">'shapes'</span><span class="p">,</span> <span class="s1">'initialization'</span><span class="p">,</span>
        <span class="s1">'initial_state'</span><span class="p">,</span> <span class="s1">'initial_state_cov'</span><span class="p">,</span> <span class="s1">'initial_variance'</span>
    <span class="p">]</span>
    <span class="n">_attributes</span> <span class="o">=</span> <span class="n">_model_attributes</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="c1"># Initialize all attributes to None</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_attributes</span><span class="p">:</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="c1"># Update the representation attributes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">update_representation</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

<div class="viewcode-block" id="FrozenRepresentation.update_representation"><a class="viewcode-back" href="../../../../generated/statsmodels.tsa.statespace.representation.FrozenRepresentation.update_representation.html#statsmodels.tsa.statespace.kalman_filter.FrozenRepresentation.update_representation">[docs]</a>    <span class="k">def</span> <span class="nf">update_representation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">"""Update model Representation"""</span>
        <span class="c1"># Model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>

        <span class="c1"># Data type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">prefix</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dtype</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">dtype</span>

        <span class="c1"># Copy the model dimensions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">nobs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_endog</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">k_endog</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_states</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">k_states</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_posdef</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">k_posdef</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">time_invariant</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">time_invariant</span>

        <span class="c1"># Save the state space representation at the time</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">endog</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">endog</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">design</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_design</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">obs_intercept</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_obs_intercept</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">obs_cov</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_obs_cov</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">transition</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_transition</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">state_intercept</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_state_intercept</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selection</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_selection</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">state_cov</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_state_cov</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">missing</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">]</span><span class="o">.</span><span class="n">missing</span><span class="p">,</span>
                                <span class="n">copy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nmissing</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">]</span><span class="o">.</span><span class="n">nmissing</span><span class="p">,</span>
                                 <span class="n">copy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="c1"># Save the final shapes of the matrices</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">shapes</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">'obs'</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">shape</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">shapes</span><span class="p">[</span><span class="s1">'obs'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">endog</span><span class="o">.</span><span class="n">shape</span>

        <span class="c1"># Save the state space initialization</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initialization</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">initialization</span>

        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">initialization</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">model</span><span class="o">.</span><span class="n">_initialize_state</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">initial_state</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span>
                <span class="n">model</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">initial_state_cov</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span>
                <span class="n">model</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state_cov</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">initial_diffuse_state_cov</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span>
                <span class="n">model</span><span class="o">.</span><span class="n">_statespaces</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">]</span><span class="o">.</span><span class="n">initial_diffuse_state_cov</span><span class="p">,</span>
                <span class="n">copy</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div></div>
</pre></div>

          </article>
        </div>
      </div>
    </main>
  </div>
  <footer class="md-footer">
    <div class="md-footer-nav">
      <nav class="md-footer-nav__inner md-grid">
          
          
        </a>
        
      </nav>
    </div>
    <div class="md-footer-meta md-typeset">
      <div class="md-footer-meta__inner md-grid">
        <div class="md-footer-copyright">
          <div class="md-footer-copyright__highlight">
              &#169; Copyright 2009-2019, Josef Perktold, Skipper Seabold, Jonathan Taylor, statsmodels-developers.
              
          </div>
            Last updated on
              Oct 06, 2021.
            <br/>
            Created using
            <a href="http://www.sphinx-doc.org/">Sphinx</a> 4.0.3.
             and
            <a href="https://github.com/bashtage/sphinx-material/">Material for
              Sphinx</a>
        </div>
      </div>
    </div>
  </footer>
  <script src="../../../../_static/javascripts/application.js"></script>
  <script>app.initialize({version: "1.0.4", url: {base: ".."}})</script>
  </body>
</html>