Are you looking for an easy yet powerful way to solve Time Series problems and build a nice GUI for them? You can deliver complete Statistical And Time Series Analysis easily by combining statsmodels and Python4Delphi library, inside Delphi and C++Builder IDE software.

This post will guide you on **how to run the statsmodels library and use Python for Delphi to display it in the Delphi Windows GUI app**.

**What is time series analysis?**

**Time Series** is an ordered sequence of data points that spread over a period of time. Thus it is a sequence of discrete-time data. The Time Series Data is monitored over constant temporal intervals. This data can be in any measurable and quantifiable parameter related to the field of business, science, finance, etc (for example: Heights of ocean tides, counts of sunspots, the daily closing value of the Dow Jones Industrial Average, etc.).

**Time Series Analysis** refers to the identification of the common patterns displayed by the data over a period of time. For these tasks, experts employ specific methods to study the data’s characteristics and extract meaningful statistics that eventually aid in forecasting.

Time Series Analysis is beneficial and commonly used for Economic Forecasting, Yield Projection, Inventory Studies, Census Analysis, Sales Forecasting, Stock Market Analysis, Budgetary Analysis, etc.

**Why use Python for time series analysis?**

- Python is a general-purpose interpreted programming language (unlike R or Matlab).
- Easy to learn and use primarily because it focuses on readability.
- It is a popular language in general, consistently appearing in the top 10 programming languages in surveys on StackOverflow (for example, the 2015 survey results).
- Python is a dynamic language and very suited to interactive development and quick prototyping with the power to support the development of large applications.
- Python is also widely used for Machine Learning and Data Science because of the excellent library support (in this post, you will learn how to implement Machine Learning for Time Series tasks).
- It means that you can perform your research and development (figuring out what models to use) in the same programming language that you use in productions, greatly simplifying the transition from development to production.

**Read more here, for Why use Python for Scientific Computing:**

**What is the statsmodels library?**

**statsmodels** is a Python module that provides classes and functions for the estimation of many different statistical models, as well as for conducting statistical tests and statistical data exploration. statsmodels provides a complement to SciPy for statistical computations including descriptive statistics and estimation and inference for statistical models.

statsmodels provides an extensive list of result statistics that are available for each estimator. The results are tested against existing statistical packages to ensure that they are correct.

Time series analysis is one of the statistical tools included in the statsmodels library. **In this example, we will perform ARMA (Autoregressive Moving Average) to the real dataset, the Sunspots Data.**

**How do I enable statsmodels inside Python4Delphi in Windows?**

First, open and run our Python GUI using project **Demo1 **from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the **Demo1** source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

The next step is installing statsmodels into your system. Here is how you can get **statsmodels** using **pip** for a stable release**,** to work with Python4Delphi to create GUI with Statistical And Time Series Analysis capabilities:

1 |
pip install statsmodels |

**How do you do time series analysis using Python statsmodels?**

**Demo for Autoregressive Moving Average (ARMA)**

The following is a code example of** statsmodels** for **ARMA modeling for Sunspots dataset** (Run this inside the lower Memo of Python4Delphi Demo01 GUI):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import numpy as np from scipy import stats import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm from statsmodels.graphics.api import qqplot # Sunspots Dataset Metadata print(sm.datasets.sunspots.NOTE) # Load Data dta = sm.datasets.sunspots.load_pandas().data # Plotting Sun Activity dta.index = pd.Index(pd.date_range("1700", end="2009", freq="A-DEC")) del dta["YEAR"] dta.plot(figsize=(12,4)); # Plotting Autocorrelation and Partial Correlation fig = plt.figure(figsize=(12,8)) ax1 = fig.add_subplot(211) fig = sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40, ax=ax1) ax2 = fig.add_subplot(212) fig = sm.graphics.tsa.plot_pacf(dta, lags=40, ax=ax2) # Print Model Parameters arma_mod20 = sm.tsa.statespace.SARIMAX(dta, order=(2,0,0), trend='c').fit(disp=False) print(arma_mod20.aic, arma_mod20.bic, arma_mod20.hqic) print(arma_mod20.params) # Print Model Parameters arma_mod30 = sm.tsa.statespace.SARIMAX(dta, order=(3,0,0), trend='c').fit(disp=False) print(arma_mod30.aic, arma_mod30.bic, arma_mod30.hqic) print(arma_mod30.params) # Does our Model Obey the Theory? ## Calculate and Plot the Residuals print(sm.stats.durbin_watson(arma_mod30.resid)) fig = plt.figure(figsize=(12,4)) ax = fig.add_subplot(111) ax = plt.plot(arma_mod30.resid) resid = arma_mod30.resid print(stats.normaltest(resid)) fig = plt.figure(figsize=(12,4)) ax = fig.add_subplot(111) fig = qqplot(resid, line='q', ax=ax, fit=True) # Show Plots plt.show() |

Here is the statsmodels for ARMA result in the Python GUI:

1 2 3 4 5 6 7 8 |
fig = plt.figure(figsize=(12,8)) ax1 = fig.add_subplot(211) fig = sm.graphics.tsa.plot_acf(resid, lags=40, ax=ax1) ax2 = fig.add_subplot(212) fig = sm.graphics.tsa.plot_pacf(resid, lags=40, ax=ax2) # Show Plots plt.show() |

1 2 3 4 5 |
r,q,p = sm.tsa.acf(resid, fft=True, qstat=True) data = np.c_[r[1:], q, p] index = pd.Index(range(1,q.shape[0]+1), name="lag") table = pd.DataFrame(data, columns=["AC", "Q", "Prob(>Q)"], index=index) print(table) |

Last, let’s try our model to make predictions. How good it will be?

1 2 3 4 5 6 7 8 |
predict_sunspots = arma_mod30.predict(start='1990', end='2012', dynamic=True) fig, ax = plt.subplots(figsize=(12, 8)) dta.loc['1950':].plot(ax=ax) predict_sunspots.plot(ax=ax, style='r'); # Show Plots plt.show() |

Here is the output: The red line is the predicted sunspot activity from 1990 to 2012.

And run the following code to print out the Mean Forecast Error:

1 2 3 4 5 |
def mean_forecast_err(y, yhat): return y.sub(yhat).mean() # Mean Forecast Error print(mean_forecast_err(dta.SUNACTIVITY, predict_sunspots)) |

Check out the full source code here!

Congratulations, now you have learned **how to run the statsmodels library using Python for Delphi to display it in the Delphi Windows GUI app**! Now you can solve various real-world time series problems using the framework created by the statsmodels library and Python4Delphi.

**Where can I find more examples of building GUI for Scientific Computing libraries?**

Check out the **statsmodels **for statistical and time series analysis framework for Python and use it in your projects: https://www.statsmodels.org/stable/install.html, and

Check out **Python4Delphi** which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi

Or read our **collections** of** Scientific Computing articles**:

**6 Scientific Computing Libs To Empower Your Apps:**

**5 Time Series Analysis Libs To Empower Your Apps: **

**6 Bioinformatics Libs To Empower Your Apps: **

**10 Ultimate Python AI Libraries:**

**Try these examples now by downloading a free demo copy of Delphi.**