# Chapter 48: Applied Economic Prediction and Classification Projects
# Fundamentals of Python for Financial Econometrics - Applied Economic Prediction and Classification Projects
# Dataset: Ceteris Lab teaching sample

# Applied Economic Prediction and Classification Projects
#
# **Opening question:** How can an end-to-end workflow turn housing and passenger data into models that are evaluated honestly and interpreted carefully?

# %% Cell 2
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

rng = np.random.default_rng(48)
area = rng.uniform(45, 240, 500)
price = 85_000 + 3_250 * area + rng.normal(0, 55_000, 500)
houses = pd.DataFrame({"area": area, "price": price})
model = LinearRegression().fit(houses[["area"]], houses["price"])
print(round(float(model.coef_[0]), 2), len(houses))

# **Interpretation check:** Interpretation. The slope is a predictive association in the dataset’s currency units per square metre, not a causal valuation rule.

# %% Cell 4
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split

rng = np.random.default_rng(49)
n = 800
passengers = pd.DataFrame({
    "travel_class": rng.integers(1, 4, n),
    "age": rng.normal(36, 14, n).clip(1, 85),
    "fare": rng.lognormal(3.2, 0.7, n),
})
score = 1.2 - 0.75 * (passengers["travel_class"] - 1) - 0.018 * (passengers["age"] - 30) + 0.006 * passengers["fare"]
probability = 1 / (1 + np.exp(-score))
passengers["survived"] = rng.binomial(1, probability)
X_train, X_test, y_train, y_test = train_test_split(
    passengers.drop(columns="survived"), passengers["survived"], test_size=0.25, random_state=29, stratify=passengers["survived"]
)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
prediction = model.predict(X_test)
print(
    round(accuracy_score(y_test, prediction), 3),
    round(precision_score(y_test, prediction), 3),
    round(recall_score(y_test, prediction), 3),
)

# **Interpretation check:** Interpretation. The three metrics illuminate different error trade-offs. Exact values are tied to the documented split and preprocessing.

# Verified source output
#
# ```text
# 84427668.1238 3473
# ```
#
# ```text
# 0.789 0.719 0.744
# ```
