get monthly crude oil (WTI) prices from FRED for the longest possible history using pandas datareader. run a regression of the price on its lagged value as x_{t+1} = a + b x_t + error. Report the long-run mean a/(1-b).
I'll help you get WTI crude oil prices from FRED and run the regression analysis. Let's do this step by step.
The analysis shows that WTI crude oil prices follow a highly persistent AR(1) process. The regression equation is:
The coefficients show: 0.236 for the intercept (a) 0.9943 for the slope coefficient (b)
The long-run mean, calculated as , is approximately 41.0556 dollars per barrel.
The scatter plot shows the relationship between consecutive monthly prices:
The very high b coefficient (0.9943) indicates that oil prices are highly persistent, meaning that prices tend to stay close to their previous values. The positive intercept contributes to a long-run mean price of about $41.06 per barrel.
create a time series plot of the crude price
The time series plot shows the historical evolution of WTI crude oil prices from 1946 to present, with the long-run mean of $41.06 shown as a red dashed line. Some key features visible in the plot:
949 |
28.23 |
29.46 |
1.17 |
3 |
18.53 |
40.28 |
133.93 |
The summary statistics show considerable variation in prices over this period, with a minimum of $1.17 and a maximum of $133.93 per barrel. The median price ($18.53) is notably lower than the mean ($28.23), indicating a right-skewed distribution of prices.
get the CPI from FRED using pandas datareader and compute crude prices in today's dollars
The plot shows both nominal (blue) and real (orange) oil prices from 1946 to 2025. When adjusted for inflation to 2025 dollars, we can see that:
938 |
61.07 |
34.83 |
20.67 |
32.63 |
48.82 |
80 |
196.94 |
The current nominal price is 71.53 USD per barrel, which is the same as the real price since we're converting to current (2025) dollars. Historically, real prices have averaged about $61.07 per barrel, with a maximum of nearly $197 (in today's dollars) during the 1980 oil crisis.
The real price analysis gives us a better picture of oil's historical cost in terms of purchasing power, showing that recent prices, while high in nominal terms, are not unprecedented when accounting for inflation.
run the autoregression using real prices
The regression for real oil prices shows:
Key findings:
The intercept (a) is 0.7569
The slope coefficient (b) is 0.9884 , indicating high persistence but slightly less than with nominal prices
The long-run mean is 65.4332 dollars per barrel in real (2025) terms
The R-squared is 0.9781 , showing that lagged prices explain about 98% of the variation in current prices
The regression shows that real oil prices are also highly persistent (b close to 1), but the persistence is slightly lower than in nominal terms. The long-run mean of about $65.43 in real terms gives us a better sense of the equilibrium price level after accounting for inflation. This is higher than the simple average of real prices we saw earlier because it accounts for the autoregressive dynamics of the series.
The scatter plot shows a tight relationship between consecutive real prices, with some notable outliers during periods of high volatility.
We've analyzed both nominal and real oil prices since 1946, finding:
What would you like to explore next? Some possibilities:
Drop all months prior to 1980 and repeat the autoregression of the real prices. Plot the real prices after 1980 with a horizontal line at the long-run mean.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[3], line 2
1 # Filter data from 1980 onwards
----> 2 df_real_1980 = df_real[df_real.index >= '1980-01-01'].copy()
4 # Run regression on post-1980 data
5 X = df_real_1980['price_lag'].values.reshape(-1, 1)
NameError: name 'df_real' is not defined
Also, evaluate what went wrong and consider options for how to fix it. Break the execution up into cell blocks.I apologize for the error. Let's recreate the analysis step by step:
For the post-1980 period, the AR(1) regression shows:
Key findings from the post-1980 analysis:
The intercept (a) is 1.3546
The slope coefficient (b) is 0.981 , showing slightly less persistence than the full sample
The long-run mean is 71.1844 dollars per barrel in real (2025) terms
The R-squared is 0.9668
The long-run mean for the post-1980 period ($71.18) is notably higher than the full-sample mean we found earlier ($65.43). This reflects the generally higher real oil prices in the modern era. The persistence coefficient (b) is slightly lower at 0.981 compared to 0.988 for the full sample, suggesting somewhat faster mean reversion in the more recent period.
The plot shows that current real prices are very close to the post-1980 long-run mean, suggesting that current oil prices are near their "normal" level for the modern era when accounting for inflation.
What is the half life of the shocks to the crude oil price given the estimates for the post 1980 period? Explain what "half life" means.
The half-life is the time it takes for half of a shock to dissipate in an AR(1) process. More precisely, it's the time needed for the deviation from the long-run mean to fall to half its initial size.
For an AR(1) process: where is the long-run mean and is the AR coefficient, the half-life is given by:
Using our estimated :
The half-life of a shock in an AR(1) process is the time it takes for half of a shock's effect to dissipate. For our estimated model:
The half-life can be calculated as:
where b is the AR(1) coefficient (0.981 in our case).
Half-life = 36.1 months or 3.0 years
To interpret this: if oil prices receive a shock (say, jumping $50 above their long-run mean due to a crisis), it would take about 3 years for prices to return halfway back to their long-run mean. After another 3 years, they would move half of the remaining distance, and so on.
This relatively long half-life confirms the high persistence we observed in the AR coefficient. Shocks to oil prices tend to have long-lasting effects, taking several years to substantially dissipate. This persistence reflects both:
The high persistence means that short-term disruptions can have effects that last for years, making the oil market particularly sensitive to geopolitical events and supply disruptions.