σμ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 2 · The core models›Topic 2.2·~50 minIntermediate

Logistic regression for direction

Skill positionModule 2 of 4 · Topic 2 of 3
2.2 Logistic regression for direction
—Lab score— / 100Total XP—Streak—

From a score to a probability

Logistic regression predicts a yes or no, such as whether tomorrow's return is positive. It first builds a linear score z=a+bxz=a+bxz=a+bx, exactly like a linear regression, and then turns it into a probability with the sigmoid p=1/(1+e−z)p=1/(1+e^{-z})p=1/(1+e−z). The score is the log of the odds: z=ln⁡p1−pz=\ln\frac{p}{1-p}z=ln1−pp​, so eze^{z}ez is the odds. A score of 000 means p=0.5p=0.5p=0.5. The model is fitted by minimising the log-loss, −ln⁡p-\ln p−lnp when the outcome was yes and −ln⁡(1−p)-\ln(1-p)−ln(1−p) when it was no, which punishes confident mistakes heavily. The class you report depends on a threshold, usually 0.50.50.5, that you can move.

p=11+e−z,z=a+bx,p1−p=ez,ℓ=−ln⁡p  (y=1)p=\frac{1}{1+e^{-z}},\qquad z=a+bx,\qquad \frac{p}{1-p}=e^{z},\qquad \ell=-\ln p\ \ (y=1)p=1+e−z1​,z=a+bx,1−pp​=ez,ℓ=−lnp  (y=1)(1)
A score of one, and real SPYTake z=−0.5+2xz=-0.5+2xz=−0.5+2x and x=0.75x=0.75x=0.75. Then z=1z=1z=1, p=1/(1+e−1)=0.731p=1/(1+e^{-1})=0.731p=1/(1+e−1)=0.731 and the odds are e1=2.718e^{1}=2.718e1=2.718. If the day really was up, the log-loss is −ln⁡0.731=0.313-\ln0.731=0.313−ln0.731=0.313. The score is 000 at x=0.25x=0.25x=0.25, where the model is undecided. Real data is humbler. In the 5,3845{,}3845,384 SPY days with a next-day return, 54.9%54.9\%54.9% were up. A logistic regression on the last return, the 5-day mean and the 20-day volatility, fitted on the first half, predicts up on 98.8%98.8\%98.8% of the second-half days and is right 55.4%55.4\%55.4% of the time, against 55.2%55.2\%55.2% for always saying up. Its log-loss, 0.68720.68720.6872, is barely below the constant guess at 0.68780.68780.6878. Guessing a constant 0.550.550.55 when the true rate is 0.550.550.55 already scores −(0.55ln⁡0.55+0.45ln⁡0.45)=0.688-(0.55\ln0.55+0.45\ln0.45)=0.688−(0.55ln0.55+0.45ln0.45)=0.688.

Accuracy alone misleads when one class is common. A model that always says up looks good if most days are up, and it is useless for trading. Judge a direction model with the log-loss against the constant-rate baseline, with the calibration of its probabilities and, above all, with the return of a strategy that acts on them after costs. In a threshold decision, the cost of a wrong buy and a wrong skip are rarely equal, so the best threshold is often not 0.50.50.5.

  • Score then sigmoid: a linear score becomes a probability between 000 and 111.
  • Log-loss punishes confidence: a confident wrong answer costs far more than a hesitant one.
  • Beat the baseline: compare with always predicting the base rate, not with 50%50\%50%.

Lab · what's graded

  • A probability from a score15 pts
  • The odds10 pts
  • A log-loss15 pts
  • Where the model is undecided10 pts
  • The constant baseline10 pts
  • Why accuracy misleads10 pts
  • Match the vocabulary10 pts
  • Why the model barely beats the baseline20 pts
0/ 8 answered
Tasks
1Numeric answer15 ptsNot answered

The score is z=−0.5+2xz=-0.5+2xz=−0.5+2x and x=0.75x=0.75x=0.75. What probability does the sigmoid 1/(1+e−z)1/(1+e^{-z})1/(1+e−z) give? Use e−1=0.3679e^{-1}=0.3679e−1=0.3679 and give three decimals.

p=p =p=accepted: ± 0.002
2Numeric answer10 ptsNot answered

A score of z=1z=1z=1 means the odds of a yes are eze^{z}ez. What are they? Give three decimals.

o=o =o=accepted: ± 0.003
3Numeric answer15 ptsNot answered

The model said p=0.731p=0.731p=0.731 and the day was really up. The log-loss is −ln⁡p-\ln p−lnp. What is it? Give three decimals.

l=l =l=accepted: ± 0.002
4Numeric answer10 ptsNot answered

The model is undecided (p=0.5p=0.5p=0.5) when the score is 000. For z=−0.5+2xz=-0.5+2xz=−0.5+2x, at which xxx does this happen? Give two decimals.

x=x =x=accepted: ± 0.005
5Numeric answer10 ptsNot answered

If 55%55\%55% of days are up and you always predict p=0.55p=0.55p=0.55, the average log-loss is −(0.55ln⁡0.55+0.45ln⁡0.45)-(0.55\ln0.55+0.45\ln0.45)−(0.55ln0.55+0.45ln0.45). What is it? Use ln⁡0.55=−0.5978\ln0.55=-0.5978ln0.55=−0.5978 and ln⁡0.45=−0.7985\ln0.45=-0.7985ln0.45=−0.7985, and give three decimals.

l=l =l=accepted: ± 0.002
6Choice10 ptsNot answered

On a test set where 55%55\%55% of the days are up, a model that always predicts up has an accuracy of 55%55\%55%. What does that tell you?

7Match10 ptsNot answered

Match each term with its meaning.

Pick the partner of each item on the left.

Sigmoid
Odds
Log-loss
Threshold
8Explain in words20 ptsNot answered

On SPY, the logistic regression predicts up on 98.8%98.8\%98.8% of the test days and its log-loss is 0.68720.68720.6872 against 0.68780.68780.6878 for a constant. In one or two sentences, explain what this says about predicting daily direction.

Math modeKeyboard or LaTeX inputTask 1 of 8 · Ctrl ⏎ to check

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub