σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
scikit-learn skill›Module 1 · The estimator way of working›Topic 1.2·~50 minIntermediate

Writing your own estimator

Skill positionModule 1 of 4 · Topic 2 of 5
1.2 Writing your own estimator
—Lab score— / 100Total XP—Streak—
Lecture0% read

2 min read

01

The estimator contract

Every scikit-learn model follows one contract, and that contract is why models can be swapped, chained and tuned. __init__ only stores the parameters it receives, under the same names, with no checks and no computation: that is what lets get_params, set_params and clone work. fit(X, y) learns from the data, stores what it learned in attributes that end with an underscore (coef_, intercept_, n_features_in_) and returns self, so calls can be chained. predict(X) first calls check_is_fitted, which raises NotFittedError if fit has not run, then validates X with check_array and applies what was learned. Inherit from BaseEstimator for the parameter handling and from RegressorMixin for a free score method (put the mixin first).

The model in this lab is ridge regression. With centred data it solves (XᵀX+αI) w=Xᵀy, and the intercept is ȳ-x̄·w. A larger α shrinks the coefficients towards zero, which is a good habit when the features are noisy lagged returns. Because your class follows the contract, make_pipeline(StandardScaler(), ShrinkageRegressor()), cross_val_score and GridSearchCV accept it, and clone(model) gives a fresh unfitted copy. That last point matters in a walk-forward evaluation: fit a clone on each training window, never the model itself, or the information of one fold leaks into the next. The score for a return forecast is the Spearman rank correlation between predictions and outcomes on the test rows, the information coefficient.

  • Parameters in, learned attributes out: __init__ stores, fit learns into names ending with an underscore and returns self.
  • Check before you predict: check_is_fitted and check_array give the standard errors that pipelines and users expect.
  • Fit a clone: in a walk-forward loop, clone(model).fit(...) keeps folds independent.

Lab · what's graded

7 tasks · 100 pts
  • Lagged features10 pts
  • Ridge from the normal equations15 pts
  • The constructor10 pts
  • Fit and predict20 pts
  • Working with Pipeline and GridSearchCV10 pts
  • A walk-forward rank correlation15 pts
  • Does the past of SPY predict its next day?20 pts
Explorer · lab-sl-13
▾ lab-sl-13
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sl-13$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sl-13 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes