0% found this document useful (0 votes)
7 views

TIME - ChatGPT Manual 001

Uploaded by

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

TIME - ChatGPT Manual 001

Uploaded by

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

Here is an end-to-end guide for performing time-series data analysis using Python, based on

Microsoft stock price data from 2015-2021. This guide covers loading the data, preprocessing,
exploratory data analysis, time-series decomposition, stationarity testing, and model building.

### 1. Load the Data

Start by loading the Microsoft stock price data from a CSV file.

#### Steps:

1. Import necessary libraries.


2. Load the data using `pandas`.

```python
import pandas as pd

# Load the data


df = pd.read_csv('MSFT_2015_2021.csv', parse_dates=['Date'], index_col='Date')

# Display the first few rows of the dataset


print(df.head())
```

### 2. Preprocessing

Handle missing values and ensure the data is sorted by date.

#### Steps:

1. Check for missing values.


2. Sort the data by date.

```python
# Check for missing values
print(df.isnull().sum())

# Sort the data by date


df = df.sort_index()
```

### 3. Exploratory Data Analysis (EDA)

Perform EDA to understand the data better.

#### Steps:

1. Plot the closing prices over time.


2. Plot moving averages.

```python
import matplotlib.pyplot as plt

# Plot the closing prices


plt.figure(figsize=(10, 6))
plt.plot(df['Close'], label='Close Price')
plt.title('Microsoft Stock Price (2015-2021)')
plt.xlabel('Date')
plt.ylabel('Close Price')
plt.legend()
plt.show()
# Calculate moving averages
df['MA20'] = df['Close'].rolling(window=20).mean()
df['MA50'] = df['Close'].rolling(window=50).mean()

# Plot moving averages


plt.figure(figsize=(10, 6))
plt.plot(df['Close'], label='Close Price')
plt.plot(df['MA20'], label='20-Day MA')
plt.plot(df['MA50'], label='50-Day MA')
plt.title('Microsoft Stock Price with Moving Averages')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
```

### 4. Time-Series Decomposition

Decompose the time series to observe the trend, seasonality, and residuals.

#### Steps:

1. Use `seasonal_decompose` from `statsmodels`.

```python
from statsmodels.tsa.seasonal import seasonal_decompose

# Decompose the time series


decomposition = seasonal_decompose(df['Close'], model='multiplicative')
# Plot the decomposed components
plt.figure(figsize=(12, 8))
decomposition.plot()
plt.show()
```

### 5. Stationarity Testing

Check if the time series is stationary using the Augmented Dickey-Fuller (ADF) test.

#### Steps:

1. Use `adfuller` from `statsmodels`.

```python
from statsmodels.tsa.stattools import adfuller

# Perform the ADF test


result = adfuller(df['Close'])
print('ADF Statistic:', result[0])
print('p-value:', result[1])
for key, value in result[4].items():
print('Critial Values:')
print(f' {key}, {value}')
```

### 6. Differencing (if necessary)

If the series is not stationary, apply differencing to achieve stationarity.


#### Steps:

1. Apply differencing.
2. Recheck stationarity.

```python
# Apply differencing
df['Close_diff'] = df['Close'].diff().dropna()

# Recheck stationarity
result = adfuller(df['Close_diff'].dropna())
print('ADF Statistic after differencing:', result[0])
print('p-value after differencing:', result[1])
```

### 7. Autocorrelation and Partial Autocorrelation

Plot ACF and PACF to identify the order of ARIMA models.

#### Steps:

1. Use `plot_acf` and `plot_pacf` from `statsmodels`.

```python
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

# Plot ACF and PACF


plt.figure(figsize=(12, 6))
plt.subplot(121)
plot_acf(df['Close_diff'].dropna(), ax=plt.gca(), lags=30)
plt.subplot(122)
plot_pacf(df['Close_diff'].dropna(), ax=plt.gca(), lags=30)
plt.show()
```

### 8. Model Building

Build ARIMA or SARIMA models based on the ACF and PACF plots.

#### Steps:

1. Import and fit ARIMA model.


2. Make predictions and evaluate the model.

```python
from statsmodels.tsa.arima.model import ARIMA

# Build and fit the ARIMA model


model = ARIMA(df['Close'], order=(p, d, q))
fit_model = model.fit()

# Print model summary


print(fit_model.summary())

# Make predictions
df['Forecast'] = fit_model.predict(start=len(df)-30, end=len(df)-1, dynamic=True)

# Plot the results


plt.figure(figsize=(12, 6))
plt.plot(df['Close'], label='Actual')
plt.plot(df['Forecast'], label='Forecast')
plt.title('Microsoft Stock Price Forecast')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
```

### Conclusion

This end-to-end guide covers loading, preprocessing, EDA, time-series decomposition,


stationarity testing, and model building for time-series analysis using Microsoft stock price data.
By following these steps, you can perform a comprehensive time-series analysis.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy