267 lines
37 KiB
HTML
267 lines
37 KiB
HTML
|
|
|
|
<!doctype html>
|
|
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"><script type="text/javascript">
|
|
|
|
var _gaq = _gaq || [];
|
|
_gaq.push(['_setAccount', 'UA-55120145-3']);
|
|
_gaq.push(['_trackPageview']);
|
|
|
|
(function() {
|
|
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
|
})();
|
|
</script>
|
|
<title>pyFTS.benchmarks.quantreg — pyFTS 1.6 documentation</title>
|
|
<link rel="stylesheet" href="../../../_static/bizstyle.css" type="text/css" />
|
|
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
|
|
|
|
<script id="documentation_options" data-url_root="../../../" 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 src="../../../_static/language_data.js"></script>
|
|
<script src="../../../_static/bizstyle.js"></script>
|
|
<link rel="index" title="Index" href="../../../genindex.html" />
|
|
<link rel="search" title="Search" href="../../../search.html" />
|
|
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
|
<!--[if lt IE 9]>
|
|
<script src="_static/css3-mediaqueries.js"></script>
|
|
<![endif]-->
|
|
</head><body>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.6 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.quantreg</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<h1>Source code for pyFTS.benchmarks.quantreg</h1><div class="highlight"><pre>
|
|
<span></span><span class="ch">#!/usr/bin/python</span>
|
|
<span class="c1"># -*- coding: utf8 -*-</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
|
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
|
|
<span class="kn">from</span> <span class="nn">statsmodels.regression.quantile_regression</span> <span class="kn">import</span> <span class="n">QuantReg</span>
|
|
<span class="kn">from</span> <span class="nn">statsmodels.tsa.tsatools</span> <span class="kn">import</span> <span class="n">lagmat</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="kn">import</span> <span class="n">SortedCollection</span><span class="p">,</span> <span class="n">fts</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.probabilistic</span> <span class="kn">import</span> <span class="n">ProbabilityDistribution</span>
|
|
|
|
|
|
<div class="viewcode-block" id="QuantileRegression"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression">[docs]</a><span class="k">class</span> <span class="nc">QuantileRegression</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
|
|
<span class="sd">"""Façade for statsmodels.regression.quantile_regression"""</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="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="nb">super</span><span class="p">(</span><span class="n">QuantileRegression</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</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">name</span> <span class="o">=</span> <span class="s2">"QAR"</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">"Quantile Regression"</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</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="s2">"alpha"</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">)</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">dist</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="s2">"dist"</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">upper_qt</span> <span class="o">=</span> <span class="kc">None</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mean_qt</span> <span class="o">=</span> <span class="kc">None</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">lower_qt</span> <span class="o">=</span> <span class="kc">None</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">dist_qt</span> <span class="o">=</span> <span class="kc">None</span>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.train"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">):</span>
|
|
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
|
|
|
|
<span class="n">lagdata</span><span class="p">,</span> <span class="n">ndata</span> <span class="o">=</span> <span class="n">lagmat</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">maxlag</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">trim</span><span class="o">=</span><span class="s2">"both"</span><span class="p">,</span> <span class="n">original</span><span class="o">=</span><span class="s1">'sep'</span><span class="p">)</span>
|
|
|
|
<span class="n">mqt</span> <span class="o">=</span> <span class="n">QuantReg</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">lagdata</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="n">uqt</span> <span class="o">=</span> <span class="n">QuantReg</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">lagdata</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
|
|
<span class="n">lqt</span> <span class="o">=</span> <span class="n">QuantReg</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">lagdata</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
|
|
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">mean_qt</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">mqt</span><span class="o">.</span><span class="n">params</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</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">upper_qt</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">uqt</span><span class="o">.</span><span class="n">params</span><span class="p">]</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">lower_qt</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">lqt</span><span class="o">.</span><span class="n">params</span><span class="p">]</span>
|
|
|
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dist</span><span class="p">:</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">dist_qt</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">alpha</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.05</span><span class="p">):</span>
|
|
<span class="n">lqt</span> <span class="o">=</span> <span class="n">QuantReg</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">lagdata</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span>
|
|
<span class="n">uqt</span> <span class="o">=</span> <span class="n">QuantReg</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">lagdata</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">alpha</span><span class="p">)</span>
|
|
<span class="n">lo_qt</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">lqt</span><span class="o">.</span><span class="n">params</span><span class="p">]</span>
|
|
<span class="n">up_qt</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">uqt</span><span class="o">.</span><span class="n">params</span><span class="p">]</span>
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">dist_qt</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">lo_qt</span><span class="p">,</span> <span class="n">up_qt</span><span class="p">])</span>
|
|
|
|
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">"QAR(</span><span class="si">{}</span><span class="s2">)-</span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.linearmodel"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.linearmodel">[docs]</a> <span class="k">def</span> <span class="nf">linearmodel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">data</span><span class="p">,</span><span class="n">params</span><span class="p">):</span>
|
|
<span class="c1">#return params[0] + sum([ data[k] * params[k+1] for k in np.arange(0, self.order) ])</span>
|
|
<span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">*</span> <span class="n">params</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)])</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.point_to_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.point_to_interval">[docs]</a> <span class="k">def</span> <span class="nf">point_to_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">lo_params</span><span class="p">,</span> <span class="n">up_params</span><span class="p">):</span>
|
|
<span class="n">lo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">linearmodel</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">lo_params</span><span class="p">)</span>
|
|
<span class="n">up</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">linearmodel</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">up_params</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="n">lo</span><span class="p">,</span> <span class="n">up</span><span class="p">]</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.interval_to_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.interval_to_interval">[docs]</a> <span class="k">def</span> <span class="nf">interval_to_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">lo_params</span><span class="p">,</span> <span class="n">up_params</span><span class="p">):</span>
|
|
<span class="n">lo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">linearmodel</span><span class="p">([</span><span class="n">k</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">],</span> <span class="n">lo_params</span><span class="p">)</span>
|
|
<span class="n">up</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">linearmodel</span><span class="p">([</span><span class="n">k</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">],</span> <span class="n">up_params</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="n">lo</span><span class="p">,</span> <span class="n">up</span><span class="p">]</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.forecast"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
|
|
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span> <span class="c1">#+1 to forecast one step ahead given all available lags</span>
|
|
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="n">k</span><span class="p">]</span>
|
|
|
|
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">linearmodel</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mean_qt</span><span class="p">))</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.forecast_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
|
|
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">,</span> <span class="n">l</span><span class="p">):</span>
|
|
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
|
|
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">point_to_interval</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lower_qt</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper_qt</span><span class="p">))</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.forecast_ahead_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
|
|
<span class="n">smoothing</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="s2">"smoothing"</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
|
|
|
|
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">nmeans</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
|
|
<span class="n">nmeans</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">ndata</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
|
|
<span class="n">intl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">point_to_interval</span><span class="p">(</span><span class="n">nmeans</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">lower_qt</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper_qt</span><span class="p">)</span>
|
|
|
|
<span class="n">tmpk</span> <span class="o">=</span> <span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span>
|
|
|
|
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">intl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="p">(</span><span class="n">tmpk</span><span class="o">*</span><span class="n">smoothing</span><span class="p">)),</span> <span class="n">intl</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="p">(</span><span class="n">tmpk</span><span class="o">*</span><span class="n">smoothing</span><span class="p">))])</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.forecast_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
|
|
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s2">"histogram"</span><span class="p">,</span>
|
|
<span class="n">uod</span><span class="o">=</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">])</span>
|
|
<span class="n">intervals</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">qt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dist_qt</span><span class="p">:</span>
|
|
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
|
|
<span class="n">intl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">point_to_interval</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">qt</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">qt</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
|
|
<span class="n">intervals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">intl</span><span class="p">)</span>
|
|
|
|
<span class="n">dist</span><span class="o">.</span><span class="n">append_interval</span><span class="p">(</span><span class="n">intervals</span><span class="p">)</span>
|
|
|
|
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret</span></div>
|
|
|
|
<div class="viewcode-block" id="QuantileRegression.forecast_ahead_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.quantreg.QuantileRegression.forecast_ahead_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="n">smoothing</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="s2">"smoothing"</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
|
|
|
|
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">nmeans</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
|
|
<span class="n">nmeans</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
|
|
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s2">"histogram"</span><span class="p">,</span>
|
|
<span class="n">uod</span><span class="o">=</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">])</span>
|
|
|
|
<span class="n">intervals</span> <span class="o">=</span> <span class="p">[[</span><span class="n">nmeans</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">],</span> <span class="n">nmeans</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">]]]</span>
|
|
<span class="k">for</span> <span class="n">qt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dist_qt</span><span class="p">:</span>
|
|
<span class="n">intl1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">point_to_interval</span><span class="p">(</span><span class="n">nmeans</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">],</span> <span class="n">qt</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">qt</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
|
|
<span class="n">tmpk</span> <span class="o">=</span> <span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
|
|
<span class="n">intl2</span> <span class="o">=</span> <span class="p">[</span><span class="n">intl1</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="p">(</span><span class="n">tmpk</span> <span class="o">*</span> <span class="n">smoothing</span><span class="p">)),</span> <span class="n">intl1</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="p">(</span><span class="n">tmpk</span> <span class="o">*</span> <span class="n">smoothing</span><span class="p">))]</span>
|
|
<span class="n">intervals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">intl2</span><span class="p">)</span>
|
|
<span class="n">dist</span><span class="o">.</span><span class="n">append_interval</span><span class="p">(</span><span class="n">intervals</span><span class="p">)</span>
|
|
|
|
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret</span></div></div>
|
|
|
|
</pre></div>
|
|
|
|
<div class="clearer"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<div id="searchbox" style="display: none" role="search">
|
|
<h3 id="searchlabel">Quick search</h3>
|
|
<div class="searchformwrapper">
|
|
<form class="search" action="../../../search.html" method="get">
|
|
<input type="text" name="q" aria-labelledby="searchlabel" />
|
|
<input type="submit" value="Go" />
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<script>$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.6 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.quantreg</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer" role="contentinfo">
|
|
© Copyright 2018, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
|
|
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
|
|
</div>
|
|
</body>
|
|
</html> |