Compare commits

..

No commits in common. "master" and "release1.7" have entirely different histories.

187 changed files with 46283 additions and 16016 deletions

57
.gitignore vendored
View File

@ -1,57 +0,0 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
# PyBuilder
target/

View File

@ -62,7 +62,7 @@
"name": "Hossein Javedani Sadaei"
}
],
"version": "pkg1.7",
"version": "pkg1.6",
"upload_type": "software",
"keywords": [
"data science",

View File

@ -1,10 +0,0 @@
cff-version: 1.1.0
message: "If you use this software, please cite it as below."
authors:
- family-names: Silva
given-names: Petrônio C. L.
orcid: https://orcid.org/0000-0002-1202-2552/
title: PYFTS/pyFTS: Stable version 1.7
doi: 10.5281/zenodo.597359
version: pkg1.7
date-released: 2023-03-20

View File

@ -104,7 +104,7 @@ pyFTS/models/seasonal/common.py
pyFTS/models/seasonal/msfts.py
pyFTS/models/seasonal/partitioner.py
pyFTS/models/seasonal/sfts.py
pyFTS/partitioners/KMeans.py
pyFTS/partitioners/CMeans.py
pyFTS/partitioners/Entropy.py
pyFTS/partitioners/FCM.py
pyFTS/partitioners/Grid.py

View File

@ -38,7 +38,7 @@ Fuzzy Time Series (FTS) are non parametric methods for time series forecasting b
2. **Universe of Discourse Partitioning**: This is the most important step. Here, the range of values of the numerical time series *Y(t)* will be splited in overlapped intervals and for each interval will be created a Fuzzy Set. This step is performed by pyFTS.partition module and its classes (for instance GridPartitioner, EntropyPartitioner, etc). The main parameters are:
- the number of intervals
- which fuzzy membership function (on [pyFTS.common.Membership](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/common/Membership.py))
- partition scheme ([GridPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Grid.py), [EntropyPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Entropy.py)[3], [FCMPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/FCM.py), [KMeansPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/KMeans.py), [HuarngPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Huarng.py)[4])
- partition scheme ([GridPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Grid.py), [EntropyPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Entropy.py)[3], [FCMPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/FCM.py), [CMeansPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/CMeans.py), [HuarngPartitioner](https://github.com/PYFTS/pyFTS/blob/master/pyFTS/partitioners/Huarng.py)[4])
Check out the jupyter notebook on [notebooks/Partitioners.ipynb](https://github.com/PYFTS/notebooks/blob/master/Partitioners.ipynb) for sample codes.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: e1454eaeb1089d326a83742cdfb66db8
config: 32d60ea8a7e69a10f6b65d4fe3722cb4
tags: 645f666f9bcd5a90fca523b33c5a78b7

View File

@ -42,13 +42,24 @@
<div class="body" role="main">
<h1>All modules for which code is available</h1>
<ul><li><a href="pyFTS/common/Activations.html">pyFTS.common.Activations</a></li>
<ul><li><a href="pyFTS/benchmarks/BSTS.html">pyFTS.benchmarks.BSTS</a></li>
<li><a href="pyFTS/benchmarks/Measures.html">pyFTS.benchmarks.Measures</a></li>
<li><a href="pyFTS/benchmarks/ResidualAnalysis.html">pyFTS.benchmarks.ResidualAnalysis</a></li>
<li><a href="pyFTS/benchmarks/Tests.html">pyFTS.benchmarks.Tests</a></li>
<li><a href="pyFTS/benchmarks/Util.html">pyFTS.benchmarks.Util</a></li>
<li><a href="pyFTS/benchmarks/arima.html">pyFTS.benchmarks.arima</a></li>
<li><a href="pyFTS/benchmarks/benchmarks.html">pyFTS.benchmarks.benchmarks</a></li>
<li><a href="pyFTS/benchmarks/knn.html">pyFTS.benchmarks.knn</a></li>
<li><a href="pyFTS/benchmarks/naive.html">pyFTS.benchmarks.naive</a></li>
<li><a href="pyFTS/benchmarks/quantreg.html">pyFTS.benchmarks.quantreg</a></li>
<li><a href="pyFTS/common/Composite.html">pyFTS.common.Composite</a></li>
<li><a href="pyFTS/common/FLR.html">pyFTS.common.FLR</a></li>
<li><a href="pyFTS/common/FuzzySet.html">pyFTS.common.FuzzySet</a></li>
<li><a href="pyFTS/common/Membership.html">pyFTS.common.Membership</a></li>
<li><a href="pyFTS/common/SortedCollection.html">pyFTS.common.SortedCollection</a></li>
<li><a href="pyFTS/common/Util.html">pyFTS.common.Util</a></li>
<li><a href="pyFTS/common/flrg.html">pyFTS.common.flrg</a></li>
<li><a href="pyFTS/common/fts.html">pyFTS.common.fts</a></li>
<li><a href="pyFTS/common/transformations/boxcox.html">pyFTS.common.transformations.boxcox</a></li>
<li><a href="pyFTS/common/transformations/differential.html">pyFTS.common.transformations.differential</a></li>
<li><a href="pyFTS/common/transformations/normalization.html">pyFTS.common.transformations.normalization</a></li>
@ -57,6 +68,7 @@
<li><a href="pyFTS/common/transformations/smoothing.html">pyFTS.common.transformations.smoothing</a></li>
<li><a href="pyFTS/common/transformations/som.html">pyFTS.common.transformations.som</a></li>
<li><a href="pyFTS/common/transformations/transformation.html">pyFTS.common.transformations.transformation</a></li>
<li><a href="pyFTS/common/transformations/trend.html">pyFTS.common.transformations.trend</a></li>
<li><a href="pyFTS/common/tree.html">pyFTS.common.tree</a></li>
<li><a href="pyFTS/data/AirPassengers.html">pyFTS.data.AirPassengers</a></li>
<li><a href="pyFTS/data/Bitcoin.html">pyFTS.data.Bitcoin</a></li>
@ -80,28 +92,60 @@
<li><a href="pyFTS/data/mackey_glass.html">pyFTS.data.mackey_glass</a></li>
<li><a href="pyFTS/data/rossler.html">pyFTS.data.rossler</a></li>
<li><a href="pyFTS/data/sunspots.html">pyFTS.data.sunspots</a></li>
<li><a href="pyFTS/hyperparam/Evolutionary.html">pyFTS.hyperparam.Evolutionary</a></li>
<li><a href="pyFTS/hyperparam/GridSearch.html">pyFTS.hyperparam.GridSearch</a></li>
<li><a href="pyFTS/hyperparam/Util.html">pyFTS.hyperparam.Util</a></li>
<li><a href="pyFTS/models/chen.html">pyFTS.models.chen</a></li>
<li><a href="pyFTS/models/cheng.html">pyFTS.models.cheng</a></li>
<li><a href="pyFTS/models/ensemble/ensemble.html">pyFTS.models.ensemble.ensemble</a></li>
<li><a href="pyFTS/models/ensemble/multiseasonal.html">pyFTS.models.ensemble.multiseasonal</a></li>
<li><a href="pyFTS/models/hofts.html">pyFTS.models.hofts</a></li>
<li><a href="pyFTS/models/hwang.html">pyFTS.models.hwang</a></li>
<li><a href="pyFTS/models/ifts.html">pyFTS.models.ifts</a></li>
<li><a href="pyFTS/models/incremental/IncrementalEnsemble.html">pyFTS.models.incremental.IncrementalEnsemble</a></li>
<li><a href="pyFTS/models/incremental/TimeVariant.html">pyFTS.models.incremental.TimeVariant</a></li>
<li><a href="pyFTS/models/ismailefendi.html">pyFTS.models.ismailefendi</a></li>
<li><a href="pyFTS/models/multivariate/FLR.html">pyFTS.models.multivariate.FLR</a></li>
<li><a href="pyFTS/models/multivariate/cmvfts.html">pyFTS.models.multivariate.cmvfts</a></li>
<li><a href="pyFTS/models/multivariate/common.html">pyFTS.models.multivariate.common</a></li>
<li><a href="pyFTS/models/multivariate/flrg.html">pyFTS.models.multivariate.flrg</a></li>
<li><a href="pyFTS/models/multivariate/granular.html">pyFTS.models.multivariate.granular</a></li>
<li><a href="pyFTS/models/multivariate/grid.html">pyFTS.models.multivariate.grid</a></li>
<li><a href="pyFTS/models/multivariate/mvfts.html">pyFTS.models.multivariate.mvfts</a></li>
<li><a href="pyFTS/models/multivariate/partitioner.html">pyFTS.models.multivariate.partitioner</a></li>
<li><a href="pyFTS/models/multivariate/variable.html">pyFTS.models.multivariate.variable</a></li>
<li><a href="pyFTS/models/multivariate/wmvfts.html">pyFTS.models.multivariate.wmvfts</a></li>
<li><a href="pyFTS/models/nonstationary/common.html">pyFTS.models.nonstationary.common</a></li>
<li><a href="pyFTS/models/nonstationary/cvfts.html">pyFTS.models.nonstationary.cvfts</a></li>
<li><a href="pyFTS/models/nonstationary/flrg.html">pyFTS.models.nonstationary.flrg</a></li>
<li><a href="pyFTS/models/nonstationary/honsfts.html">pyFTS.models.nonstationary.honsfts</a></li>
<li><a href="pyFTS/models/nonstationary/nsfts.html">pyFTS.models.nonstationary.nsfts</a></li>
<li><a href="pyFTS/models/nonstationary/partitioners.html">pyFTS.models.nonstationary.partitioners</a></li>
<li><a href="pyFTS/models/nonstationary/perturbation.html">pyFTS.models.nonstationary.perturbation</a></li>
<li><a href="pyFTS/models/nonstationary/util.html">pyFTS.models.nonstationary.util</a></li>
<li><a href="pyFTS/models/pwfts.html">pyFTS.models.pwfts</a></li>
<li><a href="pyFTS/models/sadaei.html">pyFTS.models.sadaei</a></li>
<li><a href="pyFTS/models/seasonal/SeasonalIndexer.html">pyFTS.models.seasonal.SeasonalIndexer</a></li>
<li><a href="pyFTS/models/seasonal/cmsfts.html">pyFTS.models.seasonal.cmsfts</a></li>
<li><a href="pyFTS/models/seasonal/common.html">pyFTS.models.seasonal.common</a></li>
<li><a href="pyFTS/models/seasonal/msfts.html">pyFTS.models.seasonal.msfts</a></li>
<li><a href="pyFTS/models/seasonal/partitioner.html">pyFTS.models.seasonal.partitioner</a></li>
<li><a href="pyFTS/models/seasonal/sfts.html">pyFTS.models.seasonal.sfts</a></li>
<li><a href="pyFTS/models/song.html">pyFTS.models.song</a></li>
<li><a href="pyFTS/models/yu.html">pyFTS.models.yu</a></li>
<li><a href="pyFTS/partitioners/CMeans.html">pyFTS.partitioners.CMeans</a></li>
<li><a href="pyFTS/partitioners/Class.html">pyFTS.partitioners.Class</a></li>
<li><a href="pyFTS/partitioners/Entropy.html">pyFTS.partitioners.Entropy</a></li>
<li><a href="pyFTS/partitioners/FCM.html">pyFTS.partitioners.FCM</a></li>
<li><a href="pyFTS/partitioners/Grid.html">pyFTS.partitioners.Grid</a></li>
<li><a href="pyFTS/partitioners/Huarng.html">pyFTS.partitioners.Huarng</a></li>
<li><a href="pyFTS/partitioners/Simple.html">pyFTS.partitioners.Simple</a></li>
<li><a href="pyFTS/partitioners/Singleton.html">pyFTS.partitioners.Singleton</a></li>
<li><a href="pyFTS/partitioners/SubClust.html">pyFTS.partitioners.SubClust</a></li>
<li><a href="pyFTS/partitioners/Util.html">pyFTS.partitioners.Util</a></li>
<li><a href="pyFTS/partitioners/parallel_util.html">pyFTS.partitioners.parallel_util</a></li>
<li><a href="pyFTS/partitioners/partitioner.html">pyFTS.partitioners.partitioner</a></li>
<li><a href="pyFTS/probabilistic/ProbabilityDistribution.html">pyFTS.probabilistic.ProbabilityDistribution</a></li>
<li><a href="pyFTS/probabilistic/kde.html">pyFTS.probabilistic.kde</a></li>
</ul>
<div class="clearer"></div>
@ -142,7 +186,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,211 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.benchmarks.BSTS &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.BSTS</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.benchmarks.BSTS</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf8 -*-</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">scipy.stats</span> <span class="k">as</span> <span class="nn">st</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">SortedCollection</span><span class="p">,</span> <span class="n">fts</span>
<span class="kn">from</span> <span class="nn">pyFTS.probabilistic</span> <span class="k">import</span> <span class="n">ProbabilityDistribution</span>
<div class="viewcode-block" id="ARIMA"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA">[docs]</a><span class="k">class</span> <span class="nc">ARIMA</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Façade for statsmodels.tsa.arima_model</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ARIMA</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;BSTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Bayesian Structural Time Series&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">trained_data</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&quot;order&quot;</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</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="bp">self</span><span class="o">.</span><span class="n">_decompose_order</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">def</span> <span class="nf">_decompose_order</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">order</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">set</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="n">order</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="n">order</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">order</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">+</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">0</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;BSTS(</span><span class="si">{}</span><span class="s2">,</span><span class="si">{}</span><span class="s2">,</span><span class="si">{}</span><span class="s2">)-</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
<div class="viewcode-block" id="ARIMA.train"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">pyflux</span> <span class="k">as</span> <span class="nn">pf</span>
<span class="k">if</span> <span class="s1">&#39;order&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="n">order</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;order&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_decompose_order</span><span class="p">(</span><span class="n">order</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">pf</span><span class="o">.</span><span class="n">ARIMA</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">ar</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">,</span> <span class="n">ma</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="n">integ</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">,</span> <span class="n">family</span><span class="o">=</span><span class="n">pf</span><span class="o">.</span><span class="n">Normal</span><span class="p">())</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="s1">&#39;M-H&#39;</span><span class="p">,</span> <span class="n">nsims</span><span class="o">=</span><span class="mi">20000</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="n">ex</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="o">=</span> <span class="kc">None</span></div>
<div class="viewcode-block" id="ARIMA.inference"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.inference">[docs]</a> <span class="k">def</span> <span class="nf">inference</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">steps</span><span class="p">):</span>
<span class="n">t_z</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">transform_z</span><span class="p">()</span>
<span class="n">mu</span><span class="p">,</span> <span class="n">Y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">latent_variables</span><span class="o">.</span><span class="n">get_z_values</span><span class="p">())</span>
<span class="n">date_index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">shift_dates</span><span class="p">(</span><span class="n">steps</span><span class="p">)</span>
<span class="n">sim_vector</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_sim_prediction</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="n">t_z</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>
<span class="k">return</span> <span class="n">sim_vector</span></div>
<div class="viewcode-block" id="ARIMA.forecast"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
<div class="viewcode-block" id="ARIMA.forecast_ahead"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.forecast_ahead">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">steps</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="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span></div>
<div class="viewcode-block" id="ARIMA.forecast_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
<div class="viewcode-block" id="ARIMA.forecast_ahead_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">sim_vector</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inference</span><span class="p">(</span><span class="n">steps</span><span class="p">)</span>
<span class="k">if</span> <span class="s1">&#39;alpha&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;alpha&#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span>
<span class="n">ret</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">sample</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">sim_vector</span><span class="p">):</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">percentile</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="p">[</span><span class="n">alpha</span><span class="o">*</span><span class="mi">100</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">alpha</span><span class="p">)</span><span class="o">*</span><span class="mi">100</span><span class="p">])</span><span class="o">.</span><span class="n">tolist</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">i</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="ARIMA.forecast_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">pyflux</span> <span class="k">as</span> <span class="nn">pf</span>
<span class="n">sim_vector</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inference</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">sample</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">sim_vector</span><span class="p">):</span>
<span class="n">pd</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s1">&#39;histogram&#39;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">sample</span><span class="p">,</span> <span class="n">nbins</span><span class="o">=</span><span class="mi">500</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">pd</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="ARIMA.forecast_ahead_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.BSTS.ARIMA.forecast_ahead_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">sim_vector</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inference</span><span class="p">(</span><span class="n">steps</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">sample</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">sim_vector</span><span class="p">):</span>
<span class="n">pd</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s1">&#39;histogram&#39;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">sample</span><span class="p">,</span> <span class="n">nbins</span><span class="o">=</span><span class="mi">500</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">pd</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.BSTS</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,666 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.benchmarks.Measures &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.Measures</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.benchmarks.Measures</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf8 -*-</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">pyFTS module for common benchmark metrics</span>
<span class="sd">&quot;&quot;&quot;</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">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">SortedCollection</span>
<span class="kn">from</span> <span class="nn">pyFTS.probabilistic</span> <span class="k">import</span> <span class="n">ProbabilityDistribution</span>
<div class="viewcode-block" id="acf"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.acf">[docs]</a><span class="k">def</span> <span class="nf">acf</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Autocorrelation function estimative</span>
<span class="sd"> :param data: </span>
<span class="sd"> :param k: </span>
<span class="sd"> :return: </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">n</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="n">s</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">t</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">n</span> <span class="o">-</span> <span class="n">k</span><span class="p">):</span>
<span class="n">s</span> <span class="o">+=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">-</span> <span class="n">mu</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">t</span> <span class="o">+</span> <span class="n">k</span><span class="p">]</span> <span class="o">-</span> <span class="n">mu</span><span class="p">)</span>
<span class="k">return</span> <span class="mi">1</span> <span class="o">/</span> <span class="p">((</span><span class="n">n</span> <span class="o">-</span> <span class="n">k</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span><span class="p">)</span> <span class="o">*</span> <span class="n">s</span></div>
<div class="viewcode-block" id="rmse"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.rmse">[docs]</a><span class="k">def</span> <span class="nf">rmse</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Root Mean Squared Error</span>
<span class="sd"> :param targets: array of targets</span>
<span class="sd"> :param forecasts: array of forecasts</span>
<span class="sd"> :param order: model order</span>
<span class="sd"> :param offset: forecast offset related to target. </span>
<span class="sd"> :return: </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">targets</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">targets</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="nb">list</span><span class="p">):</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="k">if</span> <span class="n">offset</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">((</span><span class="n">targets</span><span class="p">[</span><span class="n">order</span><span class="p">:]</span> <span class="o">-</span> <span class="n">forecasts</span><span class="p">[:])</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="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">((</span><span class="n">targets</span><span class="p">[</span><span class="n">order</span><span class="o">+</span><span class="n">offset</span><span class="p">:]</span> <span class="o">-</span> <span class="n">forecasts</span><span class="p">[:</span><span class="o">-</span><span class="n">offset</span><span class="p">])</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span></div>
<div class="viewcode-block" id="nmrse"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.nmrse">[docs]</a><span class="k">def</span> <span class="nf">nmrse</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Normalized Root Mean Squared Error &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">rmse</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">offset</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">targets</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">targets</span><span class="p">))</span> <span class="c1">## normalizing in targets because on forecasts might explode to inf (when model predict a line)</span></div>
<div class="viewcode-block" id="rmse_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.rmse_interval">[docs]</a><span class="k">def</span> <span class="nf">rmse_interval</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Root Mean Squared Error</span>
<span class="sd"> :param targets: </span>
<span class="sd"> :param forecasts: </span>
<span class="sd"> :return: </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">fmean</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">((</span><span class="n">fmean</span> <span class="o">-</span> <span class="n">targets</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span></div>
<div class="viewcode-block" id="mape"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.mape">[docs]</a><span class="k">def</span> <span class="nf">mape</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Mean Average Percentual Error</span>
<span class="sd"> :param targets: </span>
<span class="sd"> :param forecasts: </span>
<span class="sd"> :return: </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">targets</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">targets</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="nb">list</span><span class="p">):</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">divide</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">),</span> <span class="n">targets</span><span class="p">)))</span> <span class="o">*</span> <span class="mi">100</span></div>
<div class="viewcode-block" id="smape"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.smape">[docs]</a><span class="k">def</span> <span class="nf">smape</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Symmetric Mean Average Percentual Error</span>
<span class="sd"> :param targets: </span>
<span class="sd"> :param forecasts: </span>
<span class="sd"> :param type: </span>
<span class="sd"> :return: </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">targets</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">targets</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="nb">list</span><span class="p">):</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">forecasts</span> <span class="o">-</span> <span class="n">targets</span><span class="p">)</span> <span class="o">/</span> <span class="p">((</span><span class="n">forecasts</span> <span class="o">+</span> <span class="n">targets</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">))</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">forecasts</span> <span class="o">-</span> <span class="n">targets</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span> <span class="o">+</span> <span class="nb">abs</span><span class="p">(</span><span class="n">targets</span><span class="p">)))</span> <span class="o">*</span> <span class="mi">100</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">forecasts</span> <span class="o">-</span> <span class="n">targets</span><span class="p">))</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">forecasts</span> <span class="o">+</span> <span class="n">targets</span><span class="p">)</span></div>
<div class="viewcode-block" id="mape_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.mape_interval">[docs]</a><span class="k">def</span> <span class="nf">mape_interval</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="n">fmean</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">fmean</span> <span class="o">-</span> <span class="n">targets</span><span class="p">)</span> <span class="o">/</span> <span class="n">fmean</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span></div>
<div class="viewcode-block" id="UStatistic"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.UStatistic">[docs]</a><span class="k">def</span> <span class="nf">UStatistic</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Theil&#39;s U Statistic</span>
<span class="sd"> :param targets: </span>
<span class="sd"> :param forecasts: </span>
<span class="sd"> :return: </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">forecasts</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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">targets</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">targets</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">targets</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">targets</span><span class="p">)</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">forecasts</span><span class="o">.</span><span class="n">size</span>
<span class="n">l</span> <span class="o">=</span> <span class="mi">2</span> <span class="k">if</span> <span class="n">l</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">l</span>
<span class="n">naive</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
<span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">forecasts</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">targets</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">naive</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">targets</span><span class="p">[</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">],</span> <span class="n">targets</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">divide</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">y</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">naive</span><span class="p">)))</span></div>
<div class="viewcode-block" id="TheilsInequality"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.TheilsInequality">[docs]</a><span class="k">def</span> <span class="nf">TheilsInequality</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Theils Inequality Coefficient</span>
<span class="sd"> :param targets: </span>
<span class="sd"> :param forecasts: </span>
<span class="sd"> :return: </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">targets</span> <span class="o">-</span> <span class="n">forecasts</span>
<span class="n">t</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">us</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="n">u</span> <span class="o">**</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">res</span><span class="p">]))</span>
<span class="n">ys</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="n">y</span> <span class="o">**</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">targets</span><span class="p">]))</span>
<span class="n">fs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="n">f</span> <span class="o">**</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">]))</span>
<span class="k">return</span> <span class="n">us</span> <span class="o">/</span> <span class="p">(</span><span class="n">ys</span> <span class="o">+</span> <span class="n">fs</span><span class="p">)</span></div>
<div class="viewcode-block" id="sharpness"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.sharpness">[docs]</a><span class="k">def</span> <span class="nf">sharpness</span><span class="p">(</span><span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Sharpness - Mean size of the intervals&quot;&quot;&quot;</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span></div>
<div class="viewcode-block" id="resolution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.resolution">[docs]</a><span class="k">def</span> <span class="nf">resolution</span><span class="p">(</span><span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Resolution - Standard deviation of the intervals&quot;&quot;&quot;</span>
<span class="n">shp</span> <span class="o">=</span> <span class="n">sharpness</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="nb">abs</span><span class="p">((</span><span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">-</span> <span class="n">shp</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">forecasts</span><span class="p">]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span></div>
<div class="viewcode-block" id="coverage"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.coverage">[docs]</a><span class="k">def</span> <span class="nf">coverage</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Percent of target values that fall inside forecasted interval&quot;&quot;&quot;</span>
<span class="n">preds</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</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="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)):</span>
<span class="k">if</span> <span class="n">targets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="n">forecasts</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="ow">and</span> <span class="n">targets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">forecasts</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]:</span>
<span class="n">preds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">preds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">preds</span><span class="p">)</span></div>
<div class="viewcode-block" id="pinball"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.pinball">[docs]</a><span class="k">def</span> <span class="nf">pinball</span><span class="p">(</span><span class="n">tau</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">forecast</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Pinball loss function. Measure the distance of forecast to the tau-quantile of the target</span>
<span class="sd"> :param tau: quantile value in the range (0,1)</span>
<span class="sd"> :param target: </span>
<span class="sd"> :param forecast: </span>
<span class="sd"> :return: float, distance of forecast to the tau-quantile of the target</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">target</span> <span class="o">&gt;=</span> <span class="n">forecast</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">forecast</span><span class="p">)</span> <span class="o">*</span> <span class="n">tau</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">forecast</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">tau</span><span class="p">)</span></div>
<div class="viewcode-block" id="pinball_mean"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.pinball_mean">[docs]</a><span class="k">def</span> <span class="nf">pinball_mean</span><span class="p">(</span><span class="n">tau</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Mean pinball loss value of the forecast for a given tau-quantile of the targets</span>
<span class="sd"> :param tau: quantile value in the range (0,1)</span>
<span class="sd"> :param targets: list of target values</span>
<span class="sd"> :param forecasts: list of prediction intervals</span>
<span class="sd"> :return: float, the pinball loss mean for tau quantile</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">tau</span> <span class="o">&lt;=</span> <span class="mf">0.5</span><span class="p">:</span>
<span class="n">preds</span> <span class="o">=</span> <span class="p">[</span><span class="n">pinball</span><span class="p">(</span><span class="n">tau</span><span class="p">,</span> <span class="n">targets</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">forecasts</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="k">for</span> <span class="n">i</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="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">))]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">preds</span> <span class="o">=</span> <span class="p">[</span><span class="n">pinball</span><span class="p">(</span><span class="n">tau</span><span class="p">,</span> <span class="n">targets</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">forecasts</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span> <span class="k">for</span> <span class="n">i</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="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">))]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">preds</span><span class="p">)</span></div>
<div class="viewcode-block" id="winkler_score"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.winkler_score">[docs]</a><span class="k">def</span> <span class="nf">winkler_score</span><span class="p">(</span><span class="n">tau</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">forecast</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> R. L. Winkler, A Decision-Theoretic Approach to Interval Estimation, J. Am. Stat. Assoc. 67 (337) (1972) 187191. doi:10.2307/2284720.</span>
<span class="sd"> :param tau:</span>
<span class="sd"> :param target:</span>
<span class="sd"> :param forecast:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">delta</span> <span class="o">=</span> <span class="n">forecast</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">forecast</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="n">forecast</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">target</span> <span class="o">&lt;=</span> <span class="n">forecast</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
<span class="k">return</span> <span class="n">delta</span>
<span class="k">elif</span> <span class="n">forecast</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">target</span><span class="p">:</span>
<span class="k">return</span> <span class="n">delta</span> <span class="o">+</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">forecast</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">target</span><span class="p">))</span> <span class="o">/</span> <span class="n">tau</span>
<span class="k">elif</span> <span class="n">forecast</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">target</span><span class="p">:</span>
<span class="k">return</span> <span class="n">delta</span> <span class="o">+</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">target</span> <span class="o">-</span> <span class="n">forecast</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span> <span class="o">/</span> <span class="n">tau</span></div>
<div class="viewcode-block" id="winkler_mean"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.winkler_mean">[docs]</a><span class="k">def</span> <span class="nf">winkler_mean</span><span class="p">(</span><span class="n">tau</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Mean Winkler score value of the forecast for a given tau-quantile of the targets</span>
<span class="sd"> :param tau: quantile value in the range (0,1)</span>
<span class="sd"> :param targets: list of target values</span>
<span class="sd"> :param forecasts: list of prediction intervals</span>
<span class="sd"> :return: float, the Winkler score mean for tau quantile</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">preds</span> <span class="o">=</span> <span class="p">[</span><span class="n">winkler_score</span><span class="p">(</span><span class="n">tau</span><span class="p">,</span> <span class="n">targets</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">forecasts</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="k">for</span> <span class="n">i</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="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">))]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">preds</span><span class="p">)</span></div>
<div class="viewcode-block" id="brier_score"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.brier_score">[docs]</a><span class="k">def</span> <span class="nf">brier_score</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">densities</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Brier Score for probabilistic forecasts.</span>
<span class="sd"> Brier (1950). &quot;Verification of Forecasts Expressed in Terms of Probability&quot;. Monthly Weather Review. 78: 13.</span>
<span class="sd"> :param targets: a list with the target values</span>
<span class="sd"> :param densities: a list with pyFTS.probabil objectsistic.ProbabilityDistribution</span>
<span class="sd"> :return: float</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">densities</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">densities</span> <span class="o">=</span> <span class="p">[</span><span class="n">densities</span><span class="p">]</span>
<span class="n">targets</span> <span class="o">=</span> <span class="p">[</span><span class="n">targets</span><span class="p">]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">densities</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">bin_index</span><span class="o">.</span><span class="n">find_le</span><span class="p">(</span><span class="n">targets</span><span class="p">[</span><span class="n">ct</span><span class="p">])</span>
<span class="n">score</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="n">d</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">bins</span> <span class="k">if</span> <span class="n">k</span> <span class="o">!=</span> <span class="n">v</span><span class="p">])</span>
<span class="n">score</span> <span class="o">+=</span> <span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">score</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="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="n">d</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">bins</span><span class="p">]))</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span></div>
<div class="viewcode-block" id="logarithm_score"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.logarithm_score">[docs]</a><span class="k">def</span> <span class="nf">logarithm_score</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">densities</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Logarithm Score for probabilistic forecasts.</span>
<span class="sd"> Good IJ (1952). “Rational Decisions.”Journal of the Royal Statistical Society B,14(1),107114. URLhttps://www.jstor.org/stable/2984087.</span>
<span class="sd"> :param targets: a list with the target values</span>
<span class="sd"> :param densities: a list with pyFTS.probabil objectsistic.ProbabilityDistribution</span>
<span class="sd"> :return: float</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">_ls</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">densities</span><span class="p">,</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">):</span>
<span class="n">densities</span> <span class="o">=</span> <span class="p">[</span><span class="n">densities</span><span class="p">]</span>
<span class="n">targets</span> <span class="o">=</span> <span class="p">[</span><span class="n">targets</span><span class="p">]</span>
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">densities</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">df</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">densities</span><span class="p">):</span>
<span class="n">_ls</span> <span class="o">+=</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">targets</span><span class="p">[</span><span class="n">ct</span><span class="p">]))</span>
<span class="k">return</span> <span class="n">_ls</span> <span class="o">/</span> <span class="n">n</span></div>
<div class="viewcode-block" id="crps"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.crps">[docs]</a><span class="k">def</span> <span class="nf">crps</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">densities</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Continuous Ranked Probability Score</span>
<span class="sd"> :param targets: a list with the target values</span>
<span class="sd"> :param densities: a list with pyFTS.probabil objectsistic.ProbabilityDistribution</span>
<span class="sd"> :return: float</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">_crps</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">densities</span><span class="p">,</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">):</span>
<span class="n">densities</span> <span class="o">=</span> <span class="p">[</span><span class="n">densities</span><span class="p">]</span>
<span class="n">targets</span> <span class="o">=</span> <span class="p">[</span><span class="n">targets</span><span class="p">]</span>
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">densities</span><span class="p">)</span>
<span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">df</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">densities</span><span class="p">):</span>
<span class="n">_crps</span> <span class="o">+=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([(</span><span class="n">df</span><span class="o">.</span><span class="n">cumulative</span><span class="p">(</span><span class="nb">bin</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="mi">1</span> <span class="k">if</span> <span class="nb">bin</span> <span class="o">&gt;=</span> <span class="n">targets</span><span class="p">[</span><span class="n">ct</span><span class="p">]</span> <span class="k">else</span> <span class="mi">0</span><span class="p">))</span> <span class="o">**</span> <span class="mi">2</span> <span class="k">for</span> <span class="nb">bin</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">bins</span><span class="p">])</span>
<span class="k">return</span> <span class="n">_crps</span> <span class="o">/</span> <span class="n">n</span></div>
<div class="viewcode-block" id="get_point_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.get_point_statistics">[docs]</a><span class="k">def</span> <span class="nf">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="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Condensate all measures for point forecasters</span>
<span class="sd"> :param data: test data</span>
<span class="sd"> :param model: FTS model with point forecasting capability</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: a list with the RMSE, SMAPE and U Statistic</span>
<span class="sd"> &quot;&quot;&quot;</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">&#39;steps_ahead&#39;</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="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;point&#39;</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">&#39;indexer&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">indexer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Multivariate data must be a Pandas DataFrame!&quot;</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
<span class="k">if</span> <span class="n">steps_ahead</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="ow">and</span> <span class="n">model</span><span class="o">.</span><span class="n">has_seasonality</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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="p">[</span><span class="n">forecasts</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasts</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">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasts</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">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">rmse</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">mape</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">UStatistic</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</span><span class="p">),</span> <span class="mi">2</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">steps_ahead_sampler</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">&#39;steps_ahead_sampler&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">nforecasts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">model</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">ndata</span><span class="p">)</span> <span class="o">-</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="n">steps_ahead_sampler</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">nforecasts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="n">start</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">+</span> <span class="n">steps_ahead</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">rmse</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="n">steps_ahead_sampler</span><span class="p">],</span> <span class="n">nforecasts</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="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">mape</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="n">steps_ahead_sampler</span><span class="p">],</span> <span class="n">nforecasts</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="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">UStatistic</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="n">steps_ahead_sampler</span><span class="p">],</span> <span class="n">nforecasts</span><span class="p">),</span> <span class="mi">2</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="get_point_ahead_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.get_point_ahead_statistics">[docs]</a><span class="k">def</span> <span class="nf">get_point_ahead_statistics</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Condensate all measures for point forecasters</span>
<span class="sd"> :param data: test data</span>
<span class="sd"> :param model: FTS model with point forecasting capability</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: a list with the RMSE, SMAPE and U Statistic</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="k">if</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="n">l</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Data and intervals have different lenghts!&quot;</span><span class="p">)</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">lag</span> <span class="ow">in</span> <span class="nb">range</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="p">{}</span>
<span class="n">datum</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span>
<span class="n">forecast</span> <span class="o">=</span> <span class="n">forecasts</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;steps&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lag</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;method&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;rmse&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">rmse</span><span class="p">(</span><span class="n">datum</span><span class="p">,</span> <span class="n">forecast</span><span class="p">)</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;mape&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mape</span><span class="p">(</span><span class="n">datum</span><span class="p">,</span> <span class="n">forecast</span><span class="p">)</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">lag</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="n">lag</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="n">lag</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="p">[</span><span class="n">datum</span><span class="p">,</span> <span class="n">datum</span><span class="p">]</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;u&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">UStatistic</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">forecast</span><span class="p">)</span>
<span class="n">lags</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span>
<span class="k">return</span> <span class="n">lags</span></div>
<div class="viewcode-block" id="get_interval_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.get_interval_statistics">[docs]</a><span class="k">def</span> <span class="nf">get_interval_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="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Condensate all measures for point interval forecasters</span>
<span class="sd"> :param data: test data</span>
<span class="sd"> :param model: FTS model with interval forecasting capability</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: a list with the sharpness, resolution, coverage, .05 pinball mean,</span>
<span class="sd"> .25 pinball mean, .75 pinball mean and .95 pinball mean.</span>
<span class="sd"> &quot;&quot;&quot;</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">&#39;steps_ahead&#39;</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="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;interval&#39;</span>
<span class="n">ret</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
<span class="k">if</span> <span class="n">steps_ahead</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">sharpness</span><span class="p">(</span><span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">resolution</span><span class="p">(</span><span class="n">forecasts</span><span class="p">),</span> <span class="mi">2</span><span class="p">))</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</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">model</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">coverage</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.25</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.75</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.95</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">winkler_mean</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">winkler_mean</span><span class="p">(</span><span class="mf">0.25</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="k">else</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">model</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">data</span><span class="p">)</span> <span class="o">-</span> <span class="n">steps_ahead</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">forecasts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="n">start</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">+</span> <span class="n">steps_ahead</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">sharpness</span><span class="p">(</span><span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">resolution</span><span class="p">(</span><span class="n">forecasts</span><span class="p">),</span> <span class="mi">2</span><span class="p">))</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</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">model</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">coverage</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.25</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.75</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pinball_mean</span><span class="p">(</span><span class="mf">0.95</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">winkler_mean</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:],</span> <span class="n">forecasts</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="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">winkler_mean</span><span class="p">(</span><span class="mf">0.25</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:],</span> <span class="n">forecasts</span><span class="p">),</span> <span class="mi">2</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="get_interval_ahead_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.get_interval_ahead_statistics">[docs]</a><span class="k">def</span> <span class="nf">get_interval_ahead_statistics</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">intervals</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Condensate all measures for point interval forecasters</span>
<span class="sd"> :param data: test data</span>
<span class="sd"> :param intervals: predicted intervals for each datapoint</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: a list with the sharpness, resolution, coverage, .05 pinball mean,</span>
<span class="sd"> .25 pinball mean, .75 pinball mean and .95 pinball mean.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">intervals</span><span class="p">)</span>
<span class="k">if</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="n">l</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Data and intervals have different lenghts!&quot;</span><span class="p">)</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">lag</span> <span class="ow">in</span> <span class="nb">range</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="p">{}</span>
<span class="n">datum</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span>
<span class="n">interval</span> <span class="o">=</span> <span class="n">intervals</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;steps&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lag</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;method&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;sharpness&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">interval</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">interval</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;coverage&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">interval</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">datum</span> <span class="o">&lt;=</span> <span class="n">interval</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">else</span> <span class="mi">0</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;pinball05&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">pinball</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">datum</span><span class="p">,</span> <span class="n">interval</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;pinball25&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">pinball</span><span class="p">(</span><span class="mf">0.25</span><span class="p">,</span> <span class="n">datum</span><span class="p">,</span> <span class="n">interval</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;pinball75&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">pinball</span><span class="p">(</span><span class="mf">0.75</span><span class="p">,</span> <span class="n">datum</span><span class="p">,</span> <span class="n">interval</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="n">ret</span><span class="p">[</span><span class="s1">&#39;pinball95&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">pinball</span><span class="p">(</span><span class="mf">0.95</span><span class="p">,</span> <span class="n">datum</span><span class="p">,</span> <span class="n">interval</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="n">ret</span><span class="p">[</span><span class="s1">&#39;winkler05&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">winkler_score</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">datum</span><span class="p">,</span> <span class="n">interval</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;winkler25&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">winkler_score</span><span class="p">(</span><span class="mf">0.25</span><span class="p">,</span> <span class="n">datum</span><span class="p">,</span> <span class="n">interval</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">lags</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span>
<span class="k">return</span> <span class="n">lags</span></div>
<div class="viewcode-block" id="get_distribution_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.get_distribution_statistics">[docs]</a><span class="k">def</span> <span class="nf">get_distribution_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="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get CRPS statistic and time for a forecasting model</span>
<span class="sd"> :param data: test data</span>
<span class="sd"> :param model: FTS model with probabilistic forecasting capability</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: a list with the CRPS and execution time</span>
<span class="sd"> &quot;&quot;&quot;</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">&#39;steps_ahead&#39;</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="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;distribution&#39;</span>
<span class="n">ret</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
<span class="k">if</span> <span class="n">steps_ahead</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">_s1</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">forecasts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">_e1</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="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</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">model</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">crps</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="mi">3</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="nb">round</span><span class="p">(</span><span class="n">_e1</span> <span class="o">-</span> <span class="n">_s1</span><span class="p">,</span> <span class="mi">3</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="nb">round</span><span class="p">(</span><span class="n">brier_score</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="mi">3</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">skip</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">&#39;steps_ahead_sampler&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">_s1</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="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="n">model</span><span class="o">.</span><span class="n">max_lag</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="o">-</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="n">skip</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">forecasts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="n">_e1</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">start</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">+</span> <span class="n">steps_ahead</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</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">model</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">crps</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="n">skip</span><span class="p">],</span> <span class="n">forecasts</span><span class="p">),</span> <span class="mi">3</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="nb">round</span><span class="p">(</span><span class="n">_e1</span> <span class="o">-</span> <span class="n">_s1</span><span class="p">,</span> <span class="mi">3</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="nb">round</span><span class="p">(</span><span class="n">brier_score</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="n">skip</span><span class="p">],</span> <span class="n">forecasts</span><span class="p">),</span> <span class="mi">3</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="get_distribution_ahead_statistics"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Measures.get_distribution_ahead_statistics">[docs]</a><span class="k">def</span> <span class="nf">get_distribution_ahead_statistics</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">distributions</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get CRPS statistic and time for a forecasting model</span>
<span class="sd"> :param data: test data</span>
<span class="sd"> :param model: FTS model with probabilistic forecasting capability</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: a list with the CRPS and execution time</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">distributions</span><span class="p">)</span>
<span class="k">if</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="n">l</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Data and distributions have different lenghts!&quot;</span><span class="p">)</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">lag</span> <span class="ow">in</span> <span class="nb">range</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="p">{}</span>
<span class="n">datum</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">distributions</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;steps&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lag</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;method&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;crps&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">crps</span><span class="p">(</span><span class="n">datum</span><span class="p">,</span> <span class="n">dist</span><span class="p">),</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;brier&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">brier_score</span><span class="p">(</span><span class="n">datum</span><span class="p">,</span> <span class="n">dist</span><span class="p">),</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">ret</span><span class="p">[</span><span class="s1">&#39;log&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">logarithm_score</span><span class="p">(</span><span class="n">datum</span><span class="p">,</span> <span class="n">dist</span><span class="p">),</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">lags</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span>
<span class="k">return</span> <span class="n">lags</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.Measures</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,212 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.benchmarks.ResidualAnalysis &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.ResidualAnalysis</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.benchmarks.ResidualAnalysis</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf8 -*-</span>
<span class="sd">&quot;&quot;&quot;Residual Analysis methods&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">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.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Transformations</span><span class="p">,</span><span class="n">Util</span>
<span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span>
<span class="kn">from</span> <span class="nn">scipy</span> <span class="k">import</span> <span class="n">stats</span>
<div class="viewcode-block" id="residuals"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.ResidualAnalysis.residuals">[docs]</a><span class="k">def</span> <span class="nf">residuals</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</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="sd">&quot;&quot;&quot;First order residuals&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">targets</span><span class="p">[</span><span class="n">order</span><span class="p">:])</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">forecasts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span></div>
<div class="viewcode-block" id="ljung_box_test"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.ResidualAnalysis.ljung_box_test">[docs]</a><span class="k">def</span> <span class="nf">ljung_box_test</span><span class="p">(</span><span class="n">residuals</span><span class="p">,</span> <span class="n">lags</span><span class="o">=</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">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">):</span>
<span class="kn">from</span> <span class="nn">statsmodels.stats.diagnostic</span> <span class="k">import</span> <span class="n">acorr_ljungbox</span>
<span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="k">import</span> <span class="n">chi2</span>
<span class="n">stat</span><span class="p">,</span> <span class="n">pval</span> <span class="o">=</span> <span class="n">acorr_ljungbox</span><span class="p">(</span><span class="n">residuals</span><span class="p">,</span> <span class="n">lags</span><span class="o">=</span><span class="n">lags</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">ct</span><span class="p">,</span> <span class="n">Q</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">stat</span><span class="p">):</span>
<span class="n">lag</span> <span class="o">=</span> <span class="n">ct</span><span class="o">+</span><span class="mi">1</span>
<span class="n">p_value</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">chi2</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">Q</span><span class="p">,</span> <span class="n">df</span><span class="o">=</span><span class="n">lag</span><span class="p">)</span>
<span class="n">critical_value</span> <span class="o">=</span> <span class="n">chi2</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">df</span><span class="o">=</span><span class="n">lag</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">lag</span><span class="p">,</span> <span class="n">Q</span><span class="p">,</span> <span class="n">p_value</span><span class="p">,</span> <span class="n">critical_value</span><span class="p">,</span> <span class="s1">&#39;H0 accepted&#39;</span> <span class="k">if</span> <span class="n">Q</span> <span class="o">&gt;</span> <span class="n">critical_value</span> <span class="k">else</span> <span class="s1">&#39;H0 rejected&#39;</span><span class="p">])</span>
<span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Lag&#39;</span><span class="p">,</span><span class="s1">&#39;Statistic&#39;</span><span class="p">,</span><span class="s1">&#39;p-Value&#39;</span><span class="p">,</span><span class="s1">&#39;Critical Value&#39;</span><span class="p">,</span> <span class="s1">&#39;Result&#39;</span><span class="p">])</span></div>
<div class="viewcode-block" id="compare_residuals"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.ResidualAnalysis.compare_residuals">[docs]</a><span class="k">def</span> <span class="nf">compare_residuals</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">alpha</span><span class="o">=.</span><span class="mi">05</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Compare residual&#39;s statistics of several models</span>
<span class="sd"> :param data: test data</span>
<span class="sd"> :param models: </span>
<span class="sd"> :return: a Pandas dataframe with the Box-Ljung statistic for each model</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">statsmodels.stats.diagnostic</span> <span class="k">import</span> <span class="n">acorr_ljungbox</span>
<span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Model&quot;</span><span class="p">,</span><span class="s2">&quot;Order&quot;</span><span class="p">,</span><span class="s2">&quot;AVG&quot;</span><span class="p">,</span><span class="s2">&quot;STD&quot;</span><span class="p">,</span><span class="s2">&quot;Box-Ljung&quot;</span><span class="p">,</span><span class="s2">&quot;p-value&quot;</span><span class="p">,</span><span class="s2">&quot;Result&quot;</span><span class="p">]</span>
<span class="k">for</span> <span class="n">mfts</span> <span class="ow">in</span> <span class="n">models</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">residuals</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">forecasts</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="p">)</span>
<span class="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">sig</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">row</span> <span class="o">=</span> <span class="p">[</span><span class="n">mfts</span><span class="o">.</span><span class="n">shortname</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="n">mu</span><span class="p">,</span> <span class="n">sig</span><span class="p">]</span>
<span class="n">stat</span><span class="p">,</span> <span class="n">pval</span> <span class="o">=</span> <span class="n">acorr_ljungbox</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">test</span> <span class="o">=</span> <span class="s1">&#39;H0 Accepted&#39;</span> <span class="k">if</span> <span class="n">pval</span> <span class="o">&gt;</span> <span class="n">alpha</span> <span class="k">else</span> <span class="s1">&#39;H0 Rejected&#39;</span>
<span class="n">row</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">stat</span><span class="p">,</span> <span class="n">pval</span><span class="p">,</span> <span class="n">test</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">row</span><span class="p">)</span>
<span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span></div>
<div class="viewcode-block" id="plot_residuals_by_model"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.ResidualAnalysis.plot_residuals_by_model">[docs]</a><span class="k">def</span> <span class="nf">plot_residuals_by_model</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">models</span><span class="p">,</span> <span class="n">tam</span><span class="o">=</span><span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="mi">8</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="kn">import</span> <span class="nn">scipy</span> <span class="k">as</span> <span class="nn">sp</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="nb">len</span><span class="p">(</span><span class="n">models</span><span class="p">),</span> <span class="n">ncols</span><span class="o">=</span><span class="mi">4</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="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">mfts</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="nb">len</span><span class="p">(</span><span class="n">models</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">axes</span><span class="p">[</span><span class="n">c</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">axes</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">mfts</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">targets</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">residuals</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">forecasts</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="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">sig</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">ax</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="s2">&quot;Residuals&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="n">mfts</span><span class="o">.</span><span class="n">shortname</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
<span class="n">ax</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">res</span><span class="p">)</span>
<span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">ax</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="s2">&quot;Autocorrelation&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;ACS&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Lag&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">acorr</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">ax</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="s2">&quot;Histogram&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Freq&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Bins&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">ax</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="s2">&quot;QQ Plot&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">_</span><span class="p">,</span> <span class="p">(</span><span class="n">__</span><span class="p">,</span> <span class="n">___</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span> <span class="o">=</span> <span class="n">sp</span><span class="o">.</span><span class="n">stats</span><span class="o">.</span><span class="n">probplot</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">plot</span><span class="o">=</span><span class="n">ax</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">fit</span><span class="o">=</span><span class="kc">True</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">Util</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>
<div class="viewcode-block" id="single_plot_residuals"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.ResidualAnalysis.single_plot_residuals">[docs]</a><span class="k">def</span> <span class="nf">single_plot_residuals</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">order</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">7</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="kn">import</span> <span class="nn">scipy</span> <span class="k">as</span> <span class="nn">sp</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">ax</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">2</span><span class="p">,</span> <span class="n">ncols</span><span class="o">=</span><span class="mi">2</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="p">[</span><span class="mi">0</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="s2">&quot;Residuals&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">ax</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="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">ax</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="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s2">&quot;Autocorrelation&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">ax</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="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;ACF&#39;</span><span class="p">)</span>
<span class="n">ax</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="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Lag&#39;</span><span class="p">)</span>
<span class="n">ax</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="o">.</span><span class="n">acorr</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">1</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="s2">&quot;Histogram&quot;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="s1">&#39;large&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Freq&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Bins&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">_</span><span class="p">,</span> <span class="p">(</span><span class="n">__</span><span class="p">,</span> <span class="n">___</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span> <span class="o">=</span> <span class="n">sp</span><span class="o">.</span><span class="n">stats</span><span class="o">.</span><span class="n">probplot</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">plot</span><span class="o">=</span><span class="n">ax</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">fit</span><span class="o">=</span><span class="kc">True</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">Util</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 class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.ResidualAnalysis</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,257 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.benchmarks.Tests &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.Tests</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.benchmarks.Tests</h1><div class="highlight"><pre>
<span></span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.benchmarks.Measures</span> <span class="k">import</span> <span class="n">acf</span>
<div class="viewcode-block" id="BoxPierceStatistic"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Tests.BoxPierceStatistic">[docs]</a><span class="k">def</span> <span class="nf">BoxPierceStatistic</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">h</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Q Statistic for Box-Pierce test</span>
<span class="sd"> :param data:</span>
<span class="sd"> :param h:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">n</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="n">s</span> <span class="o">=</span> <span class="mi">0</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">1</span><span class="p">,</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">acf</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span>
<span class="k">return</span> <span class="n">n</span> <span class="o">*</span> <span class="n">s</span></div>
<div class="viewcode-block" id="BoxLjungStatistic"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Tests.BoxLjungStatistic">[docs]</a><span class="k">def</span> <span class="nf">BoxLjungStatistic</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">h</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Q Statistic for LjungBox test</span>
<span class="sd"> :param data:</span>
<span class="sd"> :param h:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">n</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="n">s</span> <span class="o">=</span> <span class="mi">0</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">1</span><span class="p">,</span> <span class="n">h</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">acf</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
<span class="n">s</span> <span class="o">+=</span> <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="n">k</span><span class="p">)</span>
<span class="k">return</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">s</span></div>
<div class="viewcode-block" id="format_experiment_table"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Tests.format_experiment_table">[docs]</a><span class="k">def</span> <span class="nf">format_experiment_table</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="p">[],</span> <span class="n">replace</span><span class="o">=</span><span class="p">{},</span> <span class="n">csv</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">std</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">columns</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">datasets</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">Dataset</span><span class="o">.</span><span class="n">unique</span><span class="p">()</span>
<span class="n">models</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">Model</span><span class="o">.</span><span class="n">unique</span><span class="p">()</span>
<span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">models</span><span class="p">:</span>
<span class="n">test</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">([</span><span class="n">model</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">])</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exclude</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">False</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">test</span><span class="p">:</span>
<span class="n">columns</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="k">for</span> <span class="n">dataset</span> <span class="ow">in</span> <span class="n">datasets</span><span class="p">:</span>
<span class="n">row</span> <span class="o">=</span> <span class="p">[</span><span class="n">dataset</span><span class="p">]</span>
<span class="k">if</span> <span class="n">std</span><span class="p">:</span>
<span class="n">row_std</span> <span class="o">=</span> <span class="p">[</span><span class="n">dataset</span><span class="p">]</span>
<span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">:</span>
<span class="n">avg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="n">df</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">Dataset</span> <span class="o">==</span> <span class="n">dataset</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">Model</span> <span class="o">==</span> <span class="n">model</span><span class="p">)][</span><span class="s2">&quot;AVG&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="n">row</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">avg</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="k">if</span> <span class="n">std</span><span class="p">:</span>
<span class="n">_std</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="n">df</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">Dataset</span> <span class="o">==</span> <span class="n">dataset</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">Model</span> <span class="o">==</span> <span class="n">model</span><span class="p">)][</span><span class="s2">&quot;STD&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="n">row_std</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;(&quot;</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">_std</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</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">row</span><span class="p">)</span>
<span class="k">if</span> <span class="n">std</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">row_std</span><span class="p">)</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">columns</span><span class="p">)):</span>
<span class="k">if</span> <span class="n">columns</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="ow">in</span> <span class="n">replace</span><span class="p">:</span>
<span class="n">columns</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">replace</span><span class="p">[</span><span class="n">columns</span><span class="p">[</span><span class="n">k</span><span class="p">]]</span>
<span class="n">columns</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="s2">&quot;dataset&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">csv</span><span class="p">:</span>
<span class="n">header</span> <span class="o">=</span> <span class="s2">&quot;&quot;</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">columns</span><span class="p">)):</span>
<span class="k">if</span> <span class="n">k</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">header</span> <span class="o">+=</span> <span class="s2">&quot;,&quot;</span>
<span class="n">header</span> <span class="o">+=</span> <span class="n">columns</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
<span class="n">body</span> <span class="o">=</span> <span class="s2">&quot;&quot;</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">rows</span><span class="p">)):</span>
<span class="n">row</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">for</span> <span class="n">w</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">rows</span><span class="p">[</span><span class="n">k</span><span class="p">])):</span>
<span class="k">if</span> <span class="n">w</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">row</span> <span class="o">+=</span> <span class="s2">&quot;,&quot;</span>
<span class="n">row</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">rows</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="n">w</span><span class="p">])</span>
<span class="n">body</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
<span class="k">return</span> <span class="n">header</span> <span class="o">+</span> <span class="n">body</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="test_mean_equality"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Tests.test_mean_equality">[docs]</a><span class="k">def</span> <span class="nf">test_mean_equality</span><span class="p">(</span><span class="n">tests</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=.</span><span class="mi">05</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;friedman&#39;</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Test for the equality of the means, with alpha confidence level.</span>
<span class="sd"> H_0: There&#39;s no significant difference between the means</span>
<span class="sd"> H_1: There is at least one significant difference between the means</span>
<span class="sd"> :param tests:</span>
<span class="sd"> :param alpha:</span>
<span class="sd"> :param method:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">stac.stac</span> <span class="k">import</span> <span class="n">nonparametric_tests</span> <span class="k">as</span> <span class="n">npt</span>
<span class="n">methods</span> <span class="o">=</span> <span class="n">tests</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="n">values</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">methods</span><span class="p">:</span>
<span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tests</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="k">if</span> <span class="n">method</span><span class="o">==</span><span class="s1">&#39;quade&#39;</span><span class="p">:</span>
<span class="n">f_value</span><span class="p">,</span> <span class="n">p_value</span><span class="p">,</span> <span class="n">rankings</span><span class="p">,</span> <span class="n">pivots</span> <span class="o">=</span> <span class="n">npt</span><span class="o">.</span><span class="n">quade_test</span><span class="p">(</span><span class="o">*</span><span class="n">values</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">method</span><span class="o">==</span><span class="s1">&#39;friedman&#39;</span><span class="p">:</span>
<span class="n">f_value</span><span class="p">,</span> <span class="n">p_value</span><span class="p">,</span> <span class="n">rankings</span><span class="p">,</span> <span class="n">pivots</span> <span class="o">=</span> <span class="n">npt</span><span class="o">.</span><span class="n">friedman_aligned_ranks_test</span><span class="p">(</span><span class="o">*</span><span class="n">values</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;Unknown test method!&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;F-Value: </span><span class="si">{}</span><span class="s2"> </span><span class="se">\t</span><span class="s2">p-Value: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">f_value</span><span class="p">,</span> <span class="n">p_value</span><span class="p">))</span>
<span class="k">if</span> <span class="n">p_value</span> <span class="o">&lt;</span> <span class="n">alpha</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">H0 is rejected!</span><span class="se">\n</span><span class="s2">&quot;</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">&quot;</span><span class="se">\n</span><span class="s2">H0 is accepted!</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="n">post_hoc</span> <span class="o">=</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">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="nb">len</span><span class="p">(</span><span class="n">methods</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">methods</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">rankings</span><span class="p">[</span><span class="n">k</span><span class="p">]])</span>
<span class="n">post_hoc</span><span class="p">[</span><span class="n">methods</span><span class="p">[</span><span class="n">k</span><span class="p">]]</span> <span class="o">=</span> <span class="n">pivots</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
<span class="k">return</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;METHOD&#39;</span><span class="p">,</span> <span class="s1">&#39;RANK&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">sort_values</span><span class="p">([</span><span class="s1">&#39;RANK&#39;</span><span class="p">]),</span> <span class="n">post_hoc</span><span class="p">]</span></div>
<div class="viewcode-block" id="post_hoc_tests"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.Tests.post_hoc_tests">[docs]</a><span class="k">def</span> <span class="nf">post_hoc_tests</span><span class="p">(</span><span class="n">post_hoc</span><span class="p">,</span> <span class="n">control_method</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=.</span><span class="mi">05</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;finner&#39;</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Finner paired post-hoc test with NSFTS as control method.</span>
<span class="sd"> $H_0$: There is no significant difference between the means</span>
<span class="sd"> $H_1$: There is a significant difference between the means</span>
<span class="sd"> :param post_hoc:</span>
<span class="sd"> :param control_method:</span>
<span class="sd"> :param alpha:</span>
<span class="sd"> :param method:</span>
<span class="sd"> :return:</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">stac.stac</span> <span class="k">import</span> <span class="n">nonparametric_tests</span> <span class="k">as</span> <span class="n">npt</span>
<span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;bonferroni_dunn&#39;</span><span class="p">:</span>
<span class="n">comparisons</span><span class="p">,</span> <span class="n">z_values</span><span class="p">,</span> <span class="n">p_values</span><span class="p">,</span> <span class="n">adj_p_values</span> <span class="o">=</span> <span class="n">npt</span><span class="o">.</span><span class="n">bonferroni_dunn_test</span><span class="p">(</span><span class="n">post_hoc</span><span class="p">,</span><span class="n">control_method</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;holm&#39;</span><span class="p">:</span>
<span class="n">comparisons</span><span class="p">,</span> <span class="n">z_values</span><span class="p">,</span> <span class="n">p_values</span><span class="p">,</span> <span class="n">adj_p_values</span> <span class="o">=</span> <span class="n">npt</span><span class="o">.</span><span class="n">holm_test</span><span class="p">(</span><span class="n">post_hoc</span><span class="p">,</span><span class="n">control_method</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;finner&#39;</span><span class="p">:</span>
<span class="n">comparisons</span><span class="p">,</span> <span class="n">z_values</span><span class="p">,</span> <span class="n">p_values</span><span class="p">,</span> <span class="n">adj_p_values</span> <span class="o">=</span> <span class="n">npt</span><span class="o">.</span><span class="n">finner_test</span><span class="p">(</span><span class="n">post_hoc</span><span class="p">,</span> <span class="n">control_method</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;Unknown test method!&#39;</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">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="nb">len</span><span class="p">(</span><span class="n">comparisons</span><span class="p">)):</span>
<span class="n">test</span> <span class="o">=</span> <span class="s1">&#39;H0 Accepted&#39;</span> <span class="k">if</span> <span class="n">adj_p_values</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">alpha</span> <span class="k">else</span> <span class="s1">&#39;H0 Rejected&#39;</span>
<span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">comparisons</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">z_values</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">p_values</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">adj_p_values</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">test</span><span class="p">])</span>
<span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;COMPARISON&#39;</span><span class="p">,</span> <span class="s1">&#39;Z-VALUE&#39;</span><span class="p">,</span> <span class="s1">&#39;P-VALUE&#39;</span><span class="p">,</span> <span class="s1">&#39;ADJUSTED P-VALUE&#39;</span><span class="p">,</span> <span class="s1">&#39;Result&#39;</span><span class="p">])</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.Tests</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,311 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.benchmarks.arima &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.arima</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.benchmarks.arima</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf8 -*-</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">statsmodels.tsa.arima_model</span> <span class="k">import</span> <span class="n">ARIMA</span> <span class="k">as</span> <span class="n">stats_arima</span>
<span class="kn">import</span> <span class="nn">scipy.stats</span> <span class="k">as</span> <span class="nn">st</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">SortedCollection</span><span class="p">,</span> <span class="n">fts</span>
<span class="kn">from</span> <span class="nn">pyFTS.probabilistic</span> <span class="k">import</span> <span class="n">ProbabilityDistribution</span>
<div class="viewcode-block" id="ARIMA"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA">[docs]</a><span class="k">class</span> <span class="nc">ARIMA</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Façade for statsmodels.tsa.arima_model</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ARIMA</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;ARIMA&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Auto Regressive Integrated Moving Average&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">trained_data</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&quot;order&quot;</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</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="bp">self</span><span class="o">.</span><span class="n">_decompose_order</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_decompose_order</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">order</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">set</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">=</span> <span class="n">order</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="n">order</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">order</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">+</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">0</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;ARIMA(</span><span class="si">{}</span><span class="s2">,</span><span class="si">{}</span><span class="s2">,</span><span class="si">{}</span><span class="s2">)-</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
<div class="viewcode-block" id="ARIMA.train"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="s1">&#39;order&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="n">order</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;order&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_decompose_order</span><span class="p">(</span><span class="n">order</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">stats_arima</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">disp</span><span class="o">=</span><span class="mi">0</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="n">ex</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="o">=</span> <span class="kc">None</span></div>
<div class="viewcode-block" id="ARIMA.ar"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.ar">[docs]</a> <span class="k">def</span> <span class="nf">ar</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span><span class="o">.</span><span class="n">arparams</span><span class="p">)</span></div>
<div class="viewcode-block" id="ARIMA.ma"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.ma">[docs]</a> <span class="k">def</span> <span class="nf">ma</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span><span class="o">.</span><span class="n">maparams</span><span class="p">)</span></div>
<div class="viewcode-block" id="ARIMA.forecast"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ar</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="bp">self</span><span class="o">.</span><span class="n">ar</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">:</span> <span class="n">k</span><span class="p">])</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">)])</span> <span class="c1">#+1 to forecast one step ahead given all available lags</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">residuals</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">p</span><span class="o">-</span><span class="mi">1</span><span class="p">:]</span> <span class="o">-</span> <span class="n">ar</span>
<span class="n">ma</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="bp">self</span><span class="o">.</span><span class="n">ma</span><span class="p">(</span><span class="n">residuals</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">:</span> <span class="n">k</span><span class="p">])</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">residuals</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)])</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">ar</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:]</span> <span class="o">+</span> <span class="n">ma</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">:]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">ar</span>
<span class="c1">#ret = self.apply_inverse_transformations(ret, params=[data[self.order - 1:]]) nforecasts = np.array(forecasts)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="ARIMA.forecast_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">if</span> <span class="s1">&#39;alpha&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span><span class="mf">0.05</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span>
<span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span><span class="o">.</span><span class="n">sigma2</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">data</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">sample</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</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="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">k</span><span class="p">)]</span>
<span class="n">mean</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mean</span><span class="p">,(</span><span class="nb">list</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">mean</span> <span class="o">=</span> <span class="n">mean</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mean</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span><span class="p">)</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mean</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</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">tmp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="ARIMA.forecast_ahead_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">if</span> <span class="s1">&#39;alpha&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span><span class="mf">0.05</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span>
<span class="n">smoothing</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;smoothing&quot;</span><span class="p">,</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span><span class="o">.</span><span class="n">sigma2</span><span class="p">)</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="n">nmeans</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">steps</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">hsigma</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">k</span><span class="o">*</span><span class="n">smoothing</span><span class="p">)</span><span class="o">*</span><span class="n">sigma</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nmeans</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">hsigma</span><span class="p">)</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nmeans</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">hsigma</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">tmp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
<div class="viewcode-block" id="ARIMA.forecast_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span><span class="o">.</span><span class="n">sigma2</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">data</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</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="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">k</span><span class="p">)]</span>
<span class="n">mean</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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">mean</span> <span class="o">=</span> <span class="n">mean</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s2">&quot;histogram&quot;</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">])</span>
<span class="n">intervals</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">alpha</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">):</span>
<span class="n">qt1</span> <span class="o">=</span> <span class="n">mean</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span>
<span class="n">qt2</span> <span class="o">=</span> <span class="n">mean</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span>
<span class="n">intervals</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">qt1</span><span class="p">,</span> <span class="n">qt2</span><span class="p">])</span>
<span class="n">dist</span><span class="o">.</span><span class="n">append_interval</span><span class="p">(</span><span class="n">intervals</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="ARIMA.forecast_ahead_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.arima.ARIMA.forecast_ahead_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">smoothing</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;smoothing&quot;</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">)</span>
<span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_fit</span><span class="o">.</span><span class="n">sigma2</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">data</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">nmeans</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">steps</span><span class="p">):</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s2">&quot;histogram&quot;</span><span class="p">,</span>
<span class="n">uod</span><span class="o">=</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">])</span>
<span class="n">intervals</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">alpha</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">hsigma</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">k</span> <span class="o">*</span> <span class="n">smoothing</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nmeans</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">hsigma</span><span class="p">)</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nmeans</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">hsigma</span><span class="p">)</span>
<span class="n">intervals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="n">dist</span><span class="o">.</span><span class="n">append_interval</span><span class="p">(</span><span class="n">intervals</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.arima</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,265 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.benchmarks.knn &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.knn</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.benchmarks.knn</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf8 -*-</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">statsmodels.tsa.tsatools</span> <span class="k">import</span> <span class="n">lagmat</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">fts</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">sklearn.neighbors</span> <span class="k">import</span> <span class="n">KDTree</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">product</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.ensemble.ensemble</span> <span class="k">import</span> <span class="n">sampler</span>
<div class="viewcode-block" id="KNearestNeighbors"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors">[docs]</a><span class="k">class</span> <span class="nc">KNearestNeighbors</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A façade for sklearn.neighbors</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">KNearestNeighbors</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;kNN&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;kNN&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;K-Nearest Neighbors&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lag</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">k</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">&quot;k&quot;</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">kdtree</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">def</span> <span class="nf">_prepare_x</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">l</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="n">X</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="n">l</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
<span class="n">l</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span>
<span class="n">X</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">data</span><span class="p">[</span><span class="n">t</span> <span class="o">-</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)])</span>
<span class="k">return</span> <span class="n">X</span>
<span class="k">def</span> <span class="nf">_prepare_xy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">l</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="n">X</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">Y</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span>
<span class="n">X</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">data</span><span class="p">[</span><span class="n">t</span> <span class="o">-</span> <span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)])</span>
<span class="n">Y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">])</span>
<span class="k">return</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="n">Y</span><span class="p">)</span>
<div class="viewcode-block" id="KNearestNeighbors.train"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">X</span><span class="p">,</span><span class="n">Y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prepare_xy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">kdtree</span> <span class="o">=</span> <span class="n">KDTree</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">X</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">Y</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;kNN(</span><span class="si">{}</span><span class="s2">)-</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span></div>
<div class="viewcode-block" id="KNearestNeighbors.knn"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors.knn">[docs]</a> <span class="k">def</span> <span class="nf">knn</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">):</span>
<span class="n">X</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prepare_x</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">_</span><span class="p">,</span> <span class="n">ix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kdtree</span><span class="o">.</span><span class="n">query</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">X</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">k</span><span class="p">)</span>
<span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">ix</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span> <span class="p">]</span></div>
<div class="viewcode-block" id="KNearestNeighbors.forecast"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="p">(</span><span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">==</span> <span class="n">l</span> <span class="k">else</span> <span class="mi">0</span><span class="p">)):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">knn</span><span class="p">(</span><span class="n">sample</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">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">forecasts</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="KNearestNeighbors.forecast_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">knn</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">percentile</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="p">[</span><span class="n">alpha</span><span class="o">*</span><span class="mi">100</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">alpha</span><span class="p">)</span><span class="o">*</span><span class="mi">100</span><span class="p">])</span><span class="o">.</span><span class="n">tolist</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">i</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="KNearestNeighbors.forecast_ahead_interval"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">start</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">&#39;start&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="n">sample</span> <span class="o">=</span> <span class="p">[[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">start</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[</span><span class="n">sample</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)]</span>
<span class="c1"># Trace the possible paths</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">lags</span><span class="p">):</span>
<span class="n">forecasts</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">knn</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
<span class="n">sample</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sampler</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">arange</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">),</span> <span class="n">bounds</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="n">interval</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">percentile</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="p">[</span><span class="n">alpha</span><span class="o">*</span><span class="mi">100</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">alpha</span><span class="p">)</span><span class="o">*</span><span class="mi">100</span><span class="p">])</span><span class="o">.</span><span class="n">tolist</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">interval</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="KNearestNeighbors.forecast_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">smooth</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">&quot;smooth&quot;</span><span class="p">,</span> <span class="s2">&quot;histogram&quot;</span><span class="p">)</span>
<span class="n">uod</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_UoD</span><span class="p">()</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">knn</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="n">smooth</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="n">uod</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">forecasts</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="KNearestNeighbors.forecast_ahead_distribution"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.knn.KNearestNeighbors.forecast_ahead_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">smooth</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">&quot;smooth&quot;</span><span class="p">,</span> <span class="s2">&quot;histogram&quot;</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">start</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">&#39;start&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="n">uod</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_UoD</span><span class="p">()</span>
<span class="n">sample</span> <span class="o">=</span> <span class="p">[[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">start</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[</span><span class="n">sample</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)]</span>
<span class="c1"># Trace the possible paths</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">lags</span><span class="p">):</span>
<span class="n">forecasts</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">knn</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="n">smooth</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="n">uod</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">forecasts</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
<span class="n">sample</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sampler</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">arange</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">),</span> <span class="n">bounds</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.knn</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,109 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.benchmarks.naive &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.naive</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.benchmarks.naive</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf8 -*-</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">fts</span>
<div class="viewcode-block" id="Naive"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.naive.Naive">[docs]</a><span class="k">class</span> <span class="nc">Naive</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Naïve Forecasting method&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">Naive</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;Naive&quot;</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Naïve Model&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Naïve Model&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<div class="viewcode-block" id="Naive.forecast"><a class="viewcode-back" href="../../../pyFTS.benchmarks.html#pyFTS.benchmarks.naive.Naive.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">return</span> <span class="n">data</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.benchmarks.naive</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

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

View File

@ -1,109 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.Activations &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.Activations</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.Activations</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Activation functions for Time Series Classification</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">pyFTS</span> <span class="kn">import</span> <span class="o">*</span>
<div class="viewcode-block" id="scale"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Activations.scale">[docs]</a><span class="k">def</span> <span class="nf">scale</span><span class="p">(</span><span class="n">dist</span> <span class="p">:</span> <span class="nb">dict</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">dict</span><span class="p">:</span>
<span class="n">norm</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">dist</span><span class="o">.</span><span class="n">values</span><span class="p">()])</span>
<span class="k">return</span> <span class="p">{</span><span class="n">k</span> <span class="p">:</span> <span class="p">(</span><span class="n">v</span> <span class="o">/</span> <span class="n">norm</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="n">dist</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="p">}</span></div>
<div class="viewcode-block" id="softmax"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Activations.softmax">[docs]</a><span class="k">def</span> <span class="nf">softmax</span><span class="p">(</span><span class="n">dist</span> <span class="p">:</span> <span class="nb">dict</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">dict</span><span class="p">:</span>
<span class="n">norm</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">dist</span><span class="o">.</span><span class="n">values</span><span class="p">()])</span>
<span class="k">return</span> <span class="p">{</span><span class="n">k</span> <span class="p">:</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">/</span> <span class="n">norm</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="n">dist</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="p">}</span></div>
<div class="viewcode-block" id="argmax"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Activations.argmax">[docs]</a><span class="k">def</span> <span class="nf">argmax</span><span class="p">(</span><span class="n">dist</span> <span class="p">:</span> <span class="nb">dict</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="n">mx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">([</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">dist</span><span class="o">.</span><span class="n">values</span><span class="p">()])</span>
<span class="k">return</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="n">dist</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">v</span> <span class="o">==</span> <span class="n">mx</span> <span class="p">][</span><span class="mi">0</span><span class="p">]</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.Activations</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -48,16 +48,16 @@
<span class="sd">&quot;&quot;&quot;</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</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="kn">import</span> <span class="n">Membership</span><span class="p">,</span> <span class="n">FuzzySet</span>
<span class="kn">from</span> <span class="nn">pyFTS</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Membership</span><span class="p">,</span> <span class="n">FuzzySet</span>
<div class="viewcode-block" id="FuzzySet"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Composite.FuzzySet">[docs]</a><span class="k">class</span> <span class="nc">FuzzySet</span><span class="p">(</span><span class="n">FuzzySet</span><span class="o">.</span><span class="n">FuzzySet</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Composite Fuzzy Set</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">superset</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">superset</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create an empty composite fuzzy set</span>
<span class="sd"> :param name: fuzzy set name</span>
<span class="sd"> &quot;&quot;&quot;</span>
@ -77,7 +77,7 @@
<div class="viewcode-block" id="FuzzySet.membership"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Composite.FuzzySet.membership">[docs]</a> <span class="k">def</span> <span class="nf">membership</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Calculate the membership value of a given input</span>
<span class="sd"> :param x: input value</span>
@ -92,7 +92,7 @@
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">x</span><span class="p">)</span></div>
<div class="viewcode-block" id="FuzzySet.append"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Composite.FuzzySet.append">[docs]</a> <span class="k">def</span> <span class="nf">append</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mf</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Adds a new function to composition</span>
<span class="sd"> :param mf:</span>
@ -103,7 +103,7 @@
<span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span></div>
<div class="viewcode-block" id="FuzzySet.append_set"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Composite.FuzzySet.append_set">[docs]</a> <span class="k">def</span> <span class="nf">append_set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">set</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Adds a new function to composition</span>
<span class="sd"> :param mf:</span>
@ -122,7 +122,7 @@
<span class="bp">self</span><span class="o">.</span><span class="n">centroid</span> <span class="o">=</span> <span class="nb">set</span><span class="o">.</span><span class="n">centroid</span></div>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="nb">str</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sets</span><span class="p">])</span>
<span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">tmp</span><span class="p">)</span></div>
@ -168,7 +168,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -48,44 +48,44 @@
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="kn">import</span> <span class="n">FuzzySet</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">FuzzySet</span>
<div class="viewcode-block" id="FLR"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FLR.FLR">[docs]</a><span class="k">class</span> <span class="nc">FLR</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fuzzy Logical Relationship</span>
<span class="sd"> Represents a temporal transition of the fuzzy set LHS on time t for the fuzzy set RHS on time t+1.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">LHS</span><span class="p">,</span> <span class="n">RHS</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">LHS</span><span class="p">,</span> <span class="n">RHS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates a Fuzzy Logical Relationship</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span> <span class="o">=</span> <span class="n">LHS</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Left Hand Side fuzzy set&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Left Hand Side fuzzy set&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="n">RHS</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Right Hand Side fuzzy set&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Right Hand Side fuzzy set&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="IndexedFLR"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FLR.IndexedFLR">[docs]</a><span class="k">class</span> <span class="nc">IndexedFLR</span><span class="p">(</span><span class="n">FLR</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Season Indexed Fuzzy Logical Relationship&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">LHS</span><span class="p">,</span> <span class="n">RHS</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Season Indexed Fuzzy Logical Relationship&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">LHS</span><span class="p">,</span> <span class="n">RHS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a Season Indexed Fuzzy Logical Relationship</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="nb">super</span><span class="p">(</span><span class="n">IndexedFLR</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">LHS</span><span class="p">,</span> <span class="n">RHS</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">index</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;seasonal index&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;seasonal index&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;: &quot;</span><span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="generate_high_order_recurrent_flr"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FLR.generate_high_order_recurrent_flr">[docs]</a><span class="k">def</span> <span class="nf">generate_high_order_recurrent_flr</span><span class="p">(</span><span class="n">fuzzyData</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a ordered FLR set from a list of fuzzy sets with recurrence</span>
<span class="sd"> :param fuzzyData: ordered list of fuzzy sets</span>
@ -107,7 +107,7 @@
<div class="viewcode-block" id="generate_recurrent_flrs"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FLR.generate_recurrent_flrs">[docs]</a><span class="k">def</span> <span class="nf">generate_recurrent_flrs</span><span class="p">(</span><span class="n">fuzzyData</span><span class="p">,</span> <span class="n">steps</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a ordered FLR set from a list of fuzzy sets with recurrence</span>
<span class="sd"> :param fuzzyData: ordered list of fuzzy sets</span>
@ -127,7 +127,7 @@
<div class="viewcode-block" id="generate_non_recurrent_flrs"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FLR.generate_non_recurrent_flrs">[docs]</a><span class="k">def</span> <span class="nf">generate_non_recurrent_flrs</span><span class="p">(</span><span class="n">fuzzyData</span><span class="p">,</span> <span class="n">steps</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a ordered FLR set from a list of fuzzy sets without recurrence</span>
<span class="sd"> :param fuzzyData: ordered list of fuzzy sets</span>
@ -141,7 +141,7 @@
<div class="viewcode-block" id="generate_indexed_flrs"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FLR.generate_indexed_flrs">[docs]</a><span class="k">def</span> <span class="nf">generate_indexed_flrs</span><span class="p">(</span><span class="n">sets</span><span class="p">,</span> <span class="n">indexer</span><span class="p">,</span> <span class="n">data</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">alpha_cut</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a season-indexed ordered FLR set from a list of fuzzy sets with recurrence</span>
<span class="sd"> :param sets: fuzzy sets</span>
@ -204,7 +204,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -44,35 +44,35 @@
<h1>Source code for pyFTS.common.FuzzySet</h1><div class="highlight"><pre>
<span></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</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="kn">import</span> <span class="n">Membership</span>
<span class="kn">from</span> <span class="nn">pyFTS</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Membership</span>
<div class="viewcode-block" id="FuzzySet"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.FuzzySet">[docs]</a><span class="k">class</span> <span class="nc">FuzzySet</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fuzzy Set</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span> <span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">mf</span><span class="p">,</span> <span class="n">parameters</span> <span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">centroid</span> <span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">alpha</span> <span class="p">:</span> <span class="nb">float</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span> <span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">mf</span><span class="p">,</span> <span class="n">parameters</span> <span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">centroid</span> <span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">alpha</span> <span class="p">:</span> <span class="nb">float</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a Fuzzy Set</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="n">name</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;The fuzzy set name&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;The fuzzy set name&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mf</span> <span class="o">=</span> <span class="n">mf</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;The membership function&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;The membership function&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">parameters</span> <span class="p">:</span> <span class="nb">list</span> <span class="o">=</span> <span class="n">parameters</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;The parameters of the membership function&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;The parameters of the membership function&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">centroid</span> <span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">centroid</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;The fuzzy set center of mass (or midpoint)&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;The fuzzy set center of mass (or midpoint)&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">alpha</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;The alpha cut value&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;The alpha cut value&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="p">:</span> <span class="nb">str</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">&#39;type&#39;</span><span class="p">,</span> <span class="s1">&#39;common&#39;</span><span class="p">)</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;The fuzzy set type (common, composite, nonstationary, etc)&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;The fuzzy set type (common, composite, nonstationary, etc)&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">variable</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">&#39;variable&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;In multivariate time series, indicate for which variable this fuzzy set belogs&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;In multivariate time series, indicate for which variable this fuzzy set belogs&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">Z</span> <span class="o">=</span> <span class="kc">None</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Partition function in respect to the membership function&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Partition function in respect to the membership function&quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">parameters</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mf</span> <span class="o">==</span> <span class="n">Membership</span><span class="o">.</span><span class="n">gaussmf</span><span class="p">:</span>
@ -89,7 +89,7 @@
<span class="bp">self</span><span class="o">.</span><span class="n">metadata</span> <span class="o">=</span> <span class="p">{}</span>
<div class="viewcode-block" id="FuzzySet.transform"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.FuzzySet.transform">[docs]</a> <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Preprocess the data point for non native types</span>
<span class="sd"> :param x:</span>
@ -99,7 +99,7 @@
<span class="k">return</span> <span class="n">x</span></div>
<div class="viewcode-block" id="FuzzySet.membership"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.FuzzySet.membership">[docs]</a> <span class="k">def</span> <span class="nf">membership</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Calculate the membership value of a given input</span>
<span class="sd"> :param x: input value </span>
@ -108,7 +108,7 @@
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span></div>
<div class="viewcode-block" id="FuzzySet.partition_function"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.FuzzySet.partition_function">[docs]</a> <span class="k">def</span> <span class="nf">partition_function</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">nbins</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Calculate the partition function over the membership function.</span>
<span class="sd"> :param uod:</span>
@ -122,12 +122,12 @@
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">Z</span></div>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s2">&quot;: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mf</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;(&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parameters</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span></div>
<span class="k">def</span> <span class="nf">__binary_search</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">fuzzy_sets</span><span class="p">,</span> <span class="n">ordered_sets</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Search for elegible fuzzy sets to fuzzyfy x</span>
<span class="sd"> :param x: input value to be fuzzyfied</span>
@ -160,7 +160,7 @@
<div class="viewcode-block" id="fuzzyfy"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.fuzzyfy">[docs]</a><span class="k">def</span> <span class="nf">fuzzyfy</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A general method for fuzzyfication.</span>
<span class="sd"> :param data: input value to be fuzzyfied</span>
@ -170,7 +170,6 @@
<span class="sd"> :keyword method: the fuzzyfication method (fuzzy: all fuzzy memberships, maximum: only the maximum membership)</span>
<span class="sd"> :keyword mode: the fuzzyfication mode (sets: return the fuzzy sets names, vector: return a vector with the membership</span>
<span class="sd"> values for all fuzzy sets, both: return a list with tuples (fuzzy set, membership value) )</span>
<span class="sd"> </span>
<span class="sd"> :returns a list with the fuzzyfied values, depending on the mode</span>
<span class="sd"> &quot;&quot;&quot;</span>
@ -205,7 +204,7 @@
<div class="viewcode-block" id="set_ordered"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.set_ordered">[docs]</a><span class="k">def</span> <span class="nf">set_ordered</span><span class="p">(</span><span class="n">fuzzy_sets</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Order a fuzzy set list by their centroids</span>
<span class="sd"> :param fuzzy_sets: a dictionary where the key is the fuzzy set name and the value is the fuzzy set object.</span>
@ -217,7 +216,7 @@
<div class="viewcode-block" id="fuzzyfy_instance"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.fuzzyfy_instance">[docs]</a><span class="k">def</span> <span class="nf">fuzzyfy_instance</span><span class="p">(</span><span class="n">inst</span><span class="p">,</span> <span class="n">fuzzy_sets</span> <span class="p">:</span> <span class="nb">dict</span> <span class="p">,</span> <span class="n">ordered_sets</span> <span class="p">:</span> <span class="nb">list</span> <span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Calculate the membership values for a data point given fuzzy sets</span>
<span class="sd"> :param inst: data point</span>
@ -238,7 +237,7 @@
<div class="viewcode-block" id="fuzzyfy_instances"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.fuzzyfy_instances">[docs]</a><span class="k">def</span> <span class="nf">fuzzyfy_instances</span><span class="p">(</span><span class="n">data</span> <span class="p">:</span> <span class="nb">list</span><span class="p">,</span> <span class="n">fuzzy_sets</span> <span class="p">:</span> <span class="nb">dict</span><span class="p">,</span> <span class="n">ordered_sets</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Calculate the membership values for a data point given fuzzy sets</span>
<span class="sd"> :param inst: data point</span>
@ -256,7 +255,7 @@
<div class="viewcode-block" id="get_fuzzysets"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.get_fuzzysets">[docs]</a><span class="k">def</span> <span class="nf">get_fuzzysets</span><span class="p">(</span><span class="n">inst</span><span class="p">,</span> <span class="n">fuzzy_sets</span> <span class="p">:</span> <span class="nb">dict</span><span class="p">,</span> <span class="n">ordered_sets</span> <span class="p">:</span> <span class="nb">list</span> <span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">alpha_cut</span> <span class="p">:</span> <span class="nb">float</span> <span class="o">=</span><span class="mf">0.0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Return the fuzzy sets which membership value for a inst is greater than the alpha_cut</span>
<span class="sd"> :param inst: data point</span>
@ -279,7 +278,7 @@
<div class="viewcode-block" id="get_maximum_membership_fuzzyset"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.get_maximum_membership_fuzzyset">[docs]</a><span class="k">def</span> <span class="nf">get_maximum_membership_fuzzyset</span><span class="p">(</span><span class="n">inst</span><span class="p">,</span> <span class="n">fuzzy_sets</span><span class="p">,</span> <span class="n">ordered_sets</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">FuzzySet</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fuzzify a data point, returning the fuzzy set with maximum membership value</span>
<span class="sd"> :param inst: data point</span>
@ -295,7 +294,7 @@
<div class="viewcode-block" id="get_maximum_membership_fuzzyset_index"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.FuzzySet.get_maximum_membership_fuzzyset_index">[docs]</a><span class="k">def</span> <span class="nf">get_maximum_membership_fuzzyset_index</span><span class="p">(</span><span class="n">inst</span><span class="p">,</span> <span class="n">fuzzy_sets</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fuzzify a data point, returning the fuzzy set with maximum membership value</span>
<span class="sd"> :param inst: data point</span>
@ -395,7 +394,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -49,11 +49,11 @@
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">pyFTS</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">pyFTS</span> <span class="k">import</span> <span class="o">*</span>
<div class="viewcode-block" id="trimf"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Membership.trimf">[docs]</a><span class="k">def</span> <span class="nf">trimf</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Triangular fuzzy membership function</span>
<span class="sd"> :param x: data point</span>
@ -73,7 +73,7 @@
<div class="viewcode-block" id="trapmf"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Membership.trapmf">[docs]</a><span class="k">def</span> <span class="nf">trapmf</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Trapezoidal fuzzy membership function</span>
<span class="sd"> :param x: data point</span>
@ -94,7 +94,7 @@
<div class="viewcode-block" id="gaussmf"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Membership.gaussmf">[docs]</a><span class="k">def</span> <span class="nf">gaussmf</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Gaussian fuzzy membership function</span>
<span class="sd"> :param x: data point</span>
@ -106,7 +106,7 @@
<div class="viewcode-block" id="bellmf"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Membership.bellmf">[docs]</a><span class="k">def</span> <span class="nf">bellmf</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Bell shaped membership function</span>
<span class="sd"> :param x:</span>
@ -118,7 +118,7 @@
<div class="viewcode-block" id="sigmf"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Membership.sigmf">[docs]</a><span class="k">def</span> <span class="nf">sigmf</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Sigmoid / Logistic membership function</span>
<span class="sd"> :param x:</span>
@ -129,7 +129,7 @@
<div class="viewcode-block" id="singleton"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Membership.singleton">[docs]</a><span class="k">def</span> <span class="nf">singleton</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Singleton membership function, a single value fuzzy function</span>
<span class="sd"> :param x:</span>
@ -178,7 +178,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -43,7 +43,7 @@
<div class="body" role="main">
<h1>Source code for pyFTS.common.SortedCollection</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">bisect</span> <span class="kn">import</span> <span class="n">bisect_left</span><span class="p">,</span> <span class="n">bisect_right</span>
<span></span><span class="kn">from</span> <span class="nn">bisect</span> <span class="k">import</span> <span class="n">bisect_left</span><span class="p">,</span> <span class="n">bisect_right</span>
<span class="c1">#</span>
<span class="c1"># Original Source Code: https://code.activestate.com/recipes/577197-sortedcollection/</span>
@ -51,7 +51,7 @@
<div class="viewcode-block" id="SortedCollection"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.SortedCollection.SortedCollection">[docs]</a><span class="k">class</span> <span class="nc">SortedCollection</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;Sequence sorted by a key function.</span>
<span class="sd">&#39;&#39;&#39;Sequence sorted by a key function.</span>
<span class="sd"> SortedCollection() is much easier to work with than using bisect() directly.</span>
<span class="sd"> It supports key functions like those use in sorted(), min(), and max().</span>
@ -119,7 +119,7 @@
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="o">=</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="o">=</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_given_key</span> <span class="o">=</span> <span class="n">key</span>
<span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">)</span> <span class="k">if</span> <span class="n">key</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">key</span>
<span class="n">decorated</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">((</span><span class="n">key</span><span class="p">(</span><span class="n">item</span><span class="p">),</span> <span class="n">item</span><span class="p">)</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">)</span>
@ -145,19 +145,19 @@
<div class="viewcode-block" id="SortedCollection.copy"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.SortedCollection.SortedCollection.copy">[docs]</a> <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_key</span><span class="p">)</span></div>
<span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_items</span><span class="p">)</span>
<span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_items</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_items</span><span class="p">)</span>
<span class="k">def</span> <span class="fm">__reversed__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__reversed__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_items</span><span class="p">)</span>
<span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">(</span><span class="si">%r</span><span class="s1">, key=</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="p">(</span>
<span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_items</span><span class="p">,</span>
@ -167,7 +167,7 @@
<span class="k">def</span> <span class="nf">__reduce__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_items</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_given_key</span><span class="p">)</span>
<span class="k">def</span> <span class="fm">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
<span class="n">k</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_key</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">bisect_left</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_keys</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
<span class="n">j</span> <span class="o">=</span> <span class="n">bisect_right</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_keys</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
@ -311,7 +311,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,643 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.Util &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.Util</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.Util</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Common facilities for pyFTS</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">time</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">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</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">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>
<div class="viewcode-block" id="plot_compared_intervals_ahead"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.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">&#39;Blues&#39;</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">&quot;&quot;&quot;</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"> &quot;&quot;&quot;</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">&#39;black&#39;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">&quot;Original&quot;</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">&lt;</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">&lt;</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">&#39;F(T)&#39;</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">&#39;T&#39;</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">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.common.html#pyFTS.common.Util.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">&quot;&quot;&quot;</span>
<span class="sd"> Auxiliar function to plot_compared_intervals_ahead</span>
<span class="sd"> &quot;&quot;&quot;</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">&#39;Density&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="plot_probability_distributions"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.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="plot_distribution"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.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="sd">&quot;&quot;&quot;</span>
<span class="sd"> Plot forecasted ProbabilityDistribution objects on a matplotlib axis</span>
<span class="sd"> :param ax: matplotlib axis</span>
<span class="sd"> :param cmap: matplotlib colormap name</span>
<span class="sd"> :param probabilitydist: list of ProbabilityDistribution objects</span>
<span class="sd"> :param fig: matplotlib figure</span>
<span class="sd"> :param time_from: starting time (on x axis) to begin the plots</span>
<span class="sd"> :param reference_data:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</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">&#39;Density&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="plot_distribution2"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.plot_distribution2">[docs]</a><span class="k">def</span> <span class="nf">plot_distribution2</span><span class="p">(</span><span class="n">probabilitydist</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Plot distributions in y-axis over the time (x-axis)</span>
<span class="sd"> :param probabilitydist: the forecasted probability distributions to plot</span>
<span class="sd"> :param data: the original test sample</span>
<span class="sd"> :keyword start_at: the time index (inside of data) to start to plot the probability distributions</span>
<span class="sd"> :keyword ax: a matplotlib axis. If no value was provided a new axis is created.</span>
<span class="sd"> :keyword cmap: a matplotlib colormap name, the default value is &#39;Blues&#39;</span>
<span class="sd"> :keyword quantiles: the list of quantiles intervals to plot, e. g. [.05, .25, .75, .95]</span>
<span class="sd"> :keyword median: a boolean value indicating if the median value will be plot.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">matplotlib.colorbar</span> <span class="k">as</span> <span class="nn">cbar</span>
<span class="kn">import</span> <span class="nn">matplotlib.cm</span> <span class="k">as</span> <span class="nn">cm</span>
<span class="n">ax</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">&#39;ax&#39;</span><span class="p">,</span><span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ax</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">ax</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">1</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">15</span><span class="p">,</span> <span class="mi">5</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">probabilitydist</span><span class="p">)</span>
<span class="n">cmap</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">&#39;cmap&#39;</span><span class="p">,</span><span class="s1">&#39;Blues&#39;</span><span class="p">)</span>
<span class="n">cmap</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">start_at</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">&#39;start_at&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="o">+</span> <span class="n">start_at</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="n">l</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
<span class="n">qt</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">&#39;quantiles&#39;</span><span class="p">,</span><span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">qt</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">qt</span> <span class="o">=</span> <span class="p">[</span><span class="nb">round</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="mi">2</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="o">.</span><span class="mi">05</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="o">.</span><span class="mi">05</span><span class="p">)]</span>
<span class="n">qt</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="o">.</span><span class="mi">01</span><span class="p">)</span>
<span class="n">qt</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="o">.</span><span class="mi">99</span><span class="p">)</span>
<span class="n">lq</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">qt</span><span class="p">)</span>
<span class="n">normal</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">Normalize</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">qt</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">qt</span><span class="p">))</span>
<span class="n">scalarMap</span> <span class="o">=</span> <span class="n">cm</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">normal</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="n">cmap</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ct</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="nb">int</span><span class="p">(</span><span class="n">lq</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[[</span><span class="n">data</span><span class="p">[</span><span class="n">start_at</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="n">start_at</span><span class="p">]]]</span>
<span class="k">for</span> <span class="n">pd</span> <span class="ow">in</span> <span class="n">probabilitydist</span><span class="p">:</span>
<span class="n">qts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">quantile</span><span class="p">([</span><span class="n">qt</span><span class="p">[</span><span class="n">ct</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">qt</span><span class="p">[</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">append</span><span class="p">(</span><span class="n">qts</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">fill_between</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">[</span><span class="n">k</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">y</span><span class="p">],</span> <span class="p">[</span><span class="n">k</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">y</span><span class="p">],</span>
<span class="n">facecolor</span><span class="o">=</span><span class="n">scalarMap</span><span class="o">.</span><span class="n">to_rgba</span><span class="p">(</span><span class="n">ct</span> <span class="o">/</span> <span class="n">lq</span><span class="p">))</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;median&#39;</span><span class="p">,</span><span class="kc">True</span><span class="p">):</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">start_at</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">pd</span> <span class="ow">in</span> <span class="n">probabilitydist</span><span class="p">:</span>
<span class="n">qts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">quantile</span><span class="p">([</span><span class="o">.</span><span class="mi">5</span><span class="p">])</span>
<span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">qts</span><span class="p">[</span><span class="mi">0</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Median&#39;</span><span class="p">)</span>
<span class="n">cax</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">cbar</span><span class="o">.</span><span class="n">make_axes</span><span class="p">(</span><span class="n">ax</span><span class="p">)</span>
<span class="n">cb</span> <span class="o">=</span> <span class="n">cbar</span><span class="o">.</span><span class="n">ColorbarBase</span><span class="p">(</span><span class="n">cax</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="n">cmap</span><span class="p">,</span> <span class="n">norm</span><span class="o">=</span><span class="n">normal</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">&#39;Density&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="plot_distribution_tiled"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.plot_distribution_tiled">[docs]</a><span class="k">def</span> <span class="nf">plot_distribution_tiled</span><span class="p">(</span><span class="n">distributions</span><span class="p">,</span><span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">rows</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span><span class="n">cols</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span><span class="n">index</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">size</span><span class="o">=</span><span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">]):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Plot one distribution individually in each axis, with probability in y-axis and UoD on x-axis</span>
<span class="sd"> :param distributions:</span>
<span class="sd"> :param data:</span>
<span class="sd"> :param rows:</span>
<span class="sd"> :param cols:</span>
<span class="sd"> :param index:</span>
<span class="sd"> :param axis:</span>
<span class="sd"> :param size:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">axis</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">axis</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="n">rows</span><span class="p">,</span> <span class="n">ncols</span><span class="o">=</span><span class="n">cols</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="n">size</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ct</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">rows</span><span class="o">*</span><span class="n">cols</span><span class="p">):</span>
<span class="n">col</span> <span class="o">=</span> <span class="n">ct</span> <span class="o">%</span> <span class="n">cols</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">ct</span> <span class="o">//</span> <span class="n">cols</span>
<span class="k">if</span> <span class="n">index</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">ix</span> <span class="o">=</span> <span class="n">ct</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ix</span> <span class="o">=</span><span class="n">index</span><span class="p">[</span><span class="n">ct</span><span class="p">]</span>
<span class="n">forecast</span> <span class="o">=</span> <span class="n">distributions</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span>
<span class="n">forecast</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="n">axis</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">])</span>
<span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">axis</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span><span class="o">.</span><span class="n">axvline</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">ix</span><span class="p">])</span>
<span class="n">axis</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">&#39;t+</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ix</span><span class="p">))</span>
<span class="n">axis</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span></div>
<div class="viewcode-block" id="plot_interval"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.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">&#39;red&#39;</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">&#39;-&#39;</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="sd">&quot;&quot;&quot;</span>
<span class="sd"> Plot forecasted intervals on matplotlib</span>
<span class="sd"> :param axis: matplotlib axis</span>
<span class="sd"> :param intervals: list of forecasted intervals</span>
<span class="sd"> :param order: order of the model that create the forecasts</span>
<span class="sd"> :param label: figure label</span>
<span class="sd"> :param color: matplotlib color name</span>
<span class="sd"> :param typeonlegend:</span>
<span class="sd"> :param ls: matplotlib line style</span>
<span class="sd"> :param linewidth: matplotlib width</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</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="o">+</span><span class="mi">1</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">&quot; (Interval)&quot;</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_interval2"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.plot_interval2">[docs]</a><span class="k">def</span> <span class="nf">plot_interval2</span><span class="p">(</span><span class="n">intervals</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Plot forecasted intervals on matplotlib</span>
<span class="sd"> :param intervals: list of forecasted intervals</span>
<span class="sd"> :param data: the original test sample</span>
<span class="sd"> :keyword start_at: the time index (inside of data) to start to plot the intervals</span>
<span class="sd"> :keyword label: figure label</span>
<span class="sd"> :keyword color: matplotlib color name</span>
<span class="sd"> :keyword typeonlegend:</span>
<span class="sd"> :keyword ls: matplotlib line style</span>
<span class="sd"> :keyword linewidth: matplotlib width</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">intervals</span><span class="p">)</span>
<span class="n">nintervals</span> <span class="o">=</span> <span class="n">intervals</span>
<span class="n">start_at</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">&#39;start_at&#39;</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">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;ax&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">ax</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">ax</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">1</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">15</span><span class="p">,</span> <span class="mi">5</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">start_at</span><span class="p">):</span>
<span class="n">nintervals</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="p">[</span><span class="kc">None</span><span class="p">,</span><span class="kc">None</span><span class="p">])</span>
<span class="n">nintervals</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">start_at</span><span class="p">,</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">start_at</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="n">start_at</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">nintervals</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">nintervals</span><span class="p">]</span>
<span class="n">typeonlegend</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">&#39;typeonlegend&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="n">color</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">&#39;color&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">)</span>
<span class="n">label</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">&#39;label&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="n">linewidth</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">&#39;linewidth&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">ls</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">&#39;ls&#39;</span><span class="p">,</span><span class="s1">&#39;-&#39;</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">&quot; (Interval)&quot;</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">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">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">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></div>
<div class="viewcode-block" id="plot_rules"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.plot_rules">[docs]</a><span class="k">def</span> <span class="nf">plot_rules</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">rules_by_axis</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Plot the FLRG rules of a FTS model on a matplotlib axis</span>
<span class="sd"> :param model: FTS model</span>
<span class="sd"> :param size: figure size</span>
<span class="sd"> :param axis: matplotlib axis</span>
<span class="sd"> :param rules_by_axis: number of rules plotted by column</span>
<span class="sd"> :param columns: number of columns</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">axis</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">rules_by_axis</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">rows</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">elif</span> <span class="n">axis</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">rules_by_axis</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">rows</span> <span class="o">=</span> <span class="p">(((</span><span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">flrgs</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span><span class="o">//</span><span class="n">rules_by_axis</span><span class="p">))</span> <span class="o">//</span> <span class="n">columns</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">axis</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="n">rows</span><span class="p">,</span> <span class="n">ncols</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="n">size</span><span class="p">)</span>
<span class="k">if</span> <span class="n">rules_by_axis</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">draw_sets_on_axis</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
<span class="n">_lhs</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">is_high_order</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="n">flrgs</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">_lhs</span><span class="p">):</span>
<span class="n">xticks</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">xtickslabels</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="n">rules_by_axis</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">axis</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">colcount</span> <span class="o">=</span> <span class="p">(</span><span class="n">ct</span> <span class="o">//</span> <span class="n">rules_by_axis</span><span class="p">)</span> <span class="o">%</span> <span class="n">columns</span>
<span class="n">rowcount</span> <span class="o">=</span> <span class="p">(</span><span class="n">ct</span> <span class="o">//</span> <span class="n">rules_by_axis</span><span class="p">)</span> <span class="o">//</span> <span class="n">columns</span>
<span class="k">if</span> <span class="n">rows</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">columns</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">axis</span><span class="p">[</span><span class="n">rowcount</span><span class="p">,</span> <span class="n">colcount</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">columns</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">axis</span><span class="p">[</span><span class="n">rowcount</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">axis</span>
<span class="k">if</span> <span class="n">ct</span> <span class="o">%</span> <span class="n">rules_by_axis</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">draw_sets_on_axis</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">is_high_order</span><span class="p">:</span>
<span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="n">ct</span> <span class="o">%</span> <span class="n">rules_by_axis</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">ax</span><span class="o">.</span><span class="n">plot</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="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">xticks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">xtickslabels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="k">for</span> <span class="n">rhs</span> <span class="ow">in</span> <span class="n">flrg</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="n">dest</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">rhs</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">ax</span><span class="o">.</span><span class="n">arrow</span><span class="p">(</span><span class="n">x</span><span class="o">+.</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="mf">0.8</span><span class="p">,</span> <span class="n">dest</span> <span class="o">-</span> <span class="n">y</span><span class="p">,</span> <span class="c1">#length_includes_head=True,</span>
<span class="n">head_width</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">head_length</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="s1">&#39;full&#39;</span><span class="p">,</span> <span class="n">overhang</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
<span class="n">fc</span><span class="o">=</span><span class="s1">&#39;k&#39;</span><span class="p">,</span> <span class="n">ec</span><span class="o">=</span><span class="s1">&#39;k&#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="n">ct</span><span class="o">%</span><span class="n">rules_by_axis</span><span class="p">)</span><span class="o">*</span><span class="n">model</span><span class="o">.</span><span class="n">order</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">ct2</span><span class="p">,</span> <span class="n">lhs</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">):</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">lhs</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="n">x</span><span class="o">+</span><span class="n">ct2</span><span class="p">],</span> <span class="p">[</span><span class="n">y</span><span class="p">],</span> <span class="s1">&#39;o&#39;</span><span class="p">)</span>
<span class="n">xticks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="n">ct2</span><span class="p">)</span>
<span class="n">xtickslabels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">lhs</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ct2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</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">fs1</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="n">ct2</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">fs2</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="n">ct2</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">fs1</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">dest</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">fs2</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="n">x</span><span class="o">+</span><span class="n">ct2</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="n">x</span><span class="o">+</span><span class="n">ct2</span><span class="p">],</span> <span class="p">[</span><span class="n">y</span><span class="p">,</span><span class="n">dest</span><span class="p">],</span><span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">centroid</span>
<span class="k">for</span> <span class="n">rhs</span> <span class="ow">in</span> <span class="n">flrg</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="n">dest</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">rhs</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">ax</span><span class="o">.</span><span class="n">arrow</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span> <span class="o">-</span><span class="mi">1</span> <span class="o">+</span> <span class="o">.</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="mf">0.8</span><span class="p">,</span> <span class="n">dest</span> <span class="o">-</span> <span class="n">y</span><span class="p">,</span> <span class="c1"># length_includes_head=True,</span>
<span class="n">head_width</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">head_length</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="s1">&#39;full&#39;</span><span class="p">,</span> <span class="n">overhang</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
<span class="n">fc</span><span class="o">=</span><span class="s1">&#39;k&#39;</span><span class="p">,</span> <span class="n">ec</span><span class="o">=</span><span class="s1">&#39;k&#39;</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">xticks</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">xtickslabels</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="n">rules_by_axis</span><span class="o">*</span><span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="o">+</span><span class="mi">1</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">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>
<div class="viewcode-block" id="draw_sets_on_axis"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.draw_sets_on_axis">[docs]</a><span class="k">def</span> <span class="nf">draw_sets_on_axis</span><span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
<span class="k">if</span> <span class="n">axis</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">axis</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">1</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="n">size</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">):</span>
<span class="n">fs</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">key</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="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">fs</span><span class="o">.</span><span class="n">parameters</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">fs</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="n">axis</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">fs</span><span class="o">.</span><span class="n">centroid</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="s2">&quot;lightgray&quot;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">axis</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">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">)])</span>
<span class="n">axis</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</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">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">)))</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&#39;</span><span class="p">]</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">)</span>
<span class="n">axis</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="n">axis</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="n">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">max</span><span class="p">])</span>
<span class="n">axis</span><span class="o">.</span><span class="n">set_yticks</span><span class="p">([</span><span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">])</span>
<span class="n">axis</span><span class="o">.</span><span class="n">set_yticklabels</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="s2">&quot; - &quot;</span> <span class="o">+</span> <span class="n">k</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">])</span></div>
<span class="n">current_milli_time</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">))</span>
<div class="viewcode-block" id="uniquefilename"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.uniquefilename">[docs]</a><span class="k">def</span> <span class="nf">uniquefilename</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="k">if</span> <span class="s1">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">name</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">tmp</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">current_milli_time</span><span class="p">())</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">tmp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">name</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">current_milli_time</span><span class="p">())</span></div>
<div class="viewcode-block" id="show_and_save_image"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.show_and_save_image">[docs]</a><span class="k">def</span> <span class="nf">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">flag</span><span class="p">,</span> <span class="n">lgd</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Show and image and save on file</span>
<span class="sd"> :param fig: Matplotlib Figure object</span>
<span class="sd"> :param file: filename to save the picture</span>
<span class="sd"> :param flag: if True the image will be saved</span>
<span class="sd"> :param lgd: legend</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="k">if</span> <span class="n">flag</span><span class="p">:</span>
<span class="k">if</span> <span class="n">lgd</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">fig</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">additional_artists</span><span class="o">=</span><span class="n">lgd</span><span class="p">,</span><span class="n">bbox_inches</span><span class="o">=</span><span class="s1">&#39;tight&#39;</span><span class="p">)</span> <span class="c1">#bbox_extra_artists=(lgd,), )</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">fig</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">fig</span><span class="p">)</span></div>
<div class="viewcode-block" id="enumerate2"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.enumerate2">[docs]</a><span class="k">def</span> <span class="nf">enumerate2</span><span class="p">(</span><span class="n">xs</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">step</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">xs</span><span class="p">:</span>
<span class="k">yield</span> <span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="n">start</span> <span class="o">+=</span> <span class="n">step</span></div>
<div class="viewcode-block" id="sliding_window"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.sliding_window">[docs]</a><span class="k">def</span> <span class="nf">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="o">=</span><span class="mf">0.8</span><span class="p">,</span> <span class="n">inc</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Sliding window method of cross validation for time series</span>
<span class="sd"> :param data: the entire dataset</span>
<span class="sd"> :param windowsize: window size</span>
<span class="sd"> :param train: percentual of the window size will be used for training the models</span>
<span class="sd"> :param inc: percentual of data used for slide the window</span>
<span class="sd"> :return: window count, training set, test set</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">multivariate</span> <span class="o">=</span> <span class="kc">True</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">)</span> <span class="k">else</span> <span class="kc">False</span>
<span class="n">l</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="k">if</span> <span class="ow">not</span> <span class="n">multivariate</span> <span class="k">else</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
<span class="n">ttrain</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">windowsize</span> <span class="o">*</span> <span class="n">train</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
<span class="n">ic</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</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="mi">0</span><span class="p">))</span>
<span class="n">progressbar</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">&#39;progress&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">rng</span> <span class="o">=</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="o">-</span><span class="n">windowsize</span><span class="o">+</span><span class="n">ic</span><span class="p">,</span><span class="n">ic</span><span class="p">)</span>
<span class="k">if</span> <span class="n">progressbar</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">rng</span> <span class="o">=</span> <span class="n">tqdm</span><span class="p">(</span><span class="n">rng</span><span class="p">)</span>
<span class="k">for</span> <span class="n">count</span> <span class="ow">in</span> <span class="n">rng</span><span class="p">:</span>
<span class="k">if</span> <span class="n">count</span> <span class="o">+</span> <span class="n">windowsize</span> <span class="o">&gt;</span> <span class="n">l</span><span class="p">:</span>
<span class="n">_end</span> <span class="o">=</span> <span class="n">l</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">_end</span> <span class="o">=</span> <span class="n">count</span> <span class="o">+</span> <span class="n">windowsize</span>
<span class="k">if</span> <span class="n">multivariate</span><span class="p">:</span>
<span class="k">yield</span> <span class="p">(</span><span class="n">count</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">count</span><span class="p">:</span> <span class="n">count</span> <span class="o">+</span> <span class="n">ttrain</span><span class="p">],</span> <span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">count</span> <span class="o">+</span> <span class="n">ttrain</span><span class="p">:</span> <span class="n">_end</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">yield</span> <span class="p">(</span><span class="n">count</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="n">count</span> <span class="p">:</span> <span class="n">count</span> <span class="o">+</span> <span class="n">ttrain</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="n">count</span> <span class="o">+</span> <span class="n">ttrain</span> <span class="p">:</span> <span class="n">_end</span><span class="p">]</span> <span class="p">)</span></div>
<div class="viewcode-block" id="persist_obj"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.persist_obj">[docs]</a><span class="k">def</span> <span class="nf">persist_obj</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">file</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Persist an object on filesystem. This function depends on Dill package</span>
<span class="sd"> :param obj: object on memory</span>
<span class="sd"> :param file: file name to store the object</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">dill</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">_file</span><span class="p">:</span>
<span class="n">dill</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">_file</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="s2">&quot;File </span><span class="si">{}</span><span class="s2"> could not be saved due exception </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">ex</span><span class="p">))</span></div>
<div class="viewcode-block" id="load_obj"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.load_obj">[docs]</a><span class="k">def</span> <span class="nf">load_obj</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Load to memory an object stored filesystem. This function depends on Dill package</span>
<span class="sd"> :param file: file name where the object is stored</span>
<span class="sd"> :return: object</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">dill</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">_file</span><span class="p">:</span>
<span class="n">obj</span> <span class="o">=</span> <span class="n">dill</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">_file</span><span class="p">)</span>
<span class="k">return</span> <span class="n">obj</span></div>
<div class="viewcode-block" id="persist_env"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.persist_env">[docs]</a><span class="k">def</span> <span class="nf">persist_env</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Persist an entire environment on file. This function depends on Dill package</span>
<span class="sd"> :param file: file name to store the environment</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">dill</span>
<span class="n">dill</span><span class="o">.</span><span class="n">dump_session</span><span class="p">(</span><span class="n">file</span><span class="p">)</span></div>
<div class="viewcode-block" id="load_env"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.Util.load_env">[docs]</a><span class="k">def</span> <span class="nf">load_env</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">dill</span>
<span class="n">dill</span><span class="o">.</span><span class="n">load_session</span><span class="p">(</span><span class="n">file</span><span class="p">)</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.Util</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -47,20 +47,20 @@
<div class="viewcode-block" id="FLRG"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.flrg.FLRG">[docs]</a><span class="k">class</span> <span class="nc">FLRG</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fuzzy Logical Relationship Group</span>
<span class="sd"> Group a set of FLR&#39;s with the same LHS. Represents the temporal patterns for time t+1 (the RHS fuzzy sets)</span>
<span class="sd"> when the LHS pattern is identified on time t.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span> <span class="o">=</span> <span class="kc">None</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Left Hand Side of the rule&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Left Hand Side of the rule&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="kc">None</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Right Hand Side of the rule&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Right Hand Side of the rule&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">order</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Number of lags on LHS&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Number of lags on LHS&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lower</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">upper</span> <span class="o">=</span> <span class="kc">None</span>
@ -70,7 +70,7 @@
<span class="k">pass</span></div>
<div class="viewcode-block" id="FLRG.get_key"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.flrg.FLRG.get_key">[docs]</a> <span class="k">def</span> <span class="nf">get_key</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Returns a unique identifier for this FLRG&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Returns a unique identifier for this FLRG&quot;&quot;&quot;</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">set</span><span class="p">)):</span>
<span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span>
@ -88,7 +88,7 @@
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span></div>
<div class="viewcode-block" id="FLRG.get_membership"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.flrg.FLRG.get_membership">[docs]</a> <span class="k">def</span> <span class="nf">get_membership</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the membership value of the FLRG for the input data</span>
<span class="sd"> :param data: input data</span>
@ -104,7 +104,7 @@
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="FLRG.get_midpoint"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.flrg.FLRG.get_midpoint">[docs]</a> <span class="k">def</span> <span class="nf">get_midpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the midpoint value for the RHS fuzzy sets</span>
<span class="sd"> :param sets: fuzzy sets</span>
@ -121,7 +121,7 @@
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span></div>
<div class="viewcode-block" id="FLRG.get_lower"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.flrg.FLRG.get_lower">[docs]</a> <span class="k">def</span> <span class="nf">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the lower bound value for the RHS fuzzy sets</span>
<span class="sd"> :param sets: fuzzy sets</span>
@ -135,7 +135,7 @@
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lower</span></div>
<div class="viewcode-block" id="FLRG.get_upper"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.flrg.FLRG.get_upper">[docs]</a> <span class="k">def</span> <span class="nf">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the upper bound value for the RHS fuzzy sets</span>
<span class="sd"> :param sets: fuzzy sets</span>
@ -148,7 +148,7 @@
<span class="bp">self</span><span class="o">.</span><span class="n">upper</span> <span class="o">=</span> <span class="nb">max</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">s</span><span class="p">]]</span><span class="o">.</span><span class="n">upper</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper</span></div>
<span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span>
<div class="viewcode-block" id="FLRG.reset_calculated_values"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.flrg.FLRG.reset_calculated_values">[docs]</a> <span class="k">def</span> <span class="nf">reset_calculated_values</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
@ -198,7 +198,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,744 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.fts &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.fts</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.fts</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">Util</span>
<span class="kn">from</span> <span class="nn">pyFTS.common.transformations</span> <span class="k">import</span> <span class="n">transformation</span>
<span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">partitioner</span>
<span class="kn">from</span> <span class="nn">pyFTS.probabilistic</span> <span class="k">import</span> <span class="n">ProbabilityDistribution</span>
<div class="viewcode-block" id="FTS"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS">[docs]</a><span class="k">class</span> <span class="nc">FTS</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fuzzy Time Series object model</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a Fuzzy Time Series model</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span> <span class="nb">dict</span> <span class="o">=</span> <span class="p">{}</span>
<span class="sd">&quot;&quot;&quot;The list of Fuzzy Logical Relationship Groups - FLRG&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="nb">int</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">&#39;order&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A integer with the model order (number of past lags are used on forecasting)&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="p">:</span> <span class="nb">str</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">&#39;name&#39;</span><span class="p">,</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A string with a short name or alias for the model&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="p">:</span> <span class="nb">str</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">&#39;name&#39;</span><span class="p">,</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A string with the model name&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="p">:</span> <span class="nb">str</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">&#39;name&#39;</span><span class="p">,</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A string with the model detailed information&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_wrapper</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;Indicates that this model is a wrapper for other(s) method(s)&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if the model support orders greater than 1, default: False&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span>
<span class="sd">&quot;&quot;&quot;In high order models, this integer value indicates the minimal order supported for the model, default: 1&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_seasonality</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if the model supports seasonal indexers, default: False&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if the model supports point forecasting, default: True&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if the model supports interval forecasting, default: False&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if the model support probabilistic forecasting, default: False&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if the model support multivariate time series (Pandas DataFrame), default: False&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_clustered</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if the model support multivariate time series (Pandas DataFrame), but works like </span>
<span class="sd"> a monovariate method, default: False&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">dump</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformations</span> <span class="p">:</span> <span class="nb">list</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="p">[]</span>
<span class="sd">&quot;&quot;&quot;A list with the data transformations (common.Transformations) applied on model pre and post processing, default: []&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformations_param</span> <span class="p">:</span> <span class="nb">list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="sd">&quot;&quot;&quot;A list with the specific parameters for each data transformation&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="sd">&quot;&quot;&quot;A float with the upper limit of the Universe of Discourse, the maximal value found on training data&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="sd">&quot;&quot;&quot;A float with the lower limit of the Universe of Discourse, the minimal value found on training data&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="p">:</span> <span class="n">partitioner</span><span class="o">.</span><span class="n">Partitioner</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">&quot;partitioner&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A pyFTS.partitioners.Partitioner object with the Universe of Discourse partitioner used on the model. This is a mandatory dependecy. &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span>
<span class="bp">self</span><span class="o">.</span><span class="n">auto_update</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating that model is incremental&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating a façade for external (non-FTS) model used on benchmarks or ensembles.&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</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="s2">&quot;indexer&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;An pyFTS.models.seasonal.Indexer object for indexing the time series data&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="p">:</span> <span class="nb">bool</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">&quot;uod_clip&quot;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;Flag indicating if the test data will be clipped inside the training Universe of Discourse&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha_cut</span> <span class="p">:</span> <span class="nb">float</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">&quot;alpha_cut&quot;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A float with the minimal membership to be considered on fuzzyfication process&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lags</span> <span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</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">&quot;lags&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The list of lag indexes for high order models&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="sd">&quot;&quot;&quot;A integer indicating the largest lag used by the model. This value also indicates the minimum number of past lags </span>
<span class="sd"> needed to forecast a single step ahead&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">log</span> <span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([],</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;Datetime&quot;</span><span class="p">,</span><span class="s2">&quot;Operation&quot;</span><span class="p">,</span><span class="s2">&quot;Value&quot;</span><span class="p">])</span>
<span class="sd">&quot;&quot;&quot;&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_time_variant</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;A boolean value indicating if this model is time variant&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">standard_horizon</span> <span class="p">:</span> <span class="nb">int</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">&quot;standard_horizon&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;Standard forecasting horizon (Default: 1)&quot;&quot;&quot;</span>
<div class="viewcode-block" id="FTS.fuzzy"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.fuzzy">[docs]</a> <span class="k">def</span> <span class="nf">fuzzy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">dict</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fuzzify a data point</span>
<span class="sd"> :param data: data point</span>
<span class="sd"> :return: maximum membership fuzzy set</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">best</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;fuzzyset&quot;</span><span class="p">:</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="s2">&quot;membership&quot;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">}</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">:</span>
<span class="n">fset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">f</span><span class="p">]</span>
<span class="k">if</span> <span class="n">best</span><span class="p">[</span><span class="s2">&quot;membership&quot;</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">fset</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="n">best</span><span class="p">[</span><span class="s2">&quot;fuzzyset&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fset</span><span class="o">.</span><span class="n">name</span>
<span class="n">best</span><span class="p">[</span><span class="s2">&quot;membership&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fset</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">return</span> <span class="n">best</span></div>
<div class="viewcode-block" id="FTS.clip_uod"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.clip_uod">[docs]</a> <span class="k">def</span> <span class="nf">clip_uod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">clip</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">max</span><span class="p">)</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">clip</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ndata</span></div>
<div class="viewcode-block" id="FTS.predict"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.predict">[docs]</a> <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Forecast using trained model</span>
<span class="sd"> :param data: time series with minimal length to the order of the model</span>
<span class="sd"> :keyword type: the forecasting type, one of these values: point(default), interval, distribution or multivariate.</span>
<span class="sd"> :keyword steps_ahead: The forecasting path H, i. e., tell the model to forecast from t+1 to t+H.</span>
<span class="sd"> :keyword step_to: The forecasting step H, i. e., tell the model to forecast to t+H for each input sample </span>
<span class="sd"> :keyword start_at: in the multi step forecasting, the index of the data where to start forecasting (default value: 0)</span>
<span class="sd"> :keyword distributed: boolean, indicate if the forecasting procedure will be distributed in a dispy cluster (default value: False)</span>
<span class="sd"> :keyword nodes: a list with the dispy cluster nodes addresses</span>
<span class="sd"> :keyword explain: try to explain, step by step, the one-step-ahead point forecasting result given the input data. (default value: False)</span>
<span class="sd"> :keyword generators: for multivariate methods on multi step ahead forecasting, generators is a dict where the keys</span>
<span class="sd"> are the dataframe columun names (except the target_variable) and the values are lambda functions that</span>
<span class="sd"> accept one value (the actual value of the variable) and return the next value or trained FTS</span>
<span class="sd"> models that accept the actual values and forecast new ones.</span>
<span class="sd"> :return: a numpy array with the forecasted data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="n">kw</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_uod</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="k">if</span> <span class="s1">&#39;distributed&#39;</span> <span class="ow">in</span> <span class="n">kw</span><span class="p">:</span>
<span class="n">distributed</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;distributed&#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">distributed</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">if</span> <span class="s1">&#39;type&#39;</span> <span class="ow">in</span> <span class="n">kw</span><span class="p">:</span>
<span class="nb">type</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;type&quot;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">type</span> <span class="o">=</span> <span class="s1">&#39;point&#39;</span>
<span class="k">if</span> <span class="n">distributed</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">distributed</span> <span class="o">==</span> <span class="kc">False</span><span class="p">:</span>
<span class="n">steps_ahead</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;steps_ahead&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">step_to</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;step_to&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="n">steps_ahead</span> <span class="o">==</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">step_to</span> <span class="o">==</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">steps_ahead</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">step_to</span> <span class="o">==</span><span class="mi">1</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;point&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;interval&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;distribution&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_distribution</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;multivariate&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_multivariate</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">step_to</span> <span class="o">==</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">steps_ahead</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;point&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;interval&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead_interval</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;distribution&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead_distribution</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;multivariate&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead_multivariate</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">steps_ahead</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">step_to</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s1">&#39;point&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_step</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">step_to</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model only perform point step ahead forecasts!&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="p">[</span><span class="s1">&#39;point&#39;</span><span class="p">,</span> <span class="s1">&#39;interval&#39;</span><span class="p">,</span> <span class="s1">&#39;distribution&#39;</span><span class="p">,</span> <span class="s1">&#39;multivariate&#39;</span><span class="p">]</span><span class="o">.</span><span class="fm">__contains__</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;The argument </span><span class="se">\&#39;</span><span class="s1">type</span><span class="se">\&#39;</span><span class="s1"> has an unknown value.&#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">&#39;dispy&#39;</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span>
<span class="n">nodes</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;nodes&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">])</span>
<span class="n">num_batches</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;num_batches&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">dispy</span><span class="o">.</span><span class="n">distributed_predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kw</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="n">num_batches</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">&#39;spark&#39;</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">spark</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">spark</span><span class="o">.</span><span class="n">distributed_predict</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">ndata</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">kw</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">type</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_inverse_transformations</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:]],</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">if</span> <span class="s1">&#39;statistics&#39;</span> <span class="ow">in</span> <span class="n">kw</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;statistics&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">kw</span><span class="p">[</span><span class="s1">&#39;statistics&#39;</span><span class="p">]</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="FTS.forecast"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Point forecast one step ahead</span>
<span class="sd"> :param data: time series data with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param kwargs: model specific parameters</span>
<span class="sd"> :return: a list with the forecasted values</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model do not perform one step ahead point forecasts!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.forecast_interval"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Interval forecast one step ahead</span>
<span class="sd"> :param data: time series data with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param kwargs: model specific parameters</span>
<span class="sd"> :return: a list with the prediction intervals</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model do not perform one step ahead interval forecasts!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.forecast_distribution"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Probabilistic forecast one step ahead</span>
<span class="sd"> :param data: time series data with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param kwargs: model specific parameters</span>
<span class="sd"> :return: a list with probabilistic.ProbabilityDistribution objects representing the forecasted Probability Distributions</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model do not perform one step ahead distribution forecasts!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.forecast_multivariate"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_multivariate">[docs]</a> <span class="k">def</span> <span class="nf">forecast_multivariate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Multivariate forecast one step ahead</span>
<span class="sd"> :param data: Pandas dataframe with one column for each variable and with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param kwargs: model specific parameters</span>
<span class="sd"> :return: a Pandas Dataframe object representing the forecasted values for each variable</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model do not perform one step ahead multivariate forecasts!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.forecast_ahead"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_ahead">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Point forecast from 1 to H steps ahead, where H is given by the steps parameter</span>
<span class="sd"> :param data: time series data with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param steps: the number of steps ahead to forecast (default: 1)</span>
<span class="sd"> :keyword start_at: in the multi step forecasting, the index of the data where to start forecasting (default: 0)</span>
<span class="sd"> :return: a list with the forecasted values</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span>
<span class="k">return</span> <span class="n">data</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</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">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</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="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">steps</span><span class="o">+</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span><span class="n">k</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="nb">isinstance</span><span class="p">(</span><span class="n">tmp</span><span class="p">,(</span><span class="nb">list</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">tmp</span> <span class="o">=</span> <span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</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">tmp</span><span class="p">)</span>
<span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
<div class="viewcode-block" id="FTS.forecast_ahead_interval"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Interval forecast from 1 to H steps ahead, where H is given by the steps parameter</span>
<span class="sd"> :param data: time series data with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param steps: the number of steps ahead to forecast</span>
<span class="sd"> :keyword start_at: in the multi step forecasting, the index of the data where to start forecasting (default: 0)</span>
<span class="sd"> :return: a list with the forecasted intervals</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model do not perform multi step ahead interval forecasts!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.forecast_ahead_distribution"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_ahead_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Probabilistic forecast from 1 to H steps ahead, where H is given by the steps parameter</span>
<span class="sd"> :param data: time series data with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param steps: the number of steps ahead to forecast</span>
<span class="sd"> :keyword start_at: in the multi step forecasting, the index of the data where to start forecasting (default: 0)</span>
<span class="sd"> :return: a list with the forecasted Probability Distributions</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model do not perform multi step ahead distribution forecasts!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.forecast_ahead_multivariate"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_ahead_multivariate">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_multivariate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Multivariate forecast n step ahead</span>
<span class="sd"> :param data: Pandas dataframe with one column for each variable and with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param steps: the number of steps ahead to forecast</span>
<span class="sd"> :keyword start_at: in the multi step forecasting, the index of the data where to start forecasting (default: 0)</span>
<span class="sd"> :return: a Pandas Dataframe object representing the forecasted values for each variable</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;This model do not perform one step ahead multivariate forecasts!&#39;</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.forecast_step"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.forecast_step">[docs]</a> <span class="k">def</span> <span class="nf">forecast_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Point forecast for H steps ahead, where H is given by the step parameter</span>
<span class="sd"> :param data: time series data with the minimal length equal to the max_lag of the model</span>
<span class="sd"> :param step: the forecasting horizon (default: 1)</span>
<span class="sd"> :keyword start_at: in the multi step forecasting, the index of the data where to start forecasting (default: 0)</span>
<span class="sd"> :return: a list with the forecasted values</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">l</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="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span>
<span class="k">return</span> <span class="n">data</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</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">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span><span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_ahead</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">step</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="nb">isinstance</span><span class="p">(</span><span class="n">tmp</span><span class="p">,(</span><span class="nb">list</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">tmp</span> <span class="o">=</span> <span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</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">tmp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="FTS.train"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Method specific parameter fitting</span>
<span class="sd"> :param data: training time series data</span>
<span class="sd"> :param kwargs: Method specific parameters</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">pass</span></div>
<div class="viewcode-block" id="FTS.fit"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.fit">[docs]</a> <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Fit the model&#39;s parameters based on the training data.</span>
<span class="sd"> :param ndata: training time series data</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :keyword num_batches: split the training data in num_batches to save memory during the training process</span>
<span class="sd"> :keyword save_model: save final model on disk</span>
<span class="sd"> :keyword batch_save: save the model between each batch</span>
<span class="sd"> :keyword file_path: path to save the model</span>
<span class="sd"> :keyword distributed: boolean, indicate if the training procedure will be distributed in a dispy cluster</span>
<span class="sd"> :keyword nodes: a list with the dispy cluster nodes addresses</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">datetime</span><span class="o">,</span> <span class="nn">copy</span>
<span class="n">kw</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">ndata</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmax</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">if</span> <span class="s1">&#39;partitioner&#39;</span> <span class="ow">in</span> <span class="n">kw</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;partitioner&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_wrapper</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Fuzzy sets were not provided for the model. Use &#39;partitioner&#39; parameter. &quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="s1">&#39;order&#39;</span> <span class="ow">in</span> <span class="n">kw</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;order&#39;</span><span class="p">)</span>
<span class="n">dump</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;dump&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">num_batches</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;num_batches&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">save</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;save_model&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="c1"># save model on disk</span>
<span class="n">batch_save</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;batch_save&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="c1">#save model between batches</span>
<span class="n">file_path</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;file_path&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">distributed</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;distributed&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="k">if</span> <span class="n">distributed</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">distributed</span><span class="p">:</span>
<span class="k">if</span> <span class="n">num_batches</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">num_batches</span> <span class="o">=</span> <span class="mi">10</span>
<span class="k">if</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">&#39;dispy&#39;</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span>
<span class="n">nodes</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;nodes&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="n">train_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">&#39;train_method&#39;</span><span class="p">,</span> <span class="n">dispy</span><span class="o">.</span><span class="n">simple_model_train</span><span class="p">)</span>
<span class="n">dispy</span><span class="o">.</span><span class="n">distributed_train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">train_method</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="n">data</span><span class="p">,</span> <span class="n">num_batches</span><span class="p">,</span> <span class="p">{},</span>
<span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">&#39;spark&#39;</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">spark</span>
<span class="n">url</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">&#39;url&#39;</span><span class="p">,</span> <span class="s1">&#39;spark://127.0.0.1:7077&#39;</span><span class="p">)</span>
<span class="n">app</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">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;pyFTS&#39;</span><span class="p">)</span>
<span class="n">spark</span><span class="o">.</span><span class="n">distributed_train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">,</span> <span class="n">app</span><span class="o">=</span><span class="n">app</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="n">dump</span> <span class="o">==</span> <span class="s1">&#39;time&#39;</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;[{0: %H:%M:%S}] Start training&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()))</span>
<span class="k">if</span> <span class="n">num_batches</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_wrapper</span><span class="p">:</span>
<span class="n">n</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="n">batch_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">num_batches</span><span class="p">)</span>
<span class="n">bcount</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">rng</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">)</span>
<span class="k">if</span> <span class="n">dump</span> <span class="o">==</span> <span class="s1">&#39;tqdm&#39;</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">rng</span> <span class="o">=</span> <span class="n">tqdm</span><span class="p">(</span><span class="n">rng</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ct</span> <span class="ow">in</span> <span class="n">rng</span><span class="p">:</span>
<span class="k">if</span> <span class="n">dump</span> <span class="o">==</span> <span class="s1">&#39;time&#39;</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;[{0: %H:%M:%S}] Starting batch &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bcount</span><span class="p">))</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">mdata</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">ct</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span><span class="n">ct</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">mdata</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">ct</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="n">ct</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">mdata</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">if</span> <span class="n">batch_save</span><span class="p">:</span>
<span class="n">Util</span><span class="o">.</span><span class="n">persist_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">file_path</span><span class="p">)</span>
<span class="k">if</span> <span class="n">dump</span> <span class="o">==</span> <span class="s1">&#39;time&#39;</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;[{0: %H:%M:%S}] Finish batch &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bcount</span><span class="p">))</span>
<span class="n">bcount</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</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="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="k">if</span> <span class="n">dump</span> <span class="o">==</span> <span class="s1">&#39;time&#39;</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;[{0: %H:%M:%S}] Finish training&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()))</span>
<span class="k">if</span> <span class="n">save</span><span class="p">:</span>
<span class="n">Util</span><span class="o">.</span><span class="n">persist_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_path</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.clone_parameters"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.clone_parameters">[docs]</a> <span class="k">def</span> <span class="nf">clone_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Import the parameters values from other model</span>
<span class="sd"> :param model: a model to clone the parameters</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">partitioner</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lags</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">lags</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">shortname</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">detail</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">is_high_order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">min_order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_seasonality</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">has_seasonality</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">has_point_forecasting</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">has_interval_forecasting</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">has_probability_forecasting</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span>
<span class="bp">self</span><span class="o">.</span><span class="n">dump</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">dump</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformations</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">transformations</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformations_param</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">transformations_param</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">original_max</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">original_min</span>
<span class="bp">self</span><span class="o">.</span><span class="n">auto_update</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">auto_update</span>
<span class="bp">self</span><span class="o">.</span><span class="n">benchmark_only</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">benchmark_only</span>
<span class="bp">self</span><span class="o">.</span><span class="n">indexer</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">indexer</span></div>
<div class="viewcode-block" id="FTS.append_rule"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.append_rule">[docs]</a> <span class="k">def</span> <span class="nf">append_rule</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrg</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Append FLRG rule to the model</span>
<span class="sd"> :param flrg: rule</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span> <span class="o">=</span> <span class="n">flrg</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">RHS</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">set</span><span class="p">)):</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">flrg</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">RHS</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">flrg</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="n">value</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.merge"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.merge">[docs]</a> <span class="k">def</span> <span class="nf">merge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Merge the FLRG rules from other model</span>
<span class="sd"> :param model: source model</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">flrg</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">flrgs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_rule</span><span class="p">(</span><span class="n">flrg</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.append_transformation"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.append_transformation">[docs]</a> <span class="k">def</span> <span class="nf">append_transformation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transformation</span><span class="p">):</span>
<span class="k">if</span> <span class="n">transformation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">transformation</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.apply_transformations"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.apply_transformations">[docs]</a> <span class="k">def</span> <span class="nf">apply_transformations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">updateUoD</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Apply the data transformations for data preprocessing</span>
<span class="sd"> :param data: input data</span>
<span class="sd"> :param params: transformation parameters</span>
<span class="sd"> :param updateUoD:</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: preprocessed data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span>
<span class="k">if</span> <span class="n">updateUoD</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">min</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.1</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.9</span>
<span class="k">if</span> <span class="nb">max</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.1</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.9</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">params</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">[</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">]</span>
<span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations</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">ndata</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">params</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="p">)</span>
<span class="k">return</span> <span class="n">ndata</span></div>
<div class="viewcode-block" id="FTS.apply_inverse_transformations"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.apply_inverse_transformations">[docs]</a> <span class="k">def</span> <span class="nf">apply_inverse_transformations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">params</span><span class="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">&quot;&quot;&quot;</span>
<span class="sd"> Apply the data transformations for data postprocessing</span>
<span class="sd"> :param data: input data</span>
<span class="sd"> :param params: transformation parameters</span>
<span class="sd"> :param updateUoD:</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: postprocessed data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">params</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">]</span>
<span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations</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">ndata</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">inverse</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">params</span><span class="p">[</span><span class="n">c</span><span class="p">],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ndata</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">data</span></div>
<div class="viewcode-block" id="FTS.get_UoD"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.get_UoD">[docs]</a> <span class="k">def</span> <span class="nf">get_UoD</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">set</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the interval of the known bounds of the universe of discourse (UoD), i. e.,</span>
<span class="sd"> the known minimum and maximum values of the time series.</span>
<span class="sd"> :return: A set with the lower and the upper bounds of the UoD</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">max</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">)</span></div>
<div class="viewcode-block" id="FTS.offset"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.offset">[docs]</a> <span class="k">def</span> <span class="nf">offset</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Returns the number of lags to skip in the input test data in order to synchronize it with</span>
<span class="sd"> the forecasted values given by the predict function. This is necessary due to the order of the</span>
<span class="sd"> model, among other parameters.</span>
<span class="sd"> :return: An integer with the number of lags to skip</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">1</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> String representation of the model</span>
<span class="sd"> :return: a string containing the name of the model and the learned rules</span>
<span class="sd"> (if the model was already trained)</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s2">&quot;:</span><span class="se">\n</span><span class="s2">&quot;</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s1">&#39;common&#39;</span><span class="p">:</span>
<span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">key</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">get_midpoint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{0}{1}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">r</span><span class="p">]))</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{0}{1}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">r</span><span class="p">]))</span>
<span class="k">return</span> <span class="n">tmp</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> The length (number of rules) of the model</span>
<span class="sd"> :return: number of rules</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">)</span>
<div class="viewcode-block" id="FTS.len_total"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.len_total">[docs]</a> <span class="k">def</span> <span class="nf">len_total</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Total length of the model, adding the number of terms in all rules</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="nb">len</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">])</span></div>
<div class="viewcode-block" id="FTS.reset_calculated_values"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.reset_calculated_values">[docs]</a> <span class="k">def</span> <span class="nf">reset_calculated_values</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Reset all pre-calculated values on the FLRG&#39;s</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">for</span> <span class="n">flrg</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="p">]</span><span class="o">.</span><span class="n">reset_calculated_values</span><span class="p">()</span></div>
<div class="viewcode-block" id="FTS.append_log"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.fts.FTS.append_log">[docs]</a> <span class="k">def</span> <span class="nf">append_log</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">operation</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">pass</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.fts</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -1,122 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.boxcox &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.boxcox</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.boxcox</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common.transformations.transformation</span> <span class="kn">import</span> <span class="n">Transformation</span>
<div class="viewcode-block" id="BoxCox"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.boxcox.BoxCox">[docs]</a><span class="k">class</span> <span class="nc">BoxCox</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Box-Cox power transformation</span>
<span class="sd"> y&#39;(t) = log( y(t) )</span>
<span class="sd"> y(t) = exp( y&#39;(t) )</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">plambda</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">BoxCox</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">plambda</span> <span class="o">=</span> <span class="n">plambda</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;BoxCox&#39;</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">plambda</span>
<div class="viewcode-block" id="BoxCox.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.boxcox.BoxCox.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">plambda</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">modified</span> <span class="o">=</span> <span class="p">[(</span><span class="n">dat</span> <span class="o">**</span> <span class="bp">self</span><span class="o">.</span><span class="n">plambda</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">plambda</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">modified</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">dat</span><span class="p">)</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">modified</span><span class="p">)</span></div>
<div class="viewcode-block" id="BoxCox.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.boxcox.BoxCox.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">plambda</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">modified</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">dat</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">plambda</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">plambda</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">modified</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">dat</span><span class="p">)</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">modified</span><span class="p">)</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.boxcox</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -1,171 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.differential &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.differential</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.differential</h1><div class="highlight"><pre>
<span></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.common.transformations.transformation</span> <span class="kn">import</span> <span class="n">Transformation</span>
<div class="viewcode-block" id="Differential"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.differential.Differential">[docs]</a><span class="k">class</span> <span class="nc">Differential</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Differentiation data transform</span>
<span class="sd"> y&#39;(t) = y(t) - y(t-1)</span>
<span class="sd"> y(t) = y(t-1) + y&#39;(t)</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lag</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">Differential</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lag</span> <span class="o">=</span> <span class="n">lag</span>
<span class="bp">self</span><span class="o">.</span><span class="n">minimal_length</span> <span class="o">=</span> <span class="mi">2</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Diff&#39;</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lag</span>
<div class="viewcode-block" id="Differential.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.differential.Differential.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">if</span> <span class="n">param</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lag</span> <span class="o">=</span> <span class="n">param</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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">np</span><span class="o">.</span><span class="n">generic</span><span class="p">)):</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</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">np</span><span class="o">.</span><span class="n">generic</span><span class="p">)):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">n</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="n">diff</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">-</span> <span class="n">data</span><span class="p">[</span><span class="n">t</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">lag</span><span class="p">]</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lag</span><span class="p">,</span> <span class="n">n</span><span class="p">)]</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lag</span><span class="p">):</span> <span class="n">diff</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="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="n">diff</span></div>
<div class="viewcode-block" id="Differential.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.differential.Differential.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</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">&quot;type&quot;</span><span class="p">,</span><span class="s2">&quot;point&quot;</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="s2">&quot;steps_ahead&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</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">np</span><span class="o">.</span><span class="n">generic</span><span class="p">)):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">]</span>
<span class="n">n</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="c1"># print(n)</span>
<span class="c1"># print(len(param))</span>
<span class="k">if</span> <span class="n">steps_ahead</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s2">&quot;point&quot;</span><span class="p">:</span>
<span class="n">inc</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">+</span> <span class="n">param</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="k">for</span> <span class="n">t</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">n</span><span class="p">)]</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s2">&quot;interval&quot;</span><span class="p">:</span>
<span class="n">inc</span> <span class="o">=</span> <span class="p">[[</span><span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">param</span><span class="p">[</span><span class="n">t</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">param</span><span class="p">[</span><span class="n">t</span><span class="p">]]</span> <span class="k">for</span> <span class="n">t</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">n</span><span class="p">)]</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s2">&quot;distribution&quot;</span><span class="p">:</span>
<span class="k">for</span> <span class="n">t</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">n</span><span class="p">):</span>
<span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">]</span><span class="o">.</span><span class="n">differential_offset</span><span class="p">(</span><span class="n">param</span><span class="p">[</span><span class="n">t</span><span class="p">])</span>
<span class="n">inc</span> <span class="o">=</span> <span class="n">data</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">type</span> <span class="o">==</span> <span class="s2">&quot;point&quot;</span><span class="p">:</span>
<span class="n">inc</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">param</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">t</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="n">steps_ahead</span><span class="p">):</span>
<span class="n">inc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">+</span> <span class="n">inc</span><span class="p">[</span><span class="n">t</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s2">&quot;interval&quot;</span><span class="p">:</span>
<span class="n">inc</span> <span class="o">=</span> <span class="p">[[</span><span class="n">data</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="o">+</span> <span class="n">param</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">data</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="o">+</span> <span class="n">param</span><span class="p">[</span><span class="mi">0</span><span class="p">]]]</span>
<span class="k">for</span> <span class="n">t</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="n">steps_ahead</span><span class="p">):</span>
<span class="n">inc</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">inc</span><span class="p">[</span><span class="n">t</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">inc</span><span class="p">[</span><span class="n">t</span><span class="o">-</span><span class="mi">1</span><span class="p">])])</span>
<span class="k">elif</span> <span class="nb">type</span> <span class="o">==</span> <span class="s2">&quot;distribution&quot;</span><span class="p">:</span>
<span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">differential_offset</span><span class="p">(</span><span class="n">param</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">for</span> <span class="n">t</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="n">steps_ahead</span><span class="p">):</span>
<span class="n">ex</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">expected_value</span><span class="p">()</span>
<span class="n">data</span><span class="p">[</span><span class="n">t</span><span class="p">]</span><span class="o">.</span><span class="n">differential_offset</span><span class="p">(</span><span class="n">ex</span><span class="p">)</span>
<span class="n">inc</span> <span class="o">=</span> <span class="n">data</span>
<span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">return</span> <span class="n">inc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">inc</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.differential</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -1,113 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.normalization &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.normalization</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.normalization</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common.transformations.transformation</span> <span class="kn">import</span> <span class="n">Transformation</span>
<div class="viewcode-block" id="Normalization"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.normalization.Normalization">[docs]</a><span class="k">class</span> <span class="nc">Normalization</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">Normalization</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Normalization&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mu</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sigma</span> <span class="o">=</span> <span class="mi">0</span>
<div class="viewcode-block" id="Normalization.train"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.normalization.Normalization.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">data</span><span class="p">)</span></div>
<div class="viewcode-block" id="Normalization.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.normalization.Normalization.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="n">modified</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">mu</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">sigma</span>
<span class="k">return</span> <span class="n">modified</span></div>
<div class="viewcode-block" id="Normalization.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.normalization.Normalization.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="n">modified</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">sigma</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">mu</span>
<span class="k">return</span> <span class="n">modified</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.normalization</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -1,114 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.roi &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.roi</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.roi</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common.transformations.transformation</span> <span class="kn">import</span> <span class="n">Transformation</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="ROI"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.roi.ROI">[docs]</a><span class="k">class</span> <span class="nc">ROI</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Return of Investment (ROI) transformation. Retrieved from Sadaei and Lee (2014) - Multilayer Stock</span>
<span class="sd"> Forecasting Model Using Fuzzy Time Series</span>
<span class="sd"> y&#39;(t) = ( y(t) - y(t-1) ) / y(t-1)</span>
<span class="sd"> y(t) = ( y(t-1) * y&#39;(t) ) + y(t-1)</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ROI</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;ROI&#39;</span>
<div class="viewcode-block" id="ROI.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.roi.ROI.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="n">modified</span> <span class="o">=</span> <span class="p">[(</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</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="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">))]</span>
<span class="n">modified</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="mf">.0</span><span class="p">)</span>
<span class="k">return</span> <span class="n">modified</span></div>
<div class="viewcode-block" id="ROI.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.roi.ROI.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="n">modified</span> <span class="o">=</span> <span class="p">[(</span><span class="n">param</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">+</span> <span class="n">param</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</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="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">))]</span>
<span class="k">return</span> <span class="n">modified</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.roi</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -1,136 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.scale &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.scale</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.scale</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common.transformations.transformation</span> <span class="kn">import</span> <span class="n">Transformation</span>
<div class="viewcode-block" id="Scale"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.scale.Scale">[docs]</a><span class="k">class</span> <span class="nc">Scale</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Scale data inside a interval [min, max]</span>
<span class="sd"> </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">Scale</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data_max</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data_min</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transf_max</span> <span class="o">=</span> <span class="nb">max</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span> <span class="o">=</span> <span class="nb">min</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Scale&#39;</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">transf_max</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span><span class="p">]</span>
<div class="viewcode-block" id="Scale.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.scale.Scale.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_max</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmax</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">data_range</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_max</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_min</span>
<span class="n">transf_range</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_max</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[(</span><span class="n">k</span> <span class="o">+</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_min</span><span class="p">))</span> <span class="o">/</span> <span class="n">data_range</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="p">[</span> <span class="p">(</span><span class="n">k</span> <span class="o">*</span> <span class="n">transf_range</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">tmp</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span> <span class="o">+</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_min</span><span class="p">))</span> <span class="o">/</span> <span class="n">data_range</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="p">(</span><span class="n">tmp</span> <span class="o">*</span> <span class="n">transf_range</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span>
<span class="k">return</span> <span class="n">tmp2</span></div>
<div class="viewcode-block" id="Scale.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.scale.Scale.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">data_range</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_max</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_min</span>
<span class="n">transf_range</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_max</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="p">[(</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span><span class="p">)</span> <span class="o">/</span> <span class="n">transf_range</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[(</span><span class="n">k</span> <span class="o">*</span> <span class="n">data_range</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_min</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">tmp2</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">transf_min</span><span class="p">)</span> <span class="o">/</span> <span class="n">transf_range</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">(</span><span class="n">tmp2</span> <span class="o">*</span> <span class="n">data_range</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_min</span>
<span class="k">return</span> <span class="n">tmp</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.scale</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -1,180 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.smoothing &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.smoothing</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.smoothing</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common.transformations.transformation</span> <span class="kn">import</span> <span class="n">Transformation</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="MovingAverage"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.MovingAverage">[docs]</a><span class="k">class</span> <span class="nc">MovingAverage</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">MovingAverage</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Moving Average Smoothing&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">steps</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">&#39;steps&#39;</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<div class="viewcode-block" id="MovingAverage.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.MovingAverage.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="n">steps</span> <span class="o">=</span> <span class="n">param</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span>
<span class="n">ma</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[:</span><span class="n">steps</span><span class="p">]]</span>
<span class="n">ma</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="n">steps</span><span class="p">:</span><span class="n">k</span><span class="p">])</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">steps</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="k">return</span> <span class="n">ma</span></div>
<div class="viewcode-block" id="MovingAverage.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.MovingAverage.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">return</span> <span class="n">data</span></div></div>
<div class="viewcode-block" id="ExponentialSmoothing"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.ExponentialSmoothing">[docs]</a><span class="k">class</span> <span class="nc">ExponentialSmoothing</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ExponentialSmoothing</span><span class="p">,</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Exponential Moving Average Smoothing&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">steps</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">&#39;steps&#39;</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">beta</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">&#39;beta&#39;</span><span class="p">,</span><span class="mf">.5</span><span class="p">)</span>
<div class="viewcode-block" id="ExponentialSmoothing.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.ExponentialSmoothing.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="n">steps</span> <span class="o">=</span> <span class="n">param</span> <span class="k">if</span> <span class="n">param</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span>
<span class="n">beta</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">&#39;beta&#39;</span><span class="p">,</span><span class="kc">None</span><span class="p">)</span>
<span class="n">beta</span> <span class="o">=</span> <span class="n">beta</span> <span class="k">if</span> <span class="n">beta</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta</span>
<span class="n">mm</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[:</span><span class="n">steps</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">steps</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">ret</span> <span class="o">=</span> <span class="mi">0</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">steps</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">+=</span> <span class="p">(</span> <span class="n">beta</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">beta</span><span class="p">)</span> <span class="o">**</span> <span class="n">k</span> <span class="p">)</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="n">k</span><span class="p">]</span>
<span class="n">mm</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
<span class="k">return</span> <span class="n">mm</span></div>
<div class="viewcode-block" id="ExponentialSmoothing.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.ExponentialSmoothing.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">return</span> <span class="n">data</span></div></div>
<div class="viewcode-block" id="AveragePooling"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.AveragePooling">[docs]</a><span class="k">class</span> <span class="nc">AveragePooling</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">AveragePooling</span><span class="p">,</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Exponential Average Smoothing&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">kernel</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">&#39;kernel&#39;</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">stride</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">&#39;stride&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">padding</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">&#39;padding&#39;</span><span class="p">,</span><span class="s1">&#39;same&#39;</span><span class="p">)</span>
<div class="viewcode-block" id="AveragePooling.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.AveragePooling.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">padding</span> <span class="o">==</span> <span class="s1">&#39;same&#39;</span><span class="p">:</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="o">/</span><span class="mi">2</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="o">+</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="o">/</span><span class="mi">2</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">stride</span><span class="p">):</span>
<span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="n">i</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="p">]):</span><span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">([</span><span class="n">i</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="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">padding</span> <span class="o">==</span> <span class="s1">&#39;valid&#39;</span><span class="p">:</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</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="bp">self</span><span class="o">.</span><span class="n">stride</span><span class="p">):</span>
<span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="p">:</span><span class="n">i</span><span class="p">]))</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Invalid padding schema&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">result</span></div>
<div class="viewcode-block" id="AveragePooling.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.AveragePooling.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">return</span> <span class="n">data</span></div></div>
<div class="viewcode-block" id="MaxPooling"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.MaxPooling">[docs]</a><span class="k">class</span> <span class="nc">MaxPooling</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">MaxPooling</span><span class="p">,</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Exponential Average Smoothing&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">kernel</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">&#39;kernel&#39;</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">stride</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">&#39;stride&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">padding</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">&#39;padding&#39;</span><span class="p">,</span><span class="s1">&#39;same&#39;</span><span class="p">)</span>
<div class="viewcode-block" id="MaxPooling.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.MaxPooling.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">padding</span> <span class="o">==</span> <span class="s1">&#39;same&#39;</span><span class="p">:</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="o">/</span><span class="mi">2</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="o">+</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="o">/</span><span class="mi">2</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">stride</span><span class="p">):</span>
<span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="n">i</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="p">]):</span><span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">([</span><span class="n">i</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="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">padding</span> <span class="o">==</span> <span class="s1">&#39;valid&#39;</span><span class="p">:</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span> <span class="o">-</span> <span class="mi">1</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="bp">self</span><span class="o">.</span><span class="n">stride</span><span class="p">):</span>
<span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">kernel</span><span class="p">:</span><span class="n">i</span><span class="p">]))</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Invalid padding schema&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">result</span></div>
<div class="viewcode-block" id="MaxPooling.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.smoothing.MaxPooling.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</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="k">return</span> <span class="n">data</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.smoothing</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -1,196 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.som &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.som</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.som</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Kohonen Self Organizing Maps for Fuzzy Time Series</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="c1">#from pyFTS.models.multivariate import wmvfts</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>
<span class="kn">from</span> <span class="nn">pyFTS.common.transformations.transformation</span> <span class="kn">import</span> <span class="n">Transformation</span>
<div class="viewcode-block" id="SOMTransformation"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.som.SOMTransformation">[docs]</a><span class="k">class</span> <span class="nc">SOMTransformation</span><span class="p">(</span><span class="n">Transformation</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">grid_dimension</span><span class="p">:</span> <span class="n">Tuple</span><span class="p">,</span>
<span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">SimpSOM</span> <span class="k">as</span> <span class="nn">sps</span>
<span class="c1"># SOM attributes</span>
<span class="bp">self</span><span class="o">.</span><span class="n">load_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">&#39;loadFile&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">net</span><span class="p">:</span> <span class="n">sps</span><span class="o">.</span><span class="n">somNet</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">grid_dimension</span><span class="p">:</span> <span class="n">Tuple</span> <span class="o">=</span> <span class="n">grid_dimension</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pbc</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">&#39;PBC&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="o">=</span> <span class="kc">True</span>
<span class="c1"># debug attributes</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Kohonen Self Organizing Maps FTS&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s1">&#39;SOM-FTS&#39;</span>
<div class="viewcode-block" id="SOMTransformation.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.som.SOMTransformation.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">data</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">,</span>
<span class="n">param</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="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Transform a M-dimensional dataset into a 3-dimensional dataset, where one dimension is the endogen variable</span>
<span class="sd"> If endogen_variable = None, the last column will be the endogen_variable.</span>
<span class="sd"> Args:</span>
<span class="sd"> data (pd.DataFrame): M-Dimensional dataset</span>
<span class="sd"> endogen_variable (str): column of dataset</span>
<span class="sd"> names (Tuple): names for new columns created by SOM Transformation.</span>
<span class="sd"> param:</span>
<span class="sd"> **kwargs: params of SOM&#39;s train process</span>
<span class="sd"> percentage_train (float). Percentage of dataset that will be used for train SOM network. default: 0.7</span>
<span class="sd"> leaning_rate (float): leaning rate of SOM network. default: 0.01</span>
<span class="sd"> epochs: epochs of SOM network. default: 10000</span>
<span class="sd"> </span>
<span class="sd"> Returns:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">endogen_variable</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">&#39;endogen_variable&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">names</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">&#39;names&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">))</span>
<span class="k">if</span> <span class="n">endogen_variable</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
<span class="n">endogen_variable</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">cols</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</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">endogen_variable</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="p">[</span><span class="n">col</span> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</span> <span class="k">if</span>
<span class="n">col</span> <span class="o">!=</span> <span class="n">endogen_variable</span><span class="p">]</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">net</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">train</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">cols</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">train</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="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">new_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">project</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">cols</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="n">new_data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">new_data</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">names</span><span class="p">)</span>
<span class="n">endogen</span> <span class="o">=</span> <span class="n">endogen_variable</span> <span class="k">if</span> <span class="n">endogen_variable</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">new_data</span><span class="p">[</span><span class="n">endogen</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">endogen</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="k">return</span> <span class="n">new_data</span></div>
<span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">status</span> <span class="o">=</span> <span class="s2">&quot;is trained&quot;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">net</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">&quot;not trained&quot;</span>
<span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s1">-</span><span class="si">{</span><span class="n">status</span><span class="si">}</span><span class="s1">&#39;</span>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
<span class="k">def</span> <span class="fm">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">net</span>
<div class="viewcode-block" id="SOMTransformation.train"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.som.SOMTransformation.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">data</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">,</span>
<span class="n">percentage_train</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">.7</span><span class="p">,</span>
<span class="n">leaning_rate</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">,</span>
<span class="n">epochs</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10000</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">dropna</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">values</span>
<span class="n">limit</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">)</span> <span class="o">*</span> <span class="n">percentage_train</span><span class="p">)</span>
<span class="n">train</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[:</span><span class="n">limit</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="bp">self</span><span class="o">.</span><span class="n">grid_dimension</span>
<span class="bp">self</span><span class="o">.</span><span class="n">net</span> <span class="o">=</span> <span class="n">sps</span><span class="o">.</span><span class="n">somNet</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">train</span><span class="p">,</span> <span class="n">PBC</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">pbc</span><span class="p">,</span>
<span class="n">loadFile</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">load_file</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">startLearnRate</span><span class="o">=</span><span class="n">leaning_rate</span><span class="p">,</span>
<span class="n">epochs</span><span class="o">=</span><span class="n">epochs</span><span class="p">)</span></div>
<div class="viewcode-block" id="SOMTransformation.save_net"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.som.SOMTransformation.save_net">[docs]</a> <span class="k">def</span> <span class="nf">save_net</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">filename</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;SomNet trained&quot;</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">load_file</span> <span class="o">=</span> <span class="n">filename</span></div>
<div class="viewcode-block" id="SOMTransformation.show_grid"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.som.SOMTransformation.show_grid">[docs]</a> <span class="k">def</span> <span class="nf">show_grid</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">graph_type</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;nodes_graph&#39;</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">graph_type</span> <span class="o">==</span> <span class="s1">&#39;nodes_graph&#39;</span><span class="p">:</span>
<span class="n">colnum</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">&#39;colnum&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">nodes_graph</span><span class="p">(</span><span class="n">colnum</span><span class="o">=</span><span class="n">colnum</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">diff_graph</span><span class="p">()</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.som</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -1,126 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.common.transformations.transformation &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.transformation</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.common.transformations.transformation</h1><div class="highlight"><pre>
<span></span>
<div class="viewcode-block" id="Transformation"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.transformation.Transformation">[docs]</a><span class="k">class</span> <span class="nc">Transformation</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Data transformation used on pre and post processing of the FTS</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_invertible</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="o">=</span> <span class="kc">False</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;detemine if this transformation can be applied to multivariate data&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">minimal_length</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<div class="viewcode-block" id="Transformation.apply"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.transformation.Transformation.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Apply the transformation on input data</span>
<span class="sd"> :param data: input data</span>
<span class="sd"> :param param:</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: numpy array with transformed data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">pass</span></div>
<div class="viewcode-block" id="Transformation.inverse"><a class="viewcode-back" href="../../../../pyFTS.common.transformations.html#pyFTS.common.transformations.transformation.Transformation.inverse">[docs]</a> <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">data</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> :param data: transformed data</span>
<span class="sd"> :param param:</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return: numpy array with inverse transformed data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">pass</span></div>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.common.transformations.transformation</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>

View File

@ -47,17 +47,17 @@
<span class="sd">Tree data structure</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">reduce</span>
<span class="kn">from</span> <span class="nn">pyFTS</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="FLRGTreeNode"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.tree.FLRGTreeNode">[docs]</a><span class="k">class</span> <span class="nc">FLRGTreeNode</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Tree node for</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">isRoot</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">children</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
@ -86,13 +86,13 @@
<span class="n">tmp</span> <span class="o">=</span> <span class="n">tmp</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">child</span><span class="o">.</span><span class="n">getStr</span><span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">return</span> <span class="n">tmp</span></div>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">getStr</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span></div>
<div class="viewcode-block" id="FLRGTree"><a class="viewcode-back" href="../../../pyFTS.common.html#pyFTS.common.tree.FLRGTree">[docs]</a><span class="k">class</span> <span class="nc">FLRGTree</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Represents a FLRG set with a tree structure&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Represents a FLRG set with a tree structure&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">root</span> <span class="o">=</span> <span class="n">FLRGTreeNode</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span></div>
@ -157,7 +157,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -49,13 +49,13 @@
<span class="sd">Source: Hyndman, R.J., Time Series Data Library, http://www-personal.buseco.monash.edu.au/~hyndman/TSDL/.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.AirPassengers.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :return: numpy array</span>
@ -65,7 +65,7 @@
<span class="k">return</span> <span class="n">dat</span></div>
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.AirPassengers.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -116,7 +116,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -52,13 +52,13 @@
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.Bitcoin.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span><span class="nb">str</span><span class="o">=</span><span class="s1">&#39;AVG&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :param field: dataset field to load</span>
@ -69,7 +69,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.Bitcoin.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -120,7 +120,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -52,13 +52,13 @@
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.DowJones.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span><span class="nb">str</span><span class="o">=</span><span class="s1">&#39;AVG&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :param field: dataset field to load</span>
@ -69,7 +69,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.DowJones.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -120,7 +120,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -50,13 +50,13 @@
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.EURGBP.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span><span class="nb">str</span><span class="o">=</span><span class="s1">&#39;avg&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :param field: dataset field to load</span>
@ -67,7 +67,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.EURGBP.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -118,7 +118,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -50,13 +50,13 @@
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.EURUSD.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;avg&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :param field: dataset field to load</span>
@ -67,7 +67,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.EURUSD.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -118,7 +118,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -47,13 +47,13 @@
<span class="sd">Yearly University of Alabama enrollments from 1971 to 1992.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.Enrollments.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :return: numpy array</span>
@ -109,7 +109,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -52,13 +52,13 @@
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.Ethereum.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;AVG&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :param field: dataset field to load</span>
@ -69,7 +69,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.Ethereum.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -120,7 +120,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -50,13 +50,13 @@
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.GBPUSD.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;avg&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :param field: dataset field to load</span>
@ -67,7 +67,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.GBPUSD.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -118,7 +118,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -51,12 +51,12 @@
<span class="sd">Source: http://www.inmet.gov.br</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.INMET.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -107,7 +107,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -48,13 +48,13 @@
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.Malaysia.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;load&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :param field: dataset field to load</span>
@ -65,7 +65,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.Malaysia.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -117,7 +117,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -51,13 +51,13 @@
<span class="sd">Source: http://www.nasdaq.com/aspx/flashquotes.aspx?symbol=IXIC&amp;selected=IXIC</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.NASDAQ.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s2">&quot;avg&quot;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :param field: the dataset field name to extract</span>
@ -69,7 +69,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.NASDAQ.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -120,7 +120,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -51,13 +51,13 @@
<span class="sd">Source: http://sonda.ccst.inpe.br/</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.SONDA.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">field</span><span class="p">:</span><span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :param field: the dataset field name to extract</span>
@ -69,7 +69,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.SONDA.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -121,7 +121,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -51,13 +51,13 @@
<span class="sd">Source: https://finance.yahoo.com/quote/%5EGSPC/history?p=%5EGSPC</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.SP500.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :return: numpy array</span>
@ -67,7 +67,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.SP500.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -118,7 +118,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -50,13 +50,13 @@
<span class="sd">Source: http://www.twse.com.tw/en/products/indices/Index_Series.php</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.TAIEX.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the univariate time series data.</span>
<span class="sd"> :return: numpy array</span>
@ -67,7 +67,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.TAIEX.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -119,7 +119,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -51,18 +51,18 @@
<div class="viewcode-block" id="SignalEmulator"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.SignalEmulator">[docs]</a><span class="k">class</span> <span class="nc">SignalEmulator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Emulate a complex signal built from several additive and non-additive components</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">SignalEmulator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">components</span> <span class="o">=</span> <span class="p">[]</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Components of the signal&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Components of the signal&quot;&quot;&quot;</span>
<div class="viewcode-block" id="SignalEmulator.stationary_gaussian"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.SignalEmulator.stationary_gaussian">[docs]</a> <span class="k">def</span> <span class="nf">stationary_gaussian</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mu</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="n">sigma</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates a continuous Gaussian signal with mean mu and variance sigma.</span>
<span class="sd"> :param mu: mean</span>
@ -82,7 +82,7 @@
<span class="k">return</span> <span class="bp">self</span></div>
<div class="viewcode-block" id="SignalEmulator.incremental_gaussian"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.SignalEmulator.incremental_gaussian">[docs]</a> <span class="k">def</span> <span class="nf">incremental_gaussian</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mu</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="n">sigma</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates an additive gaussian interference on a previous signal</span>
<span class="sd"> :param mu: increment on mean</span>
@ -100,7 +100,7 @@
<span class="k">return</span> <span class="bp">self</span></div>
<div class="viewcode-block" id="SignalEmulator.periodic_gaussian"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.SignalEmulator.periodic_gaussian">[docs]</a> <span class="k">def</span> <span class="nf">periodic_gaussian</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">:</span><span class="nb">str</span><span class="p">,</span> <span class="n">period</span><span class="p">:</span><span class="nb">int</span><span class="p">,</span> <span class="n">mu_min</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="n">sigma_min</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="n">mu_max</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="n">sigma_max</span><span class="p">:</span><span class="nb">float</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates an additive periodic gaussian interference on a previous signal</span>
<span class="sd"> :param type: &#39;linear&#39; or &#39;sinoidal&#39;</span>
@ -121,7 +121,7 @@
<span class="k">return</span> <span class="bp">self</span></div>
<div class="viewcode-block" id="SignalEmulator.blip"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.SignalEmulator.blip">[docs]</a> <span class="k">def</span> <span class="nf">blip</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates an outlier greater than the maximum or lower then the minimum previous values of the signal,</span>
<span class="sd"> and insert it on a random location of the signal.</span>
@ -133,7 +133,7 @@
<span class="k">return</span> <span class="bp">self</span></div>
<div class="viewcode-block" id="SignalEmulator.run"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.SignalEmulator.run">[docs]</a> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Render the signal</span>
<span class="sd"> :return: a list of float values</span>
@ -180,7 +180,7 @@
<span class="n">_mn</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">_mn</span><span class="p">,</span> <span class="n">vmin</span><span class="p">)</span> <span class="k">if</span> <span class="n">vmin</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="nb">min</span><span class="p">(</span><span class="n">_mn</span><span class="p">,</span> <span class="n">vmin</span><span class="p">)</span>
<span class="n">start</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</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">signal</span><span class="p">))</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">_mx</span><span class="p">]</span> <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="mf">.5</span> <span class="k">else</span> <span class="p">[</span><span class="o">-</span><span class="n">_mn</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="p">[</span><span class="n">_mx</span><span class="p">]</span> <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="o">.</span><span class="mi">5</span> <span class="k">else</span> <span class="p">[</span><span class="o">-</span><span class="n">_mn</span><span class="p">]</span>
<span class="n">last_num</span> <span class="o">=</span> <span class="n">num</span>
<span class="n">last_it</span> <span class="o">=</span> <span class="n">it</span>
@ -193,7 +193,7 @@
<div class="viewcode-block" id="generate_gaussian_linear"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.generate_gaussian_linear">[docs]</a><span class="k">def</span> <span class="nf">generate_gaussian_linear</span><span class="p">(</span><span class="n">mu_ini</span><span class="p">,</span> <span class="n">sigma_ini</span><span class="p">,</span> <span class="n">mu_inc</span><span class="p">,</span> <span class="n">sigma_inc</span><span class="p">,</span> <span class="n">it</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">vmin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">vmax</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Generate data sampled from Gaussian distribution, with constant or linear changing parameters</span>
<span class="sd"> :param mu_ini: Initial mean</span>
@ -222,7 +222,7 @@
<div class="viewcode-block" id="generate_linear_periodic_gaussian"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.generate_linear_periodic_gaussian">[docs]</a><span class="k">def</span> <span class="nf">generate_linear_periodic_gaussian</span><span class="p">(</span><span class="n">period</span><span class="p">,</span> <span class="n">mu_min</span><span class="p">,</span> <span class="n">sigma_min</span><span class="p">,</span> <span class="n">mu_max</span><span class="p">,</span> <span class="n">sigma_max</span><span class="p">,</span> <span class="n">it</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">vmin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">vmax</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Generates a periodic linear variation on mean and variance</span>
<span class="sd"> :param period: the period of recurrence</span>
@ -267,7 +267,7 @@
<div class="viewcode-block" id="generate_sinoidal_periodic_gaussian"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.generate_sinoidal_periodic_gaussian">[docs]</a><span class="k">def</span> <span class="nf">generate_sinoidal_periodic_gaussian</span><span class="p">(</span><span class="n">period</span><span class="p">,</span> <span class="n">mu_min</span><span class="p">,</span> <span class="n">sigma_min</span><span class="p">,</span> <span class="n">mu_max</span><span class="p">,</span> <span class="n">sigma_max</span><span class="p">,</span> <span class="n">it</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">vmin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">vmax</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Generates a periodic sinoidal variation on mean and variance</span>
<span class="sd"> :param period: the period of recurrence</span>
@ -304,7 +304,7 @@
<div class="viewcode-block" id="generate_uniform_linear"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.generate_uniform_linear">[docs]</a><span class="k">def</span> <span class="nf">generate_uniform_linear</span><span class="p">(</span><span class="n">min_ini</span><span class="p">,</span> <span class="n">max_ini</span><span class="p">,</span> <span class="n">min_inc</span><span class="p">,</span> <span class="n">max_inc</span><span class="p">,</span> <span class="n">it</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">vmin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">vmax</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Generate data sampled from Uniform distribution, with constant or linear changing bounds</span>
<span class="sd"> :param mu_ini: Initial mean</span>
@ -333,7 +333,7 @@
<div class="viewcode-block" id="white_noise"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.white_noise">[docs]</a><span class="k">def</span> <span class="nf">white_noise</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">500</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Simple Gaussian noise signal</span>
<span class="sd"> :param n: number of samples</span>
<span class="sd"> :return:</span>
@ -342,7 +342,7 @@
<div class="viewcode-block" id="random_walk"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.artificial.random_walk">[docs]</a><span class="k">def</span> <span class="nf">random_walk</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s1">&#39;gaussian&#39;</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Simple random walk</span>
<span class="sd"> :param n: number of samples</span>
@ -424,7 +424,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -48,12 +48,12 @@
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pkg_resources</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">request</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="k">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">urllib</span> <span class="k">import</span> <span class="n">request</span>
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.common.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">(</span><span class="n">filename</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">url</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">sep</span><span class="p">:</span><span class="nb">str</span><span class="o">=</span><span class="s2">&quot;;&quot;</span><span class="p">,</span> <span class="n">compression</span><span class="p">:</span><span class="nb">str</span><span class="o">=</span><span class="s1">&#39;infer&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> This method check if filename already exists, read the file and return its data.</span>
<span class="sd"> If the file don&#39;t already exists, it will be downloaded and decompressed.</span>
@ -114,7 +114,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -55,7 +55,7 @@
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.henon.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">var</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">a</span><span class="p">:</span><span class="nb">float</span><span class="o">=</span><span class="mf">1.4</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">float</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">initial_values</span><span class="p">:</span> <span class="nb">list</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">iterations</span><span class="p">:</span><span class="nb">int</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :param var: the dataset field name to extract</span>
@ -65,7 +65,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.henon.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="nb">float</span><span class="o">=</span><span class="mf">1.4</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">float</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">initial_values</span><span class="p">:</span> <span class="nb">list</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">iterations</span><span class="p">:</span><span class="nb">int</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return a dataframe with the bivariate Henon Map time series (x, y).</span>
<span class="sd"> :param a: Equation coefficient</span>
@ -124,7 +124,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -54,7 +54,7 @@
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.logistic_map.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">r</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span> <span class="n">initial_value</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.3</span><span class="p">,</span> <span class="n">iterations</span><span class="p">:</span> <span class="nb">int</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return a list with the logistic map chaotic time series.</span>
<span class="sd"> :param r: Equation coefficient</span>
@ -108,7 +108,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -58,7 +58,7 @@
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.lorentz.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">var</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">a</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">10.0</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">28.0</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">8.0</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">,</span> <span class="n">dt</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">,</span>
<span class="n">initial_values</span><span class="p">:</span> <span class="nb">list</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.1</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="n">iterations</span><span class="p">:</span> <span class="nb">int</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :param var: the dataset field name to extract</span>
@ -69,7 +69,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.lorentz.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">10.0</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">28.0</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">8.0</span> <span class="o">/</span> <span class="mf">3.0</span><span class="p">,</span> <span class="n">dt</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">,</span>
<span class="n">initial_values</span><span class="p">:</span> <span class="nb">list</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.1</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="n">iterations</span><span class="p">:</span> <span class="nb">int</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span><span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return a dataframe with the multivariate Lorenz Map time series (x, y, z).</span>
<span class="sd"> :param a: Equation coefficient. Default value: 10</span>
@ -135,7 +135,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -54,7 +54,7 @@
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.mackey_glass.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">b</span><span class="p">:</span> <span class="nb">float</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="n">tau</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span><span class="mi">17</span><span class="p">,</span> <span class="n">initial_values</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">1.5</span><span class="p">,</span> <span class="mi">18</span><span class="p">),</span> <span class="n">iterations</span><span class="p">:</span> <span class="nb">int</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">list</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return a list with the Mackey-Glass chaotic time series.</span>
<span class="sd"> :param b: Equation coefficient</span>
@ -111,7 +111,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -58,7 +58,7 @@
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.rossler.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">var</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">a</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.2</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.2</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">5.7</span><span class="p">,</span> <span class="n">dt</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">,</span>
<span class="n">initial_values</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.001</span><span class="p">,</span> <span class="mf">0.001</span><span class="p">,</span> <span class="mf">0.001</span><span class="p">],</span> <span class="n">iterations</span><span class="p">:</span> <span class="nb">int</span><span class="o">=</span><span class="mi">5000</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :param var: the dataset field name to extract</span>
@ -69,7 +69,7 @@
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.rossler.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.2</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.2</span><span class="p">,</span> <span class="n">c</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">5.7</span><span class="p">,</span> <span class="n">dt</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">,</span>
<span class="n">initial_values</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.001</span><span class="p">,</span> <span class="mf">0.001</span><span class="p">,</span> <span class="mf">0.001</span><span class="p">],</span> <span class="n">iterations</span><span class="p">:</span> <span class="nb">int</span><span class="o">=</span><span class="mi">5000</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return a dataframe with the multivariate Rössler Map time series (x, y, z).</span>
<span class="sd"> :param a: Equation coefficient. Default value: 0.2</span>
@ -135,7 +135,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -49,12 +49,12 @@
<span class="sd">Source: https://www.esrl.noaa.gov/psd/gcos_wgsp/Timeseries/SUNSPOT/</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="kn">import</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">pyFTS.data</span> <span class="k">import</span> <span class="n">common</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.sunspots.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get a simple univariate time series data.</span>
<span class="sd"> :return: numpy array</span>
@ -64,7 +64,7 @@
<span class="k">return</span> <span class="n">dat</span></div>
<div class="viewcode-block" id="get_dataframe"><a class="viewcode-back" href="../../../pyFTS.data.html#pyFTS.data.sunspots.get_dataframe">[docs]</a><span class="k">def</span> <span class="nf">get_dataframe</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Get the complete multivariate time series data.</span>
<span class="sd"> :return: Pandas DataFrame</span>
@ -115,7 +115,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,271 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"><script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-55120145-3']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<title>pyFTS.distributed.dispy &#8212; pyFTS 1.6 documentation</title>
<link rel="stylesheet" href="../../../_static/bizstyle.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/language_data.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.6 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.distributed.dispy</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.distributed.dispy</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">dispy</span> <span class="k">as</span> <span class="nn">dispy</span><span class="o">,</span> <span class="nn">dispy.httpd</span><span class="o">,</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="kn">import</span> <span class="n">Util</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="start_dispy_cluster"><a class="viewcode-back" href="../../../pyFTS.distributed.html#pyFTS.distributed.dispy.start_dispy_cluster">[docs]</a><span class="k">def</span> <span class="nf">start_dispy_cluster</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="n">nodes</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Start a new Dispy cluster on &#39;nodes&#39; to execute the method &#39;method&#39;</span>
<span class="sd"> :param method: function to be executed on each cluster node</span>
<span class="sd"> :param nodes: list of node names or IP&#39;s.</span>
<span class="sd"> :return: the dispy cluster instance and the http_server for monitoring</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">cluster</span> <span class="o">=</span> <span class="n">dispy</span><span class="o">.</span><span class="n">JobCluster</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="n">nodes</span><span class="o">=</span><span class="n">nodes</span><span class="p">,</span> <span class="n">loglevel</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">,</span> <span class="n">ping_interval</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
<span class="n">http_server</span> <span class="o">=</span> <span class="n">dispy</span><span class="o">.</span><span class="n">httpd</span><span class="o">.</span><span class="n">DispyHTTPServer</span><span class="p">(</span><span class="n">cluster</span><span class="p">)</span>
<span class="k">return</span> <span class="n">cluster</span><span class="p">,</span> <span class="n">http_server</span></div>
<div class="viewcode-block" id="stop_dispy_cluster"><a class="viewcode-back" href="../../../pyFTS.distributed.html#pyFTS.distributed.dispy.stop_dispy_cluster">[docs]</a><span class="k">def</span> <span class="nf">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="sd">&quot;&quot;&quot;</span>
<span class="sd"> Stop a dispy cluster and http_server</span>
<span class="sd"> :param cluster:</span>
<span class="sd"> :param http_server:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1">#cluster.wait() # wait for all jobs to finish</span>
<span class="n">cluster</span><span class="o">.</span><span class="n">print_status</span><span class="p">()</span>
<span class="n">http_server</span><span class="o">.</span><span class="n">shutdown</span><span class="p">()</span> <span class="c1"># this waits until browser gets all updates</span>
<span class="n">cluster</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>
<div class="viewcode-block" id="get_number_of_cpus"><a class="viewcode-back" href="../../../pyFTS.distributed.html#pyFTS.distributed.dispy.get_number_of_cpus">[docs]</a><span class="k">def</span> <span class="nf">get_number_of_cpus</span><span class="p">(</span><span class="n">cluster</span><span class="p">):</span>
<span class="n">cpus</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">dispy_node</span> <span class="ow">in</span> <span class="n">cluster</span><span class="o">.</span><span class="n">status</span><span class="p">()</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
<span class="n">cpus</span> <span class="o">+=</span> <span class="n">dispy_node</span><span class="o">.</span><span class="n">cpus</span>
<span class="k">return</span> <span class="n">cpus</span></div>
<div class="viewcode-block" id="simple_model_train"><a class="viewcode-back" href="../../../pyFTS.distributed.html#pyFTS.distributed.dispy.simple_model_train">[docs]</a><span class="k">def</span> <span class="nf">simple_model_train</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Cluster function that receives a FTS instance &#39;model&#39; and train using the &#39;data&#39; and &#39;parameters&#39;</span>
<span class="sd"> :param model: a FTS instance</span>
<span class="sd"> :param data: training dataset</span>
<span class="sd"> :param parameters: parameters for the training process</span>
<span class="sd"> :return: the trained model</span>
<span class="sd"> &quot;&quot;&quot;</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">model</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="o">**</span><span class="n">parameters</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">model</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;training_time&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">_end</span> <span class="o">-</span> <span class="n">_start</span>
<span class="k">return</span> <span class="n">model</span></div>
<div class="viewcode-block" id="distributed_train"><a class="viewcode-back" href="../../../pyFTS.distributed.html#pyFTS.distributed.dispy.distributed_train">[docs]</a><span class="k">def</span> <span class="nf">distributed_train</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">train_method</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">num_batches</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">train_parameters</span><span class="o">=</span><span class="p">{},</span> <span class="o">**</span><span class="n">kwargs</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="o">,</span> <span class="nn">datetime</span>
<span class="n">batch_save</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">&#39;batch_save&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="c1"># save model between batches</span>
<span class="n">batch_save_interval</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">&#39;batch_save_interval&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">file_path</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">&#39;file_path&#39;</span><span class="p">,</span> <span class="kc">None</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">start_dispy_cluster</span><span class="p">(</span><span class="n">train_method</span><span class="p">,</span> <span class="n">nodes</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;[{0: %H:%M:%S}] Distrituted Train Started with </span><span class="si">{1}</span><span class="s2"> CPU&#39;s&quot;</span>
<span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span> <span class="n">get_number_of_cpus</span><span class="p">(</span><span class="n">cluster</span><span class="p">)))</span>
<span class="n">jobs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">n</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="n">batch_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">num_batches</span><span class="p">)</span>
<span class="n">bcount</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">ct</span> <span class="ow">in</span> <span class="nb">range</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">n</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">):</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">ct</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:</span><span class="n">ct</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">ct</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">ct</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span>
<span class="n">tmp_model</span> <span class="o">=</span> <span class="n">fts_method</span><span class="p">()</span>
<span class="n">tmp_model</span><span class="o">.</span><span class="n">clone_parameters</span><span class="p">(</span><span class="n">model</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">tmp_model</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="n">train_parameters</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="n">bcount</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="n">bcount</span> <span class="o">+=</span> <span class="mi">1</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="nb">print</span><span class="p">(</span><span class="s2">&quot;[{0: %H:%M:%S}] Processing batch &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">))</span>
<span class="n">tmp</span> <span class="o">=</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">tmp</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">model</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="k">if</span> <span class="s1">&#39;training_time&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
<span class="n">model</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;training_time&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">model</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;training_time&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;training_time&#39;</span><span class="p">])</span>
<span class="k">if</span> <span class="n">batch_save</span> <span class="ow">and</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="n">batch_save_interval</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">Util</span><span class="o">.</span><span class="n">persist_obj</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">file_path</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="n">job</span><span class="o">.</span><span class="n">exception</span><span class="p">)</span>
<span class="nb">print</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">&quot;[{0: %H:%M:%S}] Finished batch &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;[{0: %H:%M:%S}] Distrituted Train Finished&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()))</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="k">return</span> <span class="n">model</span></div>
<div class="viewcode-block" id="simple_model_predict"><a class="viewcode-back" href="../../../pyFTS.distributed.html#pyFTS.distributed.dispy.simple_model_predict">[docs]</a><span class="k">def</span> <span class="nf">simple_model_predict</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">parameters</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">time</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">forecasts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">parameters</span><span class="p">)</span>
<span class="n">_stop</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="k">return</span> <span class="n">forecasts</span><span class="p">,</span> <span class="n">_stop</span> <span class="o">-</span> <span class="n">_start</span></div>
<div class="viewcode-block" id="distributed_predict"><a class="viewcode-back" href="../../../pyFTS.distributed.html#pyFTS.distributed.dispy.distributed_predict">[docs]</a><span class="k">def</span> <span class="nf">distributed_predict</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">parameters</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">num_batches</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</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">cluster</span><span class="p">,</span> <span class="n">http_server</span> <span class="o">=</span> <span class="n">start_dispy_cluster</span><span class="p">(</span><span class="n">simple_model_predict</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">n</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="n">batch_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="n">num_batches</span><span class="p">)</span>
<span class="n">bcount</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">ct</span> <span class="ow">in</span> <span class="nb">range</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">n</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">):</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">ct</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:</span><span class="n">ct</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">ct</span> <span class="o">-</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">ct</span> <span class="o">+</span> <span class="n">batch_size</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">model</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="n">parameters</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="n">bcount</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="n">bcount</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">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">jobs</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</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">tmp</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">if</span> <span class="n">job</span><span class="o">.</span><span class="n">id</span> <span class="o">&lt;</span> <span class="n">batch_size</span><span class="p">:</span>
<span class="n">ret</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">tmp</span><span class="p">[</span><span class="mi">0</span><span class="p">][:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">tmp</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">if</span> <span class="s1">&#39;forecasting_time&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
<span class="n">model</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;forecasting_time&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">model</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;forecasting_time&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">[</span><span class="mi">1</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="n">job</span><span class="o">.</span><span class="n">exception</span><span class="p">)</span>
<span class="nb">print</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="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="k">return</span> <span class="n">ret</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.6 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.distributed.dispy</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2018, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
</div>
</body>
</html>

View File

@ -0,0 +1,716 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.hyperparam.Evolutionary &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.hyperparam.Evolutionary</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.hyperparam.Evolutionary</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Distributed Evolutionary Hyperparameter Optimization (DEHO) for MVFTS</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>
<span class="kn">from</span> <span class="nn">operator</span> <span class="k">import</span> <span class="n">itemgetter</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Util</span>
<span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</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="c1"># , Huarng</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Membership</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.hyperparam</span> <span class="k">import</span> <span class="n">Util</span> <span class="k">as</span> <span class="n">hUtil</span>
<span class="n">__measures</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;f1&#39;</span><span class="p">,</span> <span class="s1">&#39;f2&#39;</span><span class="p">,</span> <span class="s1">&#39;rmse&#39;</span><span class="p">,</span> <span class="s1">&#39;size&#39;</span><span class="p">]</span>
<div class="viewcode-block" id="genotype"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.genotype">[docs]</a><span class="k">def</span> <span class="nf">genotype</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">npart</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">lags</span><span class="p">,</span> <span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create the individual genotype</span>
<span class="sd"> :param mf: membership function</span>
<span class="sd"> :param npart: number of partitions</span>
<span class="sd"> :param partitioner: partitioner method</span>
<span class="sd"> :param order: model order</span>
<span class="sd"> :param alpha: alpha-cut</span>
<span class="sd"> :param lags: array with lag indexes</span>
<span class="sd"> :param f1: accuracy fitness value</span>
<span class="sd"> :param f2: parsimony fitness value</span>
<span class="sd"> :return: the genotype, a dictionary with all hyperparameters</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ind</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">mf</span><span class="o">=</span><span class="n">mf</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">npart</span><span class="p">,</span> <span class="n">partitioner</span><span class="o">=</span><span class="n">partitioner</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">alpha</span><span class="o">=</span><span class="n">alpha</span><span class="p">,</span> <span class="n">lags</span><span class="o">=</span><span class="n">lags</span><span class="p">,</span> <span class="n">f1</span><span class="o">=</span><span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="o">=</span><span class="n">f2</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ind</span></div>
<div class="viewcode-block" id="random_genotype"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.random_genotype">[docs]</a><span class="k">def</span> <span class="nf">random_genotype</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create random genotype</span>
<span class="sd"> :return: the genotype, a dictionary with all hyperparameters</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">order</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">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="n">order</span><span class="o">+</span><span class="mi">1</span><span class="p">)]</span>
<span class="k">return</span> <span class="n">genotype</span><span class="p">(</span>
<span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
<span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span>
<span class="n">random</span><span class="o">.</span><span class="n">randint</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="n">order</span><span class="p">,</span>
<span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">),</span>
<span class="n">lags</span><span class="p">,</span>
<span class="kc">None</span><span class="p">,</span>
<span class="kc">None</span>
<span class="p">)</span></div>
<span class="c1">#</span>
<div class="viewcode-block" id="initial_population"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.initial_population">[docs]</a><span class="k">def</span> <span class="nf">initial_population</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a random population of size n</span>
<span class="sd"> :param n: the size of the population</span>
<span class="sd"> :return: a list with n random individuals</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">create_random_individual</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">&#39;random_individual&#39;</span><span class="p">,</span> <span class="n">random_genotype</span><span class="p">)</span>
<span class="n">pop</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="n">pop</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_random_individual</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="k">return</span> <span class="n">pop</span></div>
<div class="viewcode-block" id="phenotype"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.phenotype">[docs]</a><span class="k">def</span> <span class="nf">phenotype</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="p">{},</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Instantiate the genotype, creating a fitted model with the genotype hyperparameters</span>
<span class="sd"> :param individual: a genotype</span>
<span class="sd"> :param train: the training dataset</span>
<span class="sd"> :param fts_method: the FTS method </span>
<span class="sd"> :param parameters: dict with model specific arguments for fit method.</span>
<span class="sd"> :return: a fitted FTS model</span>
<span class="sd"> &quot;&quot;&quot;</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="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span>
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span>
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">gaussmf</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span>
<span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</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">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">individual</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">],</span> <span class="n">func</span><span class="o">=</span><span class="n">mf</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">partitioner</span> <span class="o">=</span> <span class="n">Entropy</span><span class="o">.</span><span class="n">EntropyPartitioner</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">individual</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">],</span> <span class="n">func</span><span class="o">=</span><span class="n">mf</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">fts_method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="n">partitioner</span><span class="p">,</span>
<span class="n">lags</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">],</span>
<span class="n">alpha_cut</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">],</span>
<span class="n">order</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">])</span>
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train</span><span class="p">,</span> <span class="o">**</span><span class="n">parameters</span><span class="p">)</span>
<span class="k">return</span> <span class="n">model</span></div>
<div class="viewcode-block" id="evaluate"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.evaluate">[docs]</a><span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Evaluate an individual using a sliding window cross validation over the dataset.</span>
<span class="sd"> :param dataset: Evaluation dataset</span>
<span class="sd"> :param individual: genotype to be tested</span>
<span class="sd"> :param window_size: The length of scrolling window for train/test on dataset</span>
<span class="sd"> :param train_rate: The train/test split ([0,1])</span>
<span class="sd"> :param increment_rate: The increment of the scrolling window, relative to the window_size ([0,1])</span>
<span class="sd"> :param parameters: dict with model specific arguments for fit method.</span>
<span class="sd"> :return: a tuple (len_lags, rmse) with the parsimony fitness value and the accuracy fitness value</span>
<span class="sd"> &quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">Util</span>
<span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span>
<span class="kn">from</span> <span class="nn">pyFTS.hyperparam.Evolutionary</span> <span class="k">import</span> <span class="n">phenotype</span><span class="p">,</span> <span class="n">__measures</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">window_size</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">&#39;window_size&#39;</span><span class="p">,</span> <span class="mi">800</span><span class="p">)</span>
<span class="n">train_rate</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">&#39;train_rate&#39;</span><span class="p">,</span> <span class="o">.</span><span class="mi">8</span><span class="p">)</span>
<span class="n">increment_rate</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">&#39;increment_rate&#39;</span><span class="p">,</span> <span class="o">.</span><span class="mi">2</span><span class="p">)</span>
<span class="n">fts_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">&#39;fts_method&#39;</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
<span class="n">parameters</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">&#39;parameters&#39;</span><span class="p">,{})</span>
<span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;f1&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;f2&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">key</span><span class="p">:</span> <span class="n">individual</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span> <span class="p">}</span>
<span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lengths</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">train</span><span class="p">,</span> <span class="n">test</span> <span class="ow">in</span> <span class="n">Util</span><span class="o">.</span><span class="n">sliding_window</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">window_size</span><span class="p">,</span> <span class="n">train</span><span class="o">=</span><span class="n">train_rate</span><span class="p">,</span> <span class="n">inc</span><span class="o">=</span><span class="n">increment_rate</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">phenotype</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">fts_method</span><span class="o">=</span><span class="n">fts_method</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="n">forecasts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
<span class="n">rmse</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">test</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="n">lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">model</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">rmse</span><span class="p">)</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nan</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">np</span><span class="o">.</span><span class="n">nan</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">_lags</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">lags</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span>
<span class="n">_rmse</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>
<span class="n">_len</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">lengths</span><span class="p">)</span>
<span class="n">f1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="o">.</span><span class="mi">6</span> <span class="o">*</span> <span class="n">_rmse</span><span class="p">,</span> <span class="o">.</span><span class="mi">4</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">nanstd</span><span class="p">(</span><span class="n">errors</span><span class="p">)])</span>
<span class="n">f2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="o">.</span><span class="mi">4</span> <span class="o">*</span> <span class="n">_len</span><span class="p">,</span> <span class="o">.</span><span class="mi">6</span> <span class="o">*</span> <span class="n">_lags</span><span class="p">])</span>
<span class="k">return</span> <span class="p">{</span><span class="s1">&#39;f1&#39;</span><span class="p">:</span> <span class="n">f1</span><span class="p">,</span> <span class="s1">&#39;f2&#39;</span><span class="p">:</span> <span class="n">f2</span><span class="p">,</span> <span class="s1">&#39;rmse&#39;</span><span class="p">:</span> <span class="n">_rmse</span><span class="p">,</span> <span class="s1">&#39;size&#39;</span><span class="p">:</span> <span class="n">_len</span> <span class="p">}</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">return</span> <span class="p">{</span><span class="s1">&#39;f1&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="s1">&#39;f2&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="s1">&#39;rmse&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="s1">&#39;size&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">}</span></div>
<div class="viewcode-block" id="tournament"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.tournament">[docs]</a><span class="k">def</span> <span class="nf">tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">objective</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Simple tournament selection strategy.</span>
<span class="sd"> :param population: the population</span>
<span class="sd"> :param objective: the objective to be considered on tournament</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">population</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">r1</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">if</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">2</span> <span class="k">else</span> <span class="mi">0</span>
<span class="n">r2</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">if</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">2</span> <span class="k">else</span> <span class="mi">1</span>
<span class="n">ix</span> <span class="o">=</span> <span class="n">r1</span> <span class="k">if</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">][</span><span class="n">objective</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">][</span><span class="n">objective</span><span class="p">]</span> <span class="k">else</span> <span class="n">r2</span>
<span class="k">return</span> <span class="n">population</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span></div>
<div class="viewcode-block" id="double_tournament"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.double_tournament">[docs]</a><span class="k">def</span> <span class="nf">double_tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Double tournament selection strategy.</span>
<span class="sd"> :param population:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">ancestor1</span> <span class="o">=</span> <span class="n">tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="s1">&#39;f1&#39;</span><span class="p">)</span>
<span class="n">ancestor2</span> <span class="o">=</span> <span class="n">tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="s1">&#39;f1&#39;</span><span class="p">)</span>
<span class="n">selected</span> <span class="o">=</span> <span class="n">tournament</span><span class="p">([</span><span class="n">ancestor1</span><span class="p">,</span> <span class="n">ancestor2</span><span class="p">],</span> <span class="s1">&#39;f2&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">selected</span></div>
<div class="viewcode-block" id="lag_crossover2"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.lag_crossover2">[docs]</a><span class="k">def</span> <span class="nf">lag_crossover2</span><span class="p">(</span><span class="n">best</span><span class="p">,</span> <span class="n">worst</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Cross over two lag genes</span>
<span class="sd"> :param best: best genotype</span>
<span class="sd"> :param worst: worst genotype</span>
<span class="sd"> :return: a tuple (order, lags)</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">order</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">]))</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">min_order</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">best</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">],</span> <span class="n">worst</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">])</span>
<span class="n">max_order</span> <span class="o">=</span> <span class="n">best</span> <span class="k">if</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">min_order</span> <span class="k">else</span> <span class="n">worst</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="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">min_order</span><span class="p">:</span>
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">][</span><span class="n">k</span><span class="p">])))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">max_order</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">][</span><span class="n">k</span><span class="p">])</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
<span class="k">while</span> <span class="n">lags</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="n">lags</span><span class="p">[</span><span class="n">k</span><span class="p">]:</span>
<span class="n">lags</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="k">return</span> <span class="n">order</span><span class="p">,</span> <span class="n">lags</span></div>
<div class="viewcode-block" id="crossover"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.crossover">[docs]</a><span class="k">def</span> <span class="nf">crossover</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Crossover operation between two parents</span>
<span class="sd"> :param population: the original population</span>
<span class="sd"> :return: a genotype</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">population</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">r1</span><span class="p">,</span> <span class="n">r2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
<span class="k">while</span> <span class="n">r1</span> <span class="o">==</span> <span class="n">r2</span><span class="p">:</span>
<span class="n">r1</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="n">r2</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="k">if</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">][</span><span class="s1">&#39;f1&#39;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">][</span><span class="s1">&#39;f1&#39;</span><span class="p">]:</span>
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">]</span>
<span class="n">worst</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">]</span>
<span class="n">worst</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">]</span>
<span class="n">npart</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">]))</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">])</span>
<span class="n">rnd</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</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">mf</span> <span class="o">=</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="n">rnd</span> <span class="o">&lt;</span> <span class="o">.</span><span class="mi">7</span> <span class="k">else</span> <span class="n">worst</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span>
<span class="n">rnd</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</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">partitioner</span> <span class="o">=</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="n">rnd</span> <span class="o">&lt;</span> <span class="o">.</span><span class="mi">7</span> <span class="k">else</span> <span class="n">worst</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span>
<span class="n">order</span><span class="p">,</span> <span class="n">lags</span> <span class="o">=</span> <span class="n">lag_crossover2</span><span class="p">(</span><span class="n">best</span><span class="p">,</span> <span class="n">worst</span><span class="p">)</span>
<span class="n">descendent</span> <span class="o">=</span> <span class="n">genotype</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">npart</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">lags</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">return</span> <span class="n">descendent</span></div>
<div class="viewcode-block" id="mutation_lags"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.mutation_lags">[docs]</a><span class="k">def</span> <span class="nf">mutation_lags</span><span class="p">(</span><span class="n">lags</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Mutation operation for lags gene</span>
<span class="sd"> :param lags:</span>
<span class="sd"> :param order:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">try</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">lags</span><span class="p">)</span>
<span class="n">new</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">lag</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="n">order</span><span class="p">):</span>
<span class="k">if</span> <span class="n">lag</span> <span class="o">&lt;</span> <span class="n">l</span><span class="p">:</span>
<span class="n">new</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="mi">50</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">lags</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">))))</span> <span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">new</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="n">new</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="p">)</span>
<span class="k">if</span> <span class="n">order</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">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="n">order</span><span class="p">):</span>
<span class="k">while</span> <span class="n">new</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">new</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]:</span>
<span class="n">new</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">new</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
<span class="k">return</span> <span class="n">new</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="n">lags</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">new</span><span class="p">,</span> <span class="n">lag</span><span class="p">)</span></div>
<div class="viewcode-block" id="mutation"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.mutation">[docs]</a><span class="k">def</span> <span class="nf">mutation</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Mutation operator</span>
<span class="sd"> :param individual: an individual genotype</span>
<span class="sd"> :param pmut: individual probability o</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">individual</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">))))</span>
<span class="n">individual</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="o">.</span><span class="mi">5</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">)))</span>
<span class="n">individual</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</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="n">individual</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</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="n">individual</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))))</span>
<span class="c1"># Chama a função mutation_lags</span>
<span class="n">individual</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mutation_lags</span><span class="p">(</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">],</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">])</span>
<span class="n">individual</span><span class="p">[</span><span class="s1">&#39;f1&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">individual</span><span class="p">[</span><span class="s1">&#39;f2&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">return</span> <span class="n">individual</span></div>
<div class="viewcode-block" id="elitism"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.elitism">[docs]</a><span class="k">def</span> <span class="nf">elitism</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">new_population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Elitism operation, always select the best individual of the population and discard the worst</span>
<span class="sd"> :param population:</span>
<span class="sd"> :param new_population:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">population</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="s1">&#39;f1&#39;</span><span class="p">))</span>
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">new_population</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">new_population</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="s1">&#39;f1&#39;</span><span class="p">))</span>
<span class="k">if</span> <span class="n">new_population</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;f1&quot;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">best</span><span class="p">[</span><span class="s2">&quot;f1&quot;</span><span class="p">]:</span>
<span class="n">new_population</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">best</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">new_population</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;f1&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">best</span><span class="p">[</span><span class="s2">&quot;f1&quot;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">new_population</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">&quot;f2&quot;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">best</span><span class="p">[</span><span class="s2">&quot;f2&quot;</span><span class="p">]:</span>
<span class="n">new_population</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">best</span><span class="p">)</span>
<span class="k">return</span> <span class="n">new_population</span></div>
<div class="viewcode-block" id="GeneticAlgorithm"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.GeneticAlgorithm">[docs]</a><span class="k">def</span> <span class="nf">GeneticAlgorithm</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Genetic algoritm for Distributed Evolutionary Hyperparameter Optimization (DEHO)</span>
<span class="sd"> :param dataset: The time series to optimize the FTS</span>
<span class="sd"> :keyword ngen: An integer value with the maximum number of generations, default value: 30</span>
<span class="sd"> :keyword mgen: An integer value with the maximum number of generations without improvement to stop, default value 7</span>
<span class="sd"> :keyword npop: An integer value with the population size, default value: 20</span>
<span class="sd"> :keyword pcross: A float value between 0 and 1 with the probability of crossover, default: .5</span>
<span class="sd"> :keyword psel: A float value between 0 and 1 with the probability of selection, default: .5</span>
<span class="sd"> :keyword pmut: A float value between 0 and 1 with the probability of mutation, default: .3</span>
<span class="sd"> :keyword fts_method: The FTS method to optimize</span>
<span class="sd"> :keyword parameters: dict with model specific arguments for fts_method</span>
<span class="sd"> :keyword elitism: A boolean value indicating if the best individual must always survive to next population</span>
<span class="sd"> :keyword initial_operator: a function that receives npop and return a random population with size npop</span>
<span class="sd"> :keyword evalutation_operator: a function that receives a dataset and an individual and return its fitness</span>
<span class="sd"> :keyword selection_operator: a function that receives the whole population and return a selected individual</span>
<span class="sd"> :keyword crossover_operator: a function that receives the whole population and return a descendent individual</span>
<span class="sd"> :keyword mutation_operator: a function that receives one individual and return a changed individual</span>
<span class="sd"> :keyword window_size: An integer value with the the length of scrolling window for train/test on dataset</span>
<span class="sd"> :keyword train_rate: A float value between 0 and 1 with the train/test split ([0,1])</span>
<span class="sd"> :keyword increment_rate: A float value between 0 and 1 with the the increment of the scrolling window,</span>
<span class="sd"> relative to the window_size ([0,1])</span>
<span class="sd"> :keyword collect_statistics: A boolean value indicating to collect statistics for each generation</span>
<span class="sd"> :keyword distributed: A value indicating it the execution will be local and sequential (distributed=False),</span>
<span class="sd"> or parallel and distributed (distributed=&#39;dispy&#39; or distributed=&#39;spark&#39;)</span>
<span class="sd"> :keyword cluster: If distributed=&#39;dispy&#39; the list of cluster nodes, else if distributed=&#39;spark&#39; it is the master node</span>
<span class="sd"> :return: the best genotype</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">statistics</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ngen</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">&#39;ngen&#39;</span><span class="p">,</span><span class="mi">30</span><span class="p">)</span>
<span class="n">mgen</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">&#39;mgen&#39;</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
<span class="n">npop</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">&#39;npop&#39;</span><span class="p">,</span><span class="mi">20</span><span class="p">)</span>
<span class="n">psel</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">&#39;psel&#39;</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">)</span>
<span class="n">pcross</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">&#39;pcross&#39;</span><span class="p">,</span><span class="o">.</span><span class="mi">5</span><span class="p">)</span>
<span class="n">pmut</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">&#39;pmut&#39;</span><span class="p">,</span><span class="o">.</span><span class="mi">3</span><span class="p">)</span>
<span class="n">distributed</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">&#39;distributed&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="n">initial_operator</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">&#39;initial_operator&#39;</span><span class="p">,</span> <span class="n">initial_population</span><span class="p">)</span>
<span class="n">evaluation_operator</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">&#39;evaluation_operator&#39;</span><span class="p">,</span> <span class="n">evaluate</span><span class="p">)</span>
<span class="n">selection_operator</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">&#39;selection_operator&#39;</span><span class="p">,</span> <span class="n">double_tournament</span><span class="p">)</span>
<span class="n">crossover_operator</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">&#39;crossover_operator&#39;</span><span class="p">,</span> <span class="n">crossover</span><span class="p">)</span>
<span class="n">mutation_operator</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">&#39;mutation_operator&#39;</span><span class="p">,</span> <span class="n">mutation</span><span class="p">)</span>
<span class="n">_elitism</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">&#39;elitism&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="n">elitism_operator</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">&#39;elitism_operator&#39;</span><span class="p">,</span> <span class="n">elitism</span><span class="p">)</span>
<span class="k">if</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">&#39;dispy&#39;</span><span class="p">:</span>
<span class="n">cluster</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;cluster&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">collect_statistics</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">&#39;collect_statistics&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="n">no_improvement_count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">new_population</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">population</span> <span class="o">=</span> <span class="n">initial_operator</span><span class="p">(</span><span class="n">npop</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">last_best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Evaluating initial population </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">distributed</span><span class="p">:</span>
<span class="k">for</span> <span class="n">individual</span> <span class="ow">in</span> <span class="n">population</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">evaluation_operator</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
<span class="n">individual</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="k">elif</span> <span class="n">distributed</span><span class="o">==</span><span class="s1">&#39;dispy&#39;</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span> <span class="k">as</span> <span class="n">dUtil</span>
<span class="kn">import</span> <span class="nn">dispy</span>
<span class="n">jobs</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">individual</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">population</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">dataset</span><span class="p">,</span> <span class="n">individual</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="n">ct</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">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">jobs</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</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">result</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">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
<span class="n">population</span><span class="p">[</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</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="nb">print</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="nb">print</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="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ngen</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;GENERATION </span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span>
<span class="n">generation_statistics</span> <span class="o">=</span> <span class="p">{}</span>
<span class="c1"># Selection</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">npop</span> <span class="o">*</span> <span class="n">psel</span><span class="p">)):</span>
<span class="n">new_population</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">selection_operator</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="c1"># Crossover</span>
<span class="n">new</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">npop</span> <span class="o">*</span> <span class="n">pcross</span><span class="p">)):</span>
<span class="n">new</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">crossover_operator</span><span class="p">(</span><span class="n">new_population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
<span class="n">new_population</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>
<span class="c1"># Mutation</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">individual</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">new_population</span><span class="p">):</span>
<span class="n">rnd</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</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="k">if</span> <span class="n">rnd</span> <span class="o">&lt;</span> <span class="n">pmut</span><span class="p">:</span>
<span class="n">new_population</span><span class="p">[</span><span class="n">ct</span><span class="p">]</span> <span class="o">=</span> <span class="n">mutation_operator</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="c1"># Evaluation</span>
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span>
<span class="n">stats</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
<span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">distributed</span><span class="p">:</span>
<span class="k">for</span> <span class="n">individual</span> <span class="ow">in</span> <span class="n">new_population</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">evaluation_operator</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
<span class="n">individual</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span> <span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
<span class="k">elif</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">&#39;dispy&#39;</span><span class="p">:</span>
<span class="n">jobs</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">individual</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">new_population</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">dataset</span><span class="p">,</span> <span class="n">individual</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="n">ct</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">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">jobs</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;job id </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">))</span>
<span class="n">result</span> <span class="o">=</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">result</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">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
<span class="n">new_population</span><span class="p">[</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span> <span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">result</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="nb">print</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="nb">print</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="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span>
<span class="n">mean_stats</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmedian</span><span class="p">(</span><span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">])</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span> <span class="p">}</span>
<span class="n">generation_statistics</span><span class="p">[</span><span class="s1">&#39;population&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mean_stats</span>
<span class="c1"># Elitism</span>
<span class="k">if</span> <span class="n">_elitism</span><span class="p">:</span>
<span class="n">population</span> <span class="o">=</span> <span class="n">elitism_operator</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">new_population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">population</span> <span class="o">=</span> <span class="n">population</span><span class="p">[:</span><span class="n">npop</span><span class="p">]</span>
<span class="n">new_population</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">last_best</span> <span class="o">=</span> <span class="n">best</span>
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span>
<span class="n">generation_statistics</span><span class="p">[</span><span class="s1">&#39;best&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">best</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span> <span class="p">}</span>
<span class="n">statistics</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">generation_statistics</span><span class="p">)</span>
<span class="k">if</span> <span class="n">last_best</span><span class="p">[</span><span class="s1">&#39;f1&#39;</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;f1&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">last_best</span><span class="p">[</span><span class="s1">&#39;f2&#39;</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">best</span><span class="p">[</span><span class="s1">&#39;f2&#39;</span><span class="p">]:</span>
<span class="n">no_improvement_count</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;WITHOUT IMPROVEMENT </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">no_improvement_count</span><span class="p">))</span>
<span class="n">pmut</span> <span class="o">+=</span> <span class="o">.</span><span class="mi">05</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">no_improvement_count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">pcross</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">&#39;pcross&#39;</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">)</span>
<span class="n">pmut</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">&#39;pmut&#39;</span><span class="p">,</span> <span class="o">.</span><span class="mi">3</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">best</span><span class="p">)</span>
<span class="k">if</span> <span class="n">no_improvement_count</span> <span class="o">==</span> <span class="n">mgen</span><span class="p">:</span>
<span class="k">break</span>
<span class="k">return</span> <span class="n">best</span><span class="p">,</span> <span class="n">statistics</span></div>
<div class="viewcode-block" id="process_experiment"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.process_experiment">[docs]</a><span class="k">def</span> <span class="nf">process_experiment</span><span class="p">(</span><span class="n">fts_method</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Persist the results of an DEHO execution in sqlite database (best hyperparameters) and json file (generation statistics)</span>
<span class="sd"> :param fts_method:</span>
<span class="sd"> :param result:</span>
<span class="sd"> :param datasetname:</span>
<span class="sd"> :param conn:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">log_result</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;individual&#39;</span><span class="p">])</span>
<span class="n">persist_statistics</span><span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;statistics&#39;</span><span class="p">])</span>
<span class="k">return</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;individual&#39;</span><span class="p">]</span></div>
<div class="viewcode-block" id="persist_statistics"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.persist_statistics">[docs]</a><span class="k">def</span> <span class="nf">persist_statistics</span><span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="n">statistics</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;statistics_</span><span class="si">{}</span><span class="s1">.json&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datasetname</span><span class="p">),</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
<span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">statistics</span><span class="p">))</span></div>
<div class="viewcode-block" id="log_result"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.log_result">[docs]</a><span class="k">def</span> <span class="nf">log_result</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
<span class="n">metrics</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;rmse&#39;</span><span class="p">,</span> <span class="s1">&#39;size&#39;</span><span class="p">,</span> <span class="s1">&#39;time&#39;</span><span class="p">]</span>
<span class="k">for</span> <span class="n">metric</span> <span class="ow">in</span> <span class="n">metrics</span><span class="p">:</span>
<span class="n">record</span> <span class="o">=</span> <span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="s1">&#39;Evolutive&#39;</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">],</span>
<span class="n">result</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">],</span>
<span class="n">result</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">],</span> <span class="nb">str</span><span class="p">(</span><span class="n">result</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">]),</span> <span class="n">metric</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="n">metric</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">record</span><span class="p">)</span>
<span class="n">hUtil</span><span class="o">.</span><span class="n">insert_hyperparam</span><span class="p">(</span><span class="n">record</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span></div>
<div class="viewcode-block" id="execute"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.execute">[docs]</a><span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="n">dataset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Batch execution of Distributed Evolutionary Hyperparameter Optimization (DEHO) for monovariate methods</span>
<span class="sd"> :param datasetname:</span>
<span class="sd"> :param dataset: The time series to optimize the FTS</span>
<span class="sd"> :keyword file:</span>
<span class="sd"> :keyword experiments:</span>
<span class="sd"> :keyword distributed:</span>
<span class="sd"> :keyword ngen: An integer value with the maximum number of generations, default value: 30</span>
<span class="sd"> :keyword mgen: An integer value with the maximum number of generations without improvement to stop, default value 7</span>
<span class="sd"> :keyword npop: An integer value with the population size, default value: 20</span>
<span class="sd"> :keyword pcross: A float value between 0 and 1 with the probability of crossover, default: .5</span>
<span class="sd"> :keyword psel: A float value between 0 and 1 with the probability of selection, default: .5</span>
<span class="sd"> :keyword pmut: A float value between 0 and 1 with the probability of mutation, default: .3</span>
<span class="sd"> :keyword fts_method: The FTS method to optimize</span>
<span class="sd"> :keyword parameters: dict with model specific arguments for fts_method</span>
<span class="sd"> :keyword elitism: A boolean value indicating if the best individual must always survive to next population</span>
<span class="sd"> :keyword initial_operator: a function that receives npop and return a random population with size npop</span>
<span class="sd"> :keyword random_individual: create an random genotype</span>
<span class="sd"> :keyword evalutation_operator: a function that receives a dataset and an individual and return its fitness</span>
<span class="sd"> :keyword selection_operator: a function that receives the whole population and return a selected individual</span>
<span class="sd"> :keyword crossover_operator: a function that receives the whole population and return a descendent individual</span>
<span class="sd"> :keyword mutation_operator: a function that receives one individual and return a changed individual</span>
<span class="sd"> :keyword window_size: An integer value with the the length of scrolling window for train/test on dataset</span>
<span class="sd"> :keyword train_rate: A float value between 0 and 1 with the train/test split ([0,1])</span>
<span class="sd"> :keyword increment_rate: A float value between 0 and 1 with the the increment of the scrolling window,</span>
<span class="sd"> relative to the window_size ([0,1])</span>
<span class="sd"> :keyword collect_statistics: A boolean value indicating to collect statistics for each generation</span>
<span class="sd"> :keyword distributed: A value indicating it the execution will be local and sequential (distributed=False),</span>
<span class="sd"> or parallel and distributed (distributed=&#39;dispy&#39; or distributed=&#39;spark&#39;)</span>
<span class="sd"> :keyword cluster: If distributed=&#39;dispy&#39; the list of cluster nodes, else if distributed=&#39;spark&#39; it is the master node</span>
<span class="sd"> :return: the best genotype</span>
<span class="sd"> &quot;&quot;&quot;</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">&#39;file&#39;</span><span class="p">,</span> <span class="s1">&#39;hyperparam.db&#39;</span><span class="p">)</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">hUtil</span><span class="o">.</span><span class="n">open_hyperparam_db</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">experiments</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">&#39;experiments&#39;</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="n">distributed</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">&#39;distributed&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="n">fts_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">&#39;fts_method&#39;</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
<span class="n">shortname</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">fts_method</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">&#39;.&#39;</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">distributed</span> <span class="o">==</span> <span class="s1">&#39;dispy&#39;</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span> <span class="k">as</span> <span class="n">dUtil</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="s1">&#39;nodes&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;127.0.0.1&#39;</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">dUtil</span><span class="o">.</span><span class="n">start_dispy_cluster</span><span class="p">(</span><span class="n">evaluate</span><span class="p">,</span> <span class="n">nodes</span><span class="o">=</span><span class="n">nodes</span><span class="p">)</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;cluster&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cluster</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</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="n">experiments</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Experiment </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">))</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">ret</span><span class="p">,</span> <span class="n">statistics</span> <span class="o">=</span> <span class="n">GeneticAlgorithm</span><span class="p">(</span><span class="n">dataset</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">ret</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">end</span> <span class="o">-</span> <span class="n">start</span>
<span class="n">experiment</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;individual&#39;</span><span class="p">:</span> <span class="n">ret</span><span class="p">,</span> <span class="s1">&#39;statistics&#39;</span><span class="p">:</span> <span class="n">statistics</span><span class="p">}</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">process_experiment</span><span class="p">(</span><span class="n">shortname</span><span class="p">,</span> <span class="n">experiment</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span>
<span class="k">if</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">&#39;dispy&#39;</span><span class="p">:</span>
<span class="n">dUtil</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="k">return</span> <span class="n">ret</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.hyperparam.Evolutionary</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,263 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.hyperparam.GridSearch &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.hyperparam.GridSearch</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.hyperparam.GridSearch</h1><div class="highlight"><pre>
<span></span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Util</span><span class="p">,</span> <span class="n">Membership</span>
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</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="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span>
<span class="kn">from</span> <span class="nn">pyFTS.hyperparam</span> <span class="k">import</span> <span class="n">Util</span> <span class="k">as</span> <span class="n">hUtil</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">itertools</span> <span class="k">import</span> <span class="n">product</span>
<div class="viewcode-block" id="dict_individual"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.GridSearch.dict_individual">[docs]</a><span class="k">def</span> <span class="nf">dict_individual</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">partitions</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">lags</span><span class="p">,</span> <span class="n">alpha_cut</span><span class="p">):</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s1">&#39;mf&#39;</span><span class="p">:</span> <span class="n">mf</span><span class="p">,</span>
<span class="s1">&#39;partitioner&#39;</span><span class="p">:</span> <span class="n">partitioner</span><span class="p">,</span>
<span class="s1">&#39;npart&#39;</span><span class="p">:</span> <span class="n">partitions</span><span class="p">,</span>
<span class="s1">&#39;alpha&#39;</span><span class="p">:</span> <span class="n">alpha_cut</span><span class="p">,</span>
<span class="s1">&#39;order&#39;</span><span class="p">:</span> <span class="n">order</span><span class="p">,</span>
<span class="s1">&#39;lags&#39;</span><span class="p">:</span> <span class="n">lags</span>
<span class="p">}</span></div>
<div class="viewcode-block" id="cluster_method"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.GridSearch.cluster_method">[docs]</a><span class="k">def</span> <span class="nf">cluster_method</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="n">dataset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Util</span><span class="p">,</span> <span class="n">Membership</span>
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</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="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span>
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span>
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">gaussmf</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span>
<span class="n">window_size</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">&#39;window_size&#39;</span><span class="p">,</span> <span class="mi">800</span><span class="p">)</span>
<span class="n">train_rate</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">&#39;train_rate&#39;</span><span class="p">,</span> <span class="o">.</span><span class="mi">8</span><span class="p">)</span>
<span class="n">increment_rate</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">&#39;increment_rate&#39;</span><span class="p">,</span> <span class="o">.</span><span class="mi">2</span><span class="p">)</span>
<span class="n">parameters</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">&#39;parameters&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">sizes</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">train</span><span class="p">,</span> <span class="n">test</span> <span class="ow">in</span> <span class="n">Util</span><span class="o">.</span><span class="n">sliding_window</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">window_size</span><span class="p">,</span> <span class="n">train</span><span class="o">=</span><span class="n">train_rate</span><span class="p">,</span> <span class="n">inc</span><span class="o">=</span><span class="n">increment_rate</span><span class="p">):</span>
<span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</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">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">individual</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">],</span> <span class="n">func</span><span class="o">=</span><span class="n">mf</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">npart</span> <span class="o">=</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">10</span> <span class="k">else</span> <span class="mi">10</span>
<span class="n">partitioner</span> <span class="o">=</span> <span class="n">Entropy</span><span class="o">.</span><span class="n">EntropyPartitioner</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">npart</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="n">mf</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="n">partitioner</span><span class="p">,</span>
<span class="n">lags</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">],</span>
<span class="n">alpha_cut</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">],</span>
<span class="n">order</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">])</span>
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train</span><span class="p">)</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
<span class="c1">#rmse, mape, u = Measures.get_point_statistics(test, model)</span>
<span class="n">rmse</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">test</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</span><span class="p">)</span>
<span class="n">size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</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">rmse</span><span class="p">)</span>
<span class="n">sizes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
<span class="k">return</span> <span class="p">{</span><span class="s1">&#39;parameters&#39;</span><span class="p">:</span> <span class="n">individual</span><span class="p">,</span> <span class="s1">&#39;rmse&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">errors</span><span class="p">),</span> <span class="s1">&#39;size&#39;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">size</span><span class="p">)}</span></div>
<div class="viewcode-block" id="process_jobs"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.GridSearch.process_jobs">[docs]</a><span class="k">def</span> <span class="nf">process_jobs</span><span class="p">(</span><span class="n">jobs</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span> <span class="k">as</span> <span class="n">dUtil</span>
<span class="kn">import</span> <span class="nn">dispy</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">job</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">jobs</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Processing job </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ct</span><span class="p">))</span>
<span class="n">result</span> <span class="o">=</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">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Processing result of </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">result</span><span class="p">))</span>
<span class="n">metrics</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;rmse&#39;</span><span class="p">:</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;rmse&#39;</span><span class="p">],</span> <span class="s1">&#39;size&#39;</span><span class="p">:</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;size&#39;</span><span class="p">]}</span>
<span class="k">for</span> <span class="n">metric</span> <span class="ow">in</span> <span class="n">metrics</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="n">param</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="s1">&#39;parameters&#39;</span><span class="p">]</span>
<span class="n">record</span> <span class="o">=</span> <span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="s1">&#39;GridSearch&#39;</span><span class="p">,</span> <span class="s1">&#39;WHOFTS&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">],</span>
<span class="n">param</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">],</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">],</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;npart&#39;</span><span class="p">],</span>
<span class="n">param</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">],</span> <span class="nb">str</span><span class="p">(</span><span class="n">param</span><span class="p">[</span><span class="s1">&#39;lags&#39;</span><span class="p">]),</span> <span class="n">metric</span><span class="p">,</span> <span class="n">metrics</span><span class="p">[</span><span class="n">metric</span><span class="p">])</span>
<span class="n">hUtil</span><span class="o">.</span><span class="n">insert_hyperparam</span><span class="p">(</span><span class="n">record</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="n">job</span><span class="o">.</span><span class="n">exception</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span></div>
<div class="viewcode-block" id="execute"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.GridSearch.execute">[docs]</a><span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">hyperparams</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">dataset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span> <span class="k">as</span> <span class="n">dUtil</span>
<span class="kn">import</span> <span class="nn">dispy</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="s1">&#39;nodes&#39;</span><span class="p">,[</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">])</span>
<span class="n">individuals</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="s1">&#39;lags&#39;</span> <span class="ow">in</span> <span class="n">hyperparams</span><span class="p">:</span>
<span class="n">lags</span> <span class="o">=</span> <span class="n">hyperparams</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;lags&#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">50</span><span class="p">)]</span>
<span class="n">keys_sorted</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">hyperparams</span><span class="o">.</span><span class="n">keys</span><span class="p">())]</span>
<span class="n">index</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">keys_sorted</span><span class="p">)):</span>
<span class="n">index</span><span class="p">[</span><span class="n">keys_sorted</span><span class="p">[</span><span class="n">k</span><span class="p">]]</span> <span class="o">=</span> <span class="n">k</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Evaluation order: </span><span class="se">\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">index</span><span class="p">))</span>
<span class="n">hp_values</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">hyperparams</span><span class="p">[</span><span class="n">hp</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">hp</span> <span class="ow">in</span> <span class="n">keys_sorted</span>
<span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Evaluation values: </span><span class="se">\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">hp_values</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">dUtil</span><span class="o">.</span><span class="n">start_dispy_cluster</span><span class="p">(</span><span class="n">cluster_method</span><span class="p">,</span> <span class="n">nodes</span><span class="o">=</span><span class="n">nodes</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">&#39;file&#39;</span><span class="p">,</span> <span class="s1">&#39;hyperparam.db&#39;</span><span class="p">)</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">hUtil</span><span class="o">.</span><span class="n">open_hyperparam_db</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="k">for</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">hp_values</span><span class="p">):</span>
<span class="n">partitions</span> <span class="o">=</span> <span class="n">instance</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="s1">&#39;partitions&#39;</span><span class="p">]]</span>
<span class="n">partitioner</span> <span class="o">=</span> <span class="n">instance</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="s1">&#39;partitioner&#39;</span><span class="p">]]</span>
<span class="n">mf</span> <span class="o">=</span> <span class="n">instance</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="s1">&#39;mf&#39;</span><span class="p">]]</span>
<span class="n">alpha_cut</span> <span class="o">=</span> <span class="n">instance</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="s1">&#39;alpha&#39;</span><span class="p">]]</span>
<span class="n">order</span> <span class="o">=</span> <span class="n">instance</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">]]</span>
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">lag1</span> <span class="ow">in</span> <span class="n">lags</span><span class="p">:</span> <span class="c1"># o é o lag1</span>
<span class="n">_lags</span> <span class="o">=</span> <span class="p">[</span><span class="n">lag1</span><span class="p">]</span>
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">if</span> <span class="n">order</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">for</span> <span class="n">lag2</span> <span class="ow">in</span> <span class="n">lags</span><span class="p">:</span> <span class="c1"># o é o lag1</span>
<span class="n">_lags2</span> <span class="o">=</span> <span class="p">[</span><span class="n">lag1</span><span class="p">,</span> <span class="n">lag1</span><span class="o">+</span><span class="n">lag2</span><span class="p">]</span>
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">if</span> <span class="n">order</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
<span class="k">for</span> <span class="n">lag3</span> <span class="ow">in</span> <span class="n">lags</span><span class="p">:</span> <span class="c1"># o é o lag1</span>
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">_lags3</span> <span class="o">=</span> <span class="p">[</span><span class="n">lag1</span><span class="p">,</span> <span class="n">lag1</span> <span class="o">+</span> <span class="n">lag2</span><span class="p">,</span> <span class="n">lag1</span> <span class="o">+</span> <span class="n">lag2</span><span class="o">+</span><span class="n">lag3</span> <span class="p">]</span>
<span class="n">individuals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dict_individual</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">partitions</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">_lags3</span><span class="p">,</span> <span class="n">alpha_cut</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">individuals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="n">dict_individual</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">partitions</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">_lags2</span><span class="p">,</span> <span class="n">alpha_cut</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">individuals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dict_individual</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">partitions</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">_lags</span><span class="p">,</span> <span class="n">alpha_cut</span><span class="p">))</span>
<span class="k">if</span> <span class="n">count</span> <span class="o">&gt;</span> <span class="mi">10</span><span class="p">:</span>
<span class="n">jobs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">individuals</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Testing individual </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ind</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">ind</span><span class="p">,</span> <span class="n">dataset</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">process_jobs</span><span class="p">(</span><span class="n">jobs</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span>
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">individuals</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">dUtil</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></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.hyperparam.GridSearch</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -51,7 +51,7 @@
<div class="viewcode-block" id="open_hyperparam_db"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Util.open_hyperparam_db">[docs]</a><span class="k">def</span> <span class="nf">open_hyperparam_db</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Open a connection with a Sqlite database designed to store benchmark results.</span>
<span class="sd"> :param name: database filenem</span>
@ -68,7 +68,7 @@
<div class="viewcode-block" id="create_hyperparam_tables"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Util.create_hyperparam_tables">[docs]</a><span class="k">def</span> <span class="nf">create_hyperparam_tables</span><span class="p">(</span><span class="n">conn</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create a sqlite3 table designed to store benchmark results.</span>
<span class="sd"> :param conn: a sqlite3 database connection</span>
@ -85,7 +85,7 @@
<div class="viewcode-block" id="insert_hyperparam"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Util.insert_hyperparam">[docs]</a><span class="k">def</span> <span class="nf">insert_hyperparam</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Insert benchmark data on database</span>
<span class="sd"> :param data: a tuple with the benchmark data with format:</span>
@ -154,7 +154,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,180 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.chen &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.chen</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.chen</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">First Order Conventional Fuzzy Time Series by Chen (1996)</span>
<span class="sd">S.-M. Chen, “Forecasting enrollments based on fuzzy time series,” Fuzzy Sets Syst., vol. 81, no. 3, pp. 311319, 1996.</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">flrg</span>
<div class="viewcode-block" id="ConventionalFLRG"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.chen.ConventionalFLRG">[docs]</a><span class="k">class</span> <span class="nc">ConventionalFLRG</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;First Order Conventional Fuzzy Logical Relationship Group&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">LHS</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ConventionalFLRG</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span> <span class="o">=</span> <span class="n">LHS</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
<div class="viewcode-block" id="ConventionalFLRG.get_key"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.chen.ConventionalFLRG.get_key">[docs]</a> <span class="k">def</span> <span class="nf">get_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">return</span> <span class="n">sets</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">name</span></div>
<div class="viewcode-block" id="ConventionalFLRG.append_rhs"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.chen.ConventionalFLRG.append_rhs">[docs]</a> <span class="k">def</span> <span class="nf">append_rhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">c</span><span class="p">)</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tmp2</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="n">tmp2</span> <span class="o">+</span> <span class="s2">&quot;,&quot;</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="n">tmp2</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="k">return</span> <span class="n">tmp</span> <span class="o">+</span> <span class="n">tmp2</span></div>
<div class="viewcode-block" id="ConventionalFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.chen.ConventionalFTS">[docs]</a><span class="k">class</span> <span class="nc">ConventionalFTS</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Conventional Fuzzy Time Series&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ConventionalFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Conventional FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Chen&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;CFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span> <span class="o">=</span> <span class="p">{}</span>
<div class="viewcode-block" id="ConventionalFTS.generate_flrg"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.chen.ConventionalFTS.generate_flrg">[docs]</a> <span class="k">def</span> <span class="nf">generate_flrg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrs</span><span class="p">):</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">flrs</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flr</span><span class="o">.</span><span class="n">LHS</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span> <span class="o">=</span> <span class="n">ConventionalFLRG</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="ConventionalFTS.train"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.chen.ConventionalFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">tmpdata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">fuzzyfy</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;maximum&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;sets&#39;</span><span class="p">)</span>
<span class="n">flrs</span> <span class="o">=</span> <span class="n">FLR</span><span class="o">.</span><span class="n">generate_non_recurrent_flrs</span><span class="p">(</span><span class="n">tmpdata</span><span class="p">,</span> <span class="n">steps</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">standard_horizon</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">generate_flrg</span><span class="p">(</span><span class="n">flrs</span><span class="p">)</span></div>
<div class="viewcode-block" id="ConventionalFTS.forecast"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.chen.ConventionalFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">explain</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">&#39;explain&#39;</span><span class="p">,</span><span class="kc">False</span><span class="p">)</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">explain</span> <span class="k">else</span> <span class="mi">1</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span>
<span class="n">actual</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="o">.</span><span class="n">get_maximum_membership_fuzzyset</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Fuzzyfication:</span><span class="se">\n\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> -&gt; </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
<span class="k">if</span> <span class="n">actual</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</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">actual</span><span class="o">.</span><span class="n">centroid</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Rules:</span><span class="se">\n\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> -&gt; </span><span class="si">{}</span><span class="s2"> (Naïve)</span><span class="se">\t</span><span class="s2"> Midpoint: </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">,</span><span class="n">actual</span><span class="o">.</span><span class="n">centroid</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">_flrg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
<span class="n">mp</span> <span class="o">=</span> <span class="n">_flrg</span><span class="o">.</span><span class="n">get_midpoint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</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">mp</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Rules:</span><span class="se">\n\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> </span><span class="se">\t</span><span class="s2"> Midpoint: </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">_flrg</span><span class="p">),</span> <span class="n">mp</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Deffuzyfied value: </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mp</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.chen</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,151 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.cheng &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.cheng</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.cheng</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Trend Weighted Fuzzy Time Series by Cheng, Chen and Wu (2009)</span>
<span class="sd">C.-H. Cheng, Y.-S. Chen, and Y.-L. Wu, “Forecasting innovation diffusion of products using trend-weighted fuzzy time-series model,” </span>
<span class="sd">Expert Syst. Appl., vol. 36, no. 2, pp. 18261832, 2009.</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span>
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">yu</span>
<div class="viewcode-block" id="TrendWeightedFLRG"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.cheng.TrendWeightedFLRG">[docs]</a><span class="k">class</span> <span class="nc">TrendWeightedFLRG</span><span class="p">(</span><span class="n">yu</span><span class="o">.</span><span class="n">WeightedFLRG</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> First Order Trend Weighted Fuzzy Logical Relationship Group</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">LHS</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">TrendWeightedFLRG</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">LHS</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="o">=</span> <span class="kc">None</span>
<div class="viewcode-block" id="TrendWeightedFLRG.weights"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.cheng.TrendWeightedFLRG.weights">[docs]</a> <span class="k">def</span> <span class="nf">weights</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">count_nochange</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">count_up</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">count_down</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">weights</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">if</span> <span class="n">sets</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span> <span class="o">==</span> <span class="n">sets</span><span class="p">[</span><span class="n">c</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span><span class="p">:</span>
<span class="n">count_nochange</span> <span class="o">+=</span> <span class="mf">1.0</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">count_nochange</span>
<span class="k">elif</span> <span class="n">sets</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span> <span class="o">&gt;</span> <span class="n">sets</span><span class="p">[</span><span class="n">c</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span><span class="p">:</span>
<span class="n">count_down</span> <span class="o">+=</span> <span class="mf">1.0</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">count_down</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">count_up</span> <span class="o">+=</span> <span class="mf">1.0</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">count_up</span>
<span class="n">weights</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="n">tot</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">weights</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">k</span> <span class="o">/</span> <span class="n">tot</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">weights</span><span class="p">])</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span></div></div>
<div class="viewcode-block" id="TrendWeightedFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.cheng.TrendWeightedFTS">[docs]</a><span class="k">class</span> <span class="nc">TrendWeightedFTS</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="sd">&quot;&quot;&quot;First Order Trend Weighted Fuzzy Time Series&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">TrendWeightedFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;TWFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Trend Weighted FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Cheng&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">False</span>
<div class="viewcode-block" id="TrendWeightedFTS.generate_FLRG"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.cheng.TrendWeightedFTS.generate_FLRG">[docs]</a> <span class="k">def</span> <span class="nf">generate_FLRG</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrs</span><span class="p">):</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">flrs</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flr</span><span class="o">.</span><span class="n">LHS</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span> <span class="o">=</span> <span class="n">TrendWeightedFLRG</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.cheng</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,444 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.ensemble.ensemble &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ensemble.ensemble</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.ensemble.ensemble</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">EnsembleFTS wraps several FTS methods to ensemble their forecasts, providing point,</span>
<span class="sd">interval and probabilistic forecasting.</span>
<span class="sd">Silva, P. C. L et al. Probabilistic Forecasting with Seasonal Ensemble Fuzzy Time-Series</span>
<span class="sd">XIII Brazilian Congress on Computational Intelligence, 2017. Rio de Janeiro, Brazil.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">SortedCollection</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">tree</span>
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">chen</span><span class="p">,</span> <span class="n">cheng</span><span class="p">,</span> <span class="n">hofts</span><span class="p">,</span> <span class="n">hwang</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">yu</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.partitioners</span> <span class="k">import</span> <span class="n">Grid</span>
<span class="kn">import</span> <span class="nn">scipy.stats</span> <span class="k">as</span> <span class="nn">st</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">product</span>
<div class="viewcode-block" id="sampler"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.sampler">[docs]</a><span class="k">def</span> <span class="nf">sampler</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">quantiles</span><span class="p">,</span> <span class="n">bounds</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">qt</span> <span class="ow">in</span> <span class="n">quantiles</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">np</span><span class="o">.</span><span class="n">nanpercentile</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">q</span><span class="o">=</span><span class="n">qt</span> <span class="o">*</span> <span class="mi">100</span><span class="p">))</span>
<span class="k">if</span> <span class="n">bounds</span><span class="p">:</span>
<span class="n">ret</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="nb">min</span><span class="p">(</span><span class="n">data</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="nb">max</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="EnsembleFTS"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS">[docs]</a><span class="k">class</span> <span class="nc">EnsembleFTS</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Ensemble FTS</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">EnsembleFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;EnsembleFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Ensemble FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_wrapper</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">models</span> <span class="o">=</span> <span class="p">[]</span>
<span class="sd">&quot;&quot;&quot;A list of FTS models, the ensemble components&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">parameters</span> <span class="o">=</span> <span class="p">[]</span>
<span class="sd">&quot;&quot;&quot;A list with the parameters for each component model&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The quantiles &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">point_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">&#39;point_method&#39;</span><span class="p">,</span> <span class="s1">&#39;mean&#39;</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The method used to mix the several model&#39;s forecasts into a unique point forecast. Options: mean, median, quantile, exponential&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">interval_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">&#39;interval_method&#39;</span><span class="p">,</span> <span class="s1">&#39;quantile&#39;</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The method used to mix the several model&#39;s forecasts into a interval forecast. Options: quantile, extremum, normal&quot;&quot;&quot;</span>
<div class="viewcode-block" id="EnsembleFTS.append_model"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.append_model">[docs]</a> <span class="k">def</span> <span class="nf">append_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Append a new trained model to the ensemble</span>
<span class="sd"> :param model: FTS model</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">models</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="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">order</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">has_seasonality</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_seasonality</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">original_min</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">original_min</span>
<span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">original_max</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">original_max</span></div>
<div class="viewcode-block" id="EnsembleFTS.get_UoD"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.get_UoD">[docs]</a> <span class="k">def</span> <span class="nf">get_UoD</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">original_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_max</span><span class="p">]</span></div>
<div class="viewcode-block" id="EnsembleFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">pass</span></div>
<div class="viewcode-block" id="EnsembleFTS.get_models_forecasts"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.get_models_forecasts">[docs]</a> <span class="k">def</span> <span class="nf">get_models_forecasts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">data</span><span class="p">):</span>
<span class="n">tmp</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="bp">self</span><span class="o">.</span><span class="n">models</span><span class="p">:</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="n">has_seasonality</span><span class="p">:</span>
<span class="n">forecast</span> <span class="o">=</span> <span class="n">model</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="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="o">-</span><span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:]</span>
<span class="n">forecast</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">forecast</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">forecast</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">forecast</span> <span class="o">=</span> <span class="n">forecast</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">forecast</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">forecast</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">forecast</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">forecast</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">forecast</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">tmp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">forecast</span><span class="p">)</span>
<span class="k">return</span> <span class="n">tmp</span></div>
<div class="viewcode-block" id="EnsembleFTS.get_point"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.get_point">[docs]</a> <span class="k">def</span> <span class="nf">get_point</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">forecasts</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">point_method</span> <span class="o">==</span> <span class="s1">&#39;mean&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">point_method</span> <span class="o">==</span> <span class="s1">&#39;median&#39;</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanpercentile</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">point_method</span> <span class="o">==</span> <span class="s1">&#39;quantile&#39;</span><span class="p">:</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span><span class="mf">0.05</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanpercentile</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="n">alpha</span><span class="o">*</span><span class="mi">100</span><span class="p">)</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">point_method</span> <span class="o">==</span> <span class="s1">&#39;exponential&#39;</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="bp">self</span><span class="o">.</span><span class="n">models</span><span class="p">)</span>
<span class="k">if</span> <span class="n">l</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">return</span> <span class="n">forecasts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">l</span> <span class="o">-</span> <span class="n">k</span><span class="p">))</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">l</span><span class="p">)])</span>
<span class="n">w</span> <span class="o">=</span> <span class="n">w</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">w</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="n">w</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">*</span> <span class="n">forecasts</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">l</span><span class="p">)])</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="EnsembleFTS.get_interval"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.get_interval">[docs]</a> <span class="k">def</span> <span class="nf">get_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">forecasts</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">interval_method</span> <span class="o">==</span> <span class="s1">&#39;extremum&#39;</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="nb">min</span><span class="p">(</span><span class="n">forecasts</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="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">interval_method</span> <span class="o">==</span> <span class="s1">&#39;quantile&#39;</span><span class="p">:</span>
<span class="n">qt_lo</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanpercentile</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="n">q</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">*</span> <span class="mi">100</span><span class="p">)</span>
<span class="n">qt_up</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanpercentile</span><span class="p">(</span><span class="n">forecasts</span><span class="p">,</span> <span class="n">q</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</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">qt_lo</span><span class="p">,</span> <span class="n">qt_up</span><span class="p">])</span>
<span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">interval_method</span> <span class="o">==</span> <span class="s1">&#39;normal&#39;</span><span class="p">:</span>
<span class="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nanvar</span><span class="p">(</span><span class="n">forecasts</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">mu</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</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">mu</span> <span class="o">+</span> <span class="n">st</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">sigma</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="EnsembleFTS.get_distribution_interquantile"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.get_distribution_interquantile">[docs]</a> <span class="k">def</span> <span class="nf">get_distribution_interquantile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">forecasts</span><span class="p">,</span> <span class="n">alpha</span><span class="p">):</span>
<span class="n">size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="n">qt_lower</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">size</span> <span class="o">*</span> <span class="n">alpha</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">qt_upper</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">size</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span> <span class="n">alpha</span><span class="p">)))</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">ret</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)[</span><span class="n">qt_lower</span> <span class="p">:</span> <span class="n">qt_upper</span><span class="p">]</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="EnsembleFTS.forecast"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="s2">&quot;method&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">point_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">&#39;method&#39;</span><span class="p">,</span><span class="s1">&#39;mean&#39;</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">data</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">point</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_point</span><span class="p">(</span><span class="n">tmp</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">point</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="EnsembleFTS.forecast_interval"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="s2">&quot;method&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">interval_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">&#39;method&#39;</span><span class="p">,</span><span class="s1">&#39;quantile&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
<span class="n">l</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="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">interval</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_interval</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">interval</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">interval</span> <span class="o">=</span> <span class="n">interval</span><span class="p">[</span><span class="o">-</span><span class="mi">1</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">interval</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="EnsembleFTS.forecast_ahead_interval"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="s1">&#39;method&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">interval_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">&#39;method&#39;</span><span class="p">,</span><span class="s1">&#39;quantile&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;alpha&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="n">sample</span> <span class="o">=</span> <span class="p">[[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sample</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">])</span>
<span class="c1"># Trace the possible paths</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">lags</span><span class="p">):</span>
<span class="n">forecasts</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
<span class="n">sample</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sampler</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">arange</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">),</span> <span class="n">bounds</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="n">interval</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_interval</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">interval</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">interval</span> <span class="o">=</span> <span class="n">interval</span><span class="p">[</span><span class="mi">0</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">interval</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
<div class="viewcode-block" id="EnsembleFTS.forecast_distribution"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">smooth</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">&quot;smooth&quot;</span><span class="p">,</span> <span class="s2">&quot;KDE&quot;</span><span class="p">)</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">uod</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_UoD</span><span class="p">()</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="k">if</span> <span class="n">alpha</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_distribution_interquantile</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span> <span class="n">alpha</span><span class="p">)</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="n">smooth</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="n">uod</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">forecasts</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="EnsembleFTS.forecast_ahead_distribution"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.EnsembleFTS.forecast_ahead_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="s1">&#39;method&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">point_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">&#39;method&#39;</span><span class="p">,</span><span class="s1">&#39;mean&#39;</span><span class="p">)</span>
<span class="n">smooth</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">&quot;smooth&quot;</span><span class="p">,</span> <span class="s2">&quot;histogram&quot;</span><span class="p">)</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="n">uod</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_UoD</span><span class="p">()</span>
<span class="n">sample</span> <span class="o">=</span> <span class="p">[[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sample</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">])</span>
<span class="c1"># Trace the possible paths</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">lags</span><span class="p">):</span>
<span class="n">forecasts</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
<span class="n">sample</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sampler</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">arange</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">),</span> <span class="n">bounds</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="k">if</span> <span class="n">alpha</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">forecasts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_distribution_interquantile</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">forecasts</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span> <span class="n">alpha</span><span class="p">)</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="n">smooth</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="n">uod</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">forecasts</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div></div>
<div class="viewcode-block" id="SimpleEnsembleFTS"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.SimpleEnsembleFTS">[docs]</a><span class="k">class</span> <span class="nc">SimpleEnsembleFTS</span><span class="p">(</span><span class="n">EnsembleFTS</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> An homogeneous FTS method ensemble with variations on partitionings and orders.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">SimpleEnsembleFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">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">&#39;fts_method&#39;</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;FTS method class that will be used on internal models&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner_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">&#39;partitioner_method&#39;</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="sd">&quot;&quot;&quot;UoD partitioner class that will be used on internal methods&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitions</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">&#39;partitions&#39;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span><span class="mi">35</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span>
<span class="sd">&quot;&quot;&quot;Possible variations of number of partitions on internal models&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">orders</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">&#39;orders&#39;</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="sd">&quot;&quot;&quot;Possible variations of order on internal models&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</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">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;EnsembleFTS-&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">method</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">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<div class="viewcode-block" id="SimpleEnsembleFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.SimpleEnsembleFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitions</span><span class="p">:</span>
<span class="n">fs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner_method</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">k</span><span class="p">)</span>
<span class="k">for</span> <span class="n">order</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">orders</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="n">fs</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">tmp</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_model</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span></div></div>
<div class="viewcode-block" id="AllMethodEnsembleFTS"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.AllMethodEnsembleFTS">[docs]</a><span class="k">class</span> <span class="nc">AllMethodEnsembleFTS</span><span class="p">(</span><span class="n">EnsembleFTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates an EnsembleFTS with all point forecast methods, sharing the same partitioner</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">AllMethodEnsembleFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">3</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span><span class="s2">&quot;Ensemble FTS&quot;</span>
<div class="viewcode-block" id="AllMethodEnsembleFTS.set_transformations"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.AllMethodEnsembleFTS.set_transformations">[docs]</a> <span class="k">def</span> <span class="nf">set_transformations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">:</span>
<span class="n">model</span><span class="o">.</span><span class="n">append_transformation</span><span class="p">(</span><span class="n">t</span><span class="p">)</span></div>
<div class="viewcode-block" id="AllMethodEnsembleFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.ensemble.AllMethodEnsembleFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">fo_methods</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">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">ismailefendi</span><span class="o">.</span><span class="n">ImprovedWeightedFTS</span><span class="p">]</span>
<span class="n">ho_methods</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">hwang</span><span class="o">.</span><span class="n">HighOrderFTS</span><span class="p">]</span>
<span class="k">for</span> <span class="n">method</span> <span class="ow">in</span> <span class="n">fo_methods</span><span class="p">:</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">set_transformations</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="k">for</span> <span class="n">method</span> <span class="ow">in</span> <span class="n">ho_methods</span><span class="p">:</span>
<span class="k">for</span> <span class="n">o</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="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="p">)</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">min_order</span> <span class="o">&gt;=</span> <span class="n">o</span><span class="p">:</span>
<span class="n">model</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">o</span>
<span class="bp">self</span><span class="o">.</span><span class="n">set_transformations</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ensemble.ensemble</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,183 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.ensemble.multiseasonal &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ensemble.multiseasonal</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.ensemble.multiseasonal</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Silva, P. C. L et al. Probabilistic Forecasting with Seasonal Ensemble Fuzzy Time-Series</span>
<span class="sd">XIII Brazilian Congress on Computational Intelligence, 2017. Rio de Janeiro, Brazil.</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">Util</span> <span class="k">as</span> <span class="n">cUtil</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.models.seasonal</span> <span class="k">import</span> <span class="n">cmsfts</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">copy</span> <span class="k">import</span> <span class="n">deepcopy</span>
<span class="kn">from</span> <span class="nn">joblib</span> <span class="k">import</span> <span class="n">Parallel</span><span class="p">,</span> <span class="n">delayed</span>
<span class="kn">import</span> <span class="nn">multiprocessing</span>
<div class="viewcode-block" id="train_individual_model"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.train_individual_model">[docs]</a><span class="k">def</span> <span class="nf">train_individual_model</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">indexer</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">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">diff</span> <span class="o">=</span> <span class="s2">&quot;_diff&quot;</span> <span class="k">if</span> <span class="n">partitioner</span><span class="o">.</span><span class="n">transformation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>
<span class="n">_key</span> <span class="o">=</span> <span class="s2">&quot;msfts_&quot;</span> <span class="o">+</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="n">partitions</span><span class="p">)</span> <span class="o">+</span> <span class="n">diff</span> <span class="o">+</span> <span class="s2">&quot;_&quot;</span> <span class="o">+</span> <span class="n">indexer</span><span class="o">.</span><span class="n">name</span>
<span class="nb">print</span><span class="p">(</span><span class="n">_key</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">cmsfts</span><span class="o">.</span><span class="n">ContextualMultiSeasonalFTS</span><span class="p">(</span><span class="n">_key</span><span class="p">,</span> <span class="n">indexer</span><span class="o">=</span><span class="n">indexer</span><span class="p">)</span>
<span class="n">model</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">model</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="n">partitioner</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">cUtil</span><span class="o">.</span><span class="n">persist_obj</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s2">&quot;models/&quot;</span><span class="o">+</span><span class="n">_key</span><span class="o">+</span><span class="s2">&quot;.pkl&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">model</span></div>
<div class="viewcode-block" id="SeasonalEnsembleFTS"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS">[docs]</a><span class="k">class</span> <span class="nc">SeasonalEnsembleFTS</span><span class="p">(</span><span class="n">ensemble</span><span class="o">.</span><span class="n">EnsembleFTS</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">SeasonalEnsembleFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Seasonal Ensemble FTS&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">indexers</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioners</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_seasonality</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_probability_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<div class="viewcode-block" id="SeasonalEnsembleFTS.update_uod"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS.update_uod">[docs]</a> <span class="k">def</span> <span class="nf">update_uod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span></div>
<div class="viewcode-block" id="SeasonalEnsembleFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">original_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="n">num_cores</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">cpu_count</span><span class="p">()</span>
<span class="n">pool</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">ix</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">indexers</span><span class="p">:</span>
<span class="k">for</span> <span class="n">pt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioners</span><span class="p">:</span>
<span class="n">pool</span><span class="p">[</span><span class="n">count</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;ix&#39;</span><span class="p">:</span> <span class="n">ix</span><span class="p">,</span> <span class="s1">&#39;pt&#39;</span><span class="p">:</span> <span class="n">pt</span><span class="p">}</span>
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">Parallel</span><span class="p">(</span><span class="n">n_jobs</span><span class="o">=</span><span class="n">num_cores</span><span class="p">)(</span>
<span class="n">delayed</span><span class="p">(</span><span class="n">train_individual_model</span><span class="p">)(</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">m</span><span class="p">][</span><span class="s1">&#39;pt&#39;</span><span class="p">]),</span> <span class="n">data</span><span class="p">,</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">m</span><span class="p">][</span><span class="s1">&#39;ix&#39;</span><span class="p">]))</span>
<span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">pool</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
<span class="k">for</span> <span class="n">tmp</span> <span class="ow">in</span> <span class="n">results</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_model</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="n">cUtil</span><span class="o">.</span><span class="n">persist_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s2">&quot;models/&quot;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="o">+</span><span class="s2">&quot;.pkl&quot;</span><span class="p">)</span></div>
<div class="viewcode-block" id="SeasonalEnsembleFTS.forecast_distribution"><a class="viewcode-back" href="../../../../pyFTS.models.ensemble.html#pyFTS.models.ensemble.multiseasonal.SeasonalEnsembleFTS.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">smooth</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">&quot;smooth&quot;</span><span class="p">,</span> <span class="s2">&quot;KDE&quot;</span><span class="p">)</span>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;alpha&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">uod</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_UoD</span><span class="p">()</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
<span class="k">if</span> <span class="n">alpha</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_distribution_interquantile</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span> <span class="n">alpha</span><span class="p">)</span>
<span class="n">name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">indexer</span><span class="o">.</span><span class="n">get_index</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">k</span><span class="p">]))</span>
<span class="n">dist</span> <span class="o">=</span> <span class="n">ProbabilityDistribution</span><span class="o">.</span><span class="n">ProbabilityDistribution</span><span class="p">(</span><span class="n">smooth</span><span class="p">,</span> <span class="n">uod</span><span class="o">=</span><span class="n">uod</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">tmp</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ensemble.multiseasonal</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,386 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.hofts &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.hofts</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.hofts</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">High Order FTS</span>
<span class="sd">Severiano, S. A. Jr; Silva, P. C. L.; Sadaei, H. J.; Guimarães, F. G. Very Short-term Solar Forecasting</span>
<span class="sd">using Fuzzy Time Series. 2017 IEEE International Conference on Fuzzy Systems. DOI10.1109/FUZZ-IEEE.2017.8015732</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">flrg</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">product</span>
<div class="viewcode-block" id="HighOrderFLRG"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFLRG">[docs]</a><span class="k">class</span> <span class="nc">HighOrderFLRG</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Conventional High Order Fuzzy Logical Relationship Group&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">HighOrderFLRG</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">strlhs</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<div class="viewcode-block" id="HighOrderFLRG.append_rhs"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFLRG.append_rhs">[docs]</a> <span class="k">def</span> <span class="nf">append_rhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">c</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">c</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">c</span></div>
<div class="viewcode-block" id="HighOrderFLRG.append_lhs"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFLRG.append_lhs">[docs]</a> <span class="k">def</span> <span class="nf">append_lhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">c</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">c</span><span class="p">,(</span><span class="nb">tuple</span><span class="p">,</span><span class="nb">list</span><span class="p">)):</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">)</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">tmp</span> <span class="o">+</span> <span class="s2">&quot;,&quot;</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="n">tmp</span> <span class="o">+</span> <span class="n">c</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span> <span class="o">+</span> <span class="n">tmp</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="WeightedHighOrderFLRG"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFLRG">[docs]</a><span class="k">class</span> <span class="nc">WeightedHighOrderFLRG</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Weighted High Order Fuzzy Logical Relationship Group&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">WeightedHighOrderFLRG</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">strlhs</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="o">=</span> <span class="kc">None</span>
<div class="viewcode-block" id="WeightedHighOrderFLRG.append_rhs"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFLRG.append_rhs">[docs]</a> <span class="k">def</span> <span class="nf">append_rhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">count</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">&#39;count&#39;</span><span class="p">,</span><span class="mf">1.0</span><span class="p">)</span>
<span class="k">if</span> <span class="n">fset</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">fset</span><span class="p">]</span> <span class="o">=</span> <span class="n">count</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">fset</span><span class="p">]</span> <span class="o">+=</span> <span class="n">count</span>
<span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">count</span></div>
<div class="viewcode-block" id="WeightedHighOrderFLRG.append_lhs"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFLRG.append_lhs">[docs]</a> <span class="k">def</span> <span class="nf">append_lhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">c</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">)</span></div>
<div class="viewcode-block" id="WeightedHighOrderFLRG.weights"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFLRG.weights">[docs]</a> <span class="k">def</span> <span class="nf">weights</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</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="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span></div>
<div class="viewcode-block" id="WeightedHighOrderFLRG.get_midpoint"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFLRG.get_midpoint">[docs]</a> <span class="k">def</span> <span class="nf">get_midpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">mp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="n">c</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">weights</span><span class="p">())</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span></div>
<div class="viewcode-block" id="WeightedHighOrderFLRG.get_lower"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFLRG.get_lower">[docs]</a> <span class="k">def</span> <span class="nf">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">lower</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">lw</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lower</span> <span class="o">=</span> <span class="n">lw</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">weights</span><span class="p">())</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lower</span></div>
<div class="viewcode-block" id="WeightedHighOrderFLRG.get_upper"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFLRG.get_upper">[docs]</a> <span class="k">def</span> <span class="nf">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">up</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">upper</span> <span class="o">=</span> <span class="n">up</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">weights</span><span class="p">())</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">_str</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="n">_str</span> <span class="o">+=</span> <span class="s2">&quot;, &quot;</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">_str</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>
<span class="n">_str</span> <span class="o">+=</span> <span class="n">k</span> <span class="o">+</span> <span class="s2">&quot; (&quot;</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="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span> <span class="o">+</span> <span class="n">_str</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="HighOrderFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS">[docs]</a><span class="k">class</span> <span class="nc">HighOrderFTS</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Conventional High Order Fuzzy Time Series&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;High Order FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;HOFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Severiano, Silva, Sadaei and Guimarães&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&quot;order&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_order</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">configure_lags</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<div class="viewcode-block" id="HighOrderFTS.configure_lags"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS.configure_lags">[docs]</a> <span class="k">def</span> <span class="nf">configure_lags</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="s2">&quot;order&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&quot;order&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_order</span><span class="p">)</span>
<span class="k">if</span> <span class="s2">&quot;lags&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lags</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">&quot;lags&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">lags</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lags</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lags</span> <span class="o">=</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="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span></div>
<div class="viewcode-block" id="HighOrderFTS.generate_lhs_flrg"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS.generate_lhs_flrg">[docs]</a> <span class="k">def</span> <span class="nf">generate_lhs_flrg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">,</span> <span class="n">explain</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="n">nsample</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">fuzzyfy</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;sets&quot;</span><span class="p">,</span> <span class="n">alpha_cut</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha_cut</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">sample</span><span class="p">]</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_log</span><span class="p">(</span><span class="s2">&quot;Fuzzyfication&quot;</span><span class="p">,</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> -&gt; </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">nsample</span><span class="p">))</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrg_fuzzyfied</span><span class="p">(</span><span class="n">nsample</span><span class="p">,</span> <span class="n">explain</span><span class="p">)</span></div>
<div class="viewcode-block" id="HighOrderFTS.generate_lhs_flrg_fuzzyfied"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS.generate_lhs_flrg_fuzzyfied">[docs]</a> <span class="k">def</span> <span class="nf">generate_lhs_flrg_fuzzyfied</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">,</span> <span class="n">explain</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">flrgs</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">o</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lags</span><span class="p">):</span>
<span class="n">lhs</span> <span class="o">=</span> <span class="n">sample</span><span class="p">[</span><span class="n">o</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">lhs</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_log</span><span class="p">(</span><span class="s2">&quot;Ordering Lags&quot;</span><span class="p">,</span> <span class="s2">&quot;Lag </span><span class="si">{}</span><span class="s2"> Value </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">o</span><span class="p">,</span> <span class="n">lhs</span><span class="p">))</span>
<span class="c1"># Trace the possible paths</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">lags</span><span class="p">):</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">HighOrderFLRG</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="k">for</span> <span class="n">lhs</span> <span class="ow">in</span> <span class="n">path</span><span class="p">:</span>
<span class="n">flrg</span><span class="o">.</span><span class="n">append_lhs</span><span class="p">(</span><span class="n">lhs</span><span class="p">)</span>
<span class="n">flrgs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">flrg</span><span class="p">)</span>
<span class="k">return</span> <span class="n">flrgs</span></div>
<div class="viewcode-block" id="HighOrderFTS.generate_flrg"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS.generate_flrg">[docs]</a> <span class="k">def</span> <span class="nf">generate_flrg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">_tmp_steps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">standard_horizon</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">l</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="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">l</span> <span class="o">-</span> <span class="n">_tmp_steps</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dump</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;FLR: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">k</span><span class="p">))</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">rhs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">fuzzyfy</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">+</span> <span class="n">_tmp_steps</span><span class="p">],</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;sets&quot;</span><span class="p">,</span> <span class="n">alpha_cut</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha_cut</span><span class="p">)</span>
<span class="n">flrgs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrg</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="k">for</span> <span class="n">flrg</span> <span class="ow">in</span> <span class="n">flrgs</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span> <span class="o">=</span> <span class="n">flrg</span><span class="p">;</span>
<span class="k">for</span> <span class="n">st</span> <span class="ow">in</span> <span class="n">rhs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">st</span><span class="p">)</span></div>
<div class="viewcode-block" id="HighOrderFTS.generate_flrg_fuzzyfied"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS.generate_flrg_fuzzyfied">[docs]</a> <span class="k">def</span> <span class="nf">generate_flrg_fuzzyfied</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">_tmp_steps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">standard_horizon</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">l</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="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">l</span> <span class="o">-</span> <span class="n">_tmp_steps</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dump</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;FLR: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">k</span><span class="p">))</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">rhs</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">+</span> <span class="n">_tmp_steps</span><span class="p">]</span>
<span class="n">flrgs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrg_fuzzyfied</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="k">for</span> <span class="n">flrg</span> <span class="ow">in</span> <span class="n">flrgs</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span> <span class="o">=</span> <span class="n">flrg</span>
<span class="k">for</span> <span class="n">st</span> <span class="ow">in</span> <span class="n">rhs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">st</span><span class="p">)</span></div>
<div class="viewcode-block" id="HighOrderFTS.train"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">configure_lags</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="ow">not</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;fuzzyfied&#39;</span><span class="p">,</span><span class="kc">False</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">generate_flrg</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">generate_flrg_fuzzyfied</span><span class="p">(</span><span class="n">data</span><span class="p">)</span></div>
<div class="viewcode-block" id="HighOrderFTS.forecast"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.HighOrderFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">explain</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">&#39;explain&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="n">fuzzyfied</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">&#39;fuzzyfied&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="n">mode</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">&#39;mode&#39;</span><span class="p">,</span> <span class="s1">&#39;mean&#39;</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">explain</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">if</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span>
<span class="k">return</span> <span class="n">ndata</span>
<span class="k">elif</span> <span class="n">l</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span>
<span class="n">l</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">fuzzyfied</span><span class="p">:</span>
<span class="n">flrgs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrg</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">explain</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">flrgs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrg_fuzzyfied</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">explain</span><span class="p">)</span>
<span class="n">midpoints</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">memberships</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">flrg</span> <span class="ow">in</span> <span class="n">flrgs</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">mp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">mv</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">sample</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="ow">not</span> <span class="n">fuzzyfied</span> <span class="k">else</span> <span class="kc">None</span>
<span class="n">midpoints</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mp</span><span class="p">)</span>
<span class="n">memberships</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_log</span><span class="p">(</span><span class="s2">&quot;Rule Matching&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> -&gt; </span><span class="si">{}</span><span class="s2"> (Naïve) Midpoint: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">),</span> <span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span>
<span class="n">mp</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span>
<span class="n">mp</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_midpoint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="n">mv</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_membership</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">fuzzyfied</span> <span class="k">else</span> <span class="kc">None</span>
<span class="n">midpoints</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mp</span><span class="p">)</span>
<span class="n">memberships</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_log</span><span class="p">(</span><span class="s2">&quot;Rule Matching&quot;</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">, Midpoint: </span><span class="si">{}</span><span class="s2"> Membership: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">(),</span> <span class="n">mp</span><span class="p">,</span> <span class="n">mv</span><span class="p">))</span>
<span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;mean&quot;</span> <span class="ow">or</span> <span class="n">fuzzyfied</span><span class="p">:</span>
<span class="n">final</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">midpoints</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">append_log</span><span class="p">(</span><span class="s2">&quot;Deffuzyfication&quot;</span><span class="p">,</span> <span class="s2">&quot;By Mean: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">final</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">final</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">midpoints</span><span class="p">,</span> <span class="n">memberships</span><span class="p">)</span><span class="o">/</span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">memberships</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">append_log</span><span class="p">(</span><span class="s2">&quot;Deffuzyfication&quot;</span><span class="p">,</span> <span class="s2">&quot;By Memberships: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">final</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">final</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div></div>
<div class="viewcode-block" id="WeightedHighOrderFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFTS">[docs]</a><span class="k">class</span> <span class="nc">WeightedHighOrderFTS</span><span class="p">(</span><span class="n">HighOrderFTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Weighted High Order Fuzzy Time Series&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">WeightedHighOrderFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Weighted High Order FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;WHOFTS&quot;</span>
<div class="viewcode-block" id="WeightedHighOrderFTS.generate_lhs_flrg_fuzzyfied"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hofts.WeightedHighOrderFTS.generate_lhs_flrg_fuzzyfied">[docs]</a> <span class="k">def</span> <span class="nf">generate_lhs_flrg_fuzzyfied</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">,</span> <span class="n">explain</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">flrgs</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">o</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lags</span><span class="p">):</span>
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sample</span><span class="p">[</span><span class="n">o</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2"> (Lag </span><span class="si">{}</span><span class="s2">) </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">o</span><span class="p">,</span> <span class="n">sample</span><span class="p">[</span><span class="n">o</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>
<span class="c1"># Trace the possible paths</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">lags</span><span class="p">):</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">WeightedHighOrderFLRG</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">)</span>
<span class="k">for</span> <span class="n">lhs</span> <span class="ow">in</span> <span class="n">path</span><span class="p">:</span>
<span class="n">flrg</span><span class="o">.</span><span class="n">append_lhs</span><span class="p">(</span><span class="n">lhs</span><span class="p">)</span>
<span class="n">flrgs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">flrg</span><span class="p">)</span>
<span class="k">return</span> <span class="n">flrgs</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.hofts</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,151 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.hwang &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.hwang</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.hwang</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">High Order Fuzzy Time Series by Hwang, Chen and Lee (1998)</span>
<span class="sd">Jeng-Ren Hwang, Shyi-Ming Chen, and Chia-Hoang Lee, “Handling forecasting problems using fuzzy time series,” </span>
<span class="sd">Fuzzy Sets Syst., no. 100, pp. 217228, 1998.</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span>
<div class="viewcode-block" id="HighOrderFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hwang.HighOrderFTS">[docs]</a><span class="k">class</span> <span class="nc">HighOrderFTS</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">HighOrderFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">2</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Hwang High Order FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;Hwang&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Hwang&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">configure_lags</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<div class="viewcode-block" id="HighOrderFTS.configure_lags"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hwang.HighOrderFTS.configure_lags">[docs]</a> <span class="k">def</span> <span class="nf">configure_lags</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="s2">&quot;order&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&quot;order&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span></div>
<div class="viewcode-block" id="HighOrderFTS.forecast"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hwang.HighOrderFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="p">)</span>
<span class="n">cn</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="mf">0.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="n">l</span><span class="p">)])</span>
<span class="n">ow</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="mf">0.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="n">l</span><span class="p">)]</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)])</span>
<span class="n">rn</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="mf">0.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="n">l</span><span class="p">)]</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)])</span>
<span class="n">ft</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="mf">0.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="n">l</span><span class="p">)])</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)):</span>
<span class="k">for</span> <span class="n">ix</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
<span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span>
<span class="n">cn</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span> <span class="n">FuzzySet</span><span class="o">.</span><span class="n">grant_bounds</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">t</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">))</span>
<span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span>
<span class="n">ow</span><span class="p">[</span><span class="n">w</span><span class="p">,</span> <span class="n">ix</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">FuzzySet</span><span class="o">.</span><span class="n">grant_bounds</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">t</span> <span class="o">-</span> <span class="n">w</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">))</span>
<span class="n">rn</span><span class="p">[</span><span class="n">w</span><span class="p">,</span> <span class="n">ix</span><span class="p">]</span> <span class="o">=</span> <span class="n">ow</span><span class="p">[</span><span class="n">w</span><span class="p">,</span> <span class="n">ix</span><span class="p">]</span> <span class="o">*</span> <span class="n">cn</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span>
<span class="n">ft</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ft</span><span class="p">[</span><span class="n">ix</span><span class="p">],</span> <span class="n">rn</span><span class="p">[</span><span class="n">w</span><span class="p">,</span> <span class="n">ix</span><span class="p">])</span>
<span class="n">mft</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ft</span><span class="p">)</span>
<span class="n">out</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">count</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">ix</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
<span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span>
<span class="k">if</span> <span class="n">ft</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span> <span class="o">==</span> <span class="n">mft</span><span class="p">:</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">out</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">count</span> <span class="o">+=</span> <span class="mf">1.0</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">out</span> <span class="o">/</span> <span class="n">count</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="HighOrderFTS.train"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.hwang.HighOrderFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">configure_lags</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.hwang</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,276 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.ifts &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ifts</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.ifts</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf8 -*-</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">High Order Interval Fuzzy Time Series</span>
<span class="sd">SILVA, Petrônio CL; SADAEI, Hossein Javedani; GUIMARÃES, Frederico Gadelha. Interval Forecasting with Fuzzy Time Series.</span>
<span class="sd">In: Computational Intelligence (SSCI), 2016 IEEE Symposium Series on. IEEE, 2016. p. 1-8.</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">tree</span>
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span>
<div class="viewcode-block" id="IntervalFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.IntervalFTS">[docs]</a><span class="k">class</span> <span class="nc">IntervalFTS</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="sd">&quot;&quot;&quot;</span>
<span class="sd"> High Order Interval Fuzzy Time Series</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">IntervalFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;IFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Interval FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Silva, P.; Guimarães, F.; Sadaei, H. (2016)&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
<div class="viewcode-block" id="IntervalFTS.get_upper"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.IntervalFTS.get_upper">[docs]</a> <span class="k">def</span> <span class="nf">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrg</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">upper</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="IntervalFTS.get_lower"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.IntervalFTS.get_lower">[docs]</a> <span class="k">def</span> <span class="nf">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrg</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">lower</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="IntervalFTS.get_sequence_membership"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.IntervalFTS.get_sequence_membership">[docs]</a> <span class="k">def</span> <span class="nf">get_sequence_membership</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">fuzzySets</span><span class="p">):</span>
<span class="n">mb</span> <span class="o">=</span> <span class="p">[</span><span class="n">fuzzySets</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="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="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">))]</span>
<span class="k">return</span> <span class="n">mb</span></div>
<div class="viewcode-block" id="IntervalFTS.forecast_interval"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.IntervalFTS.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="k">if</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
<span class="k">return</span> <span class="n">ndata</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">flrgs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrg</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">up</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lo</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">affected_flrgs_memberships</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">flrg</span> <span class="ow">in</span> <span class="n">flrgs</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">mv</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_membership</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="n">up</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_upper</span><span class="p">(</span><span class="n">flrg</span><span class="p">))</span>
<span class="n">lo</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_lower</span><span class="p">(</span><span class="n">flrg</span><span class="p">))</span>
<span class="n">affected_flrgs_memberships</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span><span class="p">)</span>
<span class="c1"># gerar o intervalo</span>
<span class="n">norm</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">affected_flrgs_memberships</span><span class="p">)</span>
<span class="n">lo_</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">lo</span><span class="p">)</span> <span class="o">/</span> <span class="n">norm</span>
<span class="n">up_</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">up</span><span class="p">)</span> <span class="o">/</span> <span class="n">norm</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">lo_</span><span class="p">,</span> <span class="n">up_</span><span class="p">])</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="IntervalFTS.forecast_ahead_interval"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.IntervalFTS.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">+</span> <span class="n">steps</span><span class="p">):</span>
<span class="n">interval_lower</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_uod</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">([</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">ret</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]])[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">interval_upper</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_uod</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">([</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">ret</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]])[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">interval</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="n">interval_lower</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmax</span><span class="p">(</span><span class="n">interval_upper</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">interval</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div></div>
<div class="viewcode-block" id="WeightedIntervalFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.WeightedIntervalFTS">[docs]</a><span class="k">class</span> <span class="nc">WeightedIntervalFTS</span><span class="p">(</span><span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Weighted High Order Interval Fuzzy Time Series</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">WeightedIntervalFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;WIFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Weighted Interval FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Silva, P.; Guimarães, F.; Sadaei, H. (2016)&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_point_forecasting</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_interval_forecasting</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_order</span> <span class="o">=</span> <span class="mi">1</span>
<div class="viewcode-block" id="WeightedIntervalFTS.get_upper"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.WeightedIntervalFTS.get_upper">[docs]</a> <span class="k">def</span> <span class="nf">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrg</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">upper</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="WeightedIntervalFTS.get_lower"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.WeightedIntervalFTS.get_lower">[docs]</a> <span class="k">def</span> <span class="nf">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrg</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">lower</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="WeightedIntervalFTS.get_sequence_membership"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.WeightedIntervalFTS.get_sequence_membership">[docs]</a> <span class="k">def</span> <span class="nf">get_sequence_membership</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">fuzzySets</span><span class="p">):</span>
<span class="n">mb</span> <span class="o">=</span> <span class="p">[</span><span class="n">fuzzySets</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="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="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">))]</span>
<span class="k">return</span> <span class="n">mb</span></div>
<div class="viewcode-block" id="WeightedIntervalFTS.forecast_interval"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.WeightedIntervalFTS.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="k">if</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
<span class="k">return</span> <span class="n">ndata</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">flrgs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrg</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">up</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lo</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">affected_flrgs_memberships</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">flrg</span> <span class="ow">in</span> <span class="n">flrgs</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">mv</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_membership</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="n">up</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_upper</span><span class="p">(</span><span class="n">flrg</span><span class="p">))</span>
<span class="n">lo</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_lower</span><span class="p">(</span><span class="n">flrg</span><span class="p">))</span>
<span class="n">affected_flrgs_memberships</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span><span class="p">)</span>
<span class="c1"># gerar o intervalo</span>
<span class="n">norm</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">affected_flrgs_memberships</span><span class="p">)</span>
<span class="n">lo_</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">lo</span><span class="p">)</span> <span class="o">/</span> <span class="n">norm</span>
<span class="n">up_</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">up</span><span class="p">)</span> <span class="o">/</span> <span class="n">norm</span>
<span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">lo_</span><span class="p">,</span> <span class="n">up_</span><span class="p">])</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="WeightedIntervalFTS.forecast_ahead_interval"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ifts.WeightedIntervalFTS.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">start</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">+</span> <span class="n">steps</span><span class="p">):</span>
<span class="n">interval_lower</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_uod</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">([</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">ret</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]])[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">interval_upper</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clip_uod</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">([</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">ret</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:</span> <span class="n">k</span><span class="p">]])[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">interval</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nanmin</span><span class="p">(</span><span class="n">interval_lower</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmax</span><span class="p">(</span><span class="n">interval_upper</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">interval</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ifts</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,215 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.incremental.IncrementalEnsemble &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.incremental.IncrementalEnsemble</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.incremental.IncrementalEnsemble</h1><div class="highlight"><pre>
<span></span><span class="sd">&#39;&#39;&#39;</span>
<span class="sd">Time Variant/Incremental Ensemble of FTS methods</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">flrg</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">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.ensemble</span> <span class="k">import</span> <span class="n">ensemble</span>
<div class="viewcode-block" id="IncrementalEnsembleFTS"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.IncrementalEnsemble.IncrementalEnsembleFTS">[docs]</a><span class="k">class</span> <span class="nc">IncrementalEnsembleFTS</span><span class="p">(</span><span class="n">ensemble</span><span class="o">.</span><span class="n">EnsembleFTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Time Variant/Incremental Ensemble of FTS methods</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">IncrementalEnsembleFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;IncrementalEnsembleFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Incremental Ensemble FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&#39;order&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner_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">&#39;partitioner_method&#39;</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="sd">&quot;&quot;&quot;The partitioner method to be called when a new model is build&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner_params</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">&#39;partitioner_params&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;npart&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="p">})</span>
<span class="sd">&quot;&quot;&quot;The partitioner method parameters&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_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">&#39;fts_method&#39;</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The FTS method to be called when a new model is build&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_params</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">&#39;fts_params&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="sd">&quot;&quot;&quot;The FTS method specific parameters&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">window_length</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">&#39;window_length&#39;</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The memory window length&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">batch_size</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">&#39;batch_size&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The batch interval between each retraining&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_models</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">&#39;num_models&#39;</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The number of models to hold in the ensemble&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">point_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">&#39;point_method&#39;</span><span class="p">,</span> <span class="s1">&#39;exponential&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">window_length</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<div class="viewcode-block" id="IncrementalEnsembleFTS.offset"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.IncrementalEnsemble.IncrementalEnsembleFTS.offset">[docs]</a> <span class="k">def</span> <span class="nf">offset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span></div>
<div class="viewcode-block" id="IncrementalEnsembleFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.IncrementalEnsemble.IncrementalEnsembleFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">partitioner</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner_method</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="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner_params</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fts_method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="n">partitioner</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">fts_params</span><span class="p">)</span>
<span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">is_high_order</span><span class="p">:</span>
<span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fts_method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="n">partitioner</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">fts_params</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">append_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">models</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_models</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span></div>
<div class="viewcode-block" id="IncrementalEnsembleFTS.forecast"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.IncrementalEnsemble.IncrementalEnsembleFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">no_update</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">&#39;no_update&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="k">if</span> <span class="n">no_update</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">point</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_point</span><span class="p">(</span><span class="n">tmp</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">point</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="n">data_window</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span>
<span class="n">k2</span> <span class="o">=</span> <span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span>
<span class="n">data_window</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">k2</span><span class="p">])</span>
<span class="k">if</span> <span class="n">k2</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">window_length</span><span class="p">:</span>
<span class="n">data_window</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">if</span> <span class="n">k</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">k2</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">window_length</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">data_window</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="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">models</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k2</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models_forecasts</span><span class="p">(</span><span class="n">sample</span><span class="p">)</span>
<span class="n">point</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_point</span><span class="p">(</span><span class="n">tmp</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">point</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="IncrementalEnsembleFTS.forecast_ahead"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.IncrementalEnsemble.IncrementalEnsembleFTS.forecast_ahead">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</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="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
<span class="k">return</span> <span class="n">data</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</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">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</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="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span><span class="o">+</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span><span class="n">k</span><span class="p">],</span> <span class="n">no_update</span><span class="o">=</span><span class="kc">True</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="nb">isinstance</span><span class="p">(</span><span class="n">tmp</span><span class="p">,(</span><span class="nb">list</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">tmp</span> <span class="o">=</span> <span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</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">tmp</span><span class="p">)</span>
<span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.incremental.IncrementalEnsemble</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,206 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.incremental.TimeVariant &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.incremental.TimeVariant</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.incremental.TimeVariant</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Meta model that wraps another FTS method and continously retrain it using a data window with</span>
<span class="sd">the most recent data</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">flrg</span>
<span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">Grid</span>
<div class="viewcode-block" id="Retrainer"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.TimeVariant.Retrainer">[docs]</a><span class="k">class</span> <span class="nc">Retrainer</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Meta model for incremental/online learning that retrain its internal model after</span>
<span class="sd"> data windows controlled by the parameter &#39;batch_size&#39;, using as the training data a</span>
<span class="sd"> window of recent lags, whose size is controlled by the parameter &#39;window_length&#39;.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">Retrainer</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner_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">&#39;partitioner_method&#39;</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="sd">&quot;&quot;&quot;The partitioner method to be called when a new model is build&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner_params</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">&#39;partitioner_params&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;npart&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="p">})</span>
<span class="sd">&quot;&quot;&quot;The partitioner method parameters&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="kc">None</span>
<span class="sd">&quot;&quot;&quot;The most recent trained partitioner&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_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">&#39;fts_method&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The FTS method to be called when a new model is build&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_params</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">&#39;fts_params&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="sd">&quot;&quot;&quot;The FTS method specific parameters&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
<span class="sd">&quot;&quot;&quot;The most recent trained model&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">window_length</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">&#39;window_length&#39;</span><span class="p">,</span><span class="mi">100</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The memory window length&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">auto_update</span> <span class="o">=</span> <span class="kc">False</span>
<span class="sd">&quot;&quot;&quot;If true the model is updated at each time and not recreated&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">batch_size</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">&#39;batch_size&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The batch interval between each retraining&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_time_variant</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">window_length</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_wrapper</span> <span class="o">=</span> <span class="kc">True</span>
<div class="viewcode-block" id="Retrainer.train"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.TimeVariant.Retrainer.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner_method</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="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner_params</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fts_method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">fts_params</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">is_high_order</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fts_method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="p">,</span>
<span class="n">order</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">fts_params</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;TimeVariant - &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">shortname</span></div>
<div class="viewcode-block" id="Retrainer.forecast"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.TimeVariant.Retrainer.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">no_update</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">&#39;no_update&#39;</span><span class="p">,</span><span class="kc">False</span><span class="p">)</span>
<span class="k">if</span> <span class="n">no_update</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">horizon</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">window_length</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="n">ret</span> <span class="o">=</span> <span class="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="n">horizon</span><span class="p">,</span> <span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
<span class="n">_train</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="n">horizon</span><span class="p">:</span> <span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">]</span>
<span class="n">_test</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span> <span class="n">k</span><span class="p">]</span>
<span class="k">if</span> <span class="n">k</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto_update</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</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="k">else</span><span class="p">:</span>
<span class="bp">self</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="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">predict</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="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="Retrainer.forecast_ahead"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.TimeVariant.Retrainer.forecast_ahead">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</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="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
<span class="k">return</span> <span class="n">data</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</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">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</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="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">steps</span><span class="o">+</span><span class="n">start</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="n">k</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span><span class="n">k</span><span class="p">],</span> <span class="n">no_update</span><span class="o">=</span><span class="kc">True</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="nb">isinstance</span><span class="p">(</span><span class="n">tmp</span><span class="p">,(</span><span class="nb">list</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">tmp</span> <span class="o">=</span> <span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</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">tmp</span><span class="p">)</span>
<span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
<div class="viewcode-block" id="Retrainer.offset"><a class="viewcode-back" href="../../../../pyFTS.models.incremental.html#pyFTS.models.incremental.TimeVariant.Retrainer.offset">[docs]</a> <span class="k">def</span> <span class="nf">offset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;String representation of the model&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> The length (number of rules) of the model</span>
<span class="sd"> :return: number of rules</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.incremental.TimeVariant</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,202 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.ismailefendi &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ismailefendi</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.ismailefendi</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">First Order Improved Weighted Fuzzy Time Series by Efendi, Ismail and Deris (2013)</span>
<span class="sd">R. Efendi, Z. Ismail, and M. M. Deris, “Improved weight Fuzzy Time Series as used in the exchange rates forecasting of </span>
<span class="sd">US Dollar to Ringgit Malaysia,” Int. J. Comput. Intell. Appl., vol. 12, no. 1, p. 1350005, 2013.</span>
<span class="sd">&quot;&quot;&quot;</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.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">flrg</span>
<div class="viewcode-block" id="ImprovedWeightedFLRG"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ismailefendi.ImprovedWeightedFLRG">[docs]</a><span class="k">class</span> <span class="nc">ImprovedWeightedFLRG</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;First Order Improved Weighted Fuzzy Logical Relationship Group&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">LHS</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ImprovedWeightedFLRG</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</span> <span class="o">=</span> <span class="n">LHS</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">rhs_counts</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="o">=</span> <span class="kc">None</span>
<div class="viewcode-block" id="ImprovedWeightedFLRG.append_rhs"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ismailefendi.ImprovedWeightedFLRG.append_rhs">[docs]</a> <span class="k">def</span> <span class="nf">append_rhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">count</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">&#39;count&#39;</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
<span class="k">if</span> <span class="n">c</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">c</span>
<span class="bp">self</span><span class="o">.</span><span class="n">rhs_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">count</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">rhs_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">+=</span> <span class="n">count</span>
<span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">count</span></div>
<div class="viewcode-block" id="ImprovedWeightedFLRG.weights"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ismailefendi.ImprovedWeightedFLRG.weights">[docs]</a> <span class="k">def</span> <span class="nf">weights</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</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="bp">self</span><span class="o">.</span><span class="n">rhs_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">LHS</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tmp2</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="n">tmp2</span> <span class="o">+</span> <span class="s2">&quot;,&quot;</span>
<span class="n">tmp2</span> <span class="o">=</span> <span class="n">tmp2</span> <span class="o">+</span> <span class="n">c</span> <span class="o">+</span> <span class="s2">&quot;(&quot;</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="bp">self</span><span class="o">.</span><span class="n">rhs_counts</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span>
<span class="k">return</span> <span class="n">tmp</span> <span class="o">+</span> <span class="n">tmp2</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="ImprovedWeightedFTS"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ismailefendi.ImprovedWeightedFTS">[docs]</a><span class="k">class</span> <span class="nc">ImprovedWeightedFTS</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;First Order Improved Weighted Fuzzy Time Series&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ImprovedWeightedFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;IWFTS&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Improved Weighted FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detail</span> <span class="o">=</span> <span class="s2">&quot;Ismail &amp; Efendi&quot;</span>
<div class="viewcode-block" id="ImprovedWeightedFTS.generate_flrg"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ismailefendi.ImprovedWeightedFTS.generate_flrg">[docs]</a> <span class="k">def</span> <span class="nf">generate_flrg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrs</span><span class="p">):</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">flrs</span><span class="p">:</span>
<span class="k">if</span> <span class="n">flr</span><span class="o">.</span><span class="n">LHS</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span> <span class="o">=</span> <span class="n">ImprovedWeightedFLRG</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="ImprovedWeightedFTS.train"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ismailefendi.ImprovedWeightedFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">tmpdata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">fuzzyfy</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;maximum&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;sets&#39;</span><span class="p">)</span>
<span class="n">flrs</span> <span class="o">=</span> <span class="n">FLR</span><span class="o">.</span><span class="n">generate_recurrent_flrs</span><span class="p">(</span><span class="n">tmpdata</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">generate_flrg</span><span class="p">(</span><span class="n">flrs</span><span class="p">)</span></div>
<div class="viewcode-block" id="ImprovedWeightedFTS.forecast"><a class="viewcode-back" href="../../../pyFTS.models.html#pyFTS.models.ismailefendi.ImprovedWeightedFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">explain</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">&#39;explain&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">ordered_sets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">ordered_sets</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ordered_sets</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="o">.</span><span class="n">set_ordered</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">explain</span> <span class="k">else</span> <span class="mi">1</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">l</span><span class="p">):</span>
<span class="n">actual</span> <span class="o">=</span> <span class="n">FuzzySet</span><span class="o">.</span><span class="n">get_maximum_membership_fuzzyset</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">,</span> <span class="n">ordered_sets</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Fuzzyfication:</span><span class="se">\n\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> -&gt; </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ndata</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
<span class="k">if</span> <span class="n">actual</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</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">actual</span><span class="o">.</span><span class="n">centroid</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Rules:</span><span class="se">\n\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> -&gt; </span><span class="si">{}</span><span class="s2"> (Naïve)</span><span class="se">\t</span><span class="s2"> Midpoint: </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">,</span><span class="n">actual</span><span class="o">.</span><span class="n">centroid</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">actual</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
<span class="n">mp</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_midpoints</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">)</span>
<span class="n">final</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">flrg</span><span class="o">.</span><span class="n">weights</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">final</span><span class="p">)</span>
<span class="k">if</span> <span class="n">explain</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Rules:</span><span class="se">\n\n</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n\n</span><span class="s2"> &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">flrg</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Midpoints: </span><span class="se">\n\n</span><span class="s2"> </span><span class="si">{}</span><span class="se">\n\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mp</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Deffuzyfied value: </span><span class="si">{}</span><span class="s2"> </span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">final</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ret</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.ismailefendi</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -46,10 +46,10 @@
<span></span>
<div class="viewcode-block" id="FLR"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.FLR.FLR">[docs]</a><span class="k">class</span> <span class="nc">FLR</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Multivariate Fuzzy Logical Relationship&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;Multivariate Fuzzy Logical Relationship&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Creates a Fuzzy Logical Relationship</span>
<span class="sd"> :param LHS: Left Hand Side fuzzy set</span>
<span class="sd"> :param RHS: Right Hand Side fuzzy set</span>
@ -63,7 +63,7 @@
<div class="viewcode-block" id="FLR.set_rhs"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.FLR.FLR.set_rhs">[docs]</a> <span class="k">def</span> <span class="nf">set_rhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">set</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="nb">set</span></div>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> -&gt; </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">([</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">LHS</span><span class="o">.</span><span class="n">values</span><span class="p">()],</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
@ -109,7 +109,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,307 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.multivariate.cmvfts &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.cmvfts</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.multivariate.cmvfts</h1><div class="highlight"><pre>
<span></span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">fts</span><span class="p">,</span> <span class="n">flrg</span>
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.multivariate</span> <span class="k">import</span> <span class="n">mvfts</span><span class="p">,</span> <span class="n">grid</span><span class="p">,</span> <span class="n">common</span>
<span class="kn">from</span> <span class="nn">types</span> <span class="k">import</span> <span class="n">LambdaType</span>
<div class="viewcode-block" id="ClusteredMVFTS"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS">[docs]</a><span class="k">class</span> <span class="nc">ClusteredMVFTS</span><span class="p">(</span><span class="n">mvfts</span><span class="o">.</span><span class="n">MVFTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Meta model for high order, clustered multivariate FTS</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ClusteredMVFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_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">&#39;fts_method&#39;</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The FTS method to be called when a new model is build&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_params</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">&#39;fts_params&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="sd">&quot;&quot;&quot;The FTS method specific parameters&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
<span class="sd">&quot;&quot;&quot;The most recent trained model&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">knn</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">&#39;knn&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_high_order</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_clustered</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&quot;order&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lags</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">&quot;lags&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha_cut</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">&#39;alpha_cut&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;ClusteredMVFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Clustered Multivariate FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</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">&#39;pre_fuzzyfy&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fuzzyfy_mode</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">&#39;fuzzyfy_mode&#39;</span><span class="p">,</span> <span class="s1">&#39;sets&#39;</span><span class="p">)</span>
<div class="viewcode-block" id="ClusteredMVFTS.fuzzyfy"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.fuzzyfy">[docs]</a> <span class="k">def</span> <span class="nf">fuzzyfy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">data</span><span class="p">):</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">iterrows</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">)</span> <span class="k">else</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="n">data_point</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">format_data</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
<span class="n">ndata</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">fuzzyfy</span><span class="p">(</span><span class="n">data_point</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fuzzyfy_mode</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ndata</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_params</span><span class="p">[</span><span class="s1">&#39;order&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fts_method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">fts_params</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_data</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">fuzzyfied</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">prune</span><span class="p">()</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.check_data"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.check_data">[docs]</a> <span class="k">def</span> <span class="nf">check_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fuzzyfy</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">format_data</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">to_dict</span><span class="p">(</span><span class="s1">&#39;records&#39;</span><span class="p">)]</span>
<span class="k">return</span> <span class="n">ndata</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.forecast"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ndata1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_data</span><span class="p">(</span><span class="n">ndata1</span><span class="p">)</span>
<span class="n">pre_fuzz</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">&#39;pre_fuzzyfy&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">fuzzyfied</span><span class="o">=</span><span class="n">pre_fuzz</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">apply_inverse_transformations</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span>
<span class="n">params</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.forecast_interval"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">has_interval_forecasting</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;The internal method does not support interval forecasting!&quot;</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">pre_fuzz</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">&#39;pre_fuzzyfy&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</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">fuzzyfied</span><span class="o">=</span><span class="n">pre_fuzz</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.forecast_distribution"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.forecast_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">has_probability_forecasting</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;The internal method does not support probabilistic forecasting!&quot;</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">pre_fuzz</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">&#39;pre_fuzzyfy&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">forecast_distribution</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">fuzzyfied</span><span class="o">=</span><span class="n">pre_fuzz</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.forecast_ahead_distribution"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.forecast_ahead_distribution">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">generators</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">&#39;generators&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">generators</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;You must provide parameter </span><span class="se">\&#39;</span><span class="s1">generators</span><span class="se">\&#39;</span><span class="s1">! generators is a dict where the keys&#39;</span> <span class="o">+</span>
<span class="s1">&#39; are the dataframe column names (except the target_variable) and the values are &#39;</span> <span class="o">+</span>
<span class="s1">&#39;lambda functions that accept one value (the actual value of the variable) &#39;</span>
<span class="s1">&#39; and return the next value or trained FTS models that accept the actual values and &#39;</span>
<span class="s1">&#39;forecast new ones.&#39;</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">start</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</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">steps</span><span class="p">):</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_distribution</span><span class="p">(</span><span class="n">sample</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)[</span><span class="mi">0</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">tmp</span><span class="p">)</span>
<span class="n">new_data_point</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">data_label</span> <span class="ow">in</span> <span class="n">generators</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="k">if</span> <span class="n">data_label</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">],</span> <span class="n">LambdaType</span><span class="p">):</span>
<span class="n">last_data_point</span> <span class="o">=</span> <span class="n">sample</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">new_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">](</span><span class="n">last_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">])</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">],</span> <span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="n">gen_model</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span>
<span class="n">last_data_point</span> <span class="o">=</span> <span class="n">sample</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="n">gen_model</span><span class="o">.</span><span class="n">order</span><span class="p">:]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">gen_model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">last_data_point</span> <span class="o">=</span> <span class="n">last_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">new_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">gen_model</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">last_data_point</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">new_data_point</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span><span class="o">.</span><span class="n">expected_value</span><span class="p">()</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">sample</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_data_point</span><span class="p">,</span> <span class="n">ignore_index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.forecast_multivariate"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.forecast_multivariate">[docs]</a> <span class="k">def</span> <span class="nf">forecast_multivariate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">generators</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">&#39;generators&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="n">already_processed_cols</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">ret</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">fuzzyfied</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">:</span>
<span class="k">if</span> <span class="n">var</span><span class="o">.</span><span class="n">data_label</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">already_processed_cols</span><span class="p">:</span>
<span class="k">if</span> <span class="n">var</span><span class="o">.</span><span class="n">data_label</span> <span class="ow">in</span> <span class="n">generators</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">],</span> <span class="n">LambdaType</span><span class="p">):</span>
<span class="n">fx</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:</span>
<span class="n">ret</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">fx</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">:]]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">fx</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])]</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">],</span> <span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">ret</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</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">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">ret</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</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="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="n">var</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span> <span class="o">=</span> <span class="n">var</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">change_target_variable</span><span class="p">(</span><span class="n">var</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">reset_calculated_values</span><span class="p">()</span>
<span class="n">ret</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">fuzzyfied</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">pre_fuzzyfy</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">already_processed_cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">)</span>
<span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">ret</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span></div>
<div class="viewcode-block" id="ClusteredMVFTS.forecast_ahead_multivariate"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.cmvfts.ClusteredMVFTS.forecast_ahead_multivariate">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_multivariate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">+</span><span class="n">start</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">steps</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">ret</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">k</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">+</span><span class="n">k</span><span class="p">]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_multivariate</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span> <span class="n">ignore_index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;String representation of the model&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> The length (number of rules) of the model</span>
<span class="sd"> :return: number of rules</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.cmvfts</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -45,15 +45,15 @@
<h1>Source code for pyFTS.models.multivariate.common</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="kn">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">Composite</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">Composite</span>
<div class="viewcode-block" id="MultivariateFuzzySet"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.common.MultivariateFuzzySet">[docs]</a><span class="k">class</span> <span class="nc">MultivariateFuzzySet</span><span class="p">(</span><span class="n">Composite</span><span class="o">.</span><span class="n">FuzzySet</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Multivariate Composite Fuzzy Set</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create an empty composite fuzzy set</span>
<span class="sd"> :param name: fuzzy set name</span>
<span class="sd"> &quot;&quot;&quot;</span>
@ -62,7 +62,7 @@
<span class="bp">self</span><span class="o">.</span><span class="n">target_variable</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">&#39;target_variable&#39;</span><span class="p">,</span><span class="kc">None</span><span class="p">)</span>
<div class="viewcode-block" id="MultivariateFuzzySet.append_set"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.common.MultivariateFuzzySet.append_set">[docs]</a> <span class="k">def</span> <span class="nf">append_set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variable</span><span class="p">,</span> <span class="nb">set</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Appends a new fuzzy set from a new variable</span>
<span class="sd"> :param variable: an multivariate.variable instance</span>
@ -161,7 +161,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -45,15 +45,15 @@
<h1>Source code for pyFTS.models.multivariate.flrg</h1><div class="highlight"><pre>
<span></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.common</span> <span class="kn">import</span> <span class="n">flrg</span> <span class="k">as</span> <span class="n">flg</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">flrg</span> <span class="k">as</span> <span class="n">flg</span>
<div class="viewcode-block" id="FLRG"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.flrg.FLRG">[docs]</a><span class="k">class</span> <span class="nc">FLRG</span><span class="p">(</span><span class="n">flg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Multivariate Fuzzy Logical Rule Group</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">FLRG</span><span class="p">,</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&#39;order&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</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">&#39;lhs&#39;</span><span class="p">,</span> <span class="p">{})</span>
@ -90,7 +90,7 @@
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper</span></div>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">_str</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="n">_str</span> <span class="o">+=</span> <span class="s2">&quot;,&quot;</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">_str</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>
@ -138,7 +138,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,118 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.multivariate.granular &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.granular</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.multivariate.granular</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.models.multivariate</span> <span class="k">import</span> <span class="n">cmvfts</span><span class="p">,</span> <span class="n">grid</span>
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span>
<div class="viewcode-block" id="GranularWMVFTS"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.granular.GranularWMVFTS">[docs]</a><span class="k">class</span> <span class="nc">GranularWMVFTS</span><span class="p">(</span><span class="n">cmvfts</span><span class="o">.</span><span class="n">ClusteredMVFTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Granular multivariate weighted high order FTS</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">GranularWMVFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">fts_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">&#39;fts_method&#39;</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
<span class="sd">&quot;&quot;&quot;The most recent trained model&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">knn</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">&#39;knn&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&quot;order&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;FIG-FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Fuzzy Information Granular FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mode</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">&#39;mode&#39;</span><span class="p">,</span><span class="s1">&#39;sets&#39;</span><span class="p">)</span>
<div class="viewcode-block" id="GranularWMVFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.granular.GranularWMVFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="n">grid</span><span class="o">.</span><span class="n">IncrementalGridCluster</span><span class="p">(</span>
<span class="n">explanatory_variables</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">,</span>
<span class="n">target_variable</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="p">,</span>
<span class="n">neighbors</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">knn</span><span class="p">)</span>
<span class="nb">super</span><span class="p">(</span><span class="n">GranularWMVFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</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">mode</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mode</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.granular</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -43,20 +43,20 @@
<div class="body" role="main">
<h1>Source code for pyFTS.models.multivariate.grid</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.models.multivariate</span> <span class="kn">import</span> <span class="n">partitioner</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.multivariate.common</span> <span class="kn">import</span> <span class="n">MultivariateFuzzySet</span><span class="p">,</span> <span class="n">fuzzyfy_instance_clustered</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">product</span>
<span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="kn">import</span> <span class="n">KDTree</span>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.models.multivariate</span> <span class="k">import</span> <span class="n">partitioner</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.multivariate.common</span> <span class="k">import</span> <span class="n">MultivariateFuzzySet</span><span class="p">,</span> <span class="n">fuzzyfy_instance_clustered</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">product</span>
<span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="k">import</span> <span class="n">KDTree</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<div class="viewcode-block" id="GridCluster"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.grid.GridCluster">[docs]</a><span class="k">class</span> <span class="nc">GridCluster</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">MultivariatePartitioner</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A cartesian product of all fuzzy sets of all variables</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">GridCluster</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;GridCluster&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
@ -106,12 +106,12 @@
<div class="viewcode-block" id="IncrementalGridCluster"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.grid.IncrementalGridCluster">[docs]</a><span class="k">class</span> <span class="nc">IncrementalGridCluster</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">MultivariatePartitioner</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create combinations of fuzzy sets of the variables on demand, incrementally increasing the</span>
<span class="sd"> multivariate fuzzy set base.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">IncrementalGridCluster</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;IncrementalGridCluster&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
@ -222,7 +222,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,418 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.multivariate.mvfts &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.mvfts</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.multivariate.mvfts</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">fts</span><span class="p">,</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">Membership</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">pyFTS.models.multivariate</span> <span class="k">import</span> <span class="n">FLR</span> <span class="k">as</span> <span class="n">MVFLR</span><span class="p">,</span> <span class="n">common</span><span class="p">,</span> <span class="n">flrg</span> <span class="k">as</span> <span class="n">mvflrg</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">product</span>
<span class="kn">from</span> <span class="nn">types</span> <span class="k">import</span> <span class="n">LambdaType</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">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<div class="viewcode-block" id="product_dict"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.product_dict">[docs]</a><span class="k">def</span> <span class="nf">product_dict</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Code by Seth Johnson</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">keys</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="n">vals</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
<span class="k">for</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">vals</span><span class="p">):</span>
<span class="k">yield</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="n">instance</span><span class="p">))</span></div>
<div class="viewcode-block" id="MVFTS"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS">[docs]</a><span class="k">class</span> <span class="nc">MVFTS</span><span class="p">(</span><span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Multivariate extension of Chen&#39;s ConventionalFTS method</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">MVFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</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">&#39;explanatory_variables&#39;</span><span class="p">,[])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">target_variable</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">&#39;target_variable&#39;</span><span class="p">,</span><span class="kc">None</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">is_multivariate</span> <span class="o">=</span> <span class="kc">True</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;MVFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Multivariate FTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="o">=</span> <span class="kc">False</span>
<div class="viewcode-block" id="MVFTS.append_transformation"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.append_transformation">[docs]</a> <span class="k">def</span> <span class="nf">append_transformation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transformation</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="ow">not</span> <span class="n">transformation</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The transformation is not multivariate&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">transformation</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformations_param</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span></div>
<div class="viewcode-block" id="MVFTS.append_variable"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.append_variable">[docs]</a> <span class="k">def</span> <span class="nf">append_variable</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">var</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Append a new endogenous variable to the model</span>
<span class="sd"> :param var: variable object</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">var</span><span class="p">)</span></div>
<div class="viewcode-block" id="MVFTS.format_data"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.format_data">[docs]</a> <span class="k">def</span> <span class="nf">format_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">:</span>
<span class="n">ndata</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">var</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">extractor</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">])</span>
<span class="k">return</span> <span class="n">ndata</span></div>
<div class="viewcode-block" id="MVFTS.apply_transformations"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.apply_transformations">[docs]</a> <span class="k">def</span> <span class="nf">apply_transformations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">updateUoD</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">deep</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">transformation</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations</span><span class="p">):</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">transformation</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">transformations_param</span><span class="p">[</span><span class="n">ct</span><span class="p">])</span>
<span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">ndata</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span> <span class="c1">#if isinstance(ndata, pd.DataFrame) else ndata[var.data_label]</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">uod_clip</span> <span class="ow">and</span> <span class="n">var</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s1">&#39;common&#39;</span><span class="p">:</span>
<span class="n">ndata</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">clip</span><span class="p">(</span><span class="n">values</span><span class="p">,</span>
<span class="n">var</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">min</span><span class="p">,</span> <span class="n">var</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">max</span><span class="p">)</span>
<span class="n">ndata</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">var</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">return</span> <span class="n">ndata</span></div>
<div class="viewcode-block" id="MVFTS.generate_lhs_flrs"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.generate_lhs_flrs">[docs]</a> <span class="k">def</span> <span class="nf">generate_lhs_flrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">flrs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">lags</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">vc</span><span class="p">,</span> <span class="n">var</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">):</span>
<span class="n">data_point</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
<span class="n">lags</span><span class="p">[</span><span class="n">var</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">fuzzyfy_instance</span><span class="p">(</span><span class="n">data_point</span><span class="p">,</span> <span class="n">var</span><span class="p">,</span> <span class="n">tuples</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">product_dict</span><span class="p">(</span><span class="o">**</span><span class="n">lags</span><span class="p">):</span>
<span class="n">flr</span> <span class="o">=</span> <span class="n">MVFLR</span><span class="o">.</span><span class="n">FLR</span><span class="p">()</span>
<span class="n">flr</span><span class="o">.</span><span class="n">LHS</span> <span class="o">=</span> <span class="n">path</span>
<span class="c1">#for var, fset in path.items():</span>
<span class="c1"># flr.set_lhs(var, fset)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">):</span>
<span class="n">flrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">flr</span><span class="p">)</span>
<span class="k">return</span> <span class="n">flrs</span></div>
<div class="viewcode-block" id="MVFTS.generate_flrs"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.generate_flrs">[docs]</a> <span class="k">def</span> <span class="nf">generate_flrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">flrs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">ct</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="nb">len</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">index</span><span class="p">)):</span>
<span class="n">ix</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="n">ct</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">data_point</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">format_data</span><span class="p">(</span> <span class="n">data</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span> <span class="p">)</span>
<span class="n">tmp_flrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrs</span><span class="p">(</span><span class="n">data_point</span><span class="p">)</span>
<span class="n">target_ix</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="n">ct</span><span class="p">]</span>
<span class="n">target_point</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">][</span><span class="n">target_ix</span><span class="p">]</span>
<span class="n">target</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">fuzzyfy_instance</span><span class="p">(</span><span class="n">target_point</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="p">)</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">tmp_flrs</span><span class="p">:</span>
<span class="k">for</span> <span class="n">v</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">target</span><span class="p">:</span>
<span class="n">new_flr</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">flr</span><span class="p">)</span>
<span class="n">new_flr</span><span class="o">.</span><span class="n">set_rhs</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="n">flrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_flr</span><span class="p">)</span>
<span class="k">return</span> <span class="n">flrs</span></div>
<div class="viewcode-block" id="MVFTS.generate_flrg"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.generate_flrg">[docs]</a> <span class="k">def</span> <span class="nf">generate_flrg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrs</span><span class="p">):</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">flrs</span><span class="p">:</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">mvflrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">(</span><span class="n">lhs</span><span class="o">=</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span> <span class="o">=</span> <span class="n">flrg</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div>
<div class="viewcode-block" id="MVFTS.train"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.train">[docs]</a> <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">flrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_flrs</span><span class="p">(</span><span class="n">ndata</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">generate_flrg</span><span class="p">(</span><span class="n">flrs</span><span class="p">)</span></div>
<div class="viewcode-block" id="MVFTS.forecast"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.forecast">[docs]</a> <span class="k">def</span> <span class="nf">forecast</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">c</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">ndata</span><span class="o">.</span><span class="n">iterrows</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">)</span> <span class="k">else</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ndata</span><span class="p">):</span>
<span class="n">data_point</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">format_data</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
<span class="n">flrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrs</span><span class="p">(</span><span class="n">data_point</span><span class="p">)</span>
<span class="n">mvs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">mps</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">flrs</span><span class="p">:</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">mvflrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">(</span><span class="n">lhs</span><span class="o">=</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="c1">#Naïve approach is applied when no rules were found</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">:</span>
<span class="n">fs</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
<span class="n">fset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">fs</span><span class="p">]</span>
<span class="n">mp</span> <span class="o">=</span> <span class="n">fset</span><span class="o">.</span><span class="n">centroid</span>
<span class="n">mv</span> <span class="o">=</span> <span class="n">fset</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">data_point</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>
<span class="n">mvs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span><span class="p">)</span>
<span class="n">mps</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mp</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">mvs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">0.</span><span class="p">)</span>
<span class="n">mps</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">0.</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">_flrg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span>
<span class="n">mvs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_flrg</span><span class="o">.</span><span class="n">get_membership</span><span class="p">(</span><span class="n">data_point</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">))</span>
<span class="n">mps</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_flrg</span><span class="o">.</span><span class="n">get_midpoint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">))</span>
<span class="n">mv</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">mvs</span><span class="p">)</span>
<span class="n">mp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">mps</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">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">mv</span><span class="p">,</span><span class="n">mp</span><span class="o">.</span><span class="n">T</span><span class="p">)</span><span class="o">/</span><span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">mv</span><span class="p">))</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">apply_inverse_transformations</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span>
<span class="n">params</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="MVFTS.forecast_ahead"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.forecast_ahead">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">generators</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">&#39;generators&#39;</span><span class="p">,</span><span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">generators</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;You must provide parameter </span><span class="se">\&#39;</span><span class="s1">generators</span><span class="se">\&#39;</span><span class="s1">! generators is a dict where the keys&#39;</span> <span class="o">+</span>
<span class="s1">&#39; are the dataframe column names (except the target_variable) and the values are &#39;</span> <span class="o">+</span>
<span class="s1">&#39;lambda functions that accept one value (the actual value of the variable) &#39;</span>
<span class="s1">&#39; and return the next value or trained FTS models that accept the actual values and &#39;</span>
<span class="s1">&#39;forecast new ones.&#39;</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">start</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">steps</span><span class="p">):</span>
<span class="n">sample</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:]</span>
<span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">sample</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="nb">isinstance</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</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">tmp</span> <span class="o">=</span> <span class="n">tmp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</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">tmp</span><span class="p">)</span>
<span class="n">new_data_point</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">data_label</span> <span class="ow">in</span> <span class="n">generators</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="k">if</span> <span class="n">data_label</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">],</span> <span class="n">LambdaType</span><span class="p">):</span>
<span class="n">last_data_point</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">ndata</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
<span class="n">new_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">](</span><span class="n">last_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">])</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">],</span> <span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="n">gen_model</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span>
<span class="n">last_data_point</span> <span class="o">=</span> <span class="n">sample</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="n">gen_model</span><span class="o">.</span><span class="n">order</span><span class="p">:]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">gen_model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">last_data_point</span> <span class="o">=</span> <span class="n">last_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">new_data_point</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">gen_model</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">last_data_point</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">new_data_point</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_data_point</span><span class="p">,</span> <span class="n">ignore_index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
<div class="viewcode-block" id="MVFTS.forecast_interval"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.forecast_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">c</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">ndata</span><span class="o">.</span><span class="n">iterrows</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ndata</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">)</span> <span class="k">else</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ndata</span><span class="p">):</span>
<span class="n">data_point</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">format_data</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
<span class="n">flrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_lhs_flrs</span><span class="p">(</span><span class="n">data_point</span><span class="p">)</span>
<span class="n">mvs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ups</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">los</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">flrs</span><span class="p">:</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">mvflrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">(</span><span class="n">lhs</span><span class="o">=</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="c1">#Naïve approach is applied when no rules were found</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">:</span>
<span class="n">fs</span> <span class="o">=</span> <span class="n">flrg</span><span class="o">.</span><span class="n">LHS</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
<span class="n">fset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">[</span><span class="n">fs</span><span class="p">]</span>
<span class="n">up</span> <span class="o">=</span> <span class="n">fset</span><span class="o">.</span><span class="n">upper</span>
<span class="n">lo</span> <span class="o">=</span> <span class="n">fset</span><span class="o">.</span><span class="n">lower</span>
<span class="n">mv</span> <span class="o">=</span> <span class="n">fset</span><span class="o">.</span><span class="n">membership</span><span class="p">(</span><span class="n">data_point</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">name</span><span class="p">])</span>
<span class="n">mvs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mv</span><span class="p">)</span>
<span class="n">ups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">up</span><span class="p">)</span>
<span class="n">los</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">lo</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">mvs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">0.</span><span class="p">)</span>
<span class="n">ups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">0.</span><span class="p">)</span>
<span class="n">los</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mf">0.</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">_flrg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span>
<span class="n">mvs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_flrg</span><span class="o">.</span><span class="n">get_membership</span><span class="p">(</span><span class="n">data_point</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span><span class="p">))</span>
<span class="n">ups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_flrg</span><span class="o">.</span><span class="n">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">))</span>
<span class="n">los</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_flrg</span><span class="o">.</span><span class="n">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">sets</span><span class="p">))</span>
<span class="n">mv</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">mvs</span><span class="p">)</span>
<span class="n">up</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">mv</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">ups</span><span class="p">)</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">mv</span><span class="p">)</span>
<span class="n">lo</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">mv</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">los</span><span class="p">)</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">mv</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">lo</span><span class="p">,</span> <span class="n">up</span><span class="p">])</span>
<span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">apply_inverse_transformations</span><span class="p">(</span><span class="n">ret</span><span class="p">,</span>
<span class="n">params</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span></div>
<div class="viewcode-block" id="MVFTS.forecast_ahead_interval"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.forecast_ahead_interval">[docs]</a> <span class="k">def</span> <span class="nf">forecast_ahead_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">steps</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">generators</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">&#39;generators&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">generators</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;You must provide parameter </span><span class="se">\&#39;</span><span class="s1">generators</span><span class="se">\&#39;</span><span class="s1">! generators is a dict where the keys&#39;</span> <span class="o">+</span>
<span class="s1">&#39; are the dataframe column names (except the target_variable) and the values are &#39;</span> <span class="o">+</span>
<span class="s1">&#39;lambda functions that accept one value (the actual value of the variable) &#39;</span>
<span class="s1">&#39; and return the next value or trained FTS models that accept the actual values and &#39;</span>
<span class="s1">&#39;forecast new ones.&#39;</span><span class="p">)</span>
<span class="n">ndata</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">apply_transformations</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">start</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">&#39;start_at&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">ix</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">start</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">]</span>
<span class="n">lo</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span> <span class="c1">#[ndata.loc[k] for k in ix]</span>
<span class="n">up</span> <span class="o">=</span> <span class="n">ndata</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span> <span class="c1">#[ndata.loc[k] for k in ix]</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">steps</span><span class="p">):</span>
<span class="n">tmp_lo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">(</span><span class="n">lo</span><span class="p">[</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">tmp_up</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forecast_interval</span><span class="p">(</span><span class="n">up</span><span class="p">[</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)[</span><span class="mi">0</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="nb">min</span><span class="p">(</span><span class="n">tmp_lo</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">tmp_up</span><span class="p">)])</span>
<span class="n">new_data_point_lo</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">new_data_point_up</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">data_label</span> <span class="ow">in</span> <span class="n">generators</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="k">if</span> <span class="n">data_label</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">],</span> <span class="n">LambdaType</span><span class="p">):</span>
<span class="n">last_data_point_lo</span> <span class="o">=</span> <span class="n">lo</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">lo</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
<span class="n">new_data_point_lo</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">](</span><span class="n">last_data_point_lo</span><span class="p">[</span><span class="n">data_label</span><span class="p">])</span>
<span class="n">last_data_point_up</span> <span class="o">=</span> <span class="n">up</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">up</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
<span class="n">new_data_point_up</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">](</span><span class="n">last_data_point_up</span><span class="p">[</span><span class="n">data_label</span><span class="p">])</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">],</span> <span class="n">fts</span><span class="o">.</span><span class="n">FTS</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">generators</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span>
<span class="n">last_data_point_lo</span> <span class="o">=</span> <span class="n">lo</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">lo</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="o">-</span><span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:]]</span>
<span class="n">last_data_point_up</span> <span class="o">=</span> <span class="n">up</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">up</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="o">-</span><span class="n">model</span><span class="o">.</span><span class="n">order</span><span class="p">:]]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">is_multivariate</span><span class="p">:</span>
<span class="n">last_data_point_lo</span> <span class="o">=</span> <span class="n">last_data_point_lo</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">last_data_point_up</span> <span class="o">=</span> <span class="n">last_data_point_up</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">new_data_point_lo</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">last_data_point_lo</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">new_data_point_up</span><span class="p">[</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">forecast</span><span class="p">(</span><span class="n">last_data_point_up</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">new_data_point_lo</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">tmp_lo</span><span class="p">)</span>
<span class="n">new_data_point_up</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">tmp_up</span><span class="p">)</span>
<span class="n">lo</span> <span class="o">=</span> <span class="n">lo</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_data_point_lo</span><span class="p">,</span> <span class="n">ignore_index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">up</span> <span class="o">=</span> <span class="n">up</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_data_point_up</span><span class="p">,</span> <span class="n">ignore_index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">[</span><span class="o">-</span><span class="n">steps</span><span class="p">:]</span></div>
<div class="viewcode-block" id="MVFTS.clone_parameters"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.mvfts.MVFTS.clone_parameters">[docs]</a> <span class="k">def</span> <span class="nf">clone_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">MVFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">clone_parameters</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">explanatory_variables</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">explanatory_variables</span>
<span class="bp">self</span><span class="o">.</span><span class="n">target_variable</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">target_variable</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">_str</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s2">&quot;:</span><span class="se">\n</span><span class="s2">&quot;</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="n">_str</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>
<span class="k">return</span> <span class="n">_str</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.mvfts</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -43,20 +43,20 @@
<div class="body" role="main">
<h1>Source code for pyFTS.models.multivariate.partitioner</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="kn">import</span> <span class="n">partitioner</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.multivariate.common</span> <span class="kn">import</span> <span class="n">MultivariateFuzzySet</span><span class="p">,</span> <span class="n">fuzzyfy_instance_clustered</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">product</span>
<span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="kn">import</span> <span class="n">KDTree</span>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">partitioner</span>
<span class="kn">from</span> <span class="nn">pyFTS.models.multivariate.common</span> <span class="k">import</span> <span class="n">MultivariateFuzzySet</span><span class="p">,</span> <span class="n">fuzzyfy_instance_clustered</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">product</span>
<span class="kn">from</span> <span class="nn">scipy.spatial</span> <span class="k">import</span> <span class="n">KDTree</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<div class="viewcode-block" id="MultivariatePartitioner"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.partitioner.MultivariatePartitioner">[docs]</a><span class="k">class</span> <span class="nc">MultivariatePartitioner</span><span class="p">(</span><span class="n">partitioner</span><span class="o">.</span><span class="n">Partitioner</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Base class for partitioners which use the MultivariateFuzzySet</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">MultivariatePartitioner</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;MultivariatePartitioner&quot;</span><span class="p">,</span> <span class="n">preprocess</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="s1">&#39;multivariate&#39;</span>
@ -103,7 +103,7 @@
<span class="bp">self</span><span class="o">.</span><span class="n">build_index</span><span class="p">()</span></div>
<div class="viewcode-block" id="MultivariatePartitioner.search"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.partitioner.MultivariatePartitioner.search">[docs]</a> <span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Perform a search for the nearest fuzzy sets of the point &#39;data&#39;. This function were designed to work with several</span>
<span class="sd"> overlapped fuzzy sets.</span>
@ -204,7 +204,7 @@
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,172 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.multivariate.variable &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.variable</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.multivariate.variable</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">fts</span><span class="p">,</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">Membership</span><span class="p">,</span> <span class="n">tree</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">pyFTS.models.multivariate</span> <span class="k">import</span> <span class="n">FLR</span> <span class="k">as</span> <span class="n">MVFLR</span>
<div class="viewcode-block" id="Variable"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.variable.Variable">[docs]</a><span class="k">class</span> <span class="nc">Variable</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A variable of a fuzzy time series multivariate model. Each variable contains its own</span>
<span class="sd"> transformations and partitioners.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> :param name:</span>
<span class="sd"> :param \**kwargs: See below</span>
<span class="sd"> :Keyword Arguments:</span>
<span class="sd"> * *alias* -- Alternative name for the variable</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
<span class="sd">&quot;&quot;&quot;A string with the name of the variable&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alias</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">&#39;alias&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A string with the alias of the variable&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data_label</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">&#39;data_label&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;A string with the column name on DataFrame&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">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="s1">&#39;type&#39;</span><span class="p">,</span> <span class="s1">&#39;common&#39;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data_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="s1">&#39;data_type&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The type of the data column on Pandas Dataframe&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mask</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">&#39;mask&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;The mask for format the data column on Pandas Dataframe&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformation</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">&#39;transformation&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;Pre processing transformation for the variable&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformation_params</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">&#39;transformation_params&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="kc">None</span>
<span class="sd">&quot;&quot;&quot;UoD partitioner for the variable data&quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">alpha_cut</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">&#39;alpha_cut&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;Minimal membership value to be considered on fuzzyfication process&quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<div class="viewcode-block" id="Variable.build"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.variable.Variable.build">[docs]</a> <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> :param kwargs:</span>
<span class="sd"> :return:</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">fs</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;partitioner&#39;</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">mf</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;func&#39;</span><span class="p">,</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span><span class="p">)</span>
<span class="n">np</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;npart&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">data</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">&#39;data&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">kw</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;partitioner_specific&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span> <span class="o">=</span> <span class="n">fs</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="bp">self</span><span class="o">.</span><span class="n">data_label</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">np</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="n">mf</span><span class="p">,</span>
<span class="n">transformation</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">transformation</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span>
<span class="n">variable</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">partitioner</span><span class="o">.</span><span class="n">name</span></div>
<div class="viewcode-block" id="Variable.apply_transformations"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.variable.Variable.apply_transformations">[docs]</a> <span class="k">def</span> <span class="nf">apply_transformations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;params&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformation_params</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">&#39;params&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformation</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformation_params</span><span class="p">)</span>
<span class="k">return</span> <span class="n">data</span></div>
<div class="viewcode-block" id="Variable.apply_inverse_transformations"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.variable.Variable.apply_inverse_transformations">[docs]</a> <span class="k">def</span> <span class="nf">apply_inverse_transformations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;params&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">transformation_params</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">&#39;params&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformation</span><span class="o">.</span><span class="n">inverse</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">transformation_params</span><span class="p">)</span>
<span class="k">return</span> <span class="n">data</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.variable</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

View File

@ -0,0 +1,169 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pyFTS.models.multivariate.wmvfts &#8212; pyFTS 1.7 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../../../_static/bizstyle.css" />
<script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
<script src="../../../../_static/jquery.js"></script>
<script src="../../../../_static/underscore.js"></script>
<script src="../../../../_static/doctools.js"></script>
<script src="../../../../_static/bizstyle.js"></script>
<link rel="index" title="Index" href="../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<!--[if lt IE 9]>
<script src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.wmvfts</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyFTS.models.multivariate.wmvfts</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">fts</span><span class="p">,</span> <span class="n">FuzzySet</span><span class="p">,</span> <span class="n">FLR</span><span class="p">,</span> <span class="n">Membership</span><span class="p">,</span> <span class="n">tree</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">pyFTS.models.multivariate</span> <span class="k">import</span> <span class="n">mvfts</span><span class="p">,</span> <span class="n">FLR</span> <span class="k">as</span> <span class="n">MVFLR</span><span class="p">,</span> <span class="n">common</span><span class="p">,</span> <span class="n">flrg</span> <span class="k">as</span> <span class="n">mvflrg</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<div class="viewcode-block" id="WeightedFLRG"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedFLRG">[docs]</a><span class="k">class</span> <span class="nc">WeightedFLRG</span><span class="p">(</span><span class="n">mvflrg</span><span class="o">.</span><span class="n">FLRG</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Weighted Multivariate Fuzzy Logical Rule Group</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">WeightedFLRG</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">order</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">&#39;order&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">LHS</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">&#39;lhs&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="o">=</span> <span class="kc">None</span>
<div class="viewcode-block" id="WeightedFLRG.append_rhs"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedFLRG.append_rhs">[docs]</a> <span class="k">def</span> <span class="nf">append_rhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">count</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">&#39;count&#39;</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
<span class="k">if</span> <span class="n">fset</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">fset</span><span class="p">]</span> <span class="o">=</span> <span class="n">count</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">fset</span><span class="p">]</span> <span class="o">+=</span> <span class="n">count</span>
<span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">count</span></div>
<div class="viewcode-block" id="WeightedFLRG.weights"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedFLRG.weights">[docs]</a> <span class="k">def</span> <span class="nf">weights</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">w</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="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">w</span></div>
<div class="viewcode-block" id="WeightedFLRG.get_midpoint"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedFLRG.get_midpoint">[docs]</a> <span class="k">def</span> <span class="nf">get_midpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">mp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="n">c</span><span class="p">]</span><span class="o">.</span><span class="n">centroid</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">weights</span><span class="p">())</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">midpoint</span></div>
<div class="viewcode-block" id="WeightedFLRG.get_lower"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedFLRG.get_lower">[docs]</a> <span class="k">def</span> <span class="nf">get_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">lower</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">lw</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">lower</span> <span class="o">=</span> <span class="n">lw</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">weights</span><span class="p">())</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lower</span></div>
<div class="viewcode-block" id="WeightedFLRG.get_upper"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedFLRG.get_upper">[docs]</a> <span class="k">def</span> <span class="nf">get_upper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sets</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">up</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">sets</span><span class="p">[</span><span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">()])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">upper</span> <span class="o">=</span> <span class="n">up</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">weights</span><span class="p">())</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">_str</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="n">_str</span> <span class="o">+=</span> <span class="s2">&quot;, &quot;</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">_str</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>
<span class="n">_str</span> <span class="o">+=</span> <span class="n">k</span> <span class="o">+</span> <span class="s2">&quot; (&quot;</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="bp">self</span><span class="o">.</span><span class="n">RHS</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span> <span class="o">+</span> <span class="n">_str</span></div>
<div class="viewcode-block" id="WeightedMVFTS"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedMVFTS">[docs]</a><span class="k">class</span> <span class="nc">WeightedMVFTS</span><span class="p">(</span><span class="n">mvfts</span><span class="o">.</span><span class="n">MVFTS</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Weighted Multivariate FTS</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">WeightedMVFTS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">shortname</span> <span class="o">=</span> <span class="s2">&quot;WeightedMVFTS&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Weighted Multivariate FTS&quot;</span>
<div class="viewcode-block" id="WeightedMVFTS.generate_flrg"><a class="viewcode-back" href="../../../../pyFTS.models.multivariate.html#pyFTS.models.multivariate.wmvfts.WeightedMVFTS.generate_flrg">[docs]</a> <span class="k">def</span> <span class="nf">generate_flrg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flrs</span><span class="p">):</span>
<span class="k">for</span> <span class="n">flr</span> <span class="ow">in</span> <span class="n">flrs</span><span class="p">:</span>
<span class="n">flrg</span> <span class="o">=</span> <span class="n">WeightedFLRG</span><span class="p">(</span><span class="n">lhs</span><span class="o">=</span><span class="n">flr</span><span class="o">.</span><span class="n">LHS</span><span class="p">)</span>
<span class="k">if</span> <span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span> <span class="o">=</span> <span class="n">flrg</span>
<span class="bp">self</span><span class="o">.</span><span class="n">flrgs</span><span class="p">[</span><span class="n">flrg</span><span class="o">.</span><span class="n">get_key</span><span class="p">()]</span><span class="o">.</span><span class="n">append_rhs</span><span class="p">(</span><span class="n">flr</span><span class="o">.</span><span class="n">RHS</span><span class="p">)</span></div></div>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</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 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="nav-item nav-item-0"><a href="../../../../index.html">pyFTS 1.7 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">pyFTS.models.multivariate.wmvfts</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
</div>
</body>
</html>

Some files were not shown because too many files have changed in this diff Show More