<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Time Series Models</title>
<link>https://www.nopredict.com/blog/</link>
<atom:link href="https://www.nopredict.com/blog/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.6.32</generator>
<lastBuildDate>Mon, 09 Dec 2024 00:00:00 GMT</lastBuildDate>
<item>
  <title>Benchmarking ICA algorithms</title>
  <dc:creator>Alexios Galanos</dc:creator>
  <link>https://www.nopredict.com/blog/posts/2024-12-09-ica-benchmark/</link>
  <description><![CDATA[ 





<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TZ =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UTC"</span>)</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load required packages</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(iTensor)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsmarch)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(flextable)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(xts)</span></code></pre></div>
</details>
</div>
<section id="sec-motivation" class="level2">
<h2 class="anchored" data-anchor-id="sec-motivation">Motivation</h2>
<p>The Generalized Orthogonal GARCH (GOGARCH) model uses Independent Component Analysis (ICA) to extract independent sources of volatility from multivariate time series, enabling more precise and interpretable modeling of their joint dynamic structure.</p>
<p>In the <a href="https://CRAN.R-project.org/package=tsmarch">tsmarch</a> package there are 2 ICA algorithms currently implemented:</p>
<ul>
<li>The RADICAL algorithm of <span class="citation" data-cites="Learned2003">Learned-Miller and Fisher (2003)</span> which is based on an efficient entropy estimator (due to <span class="citation" data-cites="Vasicek1976">Vasicek (1976)</span>) which is robust to outliers and requires no strong characterization assumptions about the data generating process. However, despite best efforts to speed up the original implementation using multi-threading, this remains a relatively slow algorithm and also suffers from convergence issues in high dimensions.</li>
<li>The FASTICA algorithm of <span class="citation" data-cites="Hyvarinen1999">Hyvarinen (1999)</span> is a fixed point iteration method which has proven to be extremely fast and very suitable for high dimensional systems.</li>
</ul>
<p>Both of the algorithms are implemented locally in <code>tsmarch</code>, and the FASTICA is a direct translation of the Matlab code of the original authors, retaining a lot of additional options not present in other translations in R. Both of the algorithms provide exactly the same type of interface for the whitening stage such as different options for the type of covariance to use.</p>
<p>This short blog post looks at extending the benchmark provided in the <a href="https://cran.r-project.org/web/packages/iTensor/vignettes/iTensor-1.html">iTensor</a> package vignette in order to include the 2 ICA implementations in the <code>tsmarch</code> package.</p>
</section>
<section id="benchmark" class="level2">
<h2 class="anchored" data-anchor-id="benchmark">Benchmark</h2>
<p>The benchmark uses a set of 4 data sets with known independent sources (<strong>S</strong>) and uses the Correlation Index of <span class="citation" data-cites="Sobhani2022">(Sobhani et al. 2022)</span> to evaluate the performance of each ICA algorithm in extracting the true signals from the mixed series (<strong>X</strong>).</p>
<p>The 4 data sets are generated from the following processes:</p>
<ol type="1">
<li>ICA with time-independent sub-Gaussian data (tisubg)</li>
<li>ICA with time-independent super-Gaussian data (tisupg)</li>
<li>ICA with data mixed with signals having no time dependence and different kurtosis (tidkurt)</li>
<li>ICA with time-dependent data (td)</li>
</ol>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(iTensor<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toyModel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ICA_Type1"</span>),</span>
<span id="cb2-2">             iTensor<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toyModel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ICA_Type2"</span>),</span>
<span id="cb2-3">             iTensor<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toyModel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ICA_Type3"</span>),</span>
<span id="cb2-4">             iTensor<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toyModel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ICA_Type4"</span>))</span>
<span id="cb2-5">allICA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(X, J){</span>
<span id="cb2-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Classic ICA Methods</span></span>
<span id="cb2-7">  out.FastICA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ICA</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> J, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">algorithm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FastICA"</span>)</span>
<span id="cb2-8">  out.InfoMax <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ICA</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> J, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">algorithm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"InfoMax"</span>)</span>
<span id="cb2-9">  out.ExtInfoMax <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ICA</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> J, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">algorithm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ExtInfoMax"</span>)</span>
<span id="cb2-10">  out.JADE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ICA2</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> J, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">algorithm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"JADE"</span>)</span>
<span id="cb2-11">  out.tsmarch_fastica <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fastica</span>(X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">components =</span> J, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tanh"</span>)</span>
<span id="cb2-12">  out.tsmarch_radical <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radical</span>(X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">components =</span> J, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">augment =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replications =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.FastICA =</span> out.FastICA, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.InfoMax =</span> out.InfoMax,</span>
<span id="cb2-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.ExtInfoMax =</span> out.ExtInfoMax, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.JADE =</span> out.JADE,</span>
<span id="cb2-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.tsmarch_fastica =</span> out.tsmarch_fastica,</span>
<span id="cb2-16">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.tsmarch_radical =</span> out.tsmarch_radical)</span>
<span id="cb2-17">}</span>
<span id="cb2-18">data_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tisubg"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tisupg"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidkurt"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"td"</span>)</span>
<span id="cb2-19">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb2-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">allICA</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> data[[i]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X_observed, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">J =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-21">})</span>
<span id="cb2-22">corr_index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb2-23">  tmp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(out[[i]], <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, S) {<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CorrIndex</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(S, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Re</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>S)))}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S =</span> data[[i]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>S_true)</span>
<span id="cb2-24">  Name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"out."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(tmp))</span>
<span id="cb2-25">  Value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(tmp), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Algorithm =</span> Name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">DGP =</span> data_names[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Value =</span> Value)</span>
<span id="cb2-27">})</span>
<span id="cb2-28">tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dcast</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbindlist</span>(corr_index), Algorithm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>DGP, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value.var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>)</span>
<span id="cb2-29">tab[Algorithm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsmarch_fastica"</span>, Algorithm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FASTICA (tsmarch)"</span>]</span>
<span id="cb2-30">tab[Algorithm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsmarch_radical"</span>, Algorithm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RADICAL (tsmarch)"</span>]</span>
<span id="cb2-31">tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">flextable</span>(tab) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">separate_header</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> flextable<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colformat_double</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-32">tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tab <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_footer_row</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colwidths =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_paragraph</span>(</span>
<span id="cb2-33">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Notes to table: td (time dependent), tidkurt (data mixed with signals having no time dependence and different kurtosis), tisupg (time-independent super-Gaussian data), tisubg (time-independent sub-Gaussian data). The values represent the Correlation Index of Sobhani (2022) (the closer to 0, the better the performance)."</span>))</span>
<span id="cb2-34">tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tab <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">align</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">j =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"justify"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">part =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"footer"</span>)</span>
<span id="cb2-35">tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tab <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_caption</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ICA Benchmarks"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">html_escape =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-36">tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tab <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_table_properties</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"autofit"</span>)</span>
<span id="cb2-37">tab</span></code></pre></div>
</details>
<div id="tbl-ica" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-ica-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: ICA Benchmark
</figcaption>
<div aria-describedby="tbl-ica-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<div class="tabwid"><style>.cl-82d50b78{table-layout:auto;}.cl-82c9ca38{font-family:'Helvetica';font-size:11pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-82d37a2e{margin:0;text-align:left;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-82d37a38{margin:0;text-align:right;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-82d37a39{margin:0;text-align:justify;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-82d38636{background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-82d38637{background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-82d38638{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-82d38640{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-82d38641{background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-82d3864a{background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-82d3864b{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(255, 255, 255, 0.00);border-top: 0 solid rgba(255, 255, 255, 0.00);border-left: 0 solid rgba(255, 255, 255, 0.00);border-right: 0 solid rgba(255, 255, 255, 0.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table data-quarto-disable-processing="true" class="cl-82d50b78"><thead><tr style="overflow-wrap:break-word;"><th class="cl-82d38636"><p class="cl-82d37a2e"><span class="cl-82c9ca38">Algorithm</span></p></th><th class="cl-82d38637"><p class="cl-82d37a38"><span class="cl-82c9ca38">td</span></p></th><th class="cl-82d38637"><p class="cl-82d37a38"><span class="cl-82c9ca38">tidkurt</span></p></th><th class="cl-82d38637"><p class="cl-82d37a38"><span class="cl-82c9ca38">tisubg</span></p></th><th class="cl-82d38637"><p class="cl-82d37a38"><span class="cl-82c9ca38">tisupg</span></p></th></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-82d38638"><p class="cl-82d37a2e"><span class="cl-82c9ca38">ExtInfoMax</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.081</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.213</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.263</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.268</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-82d38638"><p class="cl-82d37a2e"><span class="cl-82c9ca38">FastICA</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.081</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.230</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.263</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.268</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-82d38638"><p class="cl-82d37a2e"><span class="cl-82c9ca38">InfoMax</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">8.026</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">3.799</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">7.439</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.322</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-82d38638"><p class="cl-82d37a2e"><span class="cl-82c9ca38">JADE</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.080</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.408</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.221</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.374</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-82d38638"><p class="cl-82d37a2e"><span class="cl-82c9ca38">FASTICA (tsmarch)</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.080</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.213</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.267</span></p></td><td class="cl-82d38640"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.259</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-82d38641"><p class="cl-82d37a2e"><span class="cl-82c9ca38">RADICAL (tsmarch)</span></p></td><td class="cl-82d3864a"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.106</span></p></td><td class="cl-82d3864a"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.293</span></p></td><td class="cl-82d3864a"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.173</span></p></td><td class="cl-82d3864a"><p class="cl-82d37a38"><span class="cl-82c9ca38">0.342</span></p></td></tr></tbody><tfoot><tr style="overflow-wrap:break-word;"><td colspan="5" class="cl-82d3864b"><p class="cl-82d37a39"><span class="cl-82c9ca38">Notes to table: td (time dependent), tidkurt (data mixed with signals having no time dependence and different kurtosis), tisupg (time-independent super-Gaussian data), tisubg (time-independent sub-Gaussian data). The values represent the Correlation Index of Sobhani (2022) (the closer to 0, the better the performance).</span></p></td></tr></tfoot></table></div>
</div>
</div>
</figure>
</div>
</div>
<p>The results of the small toy benchmark show that both ICA implementations in the <code>tsmarch</code> package are very competitive in terms of performance providing a certain degree of confidence.</p>
</section>
<section id="gogarch-demo-and-discussion" class="level2">
<h2 class="anchored" data-anchor-id="gogarch-demo-and-discussion">GOGARCH Demo and Discussion</h2>
<p>In this next example, we illustrate the use of the 2 algorithms in the GOGARCH model using dimensionality reduction and compare their weighted output.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(globalindices)</span>
<span id="cb3-2">train_set <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1600</span></span>
<span id="cb3-3">series <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb3-4">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.xts</span>(globalindices[, series])</span>
<span id="cb3-5">model_fastica <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gogarch_modelspec</span>(y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distribution =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nig"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gjrgarch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ica =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fastica"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">components =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">estimate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1010</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stabilization =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skew"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fine =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tanh"</span>)</span>
<span id="cb3-6">model_radical <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gogarch_modelspec</span>(y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distribution =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nig"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gjrgarch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ica =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"radical"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">components =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">estimate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1010</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">augment =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-7">w_fastica <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tsaggregate</span>(model_fastica, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb3-8">w_radical <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tsaggregate</span>(model_radical, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb3-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(y), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(w_fastica<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sigma), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weighted Sigma[t]"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xaxt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex.main =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex.axis =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(y), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(w_radical<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sigma), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span>
<span id="cb3-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topleft"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FASTICA"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RADICAL"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb3-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(y), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(w_fastica<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>skewness), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weighted Skewness[t]"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex.main =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex.axis =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb3-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(y), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(w_radical<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>skewness), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2024-12-09-ica-benchmark/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Both algorithms give reasonably close results. However, the FASTICA algorithm has a lot more options, particularly with respect to the choice of non-linearity (<code>fun</code>) and fine tuning (<code>fine</code>) functions. The choices, which relate to assumptions about the underlying source densities, are:</p>
<ol type="1">
<li><code>pow3</code> is optimal for sources with power exponential density,</li>
<li><code>skew</code> is optimal for sources with density satisfying <img src="https://latex.codecogs.com/png.latex?f(x)%20%5Cpropto%20%5Cexp(ax%5E3%20+%20bx%5E2%20+%20cx)">,</li>
<li><code>gauss</code> is optimal for sources with density satisfying <img src="https://latex.codecogs.com/png.latex?f(x)%20%5Cpropto%20%5Cexp(a%5Cexp(-x%5E2/2)+bx%5E2+cx)">,</li>
<li><code>tanh</code> is optimal for sources with symmetric two-group Gaussian location mixtures.</li>
</ol>
<p>Additionally, both the <code>tanh</code> and <code>gauss</code> functions take additional parameters which determine the degree of separation of the sources, depending on the underlying densities. A more thorough discussion is available in <span class="citation" data-cites="Virta2017">Virta and Nordhausen (2017)</span>.</p>
<p>Based on some experimentation, here are some suggestions when choosing between these algorithms:</p>
<ol type="1">
<li>When <code>components</code> &lt;= 30, choose RADICAL with <code>augment</code> set to TRUE. For speed, use <code>RcppParallel::setThreadOptions</code> as part of the code is written in C++ and benefits from multi-threading.</li>
<li>When <code>components</code>&gt; 30, use FASTICA, but experiment with different non-linearity functions and the other arguments provided by the algorithm.</li>
</ol>



</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-Hyvarinen1999" class="csl-entry">
Hyvarinen, Aapo. 1999. <span>“Fast and Robust Fixed-Point Algorithms for Independent Component Analysis.”</span> <em>IEEE Transactions on Neural Networks</em> 10 (3): 626–34.
</div>
<div id="ref-Learned2003" class="csl-entry">
Learned-Miller, Erik G, and John W Fisher. 2003. <span>“ICA Using Spacings Estimates of Entropy.”</span> <em>The Journal of Machine Learning Research</em> 4: 1271–95.
</div>
<div id="ref-Sobhani2022" class="csl-entry">
Sobhani, Elaheh, Pierre Comon, Christian Jutten, and Massoud Babaie-Zadeh. 2022. <span>“CorrIndex: A Permutation Invariant Performance Index.”</span> <em>Signal Processing</em> 195: 108457.
</div>
<div id="ref-Vasicek1976" class="csl-entry">
Vasicek, Oldrich. 1976. <span>“A Test for Normality Based on Sample Entropy.”</span> <em>Journal of the Royal Statistical Society Series B: Statistical Methodology</em> 38 (1): 54–59.
</div>
<div id="ref-Virta2017" class="csl-entry">
Virta, Joni, and Klaus Nordhausen. 2017. <span>“On the Optimal Non-Linearities for Gaussian Mixtures in FastICA.”</span> In <em>Latent Variable Analysis and Signal Separation: 13th International Conference, LVA/ICA 2017, Grenoble, France, February 21-23, 2017, Proceedings 13</em>, 427–37. Springer.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{galanos2024,
  author = {Galanos, Alexios},
  title = {Benchmarking {ICA} Algorithms},
  date = {2024-12-09},
  url = {https://www.nopredict.com/blog/posts/2024-12-09-ica-benchmark/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-galanos2024" class="csl-entry quarto-appendix-citeas">
Galanos, Alexios. 2024. <span>“Benchmarking ICA Algorithms.”</span>
December 9, 2024. <a href="https://www.nopredict.com/blog/posts/2024-12-09-ica-benchmark/">https://www.nopredict.com/blog/posts/2024-12-09-ica-benchmark/</a>.
</div></div></section></div> ]]></description>
  <category>statistics</category>
  <category>tsmarch</category>
  <guid>https://www.nopredict.com/blog/posts/2024-12-09-ica-benchmark/</guid>
  <pubDate>Mon, 09 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Messy Data and Anomaly Detection</title>
  <dc:creator>Alexios Galanos</dc:creator>
  <link>https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/</link>
  <description><![CDATA[ 





<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load required packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsmethods)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsaux)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsets)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(xts)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsdistributions)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(future.apply)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr)</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span></code></pre></div>
</details>
</div>
<section id="sec-motivation" class="level2">
<h2 class="anchored" data-anchor-id="sec-motivation">Motivation</h2>
<p>Finding stable patterns in data is a key requirement if we are to have any success in forecasting the future. These patterns are identified by analyzing the historical behavior of data, and may include seasonal patterns, particular types of growth, fixed and moving holidays and know causal drivers. These can collectively be termed the <em>signal</em>, and the remainder as the <em>noise</em>. In estimating such signals, we usually make some assumptions about the structure of the noise. However, when the data is contaminated by anomalies which violate our assumptions, then it becomes increasingly difficult to cleanly identify the signals.</p>
</section>
<section id="sec-anomalies" class="level2">
<h2 class="anchored" data-anchor-id="sec-anomalies">Types of Anomalies</h2>
<p>Large spikes in data series have typically been associated with the term outliers. These lead to clear contamination of normal process variation under most types of dynamics and is therefore the type of anomaly which is most general across different types of data generating mechanisms. While such spikes usually decay almost instantaneously, others may decay at a slower pace (temporary shifts) and some may even be permanent (level shifts). One generating mechanism for these anomalies if through a recursive AR(1) filter. The figure below shows an example of how such anomalies can be generated with different decay rates. The <em>half-life</em> of the decay of such shocks can be easily derived from the theory on ARMA processes and is equal to <img src="https://latex.codecogs.com/png.latex?-%5Ctext%7Bln%7D%5Cleft(2%5Cright)/%5Ctext%7Bln%7D%5Cleft(%5Calpha%5Cright)">, where <img src="https://latex.codecogs.com/png.latex?%5Calpha"> is the AR(1) coefficient. Thus, the closer this gets to 1, the closer the shock becomes permanent whereas a coefficient close to 0 is equivalent to an instantaneous decay.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb2-2">x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"recursive"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">init =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"recursive"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">init =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"recursive"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">init =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"recursive"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">init =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topleft"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Additive Outlier (alpha = 0.0)"</span>,</span>
<span id="cb2-9">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temporary Change (alpha = 0.5)"</span>,</span>
<span id="cb2-10">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temporary Change (alpha = 0.8)"</span>,</span>
<span id="cb2-11">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Permanent Shift (alpha = 1.0)"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="sec-digression" class="level2">
<h2 class="anchored" data-anchor-id="sec-digression">Digression: change in slope</h2>
<p>Sudden changes in the slope of the dynamics, may sometimes come about as a result of structural changes, such as technological innovation or other business changes. Such changes would technically fall under anomalies. However, there is another case which is based on the life cycle hypothesis of product adoption.<sup>1</sup> When thinking of variance stabilizing transformations such as the Box-Cox, this is similar to changes in the <img src="https://latex.codecogs.com/png.latex?%5Clambda"> parameter of the transformation. For instance, in the early stages of product adoption, we may observe exponential growth which may eventually stabilize to linear growth over time. Modelling the dynamics of such a process with standard models is likely to lead to poor long range forecasts. One interesting approach is provided in <span class="citation" data-cites="Guo2019">(Guo, Lichtendahl, and Grushka-Cockayne 2019)</span>.</p>
</section>
<section id="sec-identification" class="level2">
<h2 class="anchored" data-anchor-id="sec-identification">Identification</h2>
<p>Identifying different types of anomalies is usually undertaken as an integrated part of the assumed dynamics of the process. An established and flexible framework for forecasting economic time series is the state space model, or one of its many variants. For example, the basic structural time series model of <span class="citation" data-cites="Harvey1990">(Harvey 1990)</span> is a state of the art approach to econometric forecasting, decomposing a series into a number of independent unobserved structural components:</p>
<p><span id="eq-1"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Ay_t%20%20&amp;=%20%5Cmu_t%20+%20%5Cgamma_t%20+%20%5Cvarepsilon_t,%5Cquad%20&amp;%5Cvarepsilon_t%5Csim%20N(0,%5Csigma)%5C%5C%0A%5Cmu_t%20&amp;=%5Cmu_%7Bt-1%7D%20+%20%5Cbeta_%7Bt-1%7D%20+%20%5Ceta_t,%5Cquad%20&amp;%5Ceta_t%5Csim%20N(0,%5Csigma_%5Ceta)%5C%5C%0A%5Cbeta_t%20&amp;=%20%5Cbeta_%7Bt_1%7D%20+%20%5Czeta_t,%5Cquad%20&amp;%5Czeta_t%5Csim%20N(0,%5Csigma_%5Czeta)%5C%5C%0A%5Cgamma_t%20&amp;=%20-%20%5Csum%5E%7Bs-1%7D_%7Bj=1%7D%5Cgamma_%7Bt-j%7D%20+%20%5Comega_t,%5Cquad%20&amp;%5Comega_t%5Csim%20N(0,%5Csigma_%5Comega)%5C%5C%0A%5Cend%7Baligned%7D%20%20%20%20%0A%5Ctag%7B1%7D"></span></p>
<p>which uses random walk dynamics for the level, slope and seasonal components, each with an independent noise component. Variations to this model include the innovations state space model which assumes fully correlated components and is observationally equivalent to this model but does not require the use of the Kalman filter since it starts from a steady state equilibrium and hence optimizing the log likelihood is somewhat simpler and faster.</p>
<p>In this structural time series representation, additive outliers can be identified by spikes in the observation equation; level shifts by outliers in the level equation; and a large change in the slope as an outlier in the slope equation (and similar logic applies to the seasonal equation). Therefore, testing for such outliers in each of the disturbance smoother errors of the models can form the basis for the identification of anomalies in the series.<sup>2</sup></p>
<p>Direct equivalence between a number of different variations of this model and the more well known ARIMA model have been established. In the ARIMA framework, a key method for the identification of anomalies is based on the work of <span class="citation" data-cites="Chen1993">(Chen and Liu 1993)</span>, and is the one we use in our approach.<sup>3</sup> This approach uses multi-pass identification and elimination steps based on automatic model selection to identify the best anomaly candidates (and types of candidates) based on information criteria. This is a computationally demanding approach, particularly in the presence of multiple components and long time series data sets. As such, we have implemented a slightly modified and faster approach, as an additional option, which first identifies and removes large spikes using a robust STL decomposition and MAD criterion, then deseasonalizes and detrends this cleaned data again using a robust STL decomposition, before finally passing the irregular component to the method of <span class="citation" data-cites="Chen1993">(Chen and Liu 1993)</span>, as implemented in the <strong>tsoutliers</strong> package. Finally, the outliers from the first stage are combined with the anomalies identified in the last step (and duplicates removed), together with all estimated coefficients on these anomalies which can then be used to clean the data. The <strong>tsaux</strong> package provides 2 functions: <code>auto_regressors</code> automatically returns the identified anomalies, their types and coefficients, whilst <code>auto_clean</code> automatically performs the data cleaning step. The functions allow for the use of the modified approach as well as the direct approach of passing the data to the <code>tso</code> function.</p>
<p>One shortcoming of the current approach is that for temporary change identification, the AR coefficient defaults to 0.7 and there is no search performed on other parameters. Adapting the method to search over different values is left for future research.</p>
</section>
<section id="sec-demo" class="level2">
<h2 class="anchored" data-anchor-id="sec-demo">Anomaly Detection and Decontamination Demo</h2>
<p>We start with an illustration of data contamination. Starting with a simulated series of level + slope, we then proceed to contaminate it with outliers, temporary changes and level shifts.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10125</span>)</span>
<span id="cb3-2">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tsets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ets_sample</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAN"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, </span>
<span id="cb3-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gamma =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, </span>
<span id="cb3-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed_states =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l0"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b0"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb3-5">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Simulated</span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.zoo</span>(y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb3-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span>
<span id="cb3-9">y_contaminated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.zoo</span>(y_contaminated), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+Additive Outlier[t=25]"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span>
<span id="cb3-12">y_contaminated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y_contaminated <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">temporary_change</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb3-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.zoo</span>(y_contaminated), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+Temporary Change[t=120]"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb3-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span>
<span id="cb3-15">y_contaminated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y_contaminated <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">level_shift</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb3-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.zoo</span>(y_contaminated), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+Level Shift[t=200]"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb3-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>We now pass the contaminated series to the anomaly identification function <code>auto_regressors</code>. The <code>tso</code> function directly identifies the correct number, timing and types of anomalies.<sup>4</sup></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">xreg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">auto_regressors</span>(y_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, </span>
<span id="cb4-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">return_table =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb4-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto_arima_opts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full"</span>)</span>
<span id="cb4-4">xreg[,time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(y_contaminated) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> xreg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date)]</span>
<span id="cb4-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(xreg)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>## Index: &lt;type&gt;
##      type       date filter      coef  time
##    &lt;char&gt;     &lt;Date&gt;  &lt;num&gt;     &lt;num&gt; &lt;int&gt;
## 1:     AO 1992-02-29    0.0 118.81429    25
## 2:     TC 2000-01-31    0.7 174.45303   120
## 3:     LS 2006-09-30    1.0 -65.82534   200</code></pre>
</div>
</div>
<p>While we can call directly the <code>auto_clean</code> function, we’ll instead show how to use the returned table to decontaminate the data.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(y_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> xreg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb6-2">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">temporary_change</span>(y_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> xreg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> xreg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>filter[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], </span>
<span id="cb6-3">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb6-4">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">level_shift</span>(y_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> xreg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>))</span>
<span id="cb6-5">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> xreg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef</span>
<span id="cb6-6">y_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y_contaminated <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x</span>
<span id="cb6-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb6-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.zoo</span>(y_contaminated), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Anomaly Decomtanination"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb6-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.zoo</span>(y_clean), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.zoo</span>(y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>)</span>
<span id="cb6-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid</span>()</span>
<span id="cb6-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topleft"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Contaminated"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb6-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zoo</span>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(y)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Anomalies"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>Next, we perform a simulation exercise to see how well the method works on a series with anomalies arriving at random times. To keep things simple, we contaminate the series with 4 additive outliers and 2 temporary changes, all arriving at random times with random coefficients. There are two key ways to measure the performance of this simulation; the very detailed looks at measuring the correct number, type and and timing of anomalies; the other way is to simply measure the goodness of fit of the decontaminated series against the original series. We choose the latter since the detailed method requires writing a lot of coding for matching which is not conducive to a blog post. We measure goodness of fit using MAPE since it’s simple and explainable. It should be noted that this is a purely artificial example, since most real data is much more noisy and can contain anomalies which happen at or near the same time point.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(multisession)</span>
<span id="cb7-2">simulation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">future_lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i){</span>
<span id="cb7-3"> random_parameters <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb7-4"> random_times <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb7-5"> y_contaminated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">temporary_change</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">temporary_change</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>])</span>
<span id="cb7-11"> y_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">auto_clean</span>(y_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxit.iloop =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxit.oloop =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">types =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AO"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TC"</span>), </span>
<span id="cb7-12">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto_arima_opts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cval =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check.rank =</span> T), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-13"> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inherits</span>(y_clean,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mod-error'</span>)) {</span>
<span id="cb7-14">  stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb7-15"> } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb7-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NROW</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(y_clean)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NROW</span>(y)) {</span>
<span id="cb7-17">   stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb7-18">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb7-19">   stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mape</span>(y, y_clean)</span>
<span id="cb7-20">  }</span>
<span id="cb7-21"> }</span>
<span id="cb7-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sim =</span> i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mape =</span> stat))</span>
<span id="cb7-23">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">future.seed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">future.packages =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsets"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsaux"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xts"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magrittr"</span>))</span>
<span id="cb7-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(sequential)</span>
<span id="cb7-25">simulation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbindlist</span>(simulation)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(simulation, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> mape<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-3"> ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_pander</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-4"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Goodness of Fit (%MAPE)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Decontaminated Series"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The very low MAPE we observe overall provides clear evidence that the detection algorithm performs quite well in eliminating the anomalies to the point where the decontaminated data is almost identical to the original series.</p>
<section id="sec-demo-note" class="level3">
<h3 class="anchored" data-anchor-id="sec-demo-note">A note on Level Shifts</h3>
<p>For the purpose of forecasting, level shifts should never be used in the anomaly cleaning step. Once the level is changed by such a structural shift, it forever changed and we are usually interested in forecasting from current levels rather than the some counter factual level (unless there is a specific reason to do so). However, they can be included as part of a model’s regressors in order to reduce the variance of the noise.</p>
</section>
</section>
<section id="sec-uncertainty" class="level2">
<h2 class="anchored" data-anchor-id="sec-uncertainty">Future Anomalies: Incorporating Uncertainty</h2>
<p>In some cases, we may be interested in incorporating the uncertainty of future anomalies in the data.<sup>5</sup> There are many ways to address this, but here we consider a change of distribution by infusing non-normal errors into the predict function. The example below illustrates how to use a skewed and heavy tailed distribution. This leaves the standard deviation of the decontaminated model intact, but allows for fatter tails and skewness. However, it would also be possible to simply change the standard deviation to a higher value and continue to use the Normal distribution.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12111</span>)</span>
<span id="cb9-2">random_parameters <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb9-3"> random_times <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb9-4"> y_contaminated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">additive_outlier</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">temporary_change</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">temporary_change</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> random_times[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parameter =</span> random_parameters[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>])</span>
<span id="cb9-10"> y_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">auto_clean</span>(y_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxit.iloop =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxit.oloop =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">types =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AO"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TC"</span>), </span>
<span id="cb9-11">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">auto_arima_opts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"full"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cval =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check.rank =</span> T), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb9-12">model_contaminated <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">estimate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ets_modelspec</span>(y_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAN"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>))</span>
<span id="cb9-13">model_clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">estimate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ets_modelspec</span>(y_clean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAN"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>))</span>
<span id="cb9-14">nig_distribution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">estimate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distribution_modelspec</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(model_contaminated, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raw =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)), </span>
<span id="cb9-15">                                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distribution =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nig"</span>))</span>
<span id="cb9-16">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rdist</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nig"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb9-17">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(nig_distribution)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skew"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(nig_distribution)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shape"</span>])</span>
<span id="cb9-18">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>)</span>
<span id="cb9-19">p_clean_nig <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model_clean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nsim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">innov =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">innov_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>)</span>
<span id="cb9-20">p_clean_norm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model_clean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nsim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(p_clean_nig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gradient_color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval_color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, </span>
<span id="cb10-3">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval_quantiles =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.995</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval_type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb10-4">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prediction [99% Coverage]"</span>)</span>
<span id="cb10-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(p_clean_norm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>distribution, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval_color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, </span>
<span id="cb10-6">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval_quantiles =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span> , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.995</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval_type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(y), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(y_contaminated), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb10-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topleft"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Clean|Series"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Contaminated|Series"</span>,</span>
<span id="cb10-9">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prediction|NIG"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prediction|Norm"</span>), </span>
<span id="cb10-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>With 99% coverage. we can see that the NIG distributed errors provide for heavier tails, skewed towards positive shocks, which is what we observed given the way we generated the anomalies (to be positive).</p>
</section>
<section id="sec-conclusion" class="level2">
<h2 class="anchored" data-anchor-id="sec-conclusion">Conclusion</h2>
<p>Anomalies contaminate process dynamics with noise which is not part of normal variation. This make it difficult to extract clean signals and should therefore be addressed as part of any forecasting strategy. This should be distinct from how one thinks about future uncertainty which can be addressed separately.</p>



</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-Bass1969" class="csl-entry">
Bass, Frank M. 1969. <span>“A New Product Growth for Model Consumer Durables.”</span> <em>Management Science</em> 15 (5): 215–27.
</div>
<div id="ref-Chen1993" class="csl-entry">
Chen, Chung, and Lon-Mu Liu. 1993. <span>“Joint Estimation of Model Parameters and Outlier Effects in Time Series.”</span> <em>Journal of the American Statistical Association</em> 88 (421): 284–97.
</div>
<div id="ref-deJong1998" class="csl-entry">
De Jong, Piet, and Jeremy Penzer. 1998. <span>“Diagnosing Shocks in Time Series.”</span> <em>Journal of the American Statistical Association</em> 93 (442): 796–806.
</div>
<div id="ref-Guo2019" class="csl-entry">
Guo, Xiaojia, Kenneth C Lichtendahl, and Yael Grushka-Cockayne. 2019. <span>“An Exponential Smoothing Model with a Life Cycle Trend.”</span>
</div>
<div id="ref-Harvey1990" class="csl-entry">
Harvey, Andrew C. 1990. <span>“Forecasting, Structural Time Series Models and the Kalman Filter.”</span>
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>See <span class="citation" data-cites="Bass1969">(Bass 1969)</span>↩︎</p></li>
<li id="fn2"><p>see <span class="citation" data-cites="deJong1998">(De Jong and Penzer 1998)</span>.↩︎</p></li>
<li id="fn3"><p>The approach used in the <strong>tsaux</strong> package calls the <code>tso</code> function in the <strong>tsouliers</strong> package.↩︎</p></li>
<li id="fn4"><p>This may not always be the case depending on the amount of noise and other artifacts in the underlying series and the size of the anomalies versus normal process noise.↩︎</p></li>
<li id="fn5"><p>An example would be forecasting for peaks.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{galanos2022,
  author = {Galanos, Alexios},
  title = {Messy Data and Anomaly Detection},
  date = {2022-05-16},
  url = {https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-galanos2022" class="csl-entry quarto-appendix-citeas">
Galanos, Alexios. 2022. <span>“Messy Data and Anomaly Detection.”</span>
May 16, 2022. <a href="https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/">https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/</a>.
</div></div></section></div> ]]></description>
  <category>prediction</category>
  <category>outliers</category>
  <category>tsaux</category>
  <guid>https://www.nopredict.com/blog/posts/2022-05-15-messy-data-and-anomaly-detection/</guid>
  <pubDate>Mon, 16 May 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Casting a Wide Net: Ensembling of Predictions</title>
  <dc:creator>Alexios Galanos</dc:creator>
  <link>https://www.nopredict.com/blog/posts/2022-05-14-model-ensembling/</link>
  <description><![CDATA[ 





<section id="motivation" class="level2">
<h2 class="anchored" data-anchor-id="motivation">Motivation</h2>
<p>Real world data rarely has clean patterns from which we can quickly identify the data generating process (DGP). It is messy<sup>1</sup> and contains anomalies which often times cloud the true signal. Even worse, even if the data appears regular, there are never any guarantees that the future will follow the past.</p>
<p>Choosing a <strong>best</strong> model is an almost impossible task. At best, a model we choose represents some approximation to the true DGP of the process under consideration. The observed history of most series will usually indicate an overlap between many generating processes when uncertainty is taken into account.</p>
<p>One approach to dealing with this problem is to cast a wide net by combining multiple models together in an ensemble. The question of how to combine them will not be addressed here, but suffice to say that unless we have really strong priors on ranking preference, we should really use an equal weight allocation as its the optimal choice under high uncertainty (see for example <span class="citation" data-cites="Pflug2012">Pflug, Pichler, and Wozabal (2012)</span>).</p>
<p>The particular problem we address in this article is one of ensembling for GDP uncertainty using a common information set (the series’ own history). This is separate from ensembling in the presence of multiple <strong>independent</strong> predictors which is a slightly different problem which has been addressed extensively in recent years in the machine learning community.</p>
</section>
<section id="ensembling-predictive-distributions" class="level2">
<h2 class="anchored" data-anchor-id="ensembling-predictive-distributions">Ensembling Predictive Distributions</h2>
<p>When ensembling predictive distributions on the same series, it’s important to consider how to ensemble them given the fact that they are unlikely to be independent.</p>
<p>This can be seen from an investigation of the model residuals and their correlations. As demonstrated in the tsmethods ensembling section, we can make use of a copula to generate correlated uniform variates which can then be passed to the predict function of any of the <strong>tsmodels</strong> packages which then convert them into <img src="https://latex.codecogs.com/png.latex?N(0,%5Csigma)"> innovations. In this way, the conditional 1-step ahead errors will at least retain their dependence and the distributions will be ensembled in a more accurate way. If instead we were to assume independence, then the coverage of the ensembled predictions would significantly underestimate the true coverage as a result of assuming that the off diagonals of the covariance of series residuals were zero.</p>
<p>A similar argument follows when ensembling for the purpose of aggregation, which is more typical in hierarchical forecasting approaches.</p>
</section>
<section id="demonstration" class="level2">
<h2 class="anchored" data-anchor-id="demonstration">Demonstration</h2>
<p>For the purpose of this article, we use the gas data set, which is available in the <strong>tsdatasets</strong> package, representing the US finished motor gasoline products supplied (in thousands of barrels per day) from February 1991 to May 2005. This is a weekly data set with seasonal and trend components.</p>
<p>While we could consider multiple different models, for this example we only consider the rather flexible innovations state space model (issm) with level, slope, trigonometric seasonality and ARMA terms. Whether to include or not the slope, slope dampening, the number of harmonics and the number of ARMA terms is what will determine the models we use for ensembling. To this end, we will enumerate a space of combinations using the <code>auto_issm</code> function to obtain a selection matrix ranked by AIC. We then choose the top 5 models for ensembling.</p>
<p>We split out data into a training set and leave a year (52 weeks) for testing.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load packages and data</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsissm)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(xts)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(future)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(progressr)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(copula)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsmethods)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gas"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsdatasets"</span>)</span>
<span id="cb1-10">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NROW</span>(gas)</span>
<span id="cb1-11">train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gas[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>)]</span>
<span id="cb1-12">test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(gas, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tweak</span>(remote, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workers =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"home_server"</span>),</span>
<span id="cb2-3"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tweak</span>(multiprocess, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workers =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb2-4">))</span>
<span id="cb2-5">mod <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">auto_issm</span>(train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal_damped =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), </span>
<span id="cb2-6">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal_frequency =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, </span>
<span id="cb2-7">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trigonometric"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal_harmonics =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), </span>
<span id="cb2-8">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ar =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ma =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">return_table =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(sequential)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>selection))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>##     iter  slope slope_damped seasonal    ar    ma    K1
##    &lt;int&gt; &lt;lgcl&gt;       &lt;lgcl&gt;   &lt;lgcl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
## 1:   941   TRUE        FALSE     TRUE     1     4    12
## 2:   956   TRUE        FALSE     TRUE     0     5    12
## 3:   958   TRUE         TRUE     TRUE     1     5    12
## 4:   776   TRUE        FALSE     TRUE     0     1    11
## 5:   800   TRUE        FALSE     TRUE     2     2    11
## 6:   773   TRUE        FALSE     TRUE     5     0    11
##         AIC       MAPE
##       &lt;num&gt;      &lt;num&gt;
## 1: 11879.73 0.01920864
## 2: 11880.63 0.01911436
## 3: 11914.47 0.01938119
## 4: 11924.07 0.01987139
## 5: 11933.70 0.01979659
## 6: 11934.09 0.01974837</code></pre>
</div>
</div>
<p>The returned selection table shows the list of different model choices ranked from lowest to highest AIC for the training data used. We proceed by calculating the correlation of the model residuals for use in generating random uniform variates from a Normal Copula.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">mod_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i){</span>
<span id="cb5-2"> spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">issm_modelspec</span>(train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</span> mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>selection[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>slope, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope_damped =</span> mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>selection[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>slope_damped, </span>
<span id="cb5-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal_frequency =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>,</span>
<span id="cb5-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal =</span> mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>selection[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>seasonal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trigonometric"</span>,</span>
<span id="cb5-5">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seasonal_harmonics =</span> mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>selection[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>K1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ar =</span> mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>selection[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ar,</span>
<span id="cb5-6">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ma =</span> mod<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>selection[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ma)</span>
<span id="cb5-7"> mod <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">estimate</span>(spec)</span>
<span id="cb5-8"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(mod)</span>
<span id="cb5-9">})</span>
<span id="cb5-10">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(cbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i){</span>
<span id="cb5-11"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(mod_list[[i]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">raw =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-12">}))</span>
<span id="cb5-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(r) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"model"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb5-14">C <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kendall"</span>)</span>
<span id="cb5-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(C, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>##        model1 model2 model3 model4 model5
## model1   1.00   0.82   0.80   0.75   0.77
## model2   0.82   1.00   0.96   0.84   0.87
## model3   0.80   0.96   1.00   0.85   0.87
## model4   0.75   0.84   0.85   1.00   0.95
## model5   0.77   0.87   0.87   0.95   1.00</code></pre>
</div>
</div>
<p>We generate the 5000 (samples) by 52 (forecast horizon) random draws from the Normal Copula and pass them to the predict function. All models in the <strong>tsmodels</strong> framework accept either uniform (<code>innov_type</code> = “q”), standardized (<code>innov_type</code> = “z”) or non-standardized innovations (<code>innov_type</code> = “r”). In the former case the quantile transformation will be applied to transform the uniform values to standard normal innovations. The resulting standardized innovations are then re-scaled to have standard deviation equal to that of the estimated models’ residuals.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">cop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalCopula</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">P2p</span>(C), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dispstr =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"un"</span>)</span>
<span id="cb7-2">U <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rCopula</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, cop)</span>
<span id="cb7-3">predictions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i){</span>
<span id="cb7-4"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mod_list[[i]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nsim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">innov =</span> U[,i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">innov_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q"</span>)</span>
<span id="cb7-5">})</span></code></pre></div>
</details>
</div>
<p>The final step is to ensemble the forecasts for which we make use of the functionality from the <strong>tsmethods</strong> package which takes a list of <em>tsmodel.predict</em> objects and ensembles them.<sup>2</sup></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">weights <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb8-2">spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ensemble_modelspec</span>(predictions)</span>
<span id="cb8-3">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tsensemble</span>(spec, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> weights)</span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># infuse some additional information into the ensembled object so we can use</span></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># other methods on this</span></span>
<span id="cb8-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(p) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsissm.predict"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(p))</span>
<span id="cb8-7">p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>frequency <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span></span>
<span id="cb8-8">predictions[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p</span></code></pre></div>
</details>
</div>
<p>Comparing our predictions across each of the 5 models and the ensembled one, against the testing data set, we observe some interesting, but not surprising findings. The top model selected by AIC during the training phase now has the second worst performance in the testing phase, whereas the 4th worst model by AIC in the training now has the best performance in the testing phase. This should make clear the comments made in the introduction about future uncertainty. The ensemble model sits right in the middle, coming in third in terms of MAPE, and second in terms of the distribution calibration metrics (MIS and CRPS).</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">prediction_metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i){</span>
<span id="cb9-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tsmetrics</span>(predictions[[i]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">actual =</span> test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb9-3">}))</span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(prediction_metrics) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"model"</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ensemble"</span>)</span>
<span id="cb9-5">prediction_metrics</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>##           h       MAPE      MASE        MSLRE          BIAS
## model1   52 0.02069121 0.5739093 0.0006255166 -0.0014976744
## model2   52 0.02194529 0.6087442 0.0007034767 -0.0004547551
## model3   52 0.02248476 0.6237508 0.0007739192 -0.0063832761
## model4   52 0.02847264 0.7895135 0.0011947730 -0.0030526928
## model5   52 0.02034886 0.5643485 0.0006353729 -0.0006761913
## ensemble 52 0.02087266 0.5792841 0.0006744860 -0.0024129179
##                MIS     CRPS
## model1    913.8911 130.4708
## model2   1044.9567 140.3905
## model3   1121.0820 147.7341
## model4   1321.6796 180.0566
## model5   1056.5165 134.4742
## ensemble 1031.4426 136.0655</code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_original =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ensemble GAS Prediction"</span>, </span>
<span id="cb11-2">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gradient_color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yellow"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interval_color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>)</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(test), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(test), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"violet"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-14-model-ensembling/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="some-additional-notes" class="level2">
<h2 class="anchored" data-anchor-id="some-additional-notes">Some Additional Notes</h2>
<p>Because the models were all estimated with the same transformation<sup>3</sup>, their state decompositions can also be ensembled. This could be particularly useful should custom views need to be incorporated into the prediction, which is usually done on the Trend component or the growth rate, allowing one to rebuild the full predictive distribution after intervening in some part of the state decomposition.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>There should be no doubt about the value of ensembling. The key takeaway is: <code>When in doubt, ensemble</code>.</p>



</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-Harvey1998" class="csl-entry">
Harvey, Andrew, Siem Jan Koopman, and Jeremy Penzer. 1998. <span>“Messy Time Series: A Unified Approach.”</span> <em>Advances in Econometrics</em> 13: 103–44.
</div>
<div id="ref-Pflug2012" class="csl-entry">
Pflug, Georg Ch, Alois Pichler, and David Wozabal. 2012. <span>“The 1/n Investment Strategy Is Optimal Under High Model Ambiguity.”</span> <em>Journal of Banking &amp; Finance</em> 36 (2): 410–17.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>See <span class="citation" data-cites="Harvey1998">(Harvey, Koopman, and Penzer 1998)</span>↩︎</p></li>
<li id="fn2"><p>After checking for equal sized prediction matrices in terms of samples and horizon.↩︎</p></li>
<li id="fn3"><p>No transformation, which is also equivalent to a Box Cox with lambda of 1.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{galanos2022,
  author = {Galanos, Alexios},
  title = {Casting a Wide Net: Ensembling of Predictions},
  date = {2022-05-15},
  url = {https://www.nopredict.com/blog/posts/2022-05-14-model-ensembling/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-galanos2022" class="csl-entry quarto-appendix-citeas">
Galanos, Alexios. 2022. <span>“Casting a Wide Net: Ensembling of
Predictions.”</span> May 15, 2022. <a href="https://www.nopredict.com/blog/posts/2022-05-14-model-ensembling/">https://www.nopredict.com/blog/posts/2022-05-14-model-ensembling/</a>.
</div></div></section></div> ]]></description>
  <category>prediction</category>
  <category>ensembling</category>
  <category>tsissm</category>
  <guid>https://www.nopredict.com/blog/posts/2022-05-14-model-ensembling/</guid>
  <pubDate>Sun, 15 May 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Tails of the GHST</title>
  <dc:creator>Alexios Galanos</dc:creator>
  <link>https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/</link>
  <description><![CDATA[ 





<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsdistributions)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assignInNamespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cedta.override"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(data.table<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:::</span>cedta.override,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"distill"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gridExtra)</span>
<span id="cb1-6">f1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ddist</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-7">f2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ddist</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, x, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"function"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> f1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cadetblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"function"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> f2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgrey"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-11">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_tufte</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The GHST"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>The Generalized Hyperbolic Skew Student distribution, described in <span class="citation" data-cites="Aas2006">(Aas and Haff 2006)</span>, is a sub-family of the Generalized Hyperbolic distribution of <span class="citation" data-cites="Barndorff1977">(Barndorff-Nielsen 1977)</span>. It has the unique property of allowing the 2 tails to exhibit different behaviors, namely one polynomial and one exponential. This is important in some applications such as financial time series where negative shocks exhibit heavy tails, whereas positive shocks have a more Gaussian type tail.</p>
</section>
<section id="tail-behavior" class="level2">
<h2 class="anchored" data-anchor-id="tail-behavior">Tail Behavior</h2>
<p>This distribution is a limiting case of the Generalized Hyperbolic when <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Cto%20%5Cleft%7C%20%5Cbeta%20%5Cright%7C"> and <img src="https://latex.codecogs.com/png.latex?%5Clambda=-%5Cnu/2">, where <img src="https://latex.codecogs.com/png.latex?%5Cnu"> is the shape parameter of the Student distribution. The domain of variation of the parameters is <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%5Cin%20%5Cmathbb%7BR%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cnu%3E0">, but for the variance to be finite <img src="https://latex.codecogs.com/png.latex?%5Cnu%3E4">, while for the existence of skewness and kurtosis, <img src="https://latex.codecogs.com/png.latex?%5Cnu%3E6"> and <img src="https://latex.codecogs.com/png.latex?%5Cnu%3E8"> respectively.</p>
<p>Following <span class="citation" data-cites="Aas2006">(Aas and Haff 2006)</span>, the heaviest tail decays as:</p>
<p><span id="eq-1"><img src="https://latex.codecogs.com/png.latex?%0Af_x(x)%20%5Csim%20%5Ctext%7Bconst%7D%7Cx%7C%5E%7B-%5Cnu/%7B2-1%7D%7D%5Cquad%20%5Ctext%7Bwhen%7D%0A%5Cbegin%7Bcases%7D%0A%5Cbeta%20%3C0%20%5Cquad%20and%20%5Cquad%20x%5Crightarrow%20-%5Cinfty%20&amp;%20%5C%5C%0A%5Cbeta%20%3E0%20%5Cquad%20and%20%5Cquad%20x%5Crightarrow%20+%5Cinfty%20&amp;%20%5C%5C%0A%5Cend%7Bcases%7D%0A%5Ctag%7B1%7D"></span></p>
<p>and the semi-heavy tail decays as:</p>
<p><span id="eq-2"><img src="https://latex.codecogs.com/png.latex?%0Af_x(x)%20%5Csim%20%5Ctext%7Bconst%7D%7Cx%7C%5E%7B-%5Cnu/%7B2-1%7D%7D%5Ctext%7Bexp%7D%5Cleft(-2%7C%5Cbeta%20x%7C%5Cright)%5Cquad%20%5Ctext%7Bwhen%7D%0A%5Cbegin%7Bcases%7D%0A%5Cbeta%20%3C0%20%5Cquad%20and%20%5Cquad%20x%5Crightarrow%20+%5Cinfty%20&amp;%20%5C%5C%0A%5Cbeta%20%3E0%20%5Cquad%20and%20%5Cquad%20x%5Crightarrow%20-%5Cinfty%20&amp;%20%5C%5C%0A%5Cend%7Bcases%7D%0A%5Ctag%7B2%7D"></span></p>
<p>The skew and shape parameters of the standardized parameterization in the <em>tsdistributions</em> have bounds <img src="https://latex.codecogs.com/png.latex?%5Cin%5C%7B-%5Cinfty,%20+%5Cinfty%5C%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cin%20R%5E+"> respectively. The further the skew parameter is from zero and the smaller the shape parameter is, the larger the skewness. The plot below illustrates an example of the tail behavior that can be generated from this distribution.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsdistributions)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb2-4">f1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ddist</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb2-5">f2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ddist</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plotA"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plotA"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"function"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> f1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_area</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plotB"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plotB"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"function"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> f2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-9">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_tufte</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The GHST"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plotA =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plotB =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aesthetics =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"colour"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill"</span>),</span>
<span id="cb2-14">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{skew = -30, shape = 30}"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{skew = -30, shape = 10}"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>With a skew parameter of -30 common across both, the orange distribution has a shape parameter of 30 with skewness and excess kurtosis of (-0.92, 1.9) while the blue distribution has a shape parameter of 10 with skewness and excess kurtosis of (-3.37, 40.15). For the blue distribution (with the lower shape parameter) the lower heavy and upper semi-heavy tails are quite obvious and hopefully illustrate the value of this distributions for a number of possible applications.</p>
</section>
<section id="parameter-distribution-and-consistency" class="level2">
<h2 class="anchored" data-anchor-id="parameter-distribution-and-consistency">Parameter Distribution and Consistency</h2>
<p>The question of how much data (size) is needed to confidently estimate the true parameter values is one we address here through a small simulation exercise.</p>
<p>Let’s assume that that the true parameters are given <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%200.1">, <img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%200.5">, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Bskew%7D%20=%20-5"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Bshape%7D%20=%2012">. We simulate 2000 draws from sizes of 100, 200, 400, 800, 1200, 1600, 3200 and estimate their parameters. In order to ensure the existence of the higher moments, we also set a lower bound for the shape parameter at 9 and use parallel computation to speed things up.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(future.apply)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tweak</span>(remote, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workers =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"home_server"</span>),</span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tweak</span>(multisession, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workers =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb3-5">))</span>
<span id="cb3-6">pars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&lt;-%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">future_lapply</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i){</span>
<span id="cb3-7">  r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rdist</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb3-8">  sim_lengths <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1600</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3200</span>)</span>
<span id="cb3-9">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(sim_lengths, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(j){</span>
<span id="cb3-10">   spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distribution_modelspec</span>(r[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>j], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>)</span>
<span id="cb3-11">   spec<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>parmatrix[parameter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shape"</span>, lower <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>]</span>
<span id="cb3-12">   mod <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">estimate</span>(spec), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-13">   <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inherits</span>(mod, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"try-error"</span>)) {</span>
<span id="cb3-14">    v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb3-15">    .skewness <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb3-16">    .kurtosis <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb3-17">   } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb3-18">    v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(mod)</span>
<span id="cb3-19">    .skewness <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dskewness</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb3-20">    .kurtosis <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dkurtosis</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb3-21">   }</span>
<span id="cb3-22">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sim =</span> i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> j, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]), </span>
<span id="cb3-23">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(v[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]),</span>
<span id="cb3-24">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skewness =</span> .skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kurtosis =</span> .kurtosis))</span>
<span id="cb3-25">  })</span>
<span id="cb3-26">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbindlist</span>(x)</span>
<span id="cb3-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(x)</span>
<span id="cb3-28">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">future.packages =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsdistributions"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tsmethods"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">future.seed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-29">pars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval</span>(pars)</span>
<span id="cb3-30">pars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbindlist</span>(pars)</span>
<span id="cb3-31">pars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pars[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(mu)]</span>
<span id="cb3-32"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(sequential)</span></code></pre></div>
</details>
</div>
<section id="parameter-distribution-plots" class="level3">
<h3 class="anchored" data-anchor-id="parameter-distribution-plots">Parameter Distribution Plots</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pars, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(mu, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> size)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[mu]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-8">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_pander</span>()</span>
<span id="cb4-9">p2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pars, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> size)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[sigma]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-16">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_pander</span>()</span>
<span id="cb4-17">p3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pars, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(skew, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> size)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[skew]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-24">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_pander</span>()</span>
<span id="cb4-25">p4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pars, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(shape, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> size)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[shape]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-32">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_pander</span>()</span>
<span id="cb4-33">exact_skewness <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dskewness</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb4-34">exact_kurtosis <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dkurtosis</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skew =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb4-35">p5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pars, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> size)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(exact_skewness <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> exact_skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[skewness]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-42">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_pander</span>()</span>
<span id="cb4-43">p6 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pars, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(kurtosis, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> size)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, exact_kurtosis <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  </span>
<span id="cb4-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> exact_kurtosis, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[excess kurtosis]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-50">  ggthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_pander</span>()</span>
<span id="cb4-51"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(p1,p2,p3,p4,p5,p6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="size-and-mape-plots" class="level3">
<h3 class="anchored" data-anchor-id="size-and-mape-plots">Size and MAPE Plots</h3>
<p>The following plots show the mean absolute percent error per window size for some of the parameters.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">skew_mape <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pars[,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>((skew <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)))), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>]</span>
<span id="cb5-2">p3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(skew_mape, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mape, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> size)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[skew]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent)</span>
<span id="cb5-6">shape_mape <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pars[,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>((shape <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>]</span>
<span id="cb5-7">p4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(shape_mape, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mape, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(size))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[shape]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent)</span>
<span id="cb5-11">skewness_mape <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pars[,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>((skewness <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> exact_skewness)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>exact_skewness))), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>]</span>
<span id="cb5-12">p5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(skewness_mape, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mape, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(size))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[skewness]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent)</span>
<span id="cb5-16">kurtosis_mape <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pars[,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>((kurtosis <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> exact_kurtosis)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>exact_kurtosis))), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>]</span>
<span id="cb5-17">p6 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(kurtosis_mape, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mape, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(size))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GHST[excess kurtosis]"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent) </span>
<span id="cb5-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.arrange</span>(p3,p4,p5,p6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/index_files/figure-html/mape-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>Another interesting plot is that of Kurtosis<sup>1</sup> against Skewness.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(pars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>kurtosis <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, pars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Skewness"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kurtosis"</span>)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> exact_kurtosis <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> exact_skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</span>(exact_kurtosis <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, exact_skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/index_files/figure-html/auth-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="final-thoughts" class="level2">
<h2 class="anchored" data-anchor-id="final-thoughts">Final Thoughts</h2>
<p>The GHST distribution has some interesting properties, but we also found that a considerable amount of data is needed to provide confident estimates of the higher moments, and we suspect this gets worse the lower the shape parameter is. This could also be due to the fact that our estimation code makes use of the modified Bessel function of the second kind for which the derivatives are not yet implemented. An implementation exists in <a href="https://github.com/cgeoga/besselk.jl">Julia</a> based on the paper by <span class="citation" data-cites="Geoga2022">(Geoga et al. 2022)</span> and it may be worthwhile porting this to TMB in the future.</p>



</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-Aas2006" class="csl-entry">
Aas, Kjersti, and Ingrid Hobæk Haff. 2006. <span>“The Generalized Hyperbolic Skew Student’st-Distribution.”</span> <em>Journal of Financial Econometrics</em> 4 (2): 275–309.
</div>
<div id="ref-Barndorff1977" class="csl-entry">
Barndorff-Nielsen, Ole. 1977. <span>“Exponentially Decreasing Distributions for the Logarithm of Particle Size.”</span> <em>Proceedings of the Royal Society of London. A. Mathematical and Physical Sciences</em> 353 (1674): 401–19.
</div>
<div id="ref-Geoga2022" class="csl-entry">
Geoga, Christopher J., Oana Marin, Michel Schanen, and Michael L. Stein. 2022. <span>“Fitting Matérn Smoothness Parameters Using Automatic Differentiation.”</span> <a href="https://arxiv.org/abs/2201.00090">https://arxiv.org/abs/2201.00090</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>In this plot we show the actual Kurtosis, not the excess value, in order to be consistent with the authorized domain function and a previous post on this subject↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{galanos2022,
  author = {Galanos, Alexios},
  title = {Tails of the {GHST}},
  date = {2022-05-12},
  url = {https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-galanos2022" class="csl-entry quarto-appendix-citeas">
Galanos, Alexios. 2022. <span>“Tails of the GHST.”</span> May 12, 2022.
<a href="https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/">https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/</a>.
</div></div></section></div> ]]></description>
  <category>distributions</category>
  <category>tsdistributions</category>
  <guid>https://www.nopredict.com/blog/posts/2022-05-12-tails-of-the-ghst/</guid>
  <pubDate>Thu, 12 May 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Know Your Limits: The Authorized Domain</title>
  <dc:creator>Alexios Galanos</dc:creator>
  <link>https://www.nopredict.com/blog/posts/2022-05-12-authorized-domain/</link>
  <description><![CDATA[ 





<p>The <a href="https://github.com/tsmodels/tsdistribution">tsdistributions</a> package provides the function <code>authorized_domain</code> which calculates the region of Skewness-Kurtosis for which a density exists. This is related to the Hamburger moment problem, and the maximum attainable Skewness (S) given kurtosis (K) (see <span class="citation" data-cites="widder2015laplace">(Widder 2015)</span>). A key takeaway from Widder is that for as given level of kurtosis(<img src="https://latex.codecogs.com/png.latex?%5Cmu_4">) only a finite range of skewness (<img src="https://latex.codecogs.com/png.latex?%5Cmu_3">) may be attained, with the boundary given by:</p>
<p><span id="eq-1"><img src="https://latex.codecogs.com/png.latex?%0A%5Cmu%5E2_3%5Cle%20%5Cmu_4%20-%201%20%5Cquad%5Ctext%7Bwith%7D%5Cquad%20%5Cmu_4%5Cgt%200%0A%5Ctag%7B1%7D"></span></p>
<p>This short demo shows how to generate a plot comparing a number of different skewed and heavy tailed distributions available in the package. We investigate Johnson’s SU (jsu), the Normal Inverse Gaussian (nig), the Skew Student of Fernandez and Steel (sstd), the Hyperbolic and the Generalized Hyperbolic Skewed Student (ghst) distributions.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tsdistributions)</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># default is maximum kurtosis of 30 with interval length of 25</span></span>
<span id="cb1-3">max_kurt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span></span>
<span id="cb1-4">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span></span>
<span id="cb1-5">jsud <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">authorized_domain</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jsu"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_kurt =</span> max_kurt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n)</span>
<span id="cb1-6">nigd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">authorized_domain</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nig"</span>)</span>
<span id="cb1-7">sstdd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">authorized_domain</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sstd"</span>)</span>
<span id="cb1-8">ghypd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">authorized_domain</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gh"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-9">ghstd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">authorized_domain</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>)</span></code></pre></div>
</details>
</div>
<p>The returned list contains the lower half of the Skewness-Kurtosis combinations, so we reflect those values to obtain the upper half and plot them. The curves delimit the skewness-kurtosis boundary corresponding to the authorized domain, for which a density exists.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.01</span>, max_kurt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.01</span>, max_kurt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n))</span>
<span id="cb2-2">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.01</span>, max_kurt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.01</span>, max_kurt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(K, S, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Skewness"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kurtosis"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Authorized Domain"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spline</span>(K[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n],  S[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spline</span>(K[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n], <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>S[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span> ,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(jsud<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, jsud<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(jsud<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>jsud<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(nigd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, nigd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(nigd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>nigd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(sstdd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, sstdd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(sstdd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sstdd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(ghypd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, ghypd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"violet"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(ghypd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ghypd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"violet"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(ghstd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, ghstd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(ghstd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Kurtosis, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ghstd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Skewness, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topleft"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MAX"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jsu"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nig"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sstd"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hyp"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ghst"</span>), </span>
<span id="cb2-17">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"violet"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),  </span>
<span id="cb2-18">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>)</span>
<span id="cb2-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)</span>
<span id="cb2-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.nopredict.com/blog/posts/2022-05-12-authorized-domain/index_files/figure-html/auth_dist_plot-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>




<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-widder2015laplace" class="csl-entry">
Widder, David Vernon. 2015. <span>“Laplace Transform (PMS-6).”</span> In <em>Laplace Transform (PMS-6)</em>. Princeton university press.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{galanos2022,
  author = {Galanos, Alexios},
  title = {Know Your Limits: The Authorized Domain},
  date = {2022-05-11},
  url = {https://www.nopredict.com/blog/posts/2022-05-12-authorized-domain/},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-galanos2022" class="csl-entry quarto-appendix-citeas">
Galanos, Alexios. 2022. <span>“Know Your Limits: The Authorized
Domain.”</span> May 11, 2022. <a href="https://www.nopredict.com/blog/posts/2022-05-12-authorized-domain/">https://www.nopredict.com/blog/posts/2022-05-12-authorized-domain/</a>.
</div></div></section></div> ]]></description>
  <category>distributions</category>
  <category>tsdistributions</category>
  <guid>https://www.nopredict.com/blog/posts/2022-05-12-authorized-domain/</guid>
  <pubDate>Wed, 11 May 2022 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
