# Chapter 36: Introduction to Time-Series Data
# Fundamentals of Python for Financial Econometrics - Introduction to Time-Series Data
# Dataset: Ceteris Lab teaching sample

# Introduction to Time-Series Data
#
# **Opening question:** What changes when observations are ordered in time and yesterday can influence today?

# %% Cell 2
import numpy as np
import pandas as pd

prices = pd.Series([100, 102, 101, 104], index=pd.date_range("2026-01-01", periods=4, freq="D"))
simple = prices.pct_change()
log_return = np.log(prices).diff()
print(simple.dropna().round(4).tolist())
print(log_return.dropna().round(4).tolist())

# **Interpretation check:** Interpretation. The two measures are close for small changes but are not numerically identical.

# %% Cell 4
import pandas as pd

idx = pd.date_range("2026-01-01", periods=60, freq="D")
daily = pd.DataFrame({"rate": range(60), "volume": [100]*60}, index=idx)
monthly = daily.resample("ME").agg(rate=("rate", "last"), volume=("volume", "sum"))
print(monthly)

# **Interpretation check:** Interpretation. The rate uses the last observation while volume is summed, reflecting different measurement concepts.

# %% Cell 6
import numpy as np
import pandas as pd
simple = prices.pct_change()
log_return = np.log(prices).diff()
print(simple.dropna().round(4).tolist())
print(log_return.dropna().round(4).tolist())

# Verified source output
#
# ```text
# [0.02, -0.0098, 0.0297] [0.0198, -0.0099, 0.0293]
# ```
#
# ```text
# rate volume 2026-01-31 30 3100 2026-02-28 58 2800 2026-03-31 59 100
# ```
#
# ```text
# [0.02, -0.0098, 0.0297]
# [0.0198, -0.0099, 0.0293]
# ```
#
# ```text
# rate  volume
# 2026-01-31    30    3100
# 2026-02-28    58    2800
# 2026-03-31    59     100
# ```
