<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Gadallh</title>
    <description>The latest articles on DEV Community by Ahmed Gadallh (@a_gadallh).</description>
    <link>https://dev.to/a_gadallh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4285618%2F5ac025cc-efca-4d5b-a057-ce80f377c4d9.jpeg</url>
      <title>DEV Community: Ahmed Gadallh</title>
      <link>https://dev.to/a_gadallh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/a_gadallh"/>
    <language>en</language>
    <item>
      <title>ADF vs KPSS in Python: How to Test Time Series Stationarity Before Forecasting</title>
      <dc:creator>Ahmed Gadallh</dc:creator>
      <pubDate>Sun, 06 Dec 2026 02:28:23 +0000</pubDate>
      <link>https://dev.to/vertexfrontier/adf-vs-kpss-in-python-how-to-test-time-series-stationarity-before-forecasting-3f5l</link>
      <guid>https://dev.to/vertexfrontier/adf-vs-kpss-in-python-how-to-test-time-series-stationarity-before-forecasting-3f5l</guid>
      <description>&lt;p&gt;Stationarity is one of the most important concepts in time series analysis. Before building forecasting models such as ARIMA or SARIMA, you need to understand whether your data behaves consistently over time.&lt;/p&gt;

&lt;p&gt;Two widely used statistical tests for this purpose are the &lt;strong&gt;Augmented Dickey-Fuller (ADF) test&lt;/strong&gt; and the &lt;strong&gt;Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Although both tests investigate stationarity, they approach the problem from opposite directions. Understanding their differences can help you make better decisions when preparing time series data for forecasting.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What Is Stationarity in Time Series?
&lt;/h2&gt;

&lt;p&gt;A time series is generally considered weakly stationary when its statistical properties remain stable over time.&lt;/p&gt;

&lt;p&gt;These properties include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constant mean.&lt;/li&gt;
&lt;li&gt;Constant variance.&lt;/li&gt;
&lt;li&gt;Autocovariance that depends on the time lag rather than the specific time period.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stationarity matters because many traditional forecasting techniques rely on assumptions about the statistical behavior of the underlying data.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Understanding the ADF Test
&lt;/h2&gt;

&lt;p&gt;The Augmented Dickey-Fuller test checks whether a time series contains a unit root.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Its hypotheses are:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Null hypothesis (H0):&lt;/strong&gt; The series has a unit root and is non-stationary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alternative hypothesis (H1):&lt;/strong&gt; The series does not have a unit root.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At a 0.05 significance level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A p-value below 0.05 provides evidence against the unit-root null hypothesis.&lt;/li&gt;
&lt;li&gt;A p-value above 0.05 means there is insufficient evidence to reject the null hypothesis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement the ADF test in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;statsmodels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tsa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stattools&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;adfuller&lt;/span&gt;

&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;adfuller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autolag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"AIC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ADF Statistic:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"P-value:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Evidence against a unit root."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Insufficient evidence to reject a unit root."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Understanding the KPSS Test
&lt;/h2&gt;

&lt;p&gt;The KPSS test takes a different approach. Its null hypothesis assumes that the series is stationary under a specified deterministic component.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Null hypothesis (H0):&lt;/strong&gt; The series is stationary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alternative hypothesis (H1):&lt;/strong&gt; The series is non-stationary under the selected specification.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;KPSS supports two common specifications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;regression="c"&lt;/code&gt;: Stationarity around a constant.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;regression="ct"&lt;/code&gt;: Stationarity around a constant and deterministic trend.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a Python example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;statsmodels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tsa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stattools&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;kpss&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;kpss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;regression&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;nlags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"auto"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"KPSS Statistic:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"P-value:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Lags:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Reject the stationarity null hypothesis."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Fail to reject the stationarity null hypothesis."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A p-value above 0.05 does not prove that the series is stationary. It simply means there is insufficient evidence to reject the stationarity hypothesis.&lt;/p&gt;

&lt;p&gt;For a more detailed explanation of KPSS hypotheses, p-values, implementation, and interpretation, read this practical guide:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://vertexfrontier.com/kpss-test-in-python-time-series-stationarity/" rel="noopener noreferrer"&gt;KPSS Test in Python: How to Run It and Interpret the Results&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. ADF vs KPSS: What Is the Difference?
&lt;/h2&gt;

&lt;p&gt;The most important difference between these tests is their null hypotheses.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;ADF Test&lt;/th&gt;
&lt;th&gt;KPSS Test&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Main purpose&lt;/td&gt;
&lt;td&gt;Detect a unit root&lt;/td&gt;
&lt;td&gt;Test stationarity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Null hypothesis&lt;/td&gt;
&lt;td&gt;Non-stationarity&lt;/td&gt;
&lt;td&gt;Stationarity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Small p-value&lt;/td&gt;
&lt;td&gt;Evidence against a unit root&lt;/td&gt;
&lt;td&gt;Evidence against stationarity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Large p-value&lt;/td&gt;
&lt;td&gt;Fail to reject unit root&lt;/td&gt;
&lt;td&gt;Fail to reject stationarity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python library&lt;/td&gt;
&lt;td&gt;statsmodels&lt;/td&gt;
&lt;td&gt;statsmodels&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because their null hypotheses are different, using both tests can provide a more informative diagnostic picture than relying on only one.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Combining ADF and KPSS Results
&lt;/h2&gt;

&lt;p&gt;Running both tests on the same dataset can produce four possible combinations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ADF Result&lt;/th&gt;
&lt;th&gt;KPSS Result&lt;/th&gt;
&lt;th&gt;Possible Interpretation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Reject H0&lt;/td&gt;
&lt;td&gt;Fail to reject H0&lt;/td&gt;
&lt;td&gt;Evidence consistent with stationarity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fail to reject H0&lt;/td&gt;
&lt;td&gt;Reject H0&lt;/td&gt;
&lt;td&gt;Evidence consistent with non-stationarity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fail to reject H0&lt;/td&gt;
&lt;td&gt;Fail to reject H0&lt;/td&gt;
&lt;td&gt;Inconclusive; investigate test power and specifications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reject H0&lt;/td&gt;
&lt;td&gt;Reject H0&lt;/td&gt;
&lt;td&gt;Conflicting evidence; investigate trends, breaks, and assumptions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These outcomes are diagnostic clues, not automatic classifications. Structural breaks, seasonality, changing variance, and different deterministic specifications can explain conflicting results.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. What Should You Do If Your Time Series Is Non-Stationary?
&lt;/h2&gt;

&lt;p&gt;If your tests suggest non-stationarity, investigate the data before choosing a transformation.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Differencing
&lt;/h3&gt;

&lt;p&gt;Useful when the series behaves like a difference-stationary process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;differenced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data_series&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dropna&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Detrending
&lt;/h3&gt;

&lt;p&gt;Useful when the series contains a deterministic trend.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Seasonal Differencing
&lt;/h3&gt;

&lt;p&gt;Useful when recurring seasonal patterns contribute to non-stationarity.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Structural Break Analysis
&lt;/h3&gt;

&lt;p&gt;Sudden changes in the data-generating process can affect stationarity tests and should be investigated separately.&lt;/p&gt;

&lt;p&gt;After applying a transformation, rerun the tests and inspect the resulting series.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. A Practical Python Workflow
&lt;/h2&gt;

&lt;p&gt;Here is a simple workflow for real-world time series projects:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Load and clean your time series data.&lt;/li&gt;
&lt;li&gt;Sort observations chronologically.&lt;/li&gt;
&lt;li&gt;Visualize the series.&lt;/li&gt;
&lt;li&gt;Run the ADF test.&lt;/li&gt;
&lt;li&gt;Run the KPSS test.&lt;/li&gt;
&lt;li&gt;Compare their results.&lt;/li&gt;
&lt;li&gt;Investigate trends, seasonality, and structural breaks.&lt;/li&gt;
&lt;li&gt;Apply an appropriate transformation if necessary.&lt;/li&gt;
&lt;li&gt;Run the tests again.&lt;/li&gt;
&lt;li&gt;Evaluate forecasting models using time-aware validation.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The ADF and KPSS tests are complementary tools for investigating time series stationarity in Python.&lt;/p&gt;

&lt;p&gt;ADF focuses on detecting a unit root, while KPSS tests stationarity as its null hypothesis.&lt;/p&gt;

&lt;p&gt;Using both tests together provides a stronger diagnostic foundation before moving toward forecasting models such as ARIMA and SARIMA.&lt;/p&gt;

&lt;p&gt;The key is not simply obtaining a p-value. It is understanding the assumptions behind each test and interpreting the results in the context of your data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://vertexfrontier.com/kpss-test-in-python-time-series-stationarity/" rel="noopener noreferrer"&gt;KPSS Test in Python: How to Run It and Interpret the Results&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.adfuller.html" rel="noopener noreferrer"&gt;Statsmodels: Augmented Dickey-Fuller Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.kpss.html" rel="noopener noreferrer"&gt;Statsmodels: KPSS Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;This tutorial is intended for Python developers, data scientists, and anyone working with statistical time series forecasting.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>statistics</category>
    </item>
  </channel>
</rss>
