# Chapter 47: Machine-Learning Workflow and Gradient Descent for Economic Data
# Fundamentals of Python for Financial Econometrics - Machine-Learning Workflow and Gradient Descent for Economic Data
# Dataset: Ceteris Lab teaching sample

# Machine-Learning Workflow and Gradient Descent for Economic Data
#
# **Opening question:** How can a model be trained without letting information from the future or test set leak into the learning process?

# %% Cell 2
import numpy as np

x = np.array([1., 2., 3., 4.])
y = 2.5 * x
slope = 0.0
learning_rate = 0.02
for _ in range(500):
    gradient = -2 * np.mean(x * (y - slope*x))
    slope -= learning_rate * gradient
print(round(slope, 4))

# **Interpretation check:** Interpretation. The iterative slope converges to the least-squares solution for this noiseless one-parameter problem.

# %% Cell 4
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Ridge

pipeline = Pipeline([
    ("scale", StandardScaler()),
    ("model", Ridge(alpha=1.0)),
])
print([name for name, _ in pipeline.steps])

# **Interpretation check:** Interpretation. When the pipeline is fitted inside cross-validation, scaling parameters are learned only from each training fold.

# %% Cell 6
import numpy as np
x = np.array([1., 2., 3., 4.])
gradient = -2 * np.mean(x * (y - slope*x))
print(round(slope, 4))
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

# Verified source output
#
# ```text
# 2.5
# ```
#
# ```text
# ['scale', 'model']
# ```
