0% found this document useful (0 votes)
15 views5 pages

Time_series_analysis__1718649022

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Time_series_analysis__1718649022

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Data Preparation and Exploration

Step 1: Import Libraries and Load Data

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')

data = data.asfreq('D') # Ensuring the data is in daily frequency

step 2:Data cleaning

# Check for missing values


print(data.isna().sum())

# Fill missing values


data.fillna(method='ffill', inplace=True) # Forward fill for missing data

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

Step 3: Exploratory Analysis

import pandas as pd

# Load data without parsing dates


data = pd.read_csv(r"C:\Users\sabar\Downloads\stock_prices.csv")

# Print columns to inspect names


print(data.columns)

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)

import matplotlib.pyplot as plt

# Assuming 'Close' is the correct column name for close prices


column_name = 'AA'

# Plot the time series


plt.figure(figsize=(14, 7))
plt.plot(data[column_name], label='Close Price')
plt.title('Stock Price Over Time')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
2. Time Series Decomposition

Step 1: Decomposition

import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose

# Load data (replace with your actual data loading code)


data = pd.read_csv(r"C:\Users\sabar\Downloads\stock_prices.csv", parse_dates=['Date'], index_col='Date')

# Assuming 'AA' is the correct column name for the specific stock prices
column_name = 'AA'

# Check if the datetime index has a frequency


if data.index.freq is None:
# Set the index frequency to daily ('D')
data.index = pd.to_datetime(data.index)
data = data.asfreq('D')

# Check for missing values and handle them


if data[column_name].isnull().any():
# Fill missing values with mean (you can choose another method if appropriate)
data[column_name].fillna(data[column_name].mean(), inplace=True)

# Perform seasonal decomposition


try:
decomposition = seasonal_decompose(data[column_name], model='additive')
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

# 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()

except ValueError as ve:


print(f"ValueError occurred: {ve}")

3. Stationarity and Testing

Step 1: Stationarity Check

result = adfuller(data['AA'].dropna())
print(f'ADF Statistic: {result[0]}')
print(f'p-value: {result[1]}')

ADF Statistic: -1.4422715484832984


p-value: 0.5618513502999963

Step 2: Differencing (if needed)

data_diff = data['AA'].diff().dropna()

# Plot differenced data


plt.figure(figsize=(14, 7))
plt.plot(data_diff, label='Differenced Data')
plt.legend()
plt.show()

# ADF test on differenced data


result_diff = adfuller(data_diff)
print(f'ADF Statistic: {result_diff[0]}')
print(f'p-value: {result_diff[1]}')
ADF Statistic: -14.080622280112733
p-value: 2.833044828923367e-26

4. Forecasting Model Development

Step 1: Model Selection

train, test = data['AA'][:int(0.8*len(data))], data['AA'][int(0.8*len(data)):]

model = ExponentialSmoothing(train, trend='add', seasonal='add', seasonal_periods=365).fit()


predictions = model.forecast(len(test))

# Plot predictions vs actual


plt.figure(figsize=(14, 7))
plt.plot(train.index, train, label='Train')
plt.plot(test.index, test, label='Test')
plt.plot(test.index, predictions, label='Forecast')
plt.legend()
plt.show()

Step 2: Model Evaluation

Evaluate the model using metrics like Mean Absolute Error (MAE) and Mean Squared Error (MSE).

mse = mean_squared_error(test, predictions)


mae = mean_absolute_error(test, predictions)
print(f'MSE: {mse}')
print(f'MAE: {mae}')
MSE: 286.56678689794717
MAE: 15.168730210848024

5. Insightful Reporting

Here’s how to compile your findings into a detailed report.

Detailed Report

Title: Time Series Analysis and Forecasting of [Stock Name] #AA

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.

Data Preparation and Exploration:

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.

Time Series Decomposition:

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.

Forecasting Model Development:

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

You might also like