Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Add L-BFGS solver for MAP Inference #146

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions docs/howto/composite-exogenous-effects/index.ipy
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ from prophetverse.effects import LinearEffect
from prophetverse.effects.fourier import LinearFourierSeasonality
from prophetverse.effects.trend import PiecewiseLinearTrend
from prophetverse.engine import MAPInferenceEngine
from prophetverse.engine.optimizer import AdamOptimizer
from prophetverse.sktime import Prophetverse
from prophetverse.utils.regex import exact, no_input_columns

Expand Down Expand Up @@ -96,10 +95,7 @@ model = Prophetverse(
exact("investment"),
),
],
inference_engine=MAPInferenceEngine(
optimizer=AdamOptimizer(1e-4),
num_steps=100_000,
),
inference_engine=MAPInferenceEngine(),
)

model.fit(y=y_train, X=X_train)
Expand Down
90 changes: 42 additions & 48 deletions docs/howto/composite-exogenous-effects/index.md

Large diffs are not rendered by default.

Binary file modified docs/howto/composite-exogenous-effects/index_files/output_14_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/howto/composite-exogenous-effects/index_files/output_16_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions docs/howto/custom-effects/index.ipy
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ fig.show()

from prophetverse.effects.trend import PiecewiseLinearTrend
from prophetverse.engine import MAPInferenceEngine
from prophetverse.engine.optimizer import AdamOptimizer, BFGSOptimizer
from prophetverse.sktime import Prophetverse
from prophetverse.utils.regex import exact

Expand All @@ -231,7 +230,7 @@ model = (
changepoint_range=-100,

)
>> MAPInferenceEngine(num_steps=50_000)
>> MAPInferenceEngine()
) >> (
"exog_effect",
SquaredEffect(
Expand Down
159 changes: 79 additions & 80 deletions docs/howto/custom-effects/index.md

Large diffs are not rendered by default.

Binary file modified docs/howto/custom-effects/index_files/output_10_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/howto/custom-effects/index_files/output_14_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/howto/custom-effects/index_files/output_5_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/howto/custom-effects/index_files/output_5_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/tutorial/count-data/file_files/output_10_0.png
Binary file not shown.
Binary file removed docs/tutorial/count-data/file_files/output_11_0.png
Binary file not shown.
Binary file removed docs/tutorial/count-data/file_files/output_12_0.png
Binary file not shown.
Binary file removed docs/tutorial/count-data/file_files/output_15_0.png
Binary file not shown.
Binary file removed docs/tutorial/count-data/file_files/output_16_0.png
Binary file not shown.
Binary file removed docs/tutorial/count-data/file_files/output_17_0.png
Binary file not shown.
Binary file removed docs/tutorial/count-data/file_files/output_18_0.png
Binary file not shown.
Binary file removed docs/tutorial/count-data/file_files/output_19_0.png
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import warnings

warnings.simplefilter(action="ignore")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from sktime.transformations.series.fourier import FourierFeatures
from sktime.forecasting.compose import ForecastingPipeline
from numpyro import distributions as dist
from sktime.forecasting.compose import ForecastingPipeline
from sktime.transformations.series.fourier import FourierFeatures

from prophetverse.datasets.loaders import load_pedestrian_count

# %% [markdown]
# ## Import dataset
Expand All @@ -23,7 +24,6 @@ from numpyro import distributions as dist

# %%

from prophetverse.datasets.loaders import load_pedestrian_count
y = load_pedestrian_count()

# We take only one time series for simplicity
Expand All @@ -43,7 +43,7 @@ plt.show()

# %% [markdown]

#The full dataset is actually large, and plotting it all at once does not
# The full dataset is actually large, and plotting it all at once does not
# help a lot. Either way, let's plot the full dataset to see how it looks like:

# %%
Expand All @@ -55,11 +55,11 @@ plt.show()
# %% [markdown]
# ## Fitting models
#
# The series has some clear patterns: a daily seasonality, a weekly seasonality, and
# a yearly seasonality. It also has many zeros, and a model assuming normal
# The series has some clear patterns: a daily seasonality, a weekly seasonality, and
# a yearly seasonality. It also has many zeros, and a model assuming normal
# distributed observations would not be able to capture this.
#
# First, let's fit and forecast with the standard prophet,
# First, let's fit and forecast with the standard prophet,
# then see how the negative binomial model performs.
#
# ## Prophet with normal likelihood
Expand All @@ -69,13 +69,12 @@ plt.show()
#

# %%
from prophetverse.sktime import Prophetverse
from prophetverse.effects.fourier import LinearFourierSeasonality
from prophetverse.effects.trend import FlatTrend
from prophetverse.engine import MAPInferenceEngine
from prophetverse.engine.optimizer import AdamOptimizer
from prophetverse.utils.regex import no_input_columns

from prophetverse.sktime import Prophetverse
from prophetverse.utils.regex import no_input_columns

# Here we set the prior for the seasonality effect
# And the coefficients for it
Expand All @@ -96,16 +95,13 @@ exogenous_effects = [
model = Prophetverse(
trend=FlatTrend(),
exogenous_effects=exogenous_effects,
inference_engine=MAPInferenceEngine(
optimizer=AdamOptimizer(),
num_steps=20_000,
),
inference_engine=MAPInferenceEngine(),
)
model.fit(y=y_train)

# %% [markdown]
# ### Forecasting with the normal model
# Below we see the negative predictions, which is clear a limitation of this
# Below we see the negative predictions, which is clear a limitation of this
# gaussian likelihood for this kind of data.
# %%
forecast_horizon = y_train.index[-100:].union(y_test.index[:300])
Expand Down Expand Up @@ -193,7 +189,7 @@ fig.show()

# %% [markdown]
# ## Comparing both forecasts side by side
#
#
# To make our point clear, we plot both forecasts side by side. Isn't it nice to have
# forecasts that make sense? :smile:

Expand Down Expand Up @@ -222,3 +218,5 @@ ax.axvline(
fig.legend(loc="center", ncol=4, bbox_to_anchor=(0.5, 0.8))
fig.tight_layout()
fig.show()

# %%
Loading
Loading