Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Part I Capstone — Working with Financial Data

In Part I, we introduced:

This capstone integrates these ideas through a small applied project using real financial data.

The exercises below combine:

The goal is not only to compute statistics, but also to think economically about what the data represent.


Learning Goals

By completing this capstone, you should be able to:


Background: Financial Time Series

Financial markets generate enormous quantities of time series data.

Examples include:

Raw price data alone are often difficult to interpret statistically.

For this reason, analysts usually transform prices into:

Returns provide a more meaningful measure of financial performance and risk.


Dataset

We will use:

SET Index

the ETF tracking the Thai SET index.

You may later replace this with:


Exercise 1 — Downloading and Visualizing Prices

We begin by downloading adjusted price data.

import yfinance as yf
import matplotlib.pyplot as plt

# Download SET Index data
set_index = yf.download(
    "^SET.BK",
    start="2018-01-01",
    auto_adjust=False
)

# Adjusted closing prices
prices = set_index["Adj Close"]

# Plot
prices.plot(figsize=(10,4))

plt.title("Thai SET Index Adjusted Closing Prices")

plt.ylabel("Index Level")

plt.xlabel("Date")

plt.savefig("figs/ch4_/set.png", dpi=300, bbox_inches="tight")
plt.close()   # replace with plt.show()
SET
# Save CSV file
set_index.to_csv(
    "figs/ch4_/set.csv")

Questions

  1. Does the series appear stationary?

  2. Can you identify periods of:

    • rapid growth,

    • sharp decline,

    • unusual volatility?

  3. Why might adjusted prices be preferable to raw prices?



Exercise 2 — Computing Returns

We now compute simple returns.

returns = prices.pct_change().dropna()

returns.head()

Plotting Returns

returns.plot(figsize=(10,4))

plt.title("SET Daily Returns")

plt.ylabel("Return")

plt.savefig("figs/ch4_/returns.png", dpi=300, bbox_inches="tight")
plt.close()   # replace with plt.show()
SET

Questions

  1. How does the return series differ visually from the price series?

  2. Does the return series appear more stationary?

  3. Can you identify periods of elevated volatility?



Exercise 3 — Simple vs Log Returns

We now compute log returns.

import numpy as np

log_returns = np.log(
    prices / prices.shift(1)
).dropna()

log_returns.head()

Comparing Returns

comparison = plt.figure(figsize=(10,4))

plt.plot(
    returns.index,
    returns,
    label="Simple Returns",
    alpha=0.7
)

plt.plot(
    log_returns.index,
    log_returns,
    label="Log Returns",
    alpha=0.7
)

plt.legend()

plt.title("Simple vs Log Returns")

plt.savefig("figs/ch4_/returns2.png", dpi=300, bbox_inches="tight")
plt.close()   # replace with plt.show()
Simple vs Log Returns

Questions

  1. Are simple and log returns very different for daily data?

  2. Why do economists and finance researchers often prefer log returns?

  3. Under what circumstances might differences become larger?


Exercise 4 — Measuring Volatility

Volatility measures variability in returns.

One simple measure is the standard deviation.


Daily Volatility

returns.std()

Rolling Volatility

rolling_vol = returns.rolling(30).std()

rolling_vol.plot(figsize=(10,4))

plt.title("30-Day Rolling Volatility")

plt.ylabel("Volatility")

plt.savefig("figs/ch4_/rol_vol.png", dpi=300, bbox_inches="tight")
plt.close()   # replace with plt.show()
Rolling Volatility

Questions

  1. Does volatility appear constant over time?

  2. Can you identify periods of volatility clustering?

  3. Why might volatility matter for investors?


This becomes central later when we study:


Exercise 5 — Distribution of Returns

We now examine the distribution of returns.

returns.hist(
    bins=50,
    figsize=(8,4)
)

plt.title("Distribution of Daily Returns")

plt.savefig("figs/ch4_/ret_dist.png", dpi=300, bbox_inches="tight")
plt.close()   # replace with plt.show()
Returns Distribution

Questions

  1. Does the distribution appear perfectly normal?

  2. Are extreme observations present?

  3. Why might extreme returns matter in finance?



Exercise 6 — Comparing Assets

We now compare multiple assets.

aapl = yf.download(
    "AAPL",
    start="2018-01-01",
    auto_adjust=False
)["Adj Close"]

nflx = yf.download(
    "NFLX",
    start="2018-01-01",
    auto_adjust=False
)["Adj Close"]

comparison = plt.figure(figsize=(10,4))

plt.plot(
    aapl / aapl.iloc[0],
    label="Apple"
)

plt.plot(
    nflx / nflx.iloc[0],
    label="NetFlix"
)

plt.legend()

plt.title("Normalized Stock Prices")

plt.show()

Questions

  1. Why do we normalize prices before comparison?

  2. Which asset performed better over the sample?

  3. Which asset appears more volatile?


Exercise 7 — Short Selling and Negative Returns

Suppose an investor expects prices to fall.

One possible strategy is:

A short seller:


Example

Suppose:

DayPrice
1100
290

Long Position Return

90100100=0.10\frac{90-100}{100} = -0.10

The investor loses 10%.


Short Position Return

10090100=0.10\frac{100-90}{100} = 0.10

The short seller gains 10%.


Questions

  1. Why are short positions risky?

  2. Why can short-selling losses become very large?

  3. Why might short-selling be economically useful?


Exercise 8 — Interpreting Stylized Facts

Using the exercises above, identify examples of:


These patterns strongly influence modern time series modeling.


Mini Project — Exploring Thai Financial Data

Choose one Thai financial asset, such as:

Then:

  1. Download the data.

  2. Compute returns.

  3. Plot prices and returns.

  4. Measure volatility.

  5. Compare simple and log returns.

  6. Discuss stylized facts.



Gretl Version

The same exercises can also be performed in Gretl and/or Excel.


Downloading Data

Import CSV or Excel financial data.


Plotting Data

Menu:

Variable → Time series plot

Computing Returns

Menu:

Add → Define new variable

Example:

return = (P - P(-1))/P(-1)

Or.

Right click on AdjClose and Add percent change...


Histogram

Menu:

Variable → Frequency distribution
Return distribution

Common Mistakes


Looking Ahead

Part II begins studying:

We move from:

toward:


Key Takeaways