%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%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_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20How%20to%20Handle%20Long%20Series%0A%0A%20%20%20%20When%20a%20series%20spans%20decades%2C%20patterns%20from%20the%20distant%20past%20may%20be%0A%20%20%20%20irrelevant.%20This%20notebook%20shows%20how%20to%20limit%20history%2C%20weight%20recent%0A%20%20%20%20errors%20more%20heavily%2C%20and%20downsample%20high%20frequency%20data.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20sklearn.linear_model%20import%20Ridge%0A%0A%20%20%20%20from%20yohou.datasets%20import%20fetch_sunspot%0A%20%20%20%20from%20yohou.plotting%20import%20plot_time_series%0A%0A%20%20%20%20return%20Ridge%2C%20fetch_sunspot%2C%20plot_time_series%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.%20Load%20a%20Long%20Series%0A%0A%20%20%20%20The%20sunspot%20dataset%20spans%20centuries%2C%20making%20it%20a%20good%20example%20of%20data%0A%20%20%20%20where%20distant%20history%20may%20hurt%20more%20than%20help.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fetch_sunspot%2C%20plot_time_series)%3A%0A%20%20%20%20data%20%3D%20fetch_sunspot().frame%0A%20%20%20%20print(f%22Series%20length%3A%20%7Blen(data)%7D%20observations%22)%0A%20%20%20%20plot_time_series(data%2C%20title%3D%22Full%20Sunspot%20Series%22)%0A%20%20%20%20return%20(data%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%202.%20Limit%20History%20with%20observation_horizon%0A%0A%20%20%20%20Set%20%60observation_horizon%60%20on%20stateful%20transformers%20to%20keep%20only%0A%20%20%20%20recent%20timesteps%20in%20their%20state.%20Older%20observations%20are%20dropped%20as%0A%20%20%20%20new%20ones%20arrive.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Ridge%2C%20data)%3A%0A%20%20%20%20from%20yohou.compose%20import%20FeaturePipeline%0A%20%20%20%20from%20yohou.metrics%20import%20MeanAbsoluteError%0A%20%20%20%20from%20yohou.point%20import%20PointReductionForecaster%0A%20%20%20%20from%20yohou.preprocessing%20import%20LagTransformer%0A%0A%20%20%20%20from%20yohou.model_selection%20import%20train_test_split%0A%0A%20%20%20%20y_train%2C%20y_test%20%3D%20train_test_split(data%2C%20test_size%3D24)%0A%0A%20%20%20%20%23%20Full%20history%0A%20%20%20%20pipe_full%20%3D%20FeaturePipeline(%5B(%22lags%22%2C%20LagTransformer(lag%3D%5B1%2C%206%2C%2012%5D))%5D)%0A%20%20%20%20fc_full%20%3D%20PointReductionForecaster(estimator%3DRidge()%2C%20feature_transformer%3Dpipe_full)%0A%20%20%20%20fc_full.fit(y_train%2C%20forecasting_horizon%3D24)%0A%20%20%20%20pred_full%20%3D%20fc_full.predict(forecasting_horizon%3D24)%0A%0A%20%20%20%20%23%20Limited%20history%3A%20keep%20only%20the%20last%20120%20observations%0A%20%20%20%20pipe_limited%20%3D%20FeaturePipeline(%0A%20%20%20%20%20%20%20%20%5B(%22lags%22%2C%20LagTransformer(lag%3D%5B1%2C%206%2C%2012%5D))%5D%0A%20%20%20%20)%0A%20%20%20%20fc_limited%20%3D%20PointReductionForecaster(estimator%3DRidge()%2C%20feature_transformer%3Dpipe_limited)%0A%20%20%20%20fc_limited.fit(y_train.tail(120)%2C%20forecasting_horizon%3D24)%0A%20%20%20%20pred_limited%20%3D%20fc_limited.predict(forecasting_horizon%3D24)%0A%0A%20%20%20%20scorer%20%3D%20MeanAbsoluteError()%0A%20%20%20%20scorer.fit(y_train)%0A%20%20%20%20print(f%22MAE%20(full%20history)%3A%20%20%20%20%7Bscorer.score(y_test%2C%20pred_full)%3A.2f%7D%22)%0A%20%20%20%20print(f%22MAE%20(limited%20history)%3A%20%7Bscorer.score(y_test%2C%20pred_limited)%3A.2f%7D%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%203.%20Weight%20Recent%20Errors%20with%20Exponential%20Decay%0A%0A%20%20%20%20Instead%20of%20discarding%20old%20data%20entirely%2C%20weight%20recent%20observations%0A%20%20%20%20more%20heavily.%20%5B%60exponential_decay_weight%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.utils.weighting.exponential_decay_weight%2F)%20halves%20the%20weight%20every%0A%20%20%20%20%60half_life%60%20steps.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20yohou.utils.weighting%20import%20exponential_decay_weight%2C%20linear_decay_weight%0A%0A%20%20%20%20%23%20Exponential%20decay%3A%20weight%20halves%20every%20120%20steps%0A%20%20%20%20exp_weight%20%3D%20exponential_decay_weight(half_life%3D120)%0A%0A%20%20%20%20%23%20Linear%20decay%3A%20weight%20drops%20to%200%20at%20max_steps%20ago%0A%20%20%20%20lin_weight%20%3D%20linear_decay_weight(max_steps%3D240)%0A%0A%20%20%20%20print(f%22Exponential%20weight%20function%3A%20%7Bexp_weight%7D%22)%0A%20%20%20%20print(f%22Linear%20weight%20function%3A%20%20%20%20%20%20%7Blin_weight%7D%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%204.%20Downsample%20High%20Frequency%20Data%0A%0A%20%20%20%20When%20data%20arrives%20at%20a%20higher%20frequency%20than%20the%20forecast%20requirement%2C%0A%20%20%20%20use%20%5B%60Downsampler%60%5D(%2Fpages%2Fapi%2Fgenerated%2Fyohou.preprocessing.resampling.Downsampler%2F)%20to%20aggregate.%20Place%20it%20at%20the%20start%20of%20the%20pipeline%2C%0A%20%20%20%20before%20stateful%20transformers.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20yohou.preprocessing.resampling%20import%20Downsampler%0A%0A%20%20%20%20%23%20Aggregate%20to%20quarterly%20using%20mean%0A%20%20%20%20downsampler%20%3D%20Downsampler(interval%3D%2291d%22%2C%20aggregation%3D%22mean%22)%0A%20%20%20%20print(f%22Downsampler%20config%3A%20interval%3D%7Bdownsampler.interval%7D%2C%20aggregation%3D%7Bdownsampler.aggregation%7D%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%20Next%20Steps%0A%0A%20%20%20%20-%20%5BHandle%20Long%20Series%5D(%2Fpages%2Fhow-to%2Fhandle-long-series%2F)%20for%20the%20full%20guide%0A%20%20%20%20-%20%5BClean%20and%20Resample%20Time%20Series%5D(%2Fpages%2Fhow-to%2Fclean-and-resample%2F)%20for%20related%20techniques%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
59ea971a75bfbf3ee1d8b3a25dbb9d78