Skip to content

Commit

Permalink
censored model
Browse files Browse the repository at this point in the history
  • Loading branch information
juanitorduz committed Aug 29, 2024
1 parent 066c501 commit 1cd281a
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 2 deletions.
90 changes: 88 additions & 2 deletions Presentations/pydata_amsterdam_2024/timeseries_numpyro.html
Original file line number Diff line number Diff line change
Expand Up @@ -692,13 +692,99 @@ <h2>Zero-Inflated TSB Model</h2>
</section>
<section id="time-slice-cross-validation" class="slide level2">
<h2>Time-Slice Cross Validation</h2>

<img data-src="timeseries_numpyro_files/static/images/zi_tsb_numpyro_files/zi_tsb_numpyro_35_1.png" class="quarto-figure quarto-figure-center r-stretch" width="1000"><div class="footer">
<p><a href="https://juanitorduz.github.io/zi_tsb_numpyro/">Zero-Inflated TSB Model</a></p>
</div>
</section>
<section id="forecasting-unseen-demand" class="slide level2">
<h2>Forecasting Unseen Demand</h2>

<img data-src="timeseries_numpyro_files/static/images/demand_files/demand_8_0.png" class="quarto-figure quarto-figure-center r-stretch" width="1000"><div class="footer">
<p><a href="https://juanitorduz.github.io/demand/">Demand Forecasting with Censored Likelihood</a></p>
</div>
</section>
<section id="arima-model" class="slide level2">
<h2>ARIMA Model</h2>

<img data-src="timeseries_numpyro_files/static/images/demand_files/demand_25_0.png" class="quarto-figure quarto-figure-center r-stretch" width="1000"><div class="footer">
<p><a href="https://juanitorduz.github.io/demand/">Demand Forecasting with Censored Likelihood</a></p>
</div>
</section>
<section id="censored-distributions" class="slide level2">
<h2>Censored Distributions</h2>

<img data-src="timeseries_numpyro_files/static/images/censoring_files/censoring_5_1.png" class="quarto-figure quarto-figure-center r-stretch" width="1000"><div class="footer">
<p><a href="https://juanitorduz.github.io/censoring/">Bayesian Censoring Data Modeling</a></p>
</div>
</section>
<section id="censored-distributions-naive" class="slide level2">
<h2>Censored Distributions (Naive)</h2>
<div class="quarto-figure quarto-figure-center">
<figure>
<p><img data-src="timeseries_numpyro_files/static/images/censoring_files/censoring_12_1.png" class="quarto-figure quarto-figure-center" width="600"></p>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure>
<p><img data-src="timeseries_numpyro_files/static/images/zi_tsb_numpyro_files/zi_tsb_numpyro_35_1.png" class="quarto-figure quarto-figure-center" width="1000"></p>
<p><img data-src="timeseries_numpyro_files/static/images/censoring_files/censoring_15_1.png" class="quarto-figure quarto-figure-center" width="600"></p>
</figure>
</div>
<div class="footer">
<p><a href="https://juanitorduz.github.io/zi_tsb_numpyro/">Zero-Inflated TSB Model</a></p>
<p><a href="https://juanitorduz.github.io/censoring/">Bayesian Censoring Data Modeling</a></p>
</div>
</section>
<section id="censored-distributions-1" class="slide level2">
<h2>Censored Distributions</h2>
<div class="quarto-figure quarto-figure-center">
<figure>
<p><img data-src="timeseries_numpyro_files/static/images/censoring_files/censoring_26_1.png" class="quarto-figure quarto-figure-center" width="600"></p>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure>
<p><img data-src="timeseries_numpyro_files/static/images/censoring_files/censoring_28_1.png" class="quarto-figure quarto-figure-center" width="600"></p>
</figure>
</div>
<div class="footer">
<p><a href="https://juanitorduz.github.io/censoring/">Bayesian Censoring Data Modeling</a></p>
</div>
</section>
<section id="censored-likelihood" class="slide level2">
<h2>Censored Likelihood</h2>
<div class="sourceCode" id="cb9"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href=""></a><span class="kw">def</span> censored_normal(loc, scale, y, censored):</span>
<span id="cb9-2"><a href=""></a> distribution <span class="op">=</span> dist.Normal(loc<span class="op">=</span>loc, scale<span class="op">=</span>scale)</span>
<span id="cb9-3"><a href=""></a> ccdf <span class="op">=</span> <span class="dv">1</span> <span class="op">-</span> distribution.cdf(y)</span>
<span id="cb9-4"><a href=""></a> numpyro.sample(</span>
<span id="cb9-5"><a href=""></a> <span class="st">"censored_label"</span>,</span>
<span id="cb9-6"><a href=""></a> dist.Bernoulli(probs<span class="op">=</span>ccdf).mask(censored <span class="op">==</span> <span class="dv">1</span>),</span>
<span id="cb9-7"><a href=""></a> obs<span class="op">=</span>censored</span>
<span id="cb9-8"><a href=""></a> )</span>
<span id="cb9-9"><a href=""></a> <span class="cf">return</span> numpyro.sample(<span class="st">"pred"</span>, distribution.mask(censored <span class="op">!=</span> <span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="fragment">
<p>Change likelihood distribution in a time-series model:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href=""></a> <span class="co">## Transition function for AR(2)</span></span>
<span id="cb10-2"><a href=""></a> <span class="kw">def</span> transition_fn(carry, t):</span>
<span id="cb10-3"><a href=""></a> y_prev_1, y_prev_2 <span class="op">=</span> carry</span>
<span id="cb10-4"><a href=""></a> ar_part <span class="op">=</span> phi_1 <span class="op">*</span> y_prev_1 <span class="op">+</span> phi_2 <span class="op">*</span> y_prev_2</span>
<span id="cb10-5"><a href=""></a> pred_mean <span class="op">=</span> mu <span class="op">+</span> ar_part <span class="op">+</span> seasonal[t]</span>
<span id="cb10-6"><a href=""></a> <span class="co"># Censored likelihood</span></span>
<span id="cb10-7"><a href=""></a> pred <span class="op">=</span> censored_normal(pred_mean, sigma, y[t], censored[t])</span>
<span id="cb10-8"><a href=""></a> <span class="cf">return</span> (pred, y_prev_1), pred</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="footer">
<p><a href="https://juanitorduz.github.io/demand/">Demand Forecasting with Censored Likelihood</a></p>
</div>
</div>
</section>
<section id="censored-time-series-forecast" class="slide level2">
<h2>Censored Time Series Forecast</h2>
<div class="quarto-figure quarto-figure-center">
<figure>
<p><img data-src="timeseries_numpyro_files/static/images/demand_files/demand_48_0.png" class="quarto-figure quarto-figure-center" width="1000"></p>
</figure>
</div>
<div class="footer">
<p><a href="https://juanitorduz.github.io/demand/">Demand Forecasting with Censored Likelihood</a></p>
</div>
<div class="quarto-auto-generated-content">
<p><img src="timeseries_numpyro_files/static/images/juanitorduz_logo_small.png" class="slide-logo"></p>
Expand Down
83 changes: 83 additions & 0 deletions Presentations/pydata_amsterdam_2024/timeseries_numpyro.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,87 @@ def croston_model(z: ArrayLike, p_inv: ArrayLike, future: int = 0) -> None:

::: footer
[Zero-Inflated TSB Model](https://juanitorduz.github.io/zi_tsb_numpyro/)
:::

## Forecasting Unseen Demand

![](timeseries_numpyro_files/static/images/demand_files/demand_8_0.png){fig-align="center" width="1000"}

::: footer
[Demand Forecasting with Censored Likelihood](https://juanitorduz.github.io/demand/)
:::

## ARIMA Model

![](timeseries_numpyro_files/static/images/demand_files/demand_25_0.png){fig-align="center" width="1000"}

::: footer
[Demand Forecasting with Censored Likelihood](https://juanitorduz.github.io/demand/)
:::

## Censored Distributions

![](timeseries_numpyro_files/static/images/censoring_files/censoring_5_1.png){fig-align="center" width="1000"}

::: footer
[Bayesian Censoring Data Modeling](https://juanitorduz.github.io/censoring/)
:::

## Censored Distributions (Naive)

![](timeseries_numpyro_files/static/images/censoring_files/censoring_12_1.png){fig-align="center" width="600"}

![](timeseries_numpyro_files/static/images/censoring_files/censoring_15_1.png){fig-align="center" width="600"}

::: footer
[Bayesian Censoring Data Modeling](https://juanitorduz.github.io/censoring/)
:::

## Censored Distributions

![](timeseries_numpyro_files/static/images/censoring_files/censoring_26_1.png){fig-align="center" width="600"}

![](timeseries_numpyro_files/static/images/censoring_files/censoring_28_1.png){fig-align="center" width="600"}

::: footer
[Bayesian Censoring Data Modeling](https://juanitorduz.github.io/censoring/)
:::

## Censored Likelihood

```{.python}
def censored_normal(loc, scale, y, censored):
distribution = dist.Normal(loc=loc, scale=scale)
ccdf = 1 - distribution.cdf(y)
numpyro.sample(
"censored_label",
dist.Bernoulli(probs=ccdf).mask(censored == 1),
obs=censored
)
return numpyro.sample("pred", distribution.mask(censored != 1))
```
. . .

Change likelihood distribution in a time-series model:

```{.python}
## Transition function for AR(2)
def transition_fn(carry, t):
y_prev_1, y_prev_2 = carry
ar_part = phi_1 * y_prev_1 + phi_2 * y_prev_2
pred_mean = mu + ar_part + seasonal[t]
# Censored likelihood
pred = censored_normal(pred_mean, sigma, y[t], censored[t])
return (pred, y_prev_1), pred
```
::: footer
[Demand Forecasting with Censored Likelihood](https://juanitorduz.github.io/demand/)
:::

## Censored Time Series Forecast

![](timeseries_numpyro_files/static/images/demand_files/demand_48_0.png){fig-align="center" width="1000"}

::: footer
[Demand Forecasting with Censored Likelihood](https://juanitorduz.github.io/demand/)
:::

0 comments on commit 1cd281a

Please sign in to comment.