<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>pyFTS.benchmarks.benchmarks — pyFTS 1.2.3 documentation</title> <link rel="stylesheet" href="../../../_static/bizstyle.css" type="text/css" /> <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" /> <script type="text/javascript" src="../../../_static/documentation_options.js"></script> <script type="text/javascript" src="../../../_static/jquery.js"></script> <script type="text/javascript" src="../../../_static/underscore.js"></script> <script type="text/javascript" src="../../../_static/doctools.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> <script type="text/javascript" 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 type="text/javascript" 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.2.3 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> »</li> </ul> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <p class="logo"><a href="../../../index.html"> <img class="logo" src="../../../_static/logo_heading2.png" alt="Logo"/> </a></p> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <h1>Source code for pyFTS.benchmarks.benchmarks</h1><div class="highlight"><pre> <span></span><span class="ch">#!/usr/bin/python</span> <span class="c1"># -*- coding: utf8 -*-</span> <span class="sd">"""Benchmarks methods for FTS methods"""</span> <span class="kn">import</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="nn">time</span> <span class="kn">from</span> <span class="nn">copy</span> <span class="k">import</span> <span class="n">deepcopy</span> <span class="kn">import</span> <span class="nn">traceback</span> <span class="kn">import</span> <span class="nn">matplotlib</span> <span class="k">as</span> <span class="nn">plt</span> <span class="kn">import</span> <span class="nn">matplotlib.cm</span> <span class="k">as</span> <span class="nn">cmx</span> <span class="kn">import</span> <span class="nn">matplotlib.colors</span> <span class="k">as</span> <span class="nn">pltcolors</span> <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</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">mpl_toolkits.mplot3d</span> <span class="k">import</span> <span class="n">Axes3D</span> <span class="kn">from</span> <span class="nn">pyFTS.probabilistic</span> <span class="k">import</span> <span class="n">ProbabilityDistribution</span> <span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Transformations</span> <span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">song</span><span class="p">,</span> <span class="n">chen</span><span class="p">,</span> <span class="n">yu</span><span class="p">,</span> <span class="n">ismailefendi</span><span class="p">,</span> <span class="n">sadaei</span><span class="p">,</span> <span class="n">hofts</span><span class="p">,</span> <span class="n">pwfts</span><span class="p">,</span> <span class="n">ifts</span><span class="p">,</span> <span class="n">cheng</span><span class="p">,</span> <span class="n">hwang</span> <span class="kn">from</span> <span class="nn">pyFTS.models.ensemble</span> <span class="k">import</span> <span class="n">ensemble</span> <span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span><span class="p">,</span> <span class="n">naive</span><span class="p">,</span> <span class="n">arima</span><span class="p">,</span> <span class="n">ResidualAnalysis</span><span class="p">,</span> <span class="n">quantreg</span><span class="p">,</span> <span class="n">knn</span> <span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Util</span> <span class="k">as</span> <span class="n">bUtil</span> <span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Util</span> <span class="k">as</span> <span class="n">cUtil</span> <span class="c1"># from sklearn.cross_validation import KFold</span> <span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">Grid</span> <span class="kn">from</span> <span class="nn">matplotlib</span> <span class="k">import</span> <span class="n">rc</span> <span class="c1">#rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})</span> <span class="c1">## for Palatino and other serif fonts use:</span> <span class="c1">#rc('font',**{'family':'serif','serif':['Palatino']})</span> <span class="c1">#rc('text', usetex=True)</span> <span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'grey'</span><span class="p">,</span> <span class="s1">'darkgrey'</span><span class="p">,</span> <span class="s1">'rosybrown'</span><span class="p">,</span> <span class="s1">'maroon'</span><span class="p">,</span> <span class="s1">'red'</span><span class="p">,</span><span class="s1">'orange'</span><span class="p">,</span> <span class="s1">'gold'</span><span class="p">,</span> <span class="s1">'yellow'</span><span class="p">,</span> <span class="s1">'olive'</span><span class="p">,</span> <span class="s1">'green'</span><span class="p">,</span> <span class="s1">'darkgreen'</span><span class="p">,</span> <span class="s1">'cyan'</span><span class="p">,</span> <span class="s1">'lightblue'</span><span class="p">,</span><span class="s1">'blue'</span><span class="p">,</span> <span class="s1">'darkblue'</span><span class="p">,</span> <span class="s1">'purple'</span><span class="p">,</span> <span class="s1">'darkviolet'</span> <span class="p">]</span> <span class="n">ncol</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">colors</span><span class="p">)</span> <span class="n">styles</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'-'</span><span class="p">,</span><span class="s1">'--'</span><span class="p">,</span><span class="s1">'-.'</span><span class="p">,</span><span class="s1">':'</span><span class="p">,</span><span class="s1">'.'</span><span class="p">]</span> <span class="n">nsty</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">styles</span><span class="p">)</span> <span class="k">def</span> <span class="nf">__pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">):</span> <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span> <span class="k">return</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">default</span> <div class="viewcode-block" id="get_benchmark_point_methods"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.get_benchmark_point_methods">[docs]</a><span class="k">def</span> <span class="nf">get_benchmark_point_methods</span><span class="p">():</span> <span class="sd">"""Return all non FTS methods for point forecasting"""</span> <span class="k">return</span> <span class="p">[</span><span class="n">naive</span><span class="o">.</span><span class="n">Naive</span><span class="p">,</span> <span class="n">arima</span><span class="o">.</span><span class="n">ARIMA</span><span class="p">,</span> <span class="n">quantreg</span><span class="o">.</span><span class="n">QuantileRegression</span><span class="p">]</span></div> <div class="viewcode-block" id="get_point_methods"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.get_point_methods">[docs]</a><span class="k">def</span> <span class="nf">get_point_methods</span><span class="p">():</span> <span class="sd">"""Return all FTS methods for point forecasting"""</span> <span class="k">return</span> <span class="p">[</span><span class="n">song</span><span class="o">.</span><span class="n">ConventionalFTS</span><span class="p">,</span> <span class="n">chen</span><span class="o">.</span><span class="n">ConventionalFTS</span><span class="p">,</span> <span class="n">yu</span><span class="o">.</span><span class="n">WeightedFTS</span><span class="p">,</span> <span class="n">ismailefendi</span><span class="o">.</span><span class="n">ImprovedWeightedFTS</span><span class="p">,</span> <span class="n">cheng</span><span class="o">.</span><span class="n">TrendWeightedFTS</span><span class="p">,</span> <span class="n">sadaei</span><span class="o">.</span><span class="n">ExponentialyWeightedFTS</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="n">hwang</span><span class="o">.</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">]</span></div> <div class="viewcode-block" id="get_benchmark_interval_methods"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.get_benchmark_interval_methods">[docs]</a><span class="k">def</span> <span class="nf">get_benchmark_interval_methods</span><span class="p">():</span> <span class="sd">"""Return all non FTS methods for point_to_interval forecasting"""</span> <span class="k">return</span> <span class="p">[</span> <span class="n">arima</span><span class="o">.</span><span class="n">ARIMA</span><span class="p">,</span> <span class="n">quantreg</span><span class="o">.</span><span class="n">QuantileRegression</span><span class="p">]</span></div> <div class="viewcode-block" id="get_interval_methods"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.get_interval_methods">[docs]</a><span class="k">def</span> <span class="nf">get_interval_methods</span><span class="p">():</span> <span class="sd">"""Return all FTS methods for point_to_interval forecasting"""</span> <span class="k">return</span> <span class="p">[</span><span class="n">ifts</span><span class="o">.</span><span class="n">IntervalFTS</span><span class="p">,</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">]</span></div> <div class="viewcode-block" id="get_probabilistic_methods"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.get_probabilistic_methods">[docs]</a><span class="k">def</span> <span class="nf">get_probabilistic_methods</span><span class="p">():</span> <span class="sd">"""Return all FTS methods for probabilistic forecasting"""</span> <span class="k">return</span> <span class="p">[</span><span class="n">ensemble</span><span class="o">.</span><span class="n">AllMethodEnsembleFTS</span><span class="p">,</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">]</span></div> <div class="viewcode-block" id="get_benchmark_probabilistic_methods"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.get_benchmark_probabilistic_methods">[docs]</a><span class="k">def</span> <span class="nf">get_benchmark_probabilistic_methods</span><span class="p">():</span> <span class="sd">"""Return all FTS methods for probabilistic forecasting"""</span> <span class="k">return</span> <span class="p">[</span><span class="n">arima</span><span class="o">.</span><span class="n">ARIMA</span><span class="p">,</span> <span class="n">quantreg</span><span class="o">.</span><span class="n">QuantileRegression</span><span class="p">,</span> <span class="n">knn</span><span class="o">.</span><span class="n">KNearestNeighbors</span><span class="p">]</span></div> <div class="viewcode-block" id="sliding_window_benchmarks"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.sliding_window_benchmarks">[docs]</a><span class="k">def</span> <span class="nf">sliding_window_benchmarks</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">windowsize</span><span class="p">,</span> <span class="n">train</span><span class="o">=</span><span class="mf">0.8</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"> Sliding window benchmarks for FTS forecasters.</span> <span class="sd"> For each data window, a train and test datasets will be splitted. For each train split, number of</span> <span class="sd"> partitions and partitioning method will be created a partitioner model. And for each partitioner, order,</span> <span class="sd"> steps ahead and FTS method a foreasting model will be trained.</span> <span class="sd"> Then all trained models are benchmarked on the test data and the metrics are stored on a sqlite3 database</span> <span class="sd"> (identified by the 'file' parameter) for posterior analysis.</span> <span class="sd"> All these process can be distributed on a dispy cluster, setting the atributed 'distributed' to true and</span> <span class="sd"> informing the list of dispy nodes on 'nodes' parameter.</span> <span class="sd"> The number of experiments is determined by 'windowsize' and 'inc' parameters.</span> <span class="sd"> :param data: test data</span> <span class="sd"> :param windowsize: size of sliding window</span> <span class="sd"> :param train: percentual of sliding window data used to train the models</span> <span class="sd"> :param kwargs: dict, optional arguments</span> <span class="sd"> :keyword benchmark_methods: a list with Non FTS models to benchmark. The default is None.</span> <span class="sd"> :keyword benchmark_methods_parameters: a list with Non FTS models parameters. The default is None.</span> <span class="sd"> :keyword benchmark_models: A boolean value indicating if external FTS methods will be used on benchmark. The default is False.</span> <span class="sd"> :keyword build_methods: A boolean value indicating if the default FTS methods will be used on benchmark. The default is True.</span> <span class="sd"> :keyword dataset: the dataset name to identify the current set of benchmarks results on database.</span> <span class="sd"> :keyword distributed: A boolean value indicating if the forecasting procedure will be distributed in a dispy cluster. . The default is False</span> <span class="sd"> :keyword file: file path to save the results. The default is benchmarks.db.</span> <span class="sd"> :keyword inc: a float on interval [0,1] indicating the percentage of the windowsize to move the window</span> <span class="sd"> :keyword methods: a list with FTS class names. The default depends on the forecasting type and contains the list of all FTS methods.</span> <span class="sd"> :keyword models: a list with prebuilt FTS objects. The default is None.</span> <span class="sd"> :keyword nodes: a list with the dispy cluster nodes addresses. The default is [127.0.0.1].</span> <span class="sd"> :keyword orders: a list with orders of the models (for high order models). The default is [1,2,3].</span> <span class="sd"> :keyword partitions: a list with the numbers of partitions on the Universe of Discourse. The default is [10].</span> <span class="sd"> :keyword partitioners_models: a list with prebuilt Universe of Discourse partitioners objects. The default is None.</span> <span class="sd"> :keyword partitioners_methods: a list with Universe of Discourse partitioners class names. The default is [partitioners.Grid.GridPartitioner].</span> <span class="sd"> :keyword progress: If true a progress bar will be displayed during the benchmarks. The default is False.</span> <span class="sd"> :keyword start: in the multi step forecasting, the index of the data where to start forecasting. The default is 0.</span> <span class="sd"> :keyword steps_ahead: a list with the forecasting horizons, i. e., the number of steps ahead to forecast. The default is 1.</span> <span class="sd"> :keyword tag: a name to identify the current set of benchmarks results on database.</span> <span class="sd"> :keyword type: the forecasting type, one of these values: point(default), interval or distribution. The default is point.</span> <span class="sd"> :keyword transformations: a list with data transformations do apply . The default is [None].</span> <span class="sd"> """</span> <span class="n">tag</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s1">'tag'</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">dataset</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s1">'dataset'</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">distributed</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s1">'distributed'</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">transformations</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">'transformations'</span><span class="p">,</span> <span class="p">[</span><span class="kc">None</span><span class="p">])</span> <span class="n">progress</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">'progress'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="nb">type</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">"type"</span><span class="p">,</span> <span class="s1">'point'</span><span class="p">)</span> <span class="n">orders</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"orders"</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">partitioners_models</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"partitioners_models"</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">partitioners_methods</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"partitioners_methods"</span><span class="p">,</span> <span class="p">[</span><span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">partitions</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"partitions"</span><span class="p">,</span> <span class="p">[</span><span class="mi">10</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">steps_ahead</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s1">'steps_ahead'</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">methods</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s1">'methods'</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">models</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s1">'models'</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">pool</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">models</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">models</span> <span class="k">if</span> <span class="n">methods</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'point'</span><span class="p">:</span> <span class="n">methods</span> <span class="o">=</span> <span class="n">get_point_methods</span><span class="p">()</span> <span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'interval'</span><span class="p">:</span> <span class="n">methods</span> <span class="o">=</span> <span class="n">get_interval_methods</span><span class="p">()</span> <span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'distribution'</span><span class="p">:</span> <span class="n">methods</span> <span class="o">=</span> <span class="n">get_probabilistic_methods</span><span class="p">()</span> <span class="n">build_methods</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"build_methods"</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="k">if</span> <span class="n">build_methods</span><span class="p">:</span> <span class="k">for</span> <span class="n">method</span> <span class="ow">in</span> <span class="n">methods</span><span class="p">:</span> <span class="n">mfts</span> <span class="o">=</span> <span class="n">method</span><span class="p">()</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">is_high_order</span><span class="p">:</span> <span class="k">for</span> <span class="n">order</span> <span class="ow">in</span> <span class="n">orders</span><span class="p">:</span> <span class="k">if</span> <span class="n">order</span> <span class="o">>=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">min_order</span><span class="p">:</span> <span class="n">mfts</span> <span class="o">=</span> <span class="n">method</span><span class="p">()</span> <span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">order</span> <span class="n">pool</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mfts</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="mi">1</span> <span class="n">pool</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mfts</span><span class="p">)</span> <span class="n">benchmark_models</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"benchmark_models"</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="k">if</span> <span class="n">benchmark_models</span> <span class="o">!=</span> <span class="kc">False</span><span class="p">:</span> <span class="n">benchmark_methods</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"benchmark_methods"</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">benchmark_methods_parameters</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"benchmark_methods_parameters"</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="n">benchmark_pool</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="p">(</span> <span class="n">benchmark_models</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">benchmark_models</span><span class="p">,</span> <span class="nb">list</span><span class="p">))</span> \ <span class="k">else</span> <span class="n">benchmark_models</span> <span class="k">if</span> <span class="n">benchmark_models</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">benchmark_methods</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'point'</span><span class="ow">or</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'partition'</span><span class="p">:</span> <span class="n">benchmark_methods</span> <span class="o">=</span> <span class="n">get_benchmark_point_methods</span><span class="p">()</span> <span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'interval'</span><span class="p">:</span> <span class="n">benchmark_methods</span> <span class="o">=</span> <span class="n">get_benchmark_interval_methods</span><span class="p">()</span> <span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'distribution'</span><span class="p">:</span> <span class="n">benchmark_methods</span> <span class="o">=</span> <span class="n">get_benchmark_probabilistic_methods</span><span class="p">()</span> <span class="k">if</span> <span class="n">benchmark_methods</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="k">for</span> <span class="n">transformation</span> <span class="ow">in</span> <span class="n">transformations</span><span class="p">:</span> <span class="k">for</span> <span class="n">count</span><span class="p">,</span> <span class="n">model</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">benchmark_methods</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="n">par</span> <span class="o">=</span> <span class="n">benchmark_methods_parameters</span><span class="p">[</span><span class="n">count</span><span class="p">]</span> <span class="n">mfts</span> <span class="o">=</span> <span class="n">model</span><span class="p">(</span><span class="o">**</span><span class="n">par</span><span class="p">)</span> <span class="n">mfts</span><span class="o">.</span><span class="n">append_transformation</span><span class="p">(</span><span class="n">transformation</span><span class="p">)</span> <span class="n">benchmark_pool</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mfts</span><span class="p">)</span> <span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'point'</span><span class="p">:</span> <span class="n">experiment_method</span> <span class="o">=</span> <span class="n">run_point</span> <span class="n">synthesis_method</span> <span class="o">=</span> <span class="n">process_point_jobs</span> <span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'interval'</span><span class="p">:</span> <span class="n">experiment_method</span> <span class="o">=</span> <span class="n">run_interval</span> <span class="n">synthesis_method</span> <span class="o">=</span> <span class="n">process_interval_jobs</span> <span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">'distribution'</span><span class="p">:</span> <span class="n">experiment_method</span> <span class="o">=</span> <span class="n">run_probabilistic</span> <span class="n">synthesis_method</span> <span class="o">=</span> <span class="n">process_probabilistic_jobs</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="s2">"Type parameter has a unkown value!"</span><span class="p">)</span> <span class="k">if</span> <span class="n">distributed</span><span class="p">:</span> <span class="kn">import</span> <span class="nn">dispy</span><span class="o">,</span> <span class="nn">dispy.httpd</span> <span class="n">nodes</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">"nodes"</span><span class="p">,</span> <span class="p">[</span><span class="s1">'127.0.0.1'</span><span class="p">])</span> <span class="n">cluster</span><span class="p">,</span> <span class="n">http_server</span> <span class="o">=</span> <span class="n">cUtil</span><span class="o">.</span><span class="n">start_dispy_cluster</span><span class="p">(</span><span class="n">experiment_method</span><span class="p">,</span> <span class="n">nodes</span><span class="p">)</span> <span class="n">jobs</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">inc</span> <span class="o">=</span> <span class="n">__pop</span><span class="p">(</span><span class="s2">"inc"</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span> <span class="k">if</span> <span class="n">progress</span><span class="p">:</span> <span class="kn">from</span> <span class="nn">tqdm</span> <span class="k">import</span> <span class="n">tqdm</span> <span class="n">_tdata</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">windowsize</span> <span class="o">*</span> <span class="n">inc</span><span class="p">)</span> <span class="n">_tasks</span> <span class="o">=</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">partitioners_models</span><span class="p">)</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">orders</span><span class="p">)</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">partitions</span><span class="p">)</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">transformations</span><span class="p">)</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">steps_ahead</span><span class="p">))</span> <span class="n">_tbcmk</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">benchmark_pool</span><span class="p">)</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">steps_ahead</span><span class="p">)</span> <span class="n">progressbar</span> <span class="o">=</span> <span class="n">tqdm</span><span class="p">(</span><span class="n">total</span><span class="o">=</span><span class="n">_tdata</span><span class="o">*</span><span class="n">_tasks</span> <span class="o">+</span> <span class="n">_tdata</span><span class="o">*</span><span class="n">_tbcmk</span><span class="p">,</span> <span class="n">desc</span><span class="o">=</span><span class="s2">"Benchmarks:"</span><span class="p">)</span> <span class="n">file</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">'file'</span><span class="p">,</span> <span class="s2">"benchmarks.db"</span><span class="p">)</span> <span class="n">conn</span> <span class="o">=</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">open_benchmark_db</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">test</span> <span class="ow">in</span> <span class="n">cUtil</span><span class="o">.</span><span class="n">sliding_window</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">windowsize</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">inc</span><span class="o">=</span><span class="n">inc</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="n">benchmark_models</span> <span class="o">!=</span> <span class="kc">False</span><span class="p">:</span> <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">benchmark_pool</span><span class="p">:</span> <span class="k">for</span> <span class="n">step</span> <span class="ow">in</span> <span class="n">steps_ahead</span><span class="p">:</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'steps_ahead'</span><span class="p">]</span> <span class="o">=</span> <span class="n">step</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">distributed</span><span class="p">:</span> <span class="k">if</span> <span class="n">progress</span><span class="p">:</span> <span class="n">progressbar</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="k">try</span><span class="p">:</span> <span class="n">job</span> <span class="o">=</span> <span class="n">experiment_method</span><span class="p">(</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">model</span><span class="p">),</span> <span class="kc">None</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">synthesis_method</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">job</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s1">'EXCEPTION! '</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">shortname</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">)</span> <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">()</span> <span class="k">else</span><span class="p">:</span> <span class="n">job</span> <span class="o">=</span> <span class="n">cluster</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">model</span><span class="p">),</span> <span class="kc">None</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">jobs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">job</span><span class="p">)</span> <span class="n">partitioners_pool</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">partitioners_models</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">for</span> <span class="n">transformation</span> <span class="ow">in</span> <span class="n">transformations</span><span class="p">:</span> <span class="k">for</span> <span class="n">partition</span> <span class="ow">in</span> <span class="n">partitions</span><span class="p">:</span> <span class="k">for</span> <span class="n">partitioner</span> <span class="ow">in</span> <span class="n">partitioners_methods</span><span class="p">:</span> <span class="n">data_train_fs</span> <span class="o">=</span> <span class="n">partitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">train</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">partition</span><span class="p">,</span> <span class="n">transformation</span><span class="o">=</span><span class="n">transformation</span><span class="p">)</span> <span class="n">partitioners_pool</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data_train_fs</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">partitioners_pool</span> <span class="o">=</span> <span class="n">partitioners_models</span> <span class="k">for</span> <span class="n">step</span> <span class="ow">in</span> <span class="n">steps_ahead</span><span class="p">:</span> <span class="k">for</span> <span class="n">partitioner</span> <span class="ow">in</span> <span class="n">partitioners_pool</span><span class="p">:</span> <span class="k">for</span> <span class="n">_id</span><span class="p">,</span> <span class="n">model</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span><span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">'steps_ahead'</span><span class="p">]</span> <span class="o">=</span> <span class="n">step</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">distributed</span><span class="p">:</span> <span class="k">if</span> <span class="n">progress</span><span class="p">:</span> <span class="n">progressbar</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="k">try</span><span class="p">:</span> <span class="n">job</span> <span class="o">=</span> <span class="n">experiment_method</span><span class="p">(</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">model</span><span class="p">),</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">partitioner</span><span class="p">),</span> <span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">synthesis_method</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">job</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s1">'EXCEPTION! '</span><span class="p">,</span><span class="n">model</span><span class="o">.</span><span class="n">shortname</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">partitioner</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">partitioner</span><span class="o">.</span><span class="n">partitions</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">transformation</span><span class="p">))</span> <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">()</span> <span class="k">else</span><span class="p">:</span> <span class="n">job</span> <span class="o">=</span> <span class="n">cluster</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">model</span><span class="p">),</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">partitioner</span><span class="p">),</span> <span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">job</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="nb">id</span> <span class="c1"># associate an ID to identify jobs (if needed later)</span> <span class="n">jobs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">job</span><span class="p">)</span> <span class="k">if</span> <span class="n">progress</span><span class="p">:</span> <span class="n">progressbar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="k">if</span> <span class="n">distributed</span><span class="p">:</span> <span class="k">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">jobs</span><span class="p">:</span> <span class="k">if</span> <span class="n">progress</span><span class="p">:</span> <span class="n">progressbar</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">job</span><span class="p">()</span> <span class="k">if</span> <span class="n">job</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="n">dispy</span><span class="o">.</span><span class="n">DispyJob</span><span class="o">.</span><span class="n">Finished</span> <span class="ow">and</span> <span class="n">job</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">result</span> <span class="n">synthesis_method</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">tmp</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"status"</span><span class="p">,</span><span class="n">job</span><span class="o">.</span><span class="n">status</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"result"</span><span class="p">,</span><span class="n">job</span><span class="o">.</span><span class="n">result</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"stdout"</span><span class="p">,</span><span class="n">job</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"stderr"</span><span class="p">,</span><span class="n">job</span><span class="o">.</span><span class="n">exception</span><span class="p">)</span> <span class="n">cluster</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="c1"># wait for all jobs to finish</span> <span class="n">cUtil</span><span class="o">.</span><span class="n">stop_dispy_cluster</span><span class="p">(</span><span class="n">cluster</span><span class="p">,</span> <span class="n">http_server</span><span class="p">)</span> <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div> <div class="viewcode-block" id="run_point"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.run_point">[docs]</a><span class="k">def</span> <span class="nf">run_point</span><span class="p">(</span><span class="n">mfts</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">train_data</span><span class="p">,</span> <span class="n">test_data</span><span class="p">,</span> <span class="n">window_key</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"> Run the point forecasting benchmarks</span> <span class="sd"> :param mfts: FTS model</span> <span class="sd"> :param partitioner: Universe of Discourse partitioner</span> <span class="sd"> :param train_data: data used to train the model</span> <span class="sd"> :param test_data: ata used to test the model</span> <span class="sd"> :param window_key: id of the sliding window</span> <span class="sd"> :param transformation: data transformation</span> <span class="sd"> :param indexer: seasonal indexer</span> <span class="sd"> :return: a dictionary with the benchmark results</span> <span class="sd"> """</span> <span class="kn">import</span> <span class="nn">time</span> <span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">yu</span><span class="p">,</span> <span class="n">chen</span><span class="p">,</span> <span class="n">hofts</span><span class="p">,</span> <span class="n">pwfts</span><span class="p">,</span><span class="n">ismailefendi</span><span class="p">,</span><span class="n">sadaei</span><span class="p">,</span> <span class="n">song</span><span class="p">,</span> <span class="n">cheng</span><span class="p">,</span> <span class="n">hwang</span> <span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">Grid</span><span class="p">,</span> <span class="n">Entropy</span><span class="p">,</span> <span class="n">FCM</span> <span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span><span class="p">,</span> <span class="n">naive</span><span class="p">,</span> <span class="n">arima</span><span class="p">,</span> <span class="n">quantreg</span> <span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Transformations</span> <span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">song</span><span class="o">.</span><span class="n">ConventionalFTS</span><span class="p">,</span> <span class="n">chen</span><span class="o">.</span><span class="n">ConventionalFTS</span><span class="p">,</span> <span class="n">yu</span><span class="o">.</span><span class="n">WeightedFTS</span><span class="p">,</span> <span class="n">ismailefendi</span><span class="o">.</span><span class="n">ImprovedWeightedFTS</span><span class="p">,</span> <span class="n">cheng</span><span class="o">.</span><span class="n">TrendWeightedFTS</span><span class="p">,</span> <span class="n">sadaei</span><span class="o">.</span><span class="n">ExponentialyWeightedFTS</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="n">hwang</span><span class="o">.</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">]</span> <span class="n">tmp2</span> <span class="o">=</span> <span class="p">[</span><span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">,</span> <span class="n">Entropy</span><span class="o">.</span><span class="n">EntropyPartitioner</span><span class="p">,</span> <span class="n">FCM</span><span class="o">.</span><span class="n">FCMPartitioner</span><span class="p">]</span> <span class="n">tmp4</span> <span class="o">=</span> <span class="p">[</span><span class="n">naive</span><span class="o">.</span><span class="n">Naive</span><span class="p">,</span> <span class="n">arima</span><span class="o">.</span><span class="n">ARIMA</span><span class="p">,</span> <span class="n">quantreg</span><span class="o">.</span><span class="n">QuantileRegression</span><span class="p">]</span> <span class="n">tmp3</span> <span class="o">=</span> <span class="p">[</span><span class="n">Measures</span><span class="o">.</span><span class="n">get_point_statistics</span><span class="p">]</span> <span class="n">tmp5</span> <span class="o">=</span> <span class="p">[</span><span class="n">Transformations</span><span class="o">.</span><span class="n">Differential</span><span class="p">]</span> <span class="n">indexer</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">'indexer'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">steps_ahead</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">'steps_ahead'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="n">method</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">'method'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">benchmark_only</span><span class="p">:</span> <span class="n">_key</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">""</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">pttr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="vm">__module__</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="n">_key</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="s2">" n = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">order</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" "</span> <span class="o">+</span> <span class="n">pttr</span> <span class="o">+</span> <span class="s2">" q = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">partitions</span><span class="p">)</span> <span class="n">mfts</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="n">partitioner</span> <span class="n">mfts</span><span class="o">.</span><span class="n">append_transformation</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">transformation</span><span class="p">)</span> <span class="n">_key</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">steps_ahead</span><span class="p">)</span> <span class="n">_key</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">method</span><span class="p">)</span> <span class="k">if</span> <span class="n">method</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">""</span> <span class="n">_start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">mfts</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">_end</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">times</span> <span class="o">=</span> <span class="n">_end</span> <span class="o">-</span> <span class="n">_start</span> <span class="n">_start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">_rmse</span><span class="p">,</span> <span class="n">_smape</span><span class="p">,</span> <span class="n">_u</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">get_point_statistics</span><span class="p">(</span><span class="n">test_data</span><span class="p">,</span> <span class="n">mfts</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">_end</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">times</span> <span class="o">+=</span> <span class="n">_end</span> <span class="o">-</span> <span class="n">_start</span> <span class="n">ret</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'key'</span><span class="p">:</span> <span class="n">_key</span><span class="p">,</span> <span class="s1">'obj'</span><span class="p">:</span> <span class="n">mfts</span><span class="p">,</span> <span class="s1">'rmse'</span><span class="p">:</span> <span class="n">_rmse</span><span class="p">,</span> <span class="s1">'smape'</span><span class="p">:</span> <span class="n">_smape</span><span class="p">,</span> <span class="s1">'u'</span><span class="p">:</span> <span class="n">_u</span><span class="p">,</span> <span class="s1">'time'</span><span class="p">:</span> <span class="n">times</span><span class="p">,</span> <span class="s1">'window'</span><span class="p">:</span> <span class="n">window_key</span><span class="p">,</span> <span class="s1">'steps'</span><span class="p">:</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="s1">'method'</span><span class="p">:</span> <span class="n">method</span><span class="p">}</span> <span class="k">return</span> <span class="n">ret</span></div> <div class="viewcode-block" id="run_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.run_interval">[docs]</a><span class="k">def</span> <span class="nf">run_interval</span><span class="p">(</span><span class="n">mfts</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">train_data</span><span class="p">,</span> <span class="n">test_data</span><span class="p">,</span> <span class="n">window_key</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"> Run the interval forecasting benchmarks</span> <span class="sd"> :param mfts: FTS model</span> <span class="sd"> :param partitioner: Universe of Discourse partitioner</span> <span class="sd"> :param train_data: data used to train the model</span> <span class="sd"> :param test_data: ata used to test the model</span> <span class="sd"> :param window_key: id of the sliding window</span> <span class="sd"> :param transformation: data transformation</span> <span class="sd"> :param indexer: seasonal indexer</span> <span class="sd"> :return: a dictionary with the benchmark results</span> <span class="sd"> """</span> <span class="kn">import</span> <span class="nn">time</span> <span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span><span class="p">,</span><span class="n">ifts</span><span class="p">,</span><span class="n">pwfts</span> <span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">Grid</span><span class="p">,</span> <span class="n">Entropy</span><span class="p">,</span> <span class="n">FCM</span> <span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span><span class="p">,</span> <span class="n">arima</span><span class="p">,</span> <span class="n">quantreg</span> <span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">hofts</span><span class="o">.</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="n">ifts</span><span class="o">.</span><span class="n">IntervalFTS</span><span class="p">,</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">]</span> <span class="n">tmp2</span> <span class="o">=</span> <span class="p">[</span><span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">,</span> <span class="n">Entropy</span><span class="o">.</span><span class="n">EntropyPartitioner</span><span class="p">,</span> <span class="n">FCM</span><span class="o">.</span><span class="n">FCMPartitioner</span><span class="p">]</span> <span class="n">tmp4</span> <span class="o">=</span> <span class="p">[</span><span class="n">arima</span><span class="o">.</span><span class="n">ARIMA</span><span class="p">,</span> <span class="n">quantreg</span><span class="o">.</span><span class="n">QuantileRegression</span><span class="p">]</span> <span class="n">tmp3</span> <span class="o">=</span> <span class="p">[</span><span class="n">Measures</span><span class="o">.</span><span class="n">get_interval_statistics</span><span class="p">]</span> <span class="n">steps_ahead</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">'steps_ahead'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="n">method</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">'method'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">benchmark_only</span><span class="p">:</span> <span class="n">_key</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">""</span><span class="p">)</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">pttr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="vm">__module__</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="n">_key</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="s2">" n = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">order</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" "</span> <span class="o">+</span> <span class="n">pttr</span> <span class="o">+</span> <span class="s2">" q = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">partitions</span><span class="p">)</span> <span class="n">mfts</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="n">partitioner</span> <span class="n">mfts</span><span class="o">.</span><span class="n">append_transformation</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">transformation</span><span class="p">)</span> <span class="n">_key</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">steps_ahead</span><span class="p">)</span> <span class="n">_key</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">method</span><span class="p">)</span> <span class="k">if</span> <span class="n">method</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">""</span> <span class="n">_start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">mfts</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">_end</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">times</span> <span class="o">=</span> <span class="n">_end</span> <span class="o">-</span> <span class="n">_start</span> <span class="n">_start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="c1">#_sharp, _res, _cov, _q05, _q25, _q75, _q95, _w05, _w25</span> <span class="n">metrics</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">get_interval_statistics</span><span class="p">(</span><span class="n">test_data</span><span class="p">,</span> <span class="n">mfts</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">_end</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">times</span> <span class="o">+=</span> <span class="n">_end</span> <span class="o">-</span> <span class="n">_start</span> <span class="n">ret</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'key'</span><span class="p">:</span> <span class="n">_key</span><span class="p">,</span> <span class="s1">'obj'</span><span class="p">:</span> <span class="n">mfts</span><span class="p">,</span> <span class="s1">'sharpness'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s1">'resolution'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s1">'coverage'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="s1">'time'</span><span class="p">:</span> <span class="n">times</span><span class="p">,</span><span class="s1">'Q05'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="s1">'Q25'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="s1">'Q75'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="s1">'Q95'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">6</span><span class="p">],</span> <span class="s1">'winkler05'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">7</span><span class="p">],</span> <span class="s1">'winkler25'</span><span class="p">:</span> <span class="n">metrics</span><span class="p">[</span><span class="mi">8</span><span class="p">],</span> <span class="s1">'window'</span><span class="p">:</span> <span class="n">window_key</span><span class="p">,</span><span class="s1">'steps'</span><span class="p">:</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="s1">'method'</span><span class="p">:</span> <span class="n">method</span><span class="p">}</span> <span class="k">return</span> <span class="n">ret</span></div> <div class="viewcode-block" id="run_probabilistic"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.run_probabilistic">[docs]</a><span class="k">def</span> <span class="nf">run_probabilistic</span><span class="p">(</span><span class="n">mfts</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">train_data</span><span class="p">,</span> <span class="n">test_data</span><span class="p">,</span> <span class="n">window_key</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"> Run the probabilistic forecasting benchmarks</span> <span class="sd"> :param mfts: FTS model</span> <span class="sd"> :param partitioner: Universe of Discourse partitioner</span> <span class="sd"> :param train_data: data used to train the model</span> <span class="sd"> :param test_data: ata used to test the model</span> <span class="sd"> :param steps:</span> <span class="sd"> :param resolution:</span> <span class="sd"> :param window_key: id of the sliding window</span> <span class="sd"> :param transformation: data transformation</span> <span class="sd"> :param indexer: seasonal indexer</span> <span class="sd"> :return: a dictionary with the benchmark results</span> <span class="sd"> """</span> <span class="kn">import</span> <span class="nn">time</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">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span><span class="p">,</span> <span class="n">ifts</span><span class="p">,</span> <span class="n">pwfts</span> <span class="kn">from</span> <span class="nn">pyFTS.models.ensemble</span> <span class="k">import</span> <span class="n">ensemble</span> <span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">Grid</span><span class="p">,</span> <span class="n">Entropy</span><span class="p">,</span> <span class="n">FCM</span> <span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span><span class="p">,</span> <span class="n">arima</span><span class="p">,</span> <span class="n">quantreg</span><span class="p">,</span> <span class="n">knn</span> <span class="kn">from</span> <span class="nn">pyFTS.models.seasonal</span> <span class="k">import</span> <span class="n">SeasonalIndexer</span> <span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">hofts</span><span class="o">.</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="n">ifts</span><span class="o">.</span><span class="n">IntervalFTS</span><span class="p">,</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">,</span> <span class="n">arima</span><span class="o">.</span><span class="n">ARIMA</span><span class="p">,</span> <span class="n">ensemble</span><span class="o">.</span><span class="n">AllMethodEnsembleFTS</span><span class="p">,</span> <span class="n">knn</span><span class="o">.</span><span class="n">KNearestNeighbors</span><span class="p">]</span> <span class="n">tmp2</span> <span class="o">=</span> <span class="p">[</span><span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">,</span> <span class="n">Entropy</span><span class="o">.</span><span class="n">EntropyPartitioner</span><span class="p">,</span> <span class="n">FCM</span><span class="o">.</span><span class="n">FCMPartitioner</span><span class="p">]</span> <span class="n">tmp3</span> <span class="o">=</span> <span class="p">[</span><span class="n">Measures</span><span class="o">.</span><span class="n">get_distribution_statistics</span><span class="p">,</span> <span class="n">SeasonalIndexer</span><span class="o">.</span><span class="n">SeasonalIndexer</span><span class="p">,</span> <span class="n">SeasonalIndexer</span><span class="o">.</span><span class="n">LinearSeasonalIndexer</span><span class="p">]</span> <span class="n">indexer</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">'indexer'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">steps_ahead</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">'steps_ahead'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="n">method</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">'method'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">benchmark_only</span><span class="p">:</span> <span class="n">_key</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">order</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">""</span><span class="p">)</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">pttr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="vm">__module__</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="n">_key</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="s2">" n = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">order</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" "</span> <span class="o">+</span> <span class="n">pttr</span> <span class="o">+</span> <span class="s2">" q = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">partitions</span><span class="p">)</span> <span class="n">mfts</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="n">partitioner</span> <span class="n">mfts</span><span class="o">.</span><span class="n">append_transformation</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">transformation</span><span class="p">)</span> <span class="n">_key</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">steps_ahead</span><span class="p">)</span> <span class="n">_key</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">method</span><span class="p">)</span> <span class="k">if</span> <span class="n">method</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">""</span> <span class="k">if</span> <span class="n">mfts</span><span class="o">.</span><span class="n">has_seasonality</span><span class="p">:</span> <span class="n">mfts</span><span class="o">.</span><span class="n">indexer</span> <span class="o">=</span> <span class="n">indexer</span> <span class="n">_start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">mfts</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">_end</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="n">times</span> <span class="o">=</span> <span class="n">_end</span> <span class="o">-</span> <span class="n">_start</span> <span class="n">_crps1</span><span class="p">,</span> <span class="n">_t1</span><span class="p">,</span> <span class="n">_brier</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">get_distribution_statistics</span><span class="p">(</span><span class="n">test_data</span><span class="p">,</span> <span class="n">mfts</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="n">_t1</span> <span class="o">+=</span> <span class="n">times</span> <span class="n">ret</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'key'</span><span class="p">:</span> <span class="n">_key</span><span class="p">,</span> <span class="s1">'obj'</span><span class="p">:</span> <span class="n">mfts</span><span class="p">,</span> <span class="s1">'CRPS'</span><span class="p">:</span> <span class="n">_crps1</span><span class="p">,</span> <span class="s1">'time'</span><span class="p">:</span> <span class="n">_t1</span><span class="p">,</span> <span class="s1">'brier'</span><span class="p">:</span> <span class="n">_brier</span><span class="p">,</span> <span class="s1">'window'</span><span class="p">:</span> <span class="n">window_key</span><span class="p">,</span> <span class="s1">'steps'</span><span class="p">:</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="s1">'method'</span><span class="p">:</span> <span class="n">method</span><span class="p">}</span> <span class="k">return</span> <span class="n">ret</span></div> <div class="viewcode-block" id="process_point_jobs"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.process_point_jobs">[docs]</a><span class="k">def</span> <span class="nf">process_point_jobs</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">job</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Extract information from a dictionary with point benchmark results and save it on a database</span> <span class="sd"> :param dataset: the benchmark dataset name</span> <span class="sd"> :param tag: alias for the benchmark group being executed</span> <span class="sd"> :param job: a dictionary with the benchmark results</span> <span class="sd"> :param conn: a connection to a Sqlite database</span> <span class="sd"> :return:</span> <span class="sd"> """</span> <span class="n">data</span> <span class="o">=</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">process_common_data</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="s1">'point'</span><span class="p">,</span><span class="n">job</span><span class="p">)</span> <span class="n">rmse</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">rmse</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"rmse"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"rmse"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">rmse</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">smape</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">smape</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"smape"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"smape"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">smape</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">u</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">u</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"u"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"u"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">time</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">time</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"time"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"time"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span></div> <div class="viewcode-block" id="process_interval_jobs"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.process_interval_jobs">[docs]</a><span class="k">def</span> <span class="nf">process_interval_jobs</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">job</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Extract information from an dictionary with interval benchmark results and save it on a database</span> <span class="sd"> :param dataset: the benchmark dataset name</span> <span class="sd"> :param tag: alias for the benchmark group being executed</span> <span class="sd"> :param job: a dictionary with the benchmark results</span> <span class="sd"> :param conn: a connection to a Sqlite database</span> <span class="sd"> :return:</span> <span class="sd"> """</span> <span class="n">data</span> <span class="o">=</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">process_common_data</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="s1">'interval'</span><span class="p">,</span> <span class="n">job</span><span class="p">)</span> <span class="n">sharpness</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">sharpness</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"sharpness"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"sharpness"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">sharpness</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">resolution</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">resolution</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"resolution"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"resolution"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">resolution</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">coverage</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">coverage</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"coverage"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"coverage"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">coverage</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">time</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">time</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"time"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"time"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">Q05</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">Q05</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"Q05"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"Q05"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">Q05</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">Q25</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">Q25</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"Q25"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"Q25"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">Q25</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">Q75</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">Q75</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"Q75"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"Q75"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">Q75</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">Q95</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">Q95</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"Q95"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"Q95"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">Q95</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">W05</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">W05</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"winkler05"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"winkler05"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">W05</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">W25</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">W25</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"winkler25"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"winkler25"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">W25</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span></div> <div class="viewcode-block" id="process_probabilistic_jobs"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.process_probabilistic_jobs">[docs]</a><span class="k">def</span> <span class="nf">process_probabilistic_jobs</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">job</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Extract information from an dictionary with probabilistic benchmark results and save it on a database</span> <span class="sd"> :param dataset: the benchmark dataset name</span> <span class="sd"> :param tag: alias for the benchmark group being executed</span> <span class="sd"> :param job: a dictionary with the benchmark results</span> <span class="sd"> :param conn: a connection to a Sqlite database</span> <span class="sd"> :return:</span> <span class="sd"> """</span> <span class="n">data</span> <span class="o">=</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">process_common_data</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="s1">'density'</span><span class="p">,</span> <span class="n">job</span><span class="p">)</span> <span class="n">crps</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">crps</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"crps"</span><span class="p">,</span><span class="n">job</span><span class="p">[</span><span class="s2">"CRPS"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">crps</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">time</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">time</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"time"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"time"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span> <span class="n">brier</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">brier</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s2">"brier"</span><span class="p">,</span> <span class="n">job</span><span class="p">[</span><span class="s2">"brier"</span><span class="p">]])</span> <span class="n">bUtil</span><span class="o">.</span><span class="n">insert_benchmark</span><span class="p">(</span><span class="n">brier</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span></div> <div class="viewcode-block" id="print_point_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.print_point_statistics">[docs]</a><span class="k">def</span> <span class="nf">print_point_statistics</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">models</span><span class="p">,</span> <span class="n">externalmodels</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">externalforecasts</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">indexers</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Run point benchmarks on given models and data and print the results</span> <span class="sd"> :param data: test data</span> <span class="sd"> :param models: a list of FTS models to benchmark</span> <span class="sd"> :param externalmodels: a list with benchmark models (façades for other methods)</span> <span class="sd"> :param externalforecasts:</span> <span class="sd"> :param indexers:</span> <span class="sd"> :return:</span> <span class="sd"> """</span> <span class="n">ret</span> <span class="o">=</span> <span class="s2">"Model & Order & RMSE & SMAPE & Theil's U </span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\n</span><span class="s2">"</span> <span class="k">for</span> <span class="n">count</span><span class="p">,</span><span class="n">model</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">models</span><span class="p">,</span><span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="n">_rmse</span><span class="p">,</span> <span class="n">_smape</span><span class="p">,</span> <span class="n">_u</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">get_point_statistics</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">indexers</span><span class="p">)</span> <span class="n">ret</span> <span class="o">+=</span> <span class="n">model</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_rmse</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_smape</span><span class="p">)</span><span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_u</span><span class="p">)</span> <span class="c1">#ret += str(round(Measures.TheilsInequality(np.array(data[fts.order:]), np.array(forecasts[:-1])), 4))</span> <span class="n">ret</span> <span class="o">+=</span> <span class="s2">" </span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\n</span><span class="s2">"</span> <span class="k">if</span> <span class="n">externalmodels</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</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">externalmodels</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="n">l</span><span class="p">):</span> <span class="n">ret</span> <span class="o">+=</span> <span class="n">externalmodels</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="s2">" 1 & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">externalforecasts</span><span class="p">[</span><span class="n">k</span><span class="p">][:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="mi">2</span><span class="p">))</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">Measures</span><span class="o">.</span><span class="n">smape</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">externalforecasts</span><span class="p">[</span><span class="n">k</span><span class="p">][:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="mi">2</span><span class="p">))</span><span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">Measures</span><span class="o">.</span><span class="n">UStatistic</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">externalforecasts</span><span class="p">[</span><span class="n">k</span><span class="p">][:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="mi">2</span><span class="p">))</span> <span class="n">ret</span> <span class="o">+=</span> <span class="s2">" </span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\n</span><span class="s2">"</span> <span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span></div> <div class="viewcode-block" id="print_interval_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.print_interval_statistics">[docs]</a><span class="k">def</span> <span class="nf">print_interval_statistics</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">models</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Run interval benchmarks on given models and data and print the results</span> <span class="sd"> :param data: test data</span> <span class="sd"> :param models: a list of FTS models to benchmark</span> <span class="sd"> :return:</span> <span class="sd"> """</span> <span class="n">ret</span> <span class="o">=</span> <span class="s2">"Model & Order & Sharpness & Resolution & Coverage & .05 & .25 & .75 & .95 </span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\n</span><span class="s2">"</span> <span class="k">for</span> <span class="n">fts</span> <span class="ow">in</span> <span class="n">models</span><span class="p">:</span> <span class="n">_sharp</span><span class="p">,</span> <span class="n">_res</span><span class="p">,</span> <span class="n">_cov</span><span class="p">,</span> <span class="n">_q5</span><span class="p">,</span> <span class="n">_q25</span><span class="p">,</span> <span class="n">_q75</span><span class="p">,</span> <span class="n">_q95</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">get_interval_statistics</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">fts</span><span class="p">)</span> <span class="n">ret</span> <span class="o">+=</span> <span class="n">fts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_sharp</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_res</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_cov</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" &"</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_q5</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" &"</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_q25</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" &"</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_q75</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" &"</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_q95</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"</span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\n</span><span class="s2">"</span> <span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span></div> <div class="viewcode-block" id="print_distribution_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.print_distribution_statistics">[docs]</a><span class="k">def</span> <span class="nf">print_distribution_statistics</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">models</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="n">resolution</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Run probabilistic benchmarks on given models and data and print the results</span> <span class="sd"> :param data: test data</span> <span class="sd"> :param models: a list of FTS models to benchmark</span> <span class="sd"> :return:</span> <span class="sd"> """</span> <span class="n">ret</span> <span class="o">=</span> <span class="s2">"Model & Order & Interval & Distribution </span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\n</span><span class="s2">"</span> <span class="k">for</span> <span class="n">fts</span> <span class="ow">in</span> <span class="n">models</span><span class="p">:</span> <span class="n">_crps1</span><span class="p">,</span> <span class="n">_crps2</span><span class="p">,</span> <span class="n">_t1</span><span class="p">,</span> <span class="n">_t2</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">get_distribution_statistics</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="n">resolution</span><span class="p">)</span> <span class="n">ret</span> <span class="o">+=</span> <span class="n">fts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_crps1</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">ret</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">_crps2</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" </span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\n</span><span class="s2">"</span> <span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span></div> <div class="viewcode-block" id="plot_compared_intervals_ahead"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plot_compared_intervals_ahead">[docs]</a><span class="k">def</span> <span class="nf">plot_compared_intervals_ahead</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">models</span><span class="p">,</span> <span class="n">colors</span><span class="p">,</span> <span class="n">distributions</span><span class="p">,</span> <span class="n">time_from</span><span class="p">,</span> <span class="n">time_to</span><span class="p">,</span> <span class="n">intervals</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> <span class="n">save</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tam</span><span class="o">=</span><span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="n">resolution</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="s1">'Blues'</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">1.5</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Plot the forecasts of several one step ahead models, by point or by interval</span> <span class="sd"> :param original: Original time series data (list)</span> <span class="sd"> :param models: List of models to compare</span> <span class="sd"> :param colors: List of models colors</span> <span class="sd"> :param distributions: True to plot a distribution</span> <span class="sd"> :param time_from: index of data poit to start the ahead forecasting</span> <span class="sd"> :param time_to: number of steps ahead to forecast</span> <span class="sd"> :param interpol: Fill space between distribution plots</span> <span class="sd"> :param save: Save the picture on file</span> <span class="sd"> :param file: Filename to save the picture</span> <span class="sd"> :param tam: Size of the picture</span> <span class="sd"> :param resolution: </span> <span class="sd"> :param cmap: Color map to be used on distribution plot </span> <span class="sd"> :param option: Distribution type to be passed for models</span> <span class="sd"> :return: </span> <span class="sd"> """</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="n">tam</span><span class="p">)</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span> <span class="n">cm</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">get_cmap</span><span class="p">(</span><span class="n">cmap</span><span class="p">)</span> <span class="n">cNorm</span> <span class="o">=</span> <span class="n">pltcolors</span><span class="o">.</span><span class="n">Normalize</span><span class="p">(</span><span class="n">vmin</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">vmax</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="n">scalarMap</span> <span class="o">=</span> <span class="n">cmx</span><span class="o">.</span><span class="n">ScalarMappable</span><span class="p">(</span><span class="n">norm</span><span class="o">=</span><span class="n">cNorm</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="n">cm</span><span class="p">)</span> <span class="k">if</span> <span class="n">resolution</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="n">resolution</span> <span class="o">=</span> <span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">original</span><span class="p">)</span> <span class="o">-</span> <span class="nb">min</span><span class="p">(</span><span class="n">original</span><span class="p">))</span> <span class="o">/</span> <span class="mi">100</span> <span class="n">mi</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">ma</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">count</span><span class="p">,</span> <span class="n">fts</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">models</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="k">if</span> <span class="n">fts</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="ow">and</span> <span class="n">distributions</span><span class="p">[</span><span class="n">count</span><span class="p">]:</span> <span class="n">density</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast_ahead_distribution</span><span class="p">(</span><span class="n">original</span><span class="p">[</span><span class="n">time_from</span> <span class="o">-</span> <span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">:</span><span class="n">time_from</span><span class="p">],</span> <span class="n">time_to</span><span class="p">,</span> <span class="n">resolution</span><span class="o">=</span><span class="n">resolution</span><span class="p">)</span> <span class="c1">#plot_density_scatter(ax, cmap, density, fig, resolution, time_from, time_to)</span> <span class="n">plot_density_rectange</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">cm</span><span class="p">,</span> <span class="n">density</span><span class="p">,</span> <span class="n">fig</span><span class="p">,</span> <span class="n">resolution</span><span class="p">,</span> <span class="n">time_from</span><span class="p">,</span> <span class="n">time_to</span><span class="p">)</span> <span class="k">if</span> <span class="n">fts</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="ow">and</span> <span class="n">intervals</span><span class="p">:</span> <span class="n">forecasts</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast_ahead_interval</span><span class="p">(</span><span class="n">original</span><span class="p">[</span><span class="n">time_from</span> <span class="o">-</span> <span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">:</span><span class="n">time_from</span><span class="p">],</span> <span class="n">time_to</span><span class="p">)</span> <span class="n">lower</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">]</span> <span class="n">upper</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">]</span> <span class="n">mi</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">lower</span><span class="p">))</span> <span class="n">ma</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">upper</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="n">time_from</span> <span class="o">-</span> <span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">):</span> <span class="n">lower</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">upper</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">lower</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">count</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</span><span class="o">.</span><span class="n">shortname</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">upper</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">count</span><span class="p">],</span> <span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="o">*</span><span class="mf">1.5</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'black'</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original"</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="o">*</span><span class="mf">1.5</span><span class="p">)</span> <span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="k">if</span> <span class="kc">True</span> <span class="ow">in</span> <span class="n">distributions</span><span class="p">:</span> <span class="n">lgd</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">lgd</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">bbox_to_anchor</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="n">_mi</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">mi</span><span class="p">)</span> <span class="k">if</span> <span class="n">_mi</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span> <span class="n">_mi</span> <span class="o">*=</span> <span class="mf">1.1</span> <span class="k">else</span><span class="p">:</span> <span class="n">_mi</span> <span class="o">*=</span> <span class="mf">0.9</span> <span class="n">_ma</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ma</span><span class="p">)</span> <span class="k">if</span> <span class="n">_ma</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span> <span class="n">_ma</span> <span class="o">*=</span> <span class="mf">0.9</span> <span class="k">else</span><span class="p">:</span> <span class="n">_ma</span> <span class="o">*=</span> <span class="mf">1.1</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="n">_mi</span><span class="p">,</span> <span class="n">_ma</span><span class="p">])</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'F(T)'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'T'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">original</span><span class="p">)])</span> <span class="n">cUtil</span><span class="o">.</span><span class="n">show_and_save_image</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">file</span><span class="p">,</span> <span class="n">save</span><span class="p">,</span> <span class="n">lgd</span><span class="o">=</span><span class="n">lgd</span><span class="p">)</span></div> <div class="viewcode-block" id="plot_density_rectange"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plot_density_rectange">[docs]</a><span class="k">def</span> <span class="nf">plot_density_rectange</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">cmap</span><span class="p">,</span> <span class="n">density</span><span class="p">,</span> <span class="n">fig</span><span class="p">,</span> <span class="n">resolution</span><span class="p">,</span> <span class="n">time_from</span><span class="p">,</span> <span class="n">time_to</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Auxiliar function to plot_compared_intervals_ahead</span> <span class="sd"> """</span> <span class="kn">from</span> <span class="nn">matplotlib.patches</span> <span class="k">import</span> <span class="n">Rectangle</span> <span class="kn">from</span> <span class="nn">matplotlib.collections</span> <span class="k">import</span> <span class="n">PatchCollection</span> <span class="n">patches</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">colors</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">density</span><span class="o">.</span><span class="n">index</span><span class="p">:</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">density</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span> <span class="n">s</span> <span class="o">=</span> <span class="n">Rectangle</span><span class="p">((</span><span class="n">time_from</span> <span class="o">+</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">resolution</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">lw</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="n">patches</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="n">colors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">density</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="p">]</span><span class="o">*</span><span class="mi">5</span><span class="p">)</span> <span class="n">pc</span> <span class="o">=</span> <span class="n">PatchCollection</span><span class="p">(</span><span class="n">patches</span><span class="o">=</span><span class="n">patches</span><span class="p">,</span> <span class="n">match_original</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="n">pc</span><span class="o">.</span><span class="n">set_clim</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="n">pc</span><span class="o">.</span><span class="n">set_cmap</span><span class="p">(</span><span class="n">cmap</span><span class="p">)</span> <span class="n">pc</span><span class="o">.</span><span class="n">set_array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">colors</span><span class="p">))</span> <span class="n">ax</span><span class="o">.</span><span class="n">add_collection</span><span class="p">(</span><span class="n">pc</span><span class="p">)</span> <span class="n">cb</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">colorbar</span><span class="p">(</span><span class="n">pc</span><span class="p">,</span> <span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">)</span> <span class="n">cb</span><span class="o">.</span><span class="n">set_label</span><span class="p">(</span><span class="s1">'Density'</span><span class="p">)</span></div> <div class="viewcode-block" id="plot_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plot_distribution">[docs]</a><span class="k">def</span> <span class="nf">plot_distribution</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">cmap</span><span class="p">,</span> <span class="n">probabilitydist</span><span class="p">,</span> <span class="n">fig</span><span class="p">,</span> <span class="n">time_from</span><span class="p">,</span> <span class="n">reference_data</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="kn">from</span> <span class="nn">matplotlib.patches</span> <span class="k">import</span> <span class="n">Rectangle</span> <span class="kn">from</span> <span class="nn">matplotlib.collections</span> <span class="k">import</span> <span class="n">PatchCollection</span> <span class="n">patches</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">colors</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">dt</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">probabilitydist</span><span class="p">):</span> <span class="n">disp</span> <span class="o">=</span> <span class="mf">0.0</span> <span class="k">if</span> <span class="n">reference_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="n">disp</span> <span class="o">=</span> <span class="n">reference_data</span><span class="p">[</span><span class="n">time_from</span><span class="o">+</span><span class="n">ct</span><span class="p">]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">dt</span><span class="o">.</span><span class="n">bins</span><span class="p">:</span> <span class="n">s</span> <span class="o">=</span> <span class="n">Rectangle</span><span class="p">((</span><span class="n">time_from</span><span class="o">+</span><span class="n">ct</span><span class="p">,</span> <span class="n">y</span><span class="o">+</span><span class="n">disp</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">resolution</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">lw</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="n">patches</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="n">colors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">y</span><span class="p">))</span> <span class="n">scale</span> <span class="o">=</span> <span class="n">Transformations</span><span class="o">.</span><span class="n">Scale</span><span class="p">()</span> <span class="n">colors</span> <span class="o">=</span> <span class="n">scale</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">colors</span><span class="p">)</span> <span class="n">pc</span> <span class="o">=</span> <span class="n">PatchCollection</span><span class="p">(</span><span class="n">patches</span><span class="o">=</span><span class="n">patches</span><span class="p">,</span> <span class="n">match_original</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="n">pc</span><span class="o">.</span><span class="n">set_clim</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="n">pc</span><span class="o">.</span><span class="n">set_cmap</span><span class="p">(</span><span class="n">cmap</span><span class="p">)</span> <span class="n">pc</span><span class="o">.</span><span class="n">set_array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">colors</span><span class="p">))</span> <span class="n">ax</span><span class="o">.</span><span class="n">add_collection</span><span class="p">(</span><span class="n">pc</span><span class="p">)</span> <span class="n">cb</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">colorbar</span><span class="p">(</span><span class="n">pc</span><span class="p">,</span> <span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">)</span> <span class="n">cb</span><span class="o">.</span><span class="n">set_label</span><span class="p">(</span><span class="s1">'Density'</span><span class="p">)</span></div> <div class="viewcode-block" id="plot_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plot_interval">[docs]</a><span class="k">def</span> <span class="nf">plot_interval</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">intervals</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'red'</span><span class="p">,</span> <span class="n">typeonlegend</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="s1">'-'</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span> <span class="n">lower</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">intervals</span><span class="p">]</span> <span class="n">upper</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">intervals</span><span class="p">]</span> <span class="n">mi</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">lower</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.95</span> <span class="n">ma</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">upper</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</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="n">order</span><span class="p">):</span> <span class="n">lower</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">upper</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">typeonlegend</span><span class="p">:</span> <span class="n">label</span> <span class="o">+=</span> <span class="s2">" (Interval)"</span> <span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">lower</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="n">ls</span><span class="p">,</span><span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">)</span> <span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">upper</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="n">ls</span><span class="p">,</span><span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">)</span> <span class="k">return</span> <span class="p">[</span><span class="n">mi</span><span class="p">,</span> <span class="n">ma</span><span class="p">]</span></div> <div class="viewcode-block" id="plot_point"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plot_point">[docs]</a><span class="k">def</span> <span class="nf">plot_point</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">points</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'red'</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="s1">'-'</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span> <span class="n">mi</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">points</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.95</span> <span class="n">ma</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">points</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</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="n">order</span><span class="p">):</span> <span class="n">points</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">points</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="n">ls</span><span class="p">,</span><span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">)</span> <span class="k">return</span> <span class="p">[</span><span class="n">mi</span><span class="p">,</span> <span class="n">ma</span><span class="p">]</span></div> <div class="viewcode-block" id="plot_compared_series"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plot_compared_series">[docs]</a><span class="k">def</span> <span class="nf">plot_compared_series</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">models</span><span class="p">,</span> <span class="n">colors</span><span class="p">,</span> <span class="n">typeonlegend</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">save</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tam</span><span class="o">=</span><span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="n">points</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">intervals</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">1.5</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> Plot the forecasts of several one step ahead models, by point or by interval</span> <span class="sd"> :param original: Original time series data (list)</span> <span class="sd"> :param models: List of models to compare</span> <span class="sd"> :param colors: List of models colors</span> <span class="sd"> :param typeonlegend: Add the type of forecast (point / interval) on legend</span> <span class="sd"> :param save: Save the picture on file</span> <span class="sd"> :param file: Filename to save the picture</span> <span class="sd"> :param tam: Size of the picture</span> <span class="sd"> :param points: True to plot the point forecasts, False otherwise</span> <span class="sd"> :param intervals: True to plot the interval forecasts, False otherwise</span> <span class="sd"> :param linewidth:</span> <span class="sd"> :return:</span> <span class="sd"> """</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="n">tam</span><span class="p">)</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span> <span class="n">mi</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">ma</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">legends</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'black'</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original"</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="o">*</span><span class="mf">1.5</span><span class="p">)</span> <span class="k">for</span> <span class="n">count</span><span class="p">,</span> <span class="n">fts</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">models</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="k">try</span><span class="p">:</span> <span class="k">if</span> <span class="n">fts</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="ow">and</span> <span class="n">points</span><span class="p">:</span> <span class="n">forecasts</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">original</span><span class="p">)</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">forecasts</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">forecasts</span> <span class="o">=</span> <span class="n">forecasts</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span> <span class="n">mi</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.95</span><span class="p">)</span> <span class="n">ma</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</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="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">):</span> <span class="n">forecasts</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">lbl</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span> <span class="k">if</span> <span class="n">fts</span><span class="o">.</span><span class="n">is_high_order</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">fts</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="k">else</span> <span class="s2">""</span><span class="p">)</span> <span class="k">if</span> <span class="n">typeonlegend</span><span class="p">:</span> <span class="n">lbl</span> <span class="o">+=</span> <span class="s2">" (Point)"</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">count</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="n">lbl</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="s2">"-"</span><span class="p">,</span><span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">)</span> <span class="k">if</span> <span class="n">fts</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="ow">and</span> <span class="n">intervals</span><span class="p">:</span> <span class="n">forecasts</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">(</span><span class="n">original</span><span class="p">)</span> <span class="n">lbl</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">+</span> <span class="s2">" "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span> <span class="k">if</span> <span class="n">fts</span><span class="o">.</span><span class="n">is_high_order</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">fts</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="k">else</span> <span class="s2">""</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">points</span> <span class="ow">and</span> <span class="n">intervals</span><span class="p">:</span> <span class="n">ls</span> <span class="o">=</span> <span class="s2">"-"</span> <span class="k">else</span><span class="p">:</span> <span class="n">ls</span> <span class="o">=</span> <span class="s2">"--"</span> <span class="n">tmpmi</span><span class="p">,</span> <span class="n">tmpma</span> <span class="o">=</span> <span class="n">plot_interval</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">,</span> <span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">lbl</span><span class="p">,</span> <span class="n">typeonlegend</span><span class="o">=</span><span class="n">typeonlegend</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">colors</span><span class="p">[</span><span class="n">count</span><span class="p">],</span> <span class="n">ls</span><span class="o">=</span><span class="n">ls</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">)</span> <span class="n">mi</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmpmi</span><span class="p">)</span> <span class="n">ma</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmpma</span><span class="p">)</span> <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">shortname</span><span class="p">)</span> <span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="n">lgd</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">bbox_to_anchor</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="n">legends</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">lgd</span><span class="p">)</span> <span class="c1"># ax.set_title(fts.name)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">mi</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">ma</span><span class="p">)])</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'F(T)'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'T'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">original</span><span class="p">)])</span></div> <span class="c1">#Util.show_and_save_image(fig, file, save, lgd=legends)</span> <div class="viewcode-block" id="plot_probability_distributions"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plot_probability_distributions">[docs]</a><span class="k">def</span> <span class="nf">plot_probability_distributions</span><span class="p">(</span><span class="n">pmfs</span><span class="p">,</span> <span class="n">lcolors</span><span class="p">,</span> <span class="n">tam</span><span class="o">=</span><span class="p">[</span><span class="mi">15</span><span class="p">,</span> <span class="mi">7</span><span class="p">]):</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="n">tam</span><span class="p">)</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">m</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">pmfs</span><span class="p">,</span><span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="n">m</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">lcolors</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">)</span></div> <div class="viewcode-block" id="plotCompared"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.plotCompared">[docs]</a><span class="k">def</span> <span class="nf">plotCompared</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">,</span> <span class="n">labels</span><span class="p">,</span> <span class="n">title</span><span class="p">):</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">[</span><span class="mi">13</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">'k'</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original"</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)):</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">forecasts</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="n">labels</span><span class="p">[</span><span class="n">c</span><span class="p">])</span> <span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'F(T)'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'T'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">original</span><span class="p">)])</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">original</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">original</span><span class="p">)])</span></div> <div class="viewcode-block" id="SelecaoSimples_MenorRMSE"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.SelecaoSimples_MenorRMSE">[docs]</a><span class="k">def</span> <span class="nf">SelecaoSimples_MenorRMSE</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">parameters</span><span class="p">,</span> <span class="n">modelo</span><span class="p">):</span> <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">forecasted_best</span> <span class="o">=</span> <span class="p">[]</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Série Original"</span><span class="p">)</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">12</span><span class="p">])</span> <span class="n">fig</span><span class="o">.</span><span class="n">suptitle</span><span class="p">(</span><span class="s2">"Comparação de modelos "</span><span class="p">)</span> <span class="n">ax0</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.65</span><span class="p">,</span> <span class="mf">0.45</span><span class="p">])</span> <span class="c1"># left, bottom, width, height</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">original</span><span class="p">)])</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">original</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">original</span><span class="p">)])</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Série Temporal'</span><span class="p">)</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'F(T)'</span><span class="p">)</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'T'</span><span class="p">)</span> <span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original"</span><span class="p">)</span> <span class="n">min_rmse</span> <span class="o">=</span> <span class="mf">100000.0</span> <span class="n">best</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parameters</span><span class="p">:</span> <span class="n">sets</span> <span class="o">=</span> <span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">original</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">p</span><span class="p">)</span><span class="o">.</span><span class="n">sets</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">modelo</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" particoes"</span><span class="p">)</span> <span class="n">fts</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">sets</span><span class="o">=</span><span class="n">sets</span><span class="p">)</span> <span class="c1"># print(original)</span> <span class="n">forecasted</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">original</span><span class="p">)</span> <span class="n">forecasted</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">original</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># print(forecasted)</span> <span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">forecasted</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="n">error</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasted</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">original</span><span class="p">))</span> <span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">error</span><span class="p">)</span> <span class="k">if</span> <span class="n">error</span> <span class="o"><</span> <span class="n">min_rmse</span><span class="p">:</span> <span class="n">min_rmse</span> <span class="o">=</span> <span class="n">error</span> <span class="n">best</span> <span class="o">=</span> <span class="n">fts</span> <span class="n">forecasted_best</span> <span class="o">=</span> <span class="n">forecasted</span> <span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span> <span class="o">=</span> <span class="n">ax0</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="n">ax0</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">)</span> <span class="n">ax1</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</span><span class="p">([</span><span class="mf">0.7</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.45</span><span class="p">])</span> <span class="c1"># left, bottom, width, height</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Comparação dos Erros Quadráticos Médios'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'RMSE'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'Quantidade de Partições'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">parameters</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">parameters</span><span class="p">)])</span> <span class="n">ax1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="n">errors</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">best</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">forecasted_best</span><span class="p">)</span> <span class="c1"># Modelo diferencial</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">Série Diferencial"</span><span class="p">)</span> <span class="n">difffts</span> <span class="o">=</span> <span class="n">Transformations</span><span class="o">.</span><span class="n">differential</span><span class="p">(</span><span class="n">original</span><span class="p">)</span> <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">forecastedd_best</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">ax2</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.65</span><span class="p">,</span> <span class="mf">0.45</span><span class="p">])</span> <span class="c1"># left, bottom, width, height</span> <span class="n">ax2</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">difffts</span><span class="p">)])</span> <span class="n">ax2</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">difffts</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">difffts</span><span class="p">)])</span> <span class="n">ax2</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Série Temporal'</span><span class="p">)</span> <span class="n">ax2</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'F(T)'</span><span class="p">)</span> <span class="n">ax2</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'T'</span><span class="p">)</span> <span class="n">ax2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">difffts</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original"</span><span class="p">)</span> <span class="n">min_rmse</span> <span class="o">=</span> <span class="mf">100000.0</span> <span class="n">bestd</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parameters</span><span class="p">:</span> <span class="n">sets</span> <span class="o">=</span> <span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">difffts</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">p</span><span class="p">)</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">modelo</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" particoes"</span><span class="p">)</span> <span class="n">fts</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">difffts</span><span class="p">,</span> <span class="n">sets</span><span class="o">=</span><span class="n">sets</span><span class="p">)</span> <span class="n">forecasted</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">difffts</span><span class="p">)</span> <span class="n">forecasted</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">difffts</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="n">ax2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">forecasted</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="n">error</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasted</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">difffts</span><span class="p">))</span> <span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">error</span><span class="p">)</span> <span class="k">if</span> <span class="n">error</span> <span class="o"><</span> <span class="n">min_rmse</span><span class="p">:</span> <span class="n">min_rmse</span> <span class="o">=</span> <span class="n">error</span> <span class="n">bestd</span> <span class="o">=</span> <span class="n">fts</span> <span class="n">forecastedd_best</span> <span class="o">=</span> <span class="n">forecasted</span> <span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span> <span class="o">=</span> <span class="n">ax2</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="n">ax2</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">)</span> <span class="n">ax3</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</span><span class="p">([</span><span class="mf">0.7</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.45</span><span class="p">])</span> <span class="c1"># left, bottom, width, height</span> <span class="n">ax3</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Comparação dos Erros Quadráticos Médios'</span><span class="p">)</span> <span class="n">ax3</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'RMSE'</span><span class="p">)</span> <span class="n">ax3</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'Quantidade de Partições'</span><span class="p">)</span> <span class="n">ax3</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">parameters</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">parameters</span><span class="p">)])</span> <span class="n">ax3</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="n">errors</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">bestd</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">forecastedd_best</span><span class="p">)</span> <span class="k">return</span> <span class="n">ret</span></div> <div class="viewcode-block" id="compareModelsPlot"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.compareModelsPlot">[docs]</a><span class="k">def</span> <span class="nf">compareModelsPlot</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">models_fo</span><span class="p">,</span> <span class="n">models_ho</span><span class="p">):</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">[</span><span class="mi">13</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span> <span class="n">fig</span><span class="o">.</span><span class="n">suptitle</span><span class="p">(</span><span class="s2">"Comparação de modelos "</span><span class="p">)</span> <span class="n">ax0</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</span><span class="p">([</span><span class="mi">0</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="mi">1</span><span class="p">])</span> <span class="c1"># left, bottom, width, height</span> <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">models_fo</span><span class="p">:</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">model</span><span class="p">[</span><span class="s2">"model"</span><span class="p">]</span> <span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">model</span><span class="p">[</span><span class="s2">"forecasted"</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="n">model</span><span class="p">[</span><span class="s2">"name"</span><span class="p">])</span> <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">models_ho</span><span class="p">:</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">model</span><span class="p">[</span><span class="s2">"model"</span><span class="p">]</span> <span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">model</span><span class="p">[</span><span class="s2">"forecasted"</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="n">model</span><span class="p">[</span><span class="s2">"name"</span><span class="p">])</span> <span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span> <span class="o">=</span> <span class="n">ax0</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="n">ax0</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles0</span><span class="p">,</span> <span class="n">labels0</span><span class="p">)</span></div> <div class="viewcode-block" id="compareModelsTable"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.compareModelsTable">[docs]</a><span class="k">def</span> <span class="nf">compareModelsTable</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">models_fo</span><span class="p">,</span> <span class="n">models_ho</span><span class="p">):</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">[</span><span class="mi">12</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span> <span class="n">fig</span><span class="o">.</span><span class="n">suptitle</span><span class="p">(</span><span class="s2">"Comparação de modelos "</span><span class="p">)</span> <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Modelo'</span><span class="p">,</span> <span class="s1">'Ordem'</span><span class="p">,</span> <span class="s1">'Partições'</span><span class="p">,</span> <span class="s1">'RMSE'</span><span class="p">,</span> <span class="s1">'MAPE (%)'</span><span class="p">]</span> <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">models_fo</span><span class="p">:</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">model</span><span class="p">[</span><span class="s2">"model"</span><span class="p">]</span> <span class="n">error_r</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">model</span><span class="p">[</span><span class="s2">"forecasted"</span><span class="p">],</span> <span class="n">original</span><span class="p">)</span> <span class="n">error_m</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">Measures</span><span class="o">.</span><span class="n">mape</span><span class="p">(</span><span class="n">model</span><span class="p">[</span><span class="s2">"forecasted"</span><span class="p">],</span> <span class="n">original</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">model</span><span class="p">[</span><span class="s2">"name"</span><span class="p">],</span> <span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">sets</span><span class="p">),</span> <span class="n">error_r</span><span class="p">,</span> <span class="n">error_m</span><span class="p">])</span> <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">models_ho</span><span class="p">:</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">model</span><span class="p">[</span><span class="s2">"model"</span><span class="p">]</span> <span class="n">error_r</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">model</span><span class="p">[</span><span class="s2">"forecasted"</span><span class="p">][</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">:],</span> <span class="n">original</span><span class="p">[</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">:])</span> <span class="n">error_m</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">Measures</span><span class="o">.</span><span class="n">mape</span><span class="p">(</span><span class="n">model</span><span class="p">[</span><span class="s2">"forecasted"</span><span class="p">][</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">:],</span> <span class="n">original</span><span class="p">[</span><span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">:])</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">model</span><span class="p">[</span><span class="s2">"name"</span><span class="p">],</span> <span class="n">fts</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">sets</span><span class="p">),</span> <span class="n">error_r</span><span class="p">,</span> <span class="n">error_m</span><span class="p">])</span> <span class="n">ax1</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</span><span class="p">([</span><span class="mi">0</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="mi">1</span><span class="p">])</span> <span class="c1"># left, bottom, width, height</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">([])</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_yticks</span><span class="p">([])</span> <span class="n">ax1</span><span class="o">.</span><span class="n">table</span><span class="p">(</span><span class="n">cellText</span><span class="o">=</span><span class="n">rows</span><span class="p">,</span> <span class="n">colLabels</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span> <span class="n">cellLoc</span><span class="o">=</span><span class="s1">'center'</span><span class="p">,</span> <span class="n">bbox</span><span class="o">=</span><span class="p">[</span><span class="mi">0</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="mi">1</span><span class="p">])</span> <span class="n">sup</span> <span class="o">=</span> <span class="s2">"</span><span class="se">\\</span><span class="s2">begin</span><span class="si">{tabular}</span><span class="s2">{"</span> <span class="n">header</span> <span class="o">=</span> <span class="s2">""</span> <span class="n">body</span> <span class="o">=</span> <span class="s2">""</span> <span class="n">footer</span> <span class="o">=</span> <span class="s2">""</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">:</span> <span class="n">sup</span> <span class="o">=</span> <span class="n">sup</span> <span class="o">+</span> <span class="s2">"|c"</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">header</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span> <span class="n">header</span> <span class="o">=</span> <span class="n">header</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">header</span> <span class="o">=</span> <span class="n">header</span> <span class="o">+</span> <span class="s2">"</span><span class="se">\\</span><span class="s2">textbf{"</span> <span class="o">+</span> <span class="n">c</span> <span class="o">+</span> <span class="s2">"} "</span> <span class="n">sup</span> <span class="o">=</span> <span class="n">sup</span> <span class="o">+</span> <span class="s2">"|} </span><span class="se">\\</span><span class="s2">hline</span><span class="se">\n</span><span class="s2">"</span> <span class="n">header</span> <span class="o">=</span> <span class="n">header</span> <span class="o">+</span> <span class="s2">"</span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\\</span><span class="s2">hline </span><span class="se">\n</span><span class="s2">"</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rows</span><span class="p">:</span> <span class="n">lin</span> <span class="o">=</span> <span class="s2">""</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">r</span><span class="p">:</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">lin</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span> <span class="n">lin</span> <span class="o">=</span> <span class="n">lin</span> <span class="o">+</span> <span class="s2">" & "</span> <span class="n">lin</span> <span class="o">=</span> <span class="n">lin</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="n">body</span> <span class="o">=</span> <span class="n">body</span> <span class="o">+</span> <span class="n">lin</span> <span class="o">+</span> <span class="s2">"</span><span class="se">\\\\</span><span class="s2"> </span><span class="se">\\</span><span class="s2">hline </span><span class="se">\n</span><span class="s2">"</span> <span class="k">return</span> <span class="n">sup</span> <span class="o">+</span> <span class="n">header</span> <span class="o">+</span> <span class="n">body</span> <span class="o">+</span> <span class="s2">"</span><span class="se">\\</span><span class="s2">end</span><span class="si">{tabular}</span><span class="s2">"</span></div> <div class="viewcode-block" id="simpleSearch_RMSE"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.simpleSearch_RMSE">[docs]</a><span class="k">def</span> <span class="nf">simpleSearch_RMSE</span><span class="p">(</span><span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">partitions</span><span class="p">,</span> <span class="n">orders</span><span class="p">,</span> <span class="n">save</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tam</span><span class="o">=</span><span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">15</span><span class="p">],</span> <span class="n">plotforecasts</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">elev</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span> <span class="n">azim</span><span class="o">=</span><span class="mi">144</span><span class="p">,</span> <span class="n">intervals</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">parameters</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">partitioner</span><span class="o">=</span><span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">,</span><span class="n">transformation</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">indexer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="n">_3d</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">orders</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span> <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">_3d</span><span class="p">:</span> <span class="n">errors</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="mi">0</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">partitions</span><span class="p">))]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">orders</span><span class="p">))])</span> <span class="k">else</span><span class="p">:</span> <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">forecasted_best</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="n">tam</span><span class="p">)</span> <span class="c1"># fig.suptitle("Comparação de modelos ")</span> <span class="k">if</span> <span class="n">plotforecasts</span><span class="p">:</span> <span class="n">ax0</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">])</span> <span class="c1"># left, bottom, width, height</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">train</span><span class="p">)])</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">train</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.9</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">train</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">])</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Forecasts'</span><span class="p">)</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'F(T)'</span><span class="p">)</span> <span class="n">ax0</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'T'</span><span class="p">)</span> <span class="n">min_rmse</span> <span class="o">=</span> <span class="mf">1000000.0</span> <span class="n">best</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">pc</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">partitions</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="n">sets</span> <span class="o">=</span> <span class="n">partitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">train</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">p</span><span class="p">,</span> <span class="n">transformation</span><span class="o">=</span><span class="n">transformation</span><span class="p">)</span><span class="o">.</span><span class="n">sets</span> <span class="k">for</span> <span class="n">oc</span><span class="p">,</span> <span class="n">o</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">orders</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">model</span><span class="p">(</span><span class="s2">"q = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" n = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">o</span><span class="p">))</span> <span class="n">fts</span><span class="o">.</span><span class="n">append_transformation</span><span class="p">(</span><span class="n">transformation</span><span class="p">)</span> <span class="n">fts</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">train</span><span class="p">,</span> <span class="n">sets</span><span class="o">=</span><span class="n">sets</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="n">o</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="n">parameters</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">intervals</span><span class="p">:</span> <span class="n">forecasted</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">test</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">fts</span><span class="o">.</span><span class="n">has_seasonality</span><span class="p">:</span> <span class="n">error</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">test</span><span class="p">[</span><span class="n">o</span><span class="p">:]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasted</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">error</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">test</span><span class="p">[</span><span class="n">o</span><span class="p">:]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasted</span><span class="p">))</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">o</span><span class="p">):</span> <span class="n">forecasted</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">plotforecasts</span><span class="p">:</span> <span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">forecasted</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</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">forecasted</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">(</span><span class="n">test</span><span class="p">)</span> <span class="n">error</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse_interval</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">test</span><span class="p">[</span><span class="n">o</span><span class="p">:]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasted</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">_3d</span><span class="p">:</span> <span class="n">errors</span><span class="p">[</span><span class="n">oc</span><span class="p">,</span> <span class="n">pc</span><span class="p">]</span> <span class="o">=</span> <span class="n">error</span> <span class="k">else</span><span class="p">:</span> <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="n">error</span> <span class="p">)</span> <span class="k">if</span> <span class="n">error</span> <span class="o"><</span> <span class="n">min_rmse</span><span class="p">:</span> <span class="n">min_rmse</span> <span class="o">=</span> <span class="n">error</span> <span class="n">best</span> <span class="o">=</span> <span class="n">fts</span> <span class="n">forecasted_best</span> <span class="o">=</span> <span class="n">forecasted</span> <span class="c1"># print(min_rmse)</span> <span class="k">if</span> <span class="n">plotforecasts</span><span class="p">:</span> <span class="c1"># handles0, labels0 = ax0.get_legend_handles_labels()</span> <span class="c1"># ax0.legend(handles0, labels0)</span> <span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">test</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original"</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">3.0</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"black"</span><span class="p">)</span> <span class="k">if</span> <span class="n">_3d</span><span class="p">:</span> <span class="n">ax1</span> <span class="o">=</span> <span class="n">Axes3D</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">rect</span><span class="o">=</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="mf">0.9</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">],</span> <span class="n">elev</span><span class="o">=</span><span class="n">elev</span><span class="p">,</span> <span class="n">azim</span><span class="o">=</span><span class="n">azim</span><span class="p">)</span> <span class="k">if</span> <span class="n">_3d</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">plotforecasts</span><span class="p">:</span> <span class="n">ax1</span> <span class="o">=</span> <span class="n">Axes3D</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">rect</span><span class="o">=</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="mf">0.9</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">],</span> <span class="n">elev</span><span class="o">=</span><span class="n">elev</span><span class="p">,</span> <span class="n">azim</span><span class="o">=</span><span class="n">azim</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Error Surface'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'Model order'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'Number of partitions'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_zlabel</span><span class="p">(</span><span class="s1">'RMSE'</span><span class="p">)</span> <span class="n">X</span><span class="p">,</span> <span class="n">Y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">meshgrid</span><span class="p">(</span><span class="n">partitions</span><span class="p">,</span> <span class="n">orders</span><span class="p">)</span> <span class="n">surf</span> <span class="o">=</span> <span class="n">ax1</span><span class="o">.</span><span class="n">plot_surface</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">errors</span><span class="p">,</span> <span class="n">rstride</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">cstride</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">antialiased</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">ax1</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_axes</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="mf">0.9</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">])</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Error Curve'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'Number of partitions'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'RMSE'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">partitions</span><span class="p">,</span> <span class="n">errors</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">best</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">forecasted_best</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">min_rmse</span><span class="p">)</span> <span class="c1"># plt.tight_layout()</span> <span class="n">cUtil</span><span class="o">.</span><span class="n">show_and_save_image</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">file</span><span class="p">,</span> <span class="n">save</span><span class="p">)</span> <span class="k">return</span> <span class="n">ret</span></div> <div class="viewcode-block" id="pftsExploreOrderAndPartitions"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.benchmarks.pftsExploreOrderAndPartitions">[docs]</a><span class="k">def</span> <span class="nf">pftsExploreOrderAndPartitions</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">save</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="n">fig</span><span class="p">,</span> <span class="n">axes</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">nrows</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">ncols</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">])</span> <span class="n">data_fs1</span> <span class="o">=</span> <span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">sets</span> <span class="n">mi</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">ma</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">axes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Point Forecasts by Order'</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Interval Forecasts by Order'</span><span class="p">)</span> <span class="k">for</span> <span class="n">order</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">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">):</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span> <span class="n">fts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">"n = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">order</span><span class="p">)</span> <span class="n">fts</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">sets</span><span class="o">=</span><span class="n">data_fs1</span><span class="o">.</span><span class="n">sets</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="n">order</span><span class="p">)</span> <span class="n">point_forecasts</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">interval_forecasts</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">lower</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">interval_forecasts</span><span class="p">]</span> <span class="n">upper</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">interval_forecasts</span><span class="p">]</span> <span class="n">mi</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">lower</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.95</span><span class="p">)</span> <span class="n">ma</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">upper</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</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="n">order</span><span class="p">):</span> <span class="n">point_forecasts</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">lower</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">upper</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">point_forecasts</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</span><span class="o">.</span><span class="n">shortname</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">lower</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</span><span class="o">.</span><span class="n">shortname</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">upper</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Point Forecasts by Number of Partitions'</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Interval Forecasts by Number of Partitions'</span><span class="p">)</span> <span class="k">for</span> <span class="n">partitions</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">5</span><span class="p">,</span> <span class="mi">11</span><span class="p">):</span> <span class="n">data_fs</span> <span class="o">=</span> <span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">partitions</span><span class="p">)</span><span class="o">.</span><span class="n">sets</span> <span class="n">fts</span> <span class="o">=</span> <span class="n">pwfts</span><span class="o">.</span><span class="n">ProbabilisticWeightedFTS</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span> <span class="n">fts</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">"q = "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">partitions</span><span class="p">)</span> <span class="n">fts</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">sets</span><span class="o">=</span><span class="n">data_fs</span><span class="o">.</span><span class="n">sets</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="n">point_forecasts</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">interval_forecasts</span> <span class="o">=</span> <span class="n">fts</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="n">lower</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">interval_forecasts</span><span class="p">]</span> <span class="n">upper</span> <span class="o">=</span> <span class="p">[</span><span class="n">kk</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">kk</span> <span class="ow">in</span> <span class="n">interval_forecasts</span><span class="p">]</span> <span class="n">mi</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">lower</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.95</span><span class="p">)</span> <span class="n">ma</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">upper</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">)</span> <span class="n">point_forecasts</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">lower</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">upper</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">point_forecasts</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</span><span class="o">.</span><span class="n">shortname</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">lower</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fts</span><span class="o">.</span><span class="n">shortname</span><span class="p">)</span> <span class="n">axes</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">upper</span><span class="p">)</span> <span class="k">for</span> <span class="n">ax</span> <span class="ow">in</span> <span class="n">axes</span><span class="p">:</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">'F(T)'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'T'</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Original"</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"black"</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">1.5</span><span class="p">)</span> <span class="n">handles</span><span class="p">,</span> <span class="n">labels</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">get_legend_handles_labels</span><span class="p">()</span> <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles</span><span class="p">,</span> <span class="n">labels</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">bbox_to_anchor</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="nb">min</span><span class="p">(</span><span class="n">mi</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">ma</span><span class="p">)])</span> <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)])</span> <span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span> <span class="n">cUtil</span><span class="o">.</span><span class="n">show_and_save_image</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">file</span><span class="p">,</span> <span class="n">save</span><span class="p">)</span></div> </pre></div> </div> </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.2.3 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2018, Machine Intelligence and Data Science Laboratory - UFMG - Brazil. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.2. </div> </body> </html>