%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22scikit-learn%22%2C%0A%23%20%20%20%20%20%22yohou%5Bplotting%5D%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.8%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20from%20sklearn.ensemble%20import%20RandomForestRegressor%0A%20%20%20%20from%20sklearn.linear_model%20import%20Ridge%0A%0A%20%20%20%20from%20yohou.datasets%20import%20fetch_electricity_demand%0A%20%20%20%20from%20yohou.interval%20import%20SplitConformalForecaster%0A%20%20%20%20from%20yohou.model_selection%20import%20train_test_split%0A%20%20%20%20from%20yohou.plotting%20import%20plot_calibration%2C%20plot_forecast%2C%20plot_residuals%0A%20%20%20%20from%20yohou.point%20import%20PointReductionForecaster%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20PointReductionForecaster%2C%0A%20%20%20%20%20%20%20%20RandomForestRegressor%2C%0A%20%20%20%20%20%20%20%20Ridge%2C%0A%20%20%20%20%20%20%20%20SplitConformalForecaster%2C%0A%20%20%20%20%20%20%20%20fetch_electricity_demand%2C%0A%20%20%20%20%20%20%20%20plot_calibration%2C%0A%20%20%20%20%20%20%20%20plot_forecast%2C%0A%20%20%20%20%20%20%20%20plot_residuals%2C%0A%20%20%20%20%20%20%20%20train_test_split%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20How%20to%20Visualize%20Forecasts%0A%0A%20%20%20%20This%20notebook%20demonstrates%20how%20to%20plot%20forecast%20output%20against%20actuals%0A%20%20%20%20using%20the%20%60yohou.plotting%60%20module.%20We%20cover%20point%20forecasts%2C%20multi-model%0A%20%20%20%20comparison%2C%20prediction%20interval%20bands%2C%20residual%20diagnostics%2C%20and%0A%20%20%20%20calibration%20checks.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Load%20Data%20and%20Fit%20a%20Baseline%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PointReductionForecaster%2C%0A%20%20%20%20Ridge%2C%0A%20%20%20%20fetch_electricity_demand%2C%0A%20%20%20%20train_test_split%2C%0A)%3A%0A%20%20%20%20data%20%3D%20fetch_electricity_demand()%0A%20%20%20%20y%20%3D%20data.frame.select(%22time%22%2C%20%22qun__demand%22).drop_nulls().rename(%7B%22qun__demand%22%3A%20%22demand%22%7D)%0A%0A%20%20%20%20%23%20Use%20the%20last%205000%20observations%20for%20a%20lightweight%20demo%0A%20%20%20%20y_recent%20%3D%20y.tail(5000)%0A%20%20%20%20y_train%2C%20y_test%20%3D%20train_test_split(y_recent%2C%20test_size%3D24)%0A%0A%20%20%20%20forecaster%20%3D%20PointReductionForecaster(estimator%3DRidge())%0A%20%20%20%20forecaster.fit(y_train%2C%20forecasting_horizon%3D24)%0A%20%20%20%20y_pred%20%3D%20forecaster.predict()%0A%20%20%20%20return%20y_pred%2C%20y_test%2C%20y_train%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.%20Plot%20a%20Point%20Forecast%0A%0A%20%20%20%20%5B%60plot_forecast%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.plotting.forecasting.plot_forecast%2F)%20accepts%20test%20actuals%20and%20predictions.%20Pass%20%60y_train%60%0A%20%20%20%20to%20show%20historical%20context.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plot_forecast%2C%20y_pred%2C%20y_test%2C%20y_train)%3A%0A%20%20%20%20fig_point%20%3D%20plot_forecast(y_test%2C%20y_pred%2C%20y_train%3Dy_train)%0A%20%20%20%20fig_point%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.%20Compare%20Multiple%20Models%0A%0A%20%20%20%20Pass%20a%20dict%20of%20prediction%20DataFrames%20to%20overlay%20several%20models%20in%20one%0A%20%20%20%20chart.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PointReductionForecaster%2C%0A%20%20%20%20RandomForestRegressor%2C%0A%20%20%20%20plot_forecast%2C%0A%20%20%20%20y_pred%2C%0A%20%20%20%20y_test%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20forecaster_rf%20%3D%20PointReductionForecaster(%0A%20%20%20%20%20%20%20%20estimator%3DRandomForestRegressor(n_estimators%3D50%2C%20max_depth%3D10)%0A%20%20%20%20)%0A%20%20%20%20forecaster_rf.fit(y_train%2C%20forecasting_horizon%3D24)%0A%20%20%20%20y_pred_rf%20%3D%20forecaster_rf.predict()%0A%0A%20%20%20%20fig_compare%20%3D%20plot_forecast(%0A%20%20%20%20%20%20%20%20y_test%2C%0A%20%20%20%20%20%20%20%20%7B%22Ridge%22%3A%20y_pred%2C%20%22RandomForest%22%3A%20y_pred_rf%7D%2C%0A%20%20%20%20%20%20%20%20y_train%3Dy_train%2C%0A%20%20%20%20)%0A%20%20%20%20fig_compare%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203.%20Plot%20Prediction%20Intervals%0A%0A%20%20%20%20%5B%60plot_forecast%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.plotting.forecasting.plot_forecast%2F)%20renders%20shaded%20bands%20when%20%60y_pred%60%20contains%20interval%0A%20%20%20%20columns%20(named%20%60%7Btarget%7D__lower_%7Brate%7D%60%20and%20%60%7Btarget%7D__upper_%7Brate%7D%60).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PointReductionForecaster%2C%0A%20%20%20%20Ridge%2C%0A%20%20%20%20SplitConformalForecaster%2C%0A%20%20%20%20plot_forecast%2C%0A%20%20%20%20y_test%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20cp%20%3D%20SplitConformalForecaster(%0A%20%20%20%20%20%20%20%20point_forecaster%3DPointReductionForecaster(estimator%3DRidge())%2C%0A%20%20%20%20)%0A%20%20%20%20cp.fit(y_train%2C%20forecasting_horizon%3D24)%0A%20%20%20%20y_pred_interval%20%3D%20cp.predict_interval(coverage_rates%3D%5B0.90%5D)%0A%0A%20%20%20%20fig_interval%20%3D%20plot_forecast(y_test%2C%20y_pred_interval%2C%20y_train%3Dy_train)%0A%20%20%20%20fig_interval%0A%20%20%20%20return%20(y_pred_interval%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204.%20Inspect%20Residuals%0A%0A%20%20%20%20%5B%60plot_residuals%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.plotting.evaluation.plot_residuals%2F)%20produces%20a%20diagnostic%20view%20(residuals%20over%20time%2C%0A%20%20%20%20residuals%20vs%20fitted%2C%20histogram%2C%20Q-Q%20plot).%20A%20well-specified%20model%20has%0A%20%20%20%20residuals%20centred%20at%20zero%20with%20no%20time-dependent%20patterns.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plot_residuals%2C%20y_pred%2C%20y_test)%3A%0A%20%20%20%20fig_resid%20%3D%20plot_residuals(y_pred%2C%20y_test)%0A%20%20%20%20fig_resid%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205.%20Check%20Calibration%0A%0A%20%20%20%20%5B%60plot_calibration%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.plotting.evaluation.plot_calibration%2F)%20compares%20nominal%20coverage%20rates%20against%20empirical%0A%20%20%20%20coverage.%20Points%20close%20to%20the%20diagonal%20indicate%20a%20well-calibrated%0A%20%20%20%20forecaster%3B%20points%20below%20indicate%20under-coverage%20(intervals%20too%20narrow).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plot_calibration%2C%20y_pred_interval%2C%20y_test)%3A%0A%20%20%20%20fig_cal%20%3D%20plot_calibration(y_pred_interval%2C%20y_test%2C%20coverage_rates%3D%5B0.90%5D)%0A%20%20%20%20fig_cal%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20See%20Also%0A%0A%20%20%20%20-%20%5BHow%20to%20Visualize%20Forecasts%5D(%2Fpages%2Fhow-to%2Fvisualize-forecasts%2F)%20(companion%20page)%0A%20%20%20%20-%20%5BHow%20to%20Visualize%20and%20Compare%20Model%20Scores%5D(%2Fpages%2Fhow-to%2Fvisualize-scores%2F)%0A%20%20%20%20-%20%5B%60yohou.plotting%60%20API%20Reference%5D(%2Fpages%2Fapi%2Fplotting%2F)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
4858e24ec35b0579526f942d18f4a575