|
| 1 | +# Copyright 2022 - 2025 The PyMC Labs Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Streamlit page for fourier modes.""" |
| 15 | + |
| 16 | +import plotly.graph_objects as go |
| 17 | + |
| 18 | +import streamlit as st |
| 19 | +from pymc_marketing.mmm import MonthlyFourier, YearlyFourier |
| 20 | +from pymc_marketing.prior import Prior |
| 21 | + |
| 22 | +# Constants |
| 23 | +PLOT_HEIGHT = 500 |
| 24 | +PLOT_WIDTH = 1000 |
| 25 | + |
| 26 | +# -------------------------- TOP OF PAGE INFORMATION ------------------------- |
| 27 | + |
| 28 | +# Set browser / tab config |
| 29 | +st.set_page_config( |
| 30 | + page_title="MMM App - Fourier Modes", |
| 31 | + page_icon="🧊", |
| 32 | +) |
| 33 | + |
| 34 | +# Give some context for what the page displays |
| 35 | +st.title("Fourier Modes") |
| 36 | + |
| 37 | +st.markdown( |
| 38 | + "This page demonstrates Fourier seasonality transformations for use \ |
| 39 | + in MMM. Fourier seasonality relies on sine and cosine \ |
| 40 | + functions to capture recurring patterns in the data, making it useful \ |
| 41 | + for modeling periodic trends." |
| 42 | +) |
| 43 | + |
| 44 | +st.markdown("___The Fourier component takes the form:___") |
| 45 | + |
| 46 | +# LaTeX string for Fourier seasonal component |
| 47 | +fourier_formula = r""" |
| 48 | +f(t) = \sum_{k=1}^{K} \Bigg[ a_k \cos\Big(\frac{2 \pi k t}{T}\Big) |
| 49 | + + b_k \sin\Big(\frac{2 \pi k t}{T}\Big) \Bigg] |
| 50 | +""" |
| 51 | +st.latex(fourier_formula) |
| 52 | + |
| 53 | +st.markdown(""" |
| 54 | +**Where:** |
| 55 | +
|
| 56 | +- $t$ = time index (e.g., day, week, month) |
| 57 | +- $T$ = period of the seasonality (e.g., 12 for monthly, 365 for yearly) |
| 58 | +- $K$ = order of the Fourier series (number of sine/cosine pairs) |
| 59 | +- $a_k, b_k$ = Fourier coefficients |
| 60 | +""") |
| 61 | + |
| 62 | +st.markdown( |
| 63 | + "🗒️ **Note:** \n \ |
| 64 | +- Yearly Fourier: A yearly seasonality with a period ($T$) of **_:red[365.25 days]_** \n \ |
| 65 | +- Monthly Fourier: A monthly seasonality with a period ($T$) of **_:red[365.25 / 12 days]_**" |
| 66 | +) |
| 67 | + |
| 68 | +st.divider() |
| 69 | + |
| 70 | +# User inputs |
| 71 | +st.subheader(":orange[User Inputs]") |
| 72 | +# Slider for selecting the order |
| 73 | +n_order = st.slider( |
| 74 | + "Fourier order $K$ (n_order)", min_value=1, max_value=20, value=6, step=1 |
| 75 | +) |
| 76 | +# Slider for selecting the scale param |
| 77 | +b = st.slider( |
| 78 | + "Laplace scale (__b__)", min_value=0.01, max_value=1.0, value=0.1, step=0.01 |
| 79 | +) |
| 80 | + |
| 81 | +# Setup |
| 82 | +prior = Prior("Laplace", mu=0, b=b, dims="fourier") |
| 83 | + |
| 84 | +# Create tabs for plots |
| 85 | +tab1, tab2 = st.tabs(["Yearly", "Monthly"]) |
| 86 | + |
| 87 | +# -------------------------- YEARLY SEASONALITY ------------------------- |
| 88 | +with tab1: |
| 89 | + st.subheader(":orange[Yearly Seasonality]") |
| 90 | + |
| 91 | + fourier = YearlyFourier(n_order=n_order, prior=prior) |
| 92 | + |
| 93 | + # Displayed in the APP |
| 94 | + parameters = fourier.sample_prior() |
| 95 | + curve = fourier.sample_curve(parameters) |
| 96 | + # Drop chain if it's always 1 |
| 97 | + curve = curve.squeeze("chain") |
| 98 | + # Compute mean and quantiles across draws |
| 99 | + mean_trend = curve.mean("draw") |
| 100 | + # Grab the days for the x-axis |
| 101 | + days = curve.coords["day"].values |
| 102 | + |
| 103 | + # Build Plotly figure |
| 104 | + fig = go.Figure() |
| 105 | + |
| 106 | + # Mean line |
| 107 | + fig.add_trace( |
| 108 | + go.Scatter( |
| 109 | + x=days, |
| 110 | + y=mean_trend.values, |
| 111 | + mode="lines", |
| 112 | + line=dict(color="blue"), |
| 113 | + name="Mean trend", |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + fig.update_layout( |
| 118 | + title="Yearly Fourier Trend", |
| 119 | + xaxis_title="Day", |
| 120 | + yaxis_title="Trend", |
| 121 | + height=PLOT_HEIGHT, |
| 122 | + width=PLOT_WIDTH, |
| 123 | + ) |
| 124 | + |
| 125 | + st.plotly_chart(fig, use_container_width=True) |
| 126 | + |
| 127 | +# -------------------------- MONTHLY SEASONALITY ------------------------- |
| 128 | +with tab2: |
| 129 | + st.subheader(":orange[Monthly Seasonality]") |
| 130 | + |
| 131 | + fourier = MonthlyFourier(n_order=n_order, prior=prior) |
| 132 | + |
| 133 | + # Displayed in the APP |
| 134 | + parameters = fourier.sample_prior() |
| 135 | + curve = fourier.sample_curve(parameters) |
| 136 | + # Drop chain if it's always 1 |
| 137 | + curve = curve.squeeze("chain") |
| 138 | + # Compute mean and quantiles across draws |
| 139 | + mean_trend = curve.mean("draw") |
| 140 | + # Grab the days for the x-axis |
| 141 | + days = curve.coords["day"].values |
| 142 | + |
| 143 | + # Build Plotly figure |
| 144 | + fig = go.Figure() |
| 145 | + |
| 146 | + # Mean line |
| 147 | + fig.add_trace( |
| 148 | + go.Scatter( |
| 149 | + x=days, |
| 150 | + y=mean_trend.values, |
| 151 | + mode="lines", |
| 152 | + line=dict(color="blue"), |
| 153 | + name="Mean trend", |
| 154 | + ) |
| 155 | + ) |
| 156 | + |
| 157 | + fig.update_layout( |
| 158 | + title="Monthly Fourier Trend", |
| 159 | + xaxis_title="Day", |
| 160 | + yaxis_title="Trend", |
| 161 | + height=PLOT_HEIGHT, |
| 162 | + width=PLOT_WIDTH, |
| 163 | + ) |
| 164 | + |
| 165 | + st.plotly_chart(fig, use_container_width=True) |
0 commit comments