Icon

final project. 29 June Send to friends

Analysis (Queation: Where CPI surprises exceed +0.2 percentage points of the forecasted CPI (from year 2021-2025 CPI), does the Energy sector (XLE) outperform Technology (XLK) over the subsequent 30 days, relative to their historical return gap in their stock prices?)

  • XLE

  • XLK

  • CPI

Preprocessing

30 days return and spread

  • Spread > 0 = XLE (Energy) beat XLK (Tech) over that 30-day window

  • Spread < 0 = XLK (Tech) beat XLE (Energy)

  • Spread = 0 = they performed identically

Link to CPI event

  • CPI release dates are calendar dates — they don't align row-for-row with the price table

  • Needed a way to find "the price 30 trading days after this CPI release" using calendar dates

  • Why 42: 30 trading days = 6 trading weeks → 6 × 7 calendar days/week = 42 calendar days (converts trading-day count into a calendar-day shift)

  • Shifted each CPI release date +42 days, then joined that shifted date to the price table's Date column — this lines up each CPI event with the return that ends ~30 trading days later

The math:

  • A trading week = 5 days (Mon–Fri)

  • A calendar week = 7 days

  • So 30 trading days ÷ 5 days/week = 6 weeks

  • 6 weeks × 7 calendar days/week = 42 calendar days

  • In normal months, XLE slightly beats XLK on average (+0.8%)

  • In surprise months (hot CPI), XLE actually underperforms XLK by 3.1% on average

  • Hot CPI surprises don't push Energy (XLE) ahead of Tech (XLK) like expected — actually the opposite happens

  • In surprise months, Tech outperforms Energy by ~3-4 percentage points on average

  • This contradicts the common "inflation hurts tech, helps energy" narrative

  • Possible reasons: tech's AI-driven earnings growth offset rate fears; energy is driven more by oil supply/OPEC than CPI

  • p-value = 0.336 — far above the standard 0.05 threshold → the difference is NOT statistically significant

  • The 95% confidence interval (−0.045 to 0.124) includes zero, meaning we cannot rule out "no real difference" between surprise and normal months

  • In plain terms: the −3.1% vs +0.8% gap you saw earlier could plausibly just be random noise, not a real pattern

This is your key results table — let's read it carefully.

Group Mean Spread Std Dev Count Normal months (Flag=0) +0.008 (0.8%) 0.106 44 Surprise months (Flag=1) −0.031 (−3.1%) 0.120 11


This is the opposite of what your hypothesis predicted.

Your original question expected: when CPI surprises to the upside, XLE should outperform XLK (positive spread). Instead, you're finding:

  • In normal months, XLE slightly beats XLK on average (+0.8%)

  • In surprise months (hot CPI), XLE actually underperforms XLK by 3.1% on average

This is a genuinely interesting finding — it contradicts the simple "inflation surprise → rotate into energy" story. A few honest possible explanations to discuss in your report:

  1. Small sample — only 11 surprise months out of 55. One or two extreme observations could be swinging this a lot.

  2. Tech resilience — in 2021–2025, mega-cap tech (driving XLK) may have been resilient to rate fears due to strong earnings, especially AI-driven names.

  3. Energy-specific drivers — oil price swings (OPEC decisions, geopolitical shocks) may dominate XLE more than CPI surprises do.

Overall (Calculate from Return of the next 30 days of CPI surprise - The 1st Day after CPI surprise)

Linear correlation shows:

1. XLE vs XLK Return (r = -0.109, p = 0.427)

  • The relationship is weakly negative and not statistically significant (p > 0.05)

  • This means both tend to move slightly in opposite directions, but are still considered independent of each other

  • Better than before for Pair Trading, as negative correlation helps reduce portfolio risk

2. XLE vs Spread (r = 0.798, p ≈ 0)

  • The spread widens when XLE outperforms → XLE is the primary driver pushing the spread higher

3. XLK vs Spread (r = -0.696, p ≈ 0)

  • The spread narrows when XLK outperforms → XLK is the primary driver pushing the spread lower

Regression predictor shows almost 100% accurate prediction for XLE, XLK return and Spread return predictions with learning 70% of information from data set.

Regression

XLK, XLE return and spread

Correlation Analysis: CPI Surprise, Surprise Flag, and Spread

Pair 1 : CPI_Surprise vs Surprise_Flag (r = +0.771, p ≈ 0)

Meaning: This relationship is strong and highly statistically significant, which is expected by construction — Surprise_Flag = 1 was directly derived from CPI_Surprise > 0.2.

This means: Both variables measure the same underlying phenomenon in different forms:

  • CPI_Surprise = magnitude of the surprise (continuous)

  • Surprise_Flag = whether a surprise occurred or not (binary 0/1)

r does not reach 1.0 because Flag is binary — a month with surprise = 0.19 (just below the threshold) and surprise = 0.50 are both treated as Flag = 0, causing some information to be lost in the discretization process.


Pair 2 : CPI_Surprise vs Spread (r = −0.043, p = 0.755)

Meaning: There is virtually no relationship at all, and the result is not statistically significant.

This means: The magnitude of a CPI surprise does not predict how much XLE will outperform XLK over the subsequent 30 days — regardless of whether CPI exceeded the forecast by a large or small margin, the resulting spread is no different.

CPI Surprise increases → Spread does not change (r ≈ 0)

This constitutes a direct rejection of the project's main hypothesis.


Pair 3 : Surprise_Flag vs Spread (r = −0.146, p = 0.288)

Meaning: There is a slight negative direction (surprise months tend to have lower spread), but the result is entirely lacking in statistical significance.

This means: Even classifying months as "surprise months" still fails to explain the spread in any meaningful way — consistent with the earlier t-test result of p-value = 0.336.

็Histrogram shows that the left side (negative spread) has noticeably denser bars than the right side (positive spread), indicating that XLK outperforms XLE more frequently.


The visual pattern (in Scatter Plot) is consistent with the correlation result of r = −0.146 (p = 0.288), confirming that Surprise_Flag has no statistically significant linear relationship with Spread. The wide dispersion of spread values within both groups suggests that factors other than CPI surprises are the primary drivers of the return gap between XLE and XLK over the subsequent 30-day window.


Rolling Statistics (Rolling Mean ± k·Rolling Std)

This method flags a data point as an outlier when it falls outside a band defined by the rolling mean plus or minus a multiple of the rolling standard deviation over a fixed window. Since it uses the mean and standard deviation, it is heavily influenced by extreme values within the window, meaning a single large spike can shift the mean and widen the band, actually making the method less sensitive to nearby outliers.

Hampel Filter

This method uses the rolling median and Median Absolute Deviation (MAD) instead. The median is far more resistant to extreme values, and MAD measures spread based on the median rather than the mean. As a result, a single spike does not distort the central estimate, making the filter much more robust and sensitive to genuine outliers.

Why Outlier Detection Must Come Before Analysis, Forecasting, and Machine Learning?
The Core Reason

Outliers are data points that deviate significantly from the rest of the dataset. If left unaddressed, they can silently distort every downstream step, from summary statistics all the way to machine learning predictions, without any obvious warning that something has gone wrong..

However, in this case, upon careful examination, we found that all detected outliers represent genuine market observations rather than data errors or recording mistakes. Therefore, no adjustments or replacements were made. Retaining the original values was considered the more appropriate choice, as preserving these real-world data points allows the analysis and machine learning models to reflect true market conditions more accurately and realistically, capturing the actual behavior of Energy and Technology sector returns in response to CPI surprises, including periods of extreme market stress such as the COVID-19 crash and the 2022 energy crisis.

The line plot visually confirms the statistical findings: there is no consistent directional relationship between CPI Surprise Flag and the subsequent 30-day spread between XLE and XLK. Surprise months are scattered across both positive and negative spread values, while the spread continues to fluctuate independently during non-surprise periods, suggesting that sector-specific factors beyond CPI surprises are the primary determinants of relative performance between the Energy and Technology sectors.

Sources of data

  1. Consumer Price Index (CPI) Economic Calendar

Investing.com. (n.d.). Consumer Price Index (CPI) economic calendar. https://www.investing.com/economic-calendar/cpi-733

  1. Technology Select Sector SPDR Fund (XLK)

Investing.com. (n.d.). Technology Select Sector SPDR Fund (XLK). https://www.investing.com/etfs/spdr-select-sector---technology

  1. Energy Select Sector SPDR Fund (XLE)

Investing.com. (n.d.). Energy Select Sector SPDR Fund (XLE). https://www.investing.com/etfs/spdr-energy-select-sector-fund

Answer of the question

Outliners of XLK
find lag 30 days price
Lag Column
find percent return
Math Formula
For spread prediction
Table Partitioner
Linear Correlation
Join XLE and XLK information with the same date
Joiner
To Find accuracy of the test
Numeric Scorer
XLE return - XLK return
Math Formula
Predict vs real
Line Plot
find lag 30 days price
Lag Column
learn 70% of data
Linear Regression Learner
find percent return
Math Formula
Predict 30% of Data
Regression Predictor
Find statistical value of answers
GroupBy
Read the result of 2nd column (means which has CPI surprise)
Independent groups t-test
#26 find release clean date target to match with days after CPI surprise
Date Shifter
#27 matches the dates
Joiner
To Find accuracy of the test
Numeric Scorer
XLE return prediction
Table Partitioner
Predict 30% of Data
Regression Predictor
Predict 30% of Data
Regression Predictor
Delete Duplicate date out
Duplicate Row Filter
To Find accuracy of the test
Numeric Scorer
Learn 70% of data
Linear Regression Learner
Delete Duplicate date out
Duplicate Row Filter
Predict vs real
Line Plot
Learn 70% of data
Linear Regression Learner
Filter missing value out
Missing Value
Outliners of CPI
Predict vs real
Line Plot
XLK return prediction
Table Partitioner
Spread and surprise flag
Line Plot
Missing values filled by Previous value carry-forward
Missing Value
Filter missing value out
Missing Value
Delete Duplicate date out
Duplicate Row Filter
XLE Price information
Excel Reader
XLK Price information
Excel Reader
Read CPI information
Excel Reader
Change date from "string" to "date" data type
String to Date&Time
Ascending date
Sorter
Change Volume from "string" to "number" data type
String Manipulation
CPI surprise and Spread
Linear Correlation
Surprise Flag count
Value Counter
Actual vs Forecast CPI
Line Plot
Surprise Flag
Bar Chart
Histogram of Spread
Histogram
Spread vs Surprise Flag
Scatter Plot
Outliners of XLE
Change Volume from "string" to "number" data type
String Manipulation
Change date from "string" to "date" data type
String to Date&Time
Ascending date
Sorter
find amount of Forecast CPI > real CPI
Math Formula
finding CPI surprise flag
Math Formula
find release clean date
String Manipulation
change to accurate date format
String to Date&Time

Nodes

Extensions

  • No modules found

Links