Package: pasts
Python AnalySis for Time SeriesThis package aims to structure the way time series analysis and forecasting is done.Purpose of the PackageThe purpose of the package is to
provide a collection of forecasting models
and analysis methods for time series in one unified library.FeaturesCollection of analysis methods:Scipy and statsmodel for testingTime series processingStatistical testing (stationarity, check seasonality, ...  )VisualizationCollection of forecasting models using Darts, which is itself an aggregator ofscikit-learntenserflowprophetAuto regression models (ARIMA, SARIMA, ....)etc.InstallationThe package can be installed by :python3-mpipinstallgit+https://github.com/eurobios-mews-labs/pastsBuilding the documentationFirst, make sure you have sphinx and the Readthedocs theme installed.If you use pip, open a terminal and enter the following commands:pipinstallsphinx
pipinstallsphinx_rtd_themeIf you use conda, open an Anaconda Powershell Prompt and enter the following commands:condainstallsphinx
condainstallsphinx_rtd_themeThen, in the same terminal or anaconda prompt, build the doc with:cddoc
makehtmlThe documentation can then be accessed fromdoc/_build/html/index.html.Usage and exampleYou can find examples for theSignalclass for univariate and multivariate series here:examples/ex_model.pyTheOperationclass can be used on its own. Find an example here:examples/ex_operations.pyStart projectTo start using the package, import you data as a pandas dataframe with a temporal index and use theSignalclass.importpandasaspdfromdarts.datasetsimportAirPassengersDatasetfromdarts.modelsimportAutoARIMA,Prophet,ExponentialSmoothing,XGBModel,VARIMAfromdarts.utils.utilsimportModelMode,SeasonalityModefrompasts.signalimportSignalfrompasts.visualizationimportVisualizationseries=AirPassengersDataset().load()dt=pd.DataFrame(series.values())dt.rename(columns={0:'passengers'},inplace=True)dt.index=series.time_indexsignal=Signal(dt)Visualize and analyze dataThepropertiesattribute contains some information about the data.
Use theVisualizationclass to generate various types of plots.print(signal.properties)Output:>>>{'shape':(144,1),'types':passengersfloat64dtype:object,'is_univariate':True,'nanSum':passengers0dtype:int64,'quantiles':0.000.050.500.950.991.00passengers104.0121.6265.5488.15585.79622.0}Visualization(signal).plot_signal()Visualization(signal).acf_plot()Yield:You can also perform some statistical tests specific to time series.signal.apply_stat_test('stationary')signal.apply_stat_test('stationary','kpss')signal.apply_stat_test('seasonality')print(signal.tests_stat)Output: Whether the null hypothesis is rejected and p-value for stationarity, seasonality period for seasonality.>>>{'stationary: adfuller':(False,0.9918802434376409),'stationary: kpss':(False,0.01),'seasonality: check_seasonality':(<functioncheck_seasonalityat0x000001D1D62EE310>,(True,12))}Machine LearningChoose a date to split the series between train and test.timestamp='1958-12-01'signal.validation_split(timestamp=timestamp)The library provides some operations to apply before using forecasting models.
In this example, both linear trend and seasonality are removed. Machine Learning models will be trained on the remainig series, then inverse functions will be applied to the predicted series.signal.apply_operations(['trend','seasonality'])Visualization(signal).plot_signal()Use the methodapply_modelto apply models of your choice. If the parametersgridsearchandparametersare passed, a gridsearch will be performed.signal.apply_model(ExponentialSmoothing())signal.apply_model(AutoARIMA())signal.apply_model(Prophet())# Be careful : if trend and seasonality were removed, this specific gridsearch cannot be performed.param_grid={'trend':[ModelMode.ADDITIVE,ModelMode.MULTIPLICATIVE,ModelMode.NONE],'seasonal':[SeasonalityMode.ADDITIVE,SeasonalityMode.MULTIPLICATIVE,SeasonalityMode.NONE],}signal.apply_model(ExponentialSmoothing(),gridsearch=True,parameters=param_grid)You can pass a list of metrics to the methodcompute_scores. By default, it tries to compute R², MSE, RMSE, MAPE, SMAPE and MAE. Warnings are raised if some metrics are impossible to compute with this type of data.
You can choose to compute scores time-wise or unit-wise with parameteraxis. However, with univariate data it is preferable to keep the default value (axis=1, unit-wise).signal.compute_scores(list_metrics=['rmse','r2'])signal.compute_scores()print(signal.models['Prophet']['scores'])Output:>>>{'unit_wise':r2msermsemapesmapemaecomponentpassengers0.866962590.0855724.2916773.6943113.74324118.008002,'time_wise':{}}print(signal.performance_models['unit_wise']['rmse'])Output:>>>ProphetExponentialSmoothingAutoARIMAcomponentpassengers24.29167740.30677126.718103Visualize predictions with theVisualizationclass:Visualization(signal).show_predictions()When models have been trained, you can compute predictions for future dates using theforecastmethod by passing it the name of a trained model and the horizon of prediction.signal.forecast("Prophet",100)signal.forecast("AutoARIMA",100)signal.forecast("ExponentialSmoothing",100)Visualization(signal).show_forecast()Aggregation of modelsThe methodapply_aggregated_modelaggregates the passed list of trained estimators according to their RMSE on train data. All passed models will be kept, so make sure to exclude models that are always less performant. The more a model performs compared to others, the greater its weight in agregation.signal.apply_aggregated_model([AutoARIMA(),Prophet()])signal.compute_scores(axis=1)Visualization(signal).show_predictions()signal.forecast("AggregatedModel",100)Author