Time_series_analysis__1718649022
Time_series_analysis__1718649022
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from sklearn.metrics import mean_squared_error, mean_absolute_error
# Load data
data = pd.read_csv(r"C:\Users\sabar\Downloads\stock_prices.csv", parse_dates=['Date'], index_col='Date')
A 1296
AA 1296
AAPL 1296
ABBV 3668
ABC 1296
...
XRX 1296
XYL 3363
YUM 1296
ZION 1296
ZNGA 3408
Length: 479, dtype: int64
C:\Users\sabar\AppData\Local\Temp\ipykernel_20288\3597669525.py:5: FutureWarning: DataFrame.fillna with 'method'
is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
data.fillna(method='ffill', inplace=True) # Forward fill for missing data
import pandas as pd
Index(['Date', 'A', 'AA', 'AAPL', 'ABBV', 'ABC', 'ABT', 'ACN', 'ADBE', 'ADI',
...
'XEL', 'XL', 'XLNX', 'XOM', 'XRAY', 'XRX', 'XYL', 'YUM', 'ZION',
'ZNGA'],
dtype='object', length=480)
Step 1: Decomposition
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose
# Assuming 'AA' is the correct column name for the specific stock prices
column_name = 'AA'
# Plot decomposition
plt.figure(figsize=(14, 10))
# Original series
plt.subplot(411)
plt.plot(data[column_name], label='Original')
plt.legend(loc='upper left')
plt.title('Decomposition of Stock Prices (' + column_name + ')')
plt.ylabel('Price')
# Trend component
plt.subplot(412)
plt.plot(trend, label='Trend')
plt.legend(loc='upper left')
plt.ylabel('Trend')
# Seasonal component
plt.subplot(413)
plt.plot(seasonal, label='Seasonality')
plt.legend(loc='upper left')
plt.ylabel('Seasonality')
# Residual component
plt.subplot(414)
plt.plot(residual, label='Residuals')
plt.legend(loc='upper left')
plt.ylabel('Residuals')
plt.tight_layout()
plt.show()
result = adfuller(data['AA'].dropna())
print(f'ADF Statistic: {result[0]}')
print(f'p-value: {result[1]}')
data_diff = data['AA'].diff().dropna()
Evaluate the model using metrics like Mean Absolute Error (MAE) and Mean Squared Error (MSE).
5. Insightful Reporting
Detailed Report
Introduction: This report presents a comprehensive time series analysis of the historical prices of [Stock Name]EXAMBLE - AA . The
analysis involves data preparation, decomposition, stationarity testing, and forecasting. The goal is to understand the underlying patterns
in the data and make future price predictions.
Loaded and cleaned the historical stock price data. Filled missing values using the forward fill method. Conducted an exploratory analysis
to visualize trends and calculate summary statistics.
Decomposed the time series into trend, seasonality, and residual components using an additive model. Identified clear seasonal patterns
and a long-term trend in the data.
Stationarity Testing:
Conducted the Augmented Dickey-Fuller (ADF) test to check for stationarity. The series was found to be non-stationary, so differencing
was applied to achieve stationarity.
Built an Exponential Smoothing model (Holt-Winters method) for forecasting. Evaluated the model's performance using Mean Squared
Error (MSE) and Mean Absolute Error (MAE).
Results:
The model provided accurate forecasts with MSE of [value] and MAE of [value]. The forecasted values closely aligned with the actual test
data, indicating the model's reliability.
Conclusion:
The analysis successfully demonstrated the efficacy of time series decomposition and forecasting techniques. The Exponential
Smoothing model proved to be a robust choice for forecasting the stock prices. Future work could involve exploring more complex
models like ARIMA, SARIMA, or machine learning approaches such as LSTM.
Appendix:
Include all code snippets used in the analysis. Provide additional plots and tables as necessary.
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js