σμ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.3·~55 minIntermediate

Trees and random forests

Skill positionModule 2 of 4 · Topic 3 of 3
2.3 Trees and random forests
—Lab score— / 100Total XP—Streak—

Questions in a row, and many trees

A decision tree asks a chain of yes-or-no questions about the features, such as whether momentum is above a value, and ends in a leaf that predicts the average (or the majority class) of the training rows that reached it. It picks each question greedily: of all features and cut points, it takes the one that makes the two child groups purest. For classes, purity is measured by the Gini impurity 1−∑pk21-\sum p_k^{2}1−∑pk2​, which is 000 for a pure group. The gain of a split is the parent impurity minus the size-weighted impurity of the children. A tree can fit anything, so a deep tree memorises the noise in the training rows.

G=1−∑kpk2,ΔG=Gparent−(nLnGL+nRnGR),Var ⁣(fˉ)=ρσ2+1−ρBσ2G=1-\sum_k p_k^{2},\qquad \Delta G=G_{\mathrm{parent}}-\left(\frac{n_L}{n}G_L+\frac{n_R}{n}G_R\right),\qquad \mathrm{Var}\!\left(\bar f\right)=\rho\sigma^{2}+\frac{1-\rho}{B}\sigma^{2}G=1−k∑​pk2​,ΔG=Gparent​−(nnL​​GL​+nnR​​GR​),Var(fˉ​)=ρσ2+B1−ρ​σ2(1)

A random forest averages many trees. Each tree is grown on a bootstrap sample of the rows and, at each split, looks at only a random subset of the features, so the trees differ. If BBB trees each have variance σ2\sigma^{2}σ2 and pairwise correlation ρ\rhoρ, their average has variance ρσ2+(1−ρ)σ2/B\rho\sigma^{2}+(1-\rho)\sigma^{2}/Bρσ2+(1−ρ)σ2/B: averaging kills the part that is independent between trees and keeps only the shared part. A forest therefore overfits less than one deep tree, but it still fits noise when the true signal is weak, as in returns. Always judge it on later data.

A split by hand and a real forestA node holds 666 up days and 444 down days: G=1−0.62−0.42=0.48G=1-0.6^{2}-0.4^{2}=0.48G=1−0.62−0.42=0.48. A split sends [5 up,1 down][5\text{ up},1\text{ down}][5 up,1 down] left, with G=1−(5/6)2−(1/6)2=0.278G=1-(5/6)^{2}-(1/6)^{2}=0.278G=1−(5/6)2−(1/6)2=0.278, and [1,3][1,3][1,3] right, with G=0.375G=0.375G=0.375. The weighted impurity is 0.6×0.278+0.4×0.375=0.3170.6\times0.278+0.4\times0.375=0.3170.6×0.278+0.4×0.375=0.317, so the gain is 0.48−0.317=0.1630.48-0.317=0.1630.48−0.317=0.163. For a forest with ρ=0.3\rho=0.3ρ=0.3, σ2=1\sigma^{2}=1σ2=1 and B=100B=100B=100 trees, the variance is 0.3+0.7/100=0.3070.3+0.7/100=0.3070.3+0.7/100=0.307, barely below the 0.30.30.3 floor that no number of trees can beat. On the real monthly stock panel, a forest of 404040 trees of depth 888 has an in-sample information coefficient of 0.1020.1020.102, but only −0.008-0.008−0.008 in walk-forward tests, against +0.033+0.033+0.033 for the ridge regression.
  • Greedy purity: each split is the one that lowers impurity the most right now.
  • Averaging cuts variance: a forest has the shared part ρσ2\rho\sigma^{2}ρσ2 left, however many trees you add.
  • Flexible models overfit weak signals: in-sample skill can be real and out-of-sample skill zero.

Lab · what's graded

  • Impurity of a node10 pts
  • Impurity of a child10 pts
  • The weighted impurity15 pts
  • The gain of the split15 pts
  • The variance of a forest15 pts
  • Why averaging trees helps10 pts
  • Match the vocabulary10 pts
  • Good in sample, poor later15 pts
0/ 8 answered
Tasks
1Numeric answer10 ptsNot answered

A node holds 666 up days and 444 down days. What is its Gini impurity 1−pup2−pdown21-p_{\mathrm{up}}^{2}-p_{\mathrm{down}}^{2}1−pup2​−pdown2​? Give two decimals.

g=g =g=accepted: ± 0.005
2Numeric answer10 ptsNot answered

A split sends 555 up days and 111 down day to the left child. What is its Gini impurity? Give three decimals.

g=g =g=accepted: ± 0.002
3Numeric answer15 ptsNot answered

The left child (666 of 101010 rows) has impurity 0.2780.2780.278 and the right child (444 of 101010 rows) has impurity 0.3750.3750.375. What is the size-weighted impurity of the children? Give three decimals.

g=g =g=accepted: ± 0.002
4Numeric answer15 ptsNot answered

The parent impurity is 0.480.480.48 and the weighted impurity of the children is 0.3170.3170.317. What is the gain of the split? Give three decimals.

d=d =d=accepted: ± 0.002
5Numeric answer15 ptsNot answered

Trees have variance σ2=1\sigma^{2}=1σ2=1 and pairwise correlation ρ=0.3\rho=0.3ρ=0.3. The average of B=100B=100B=100 trees has variance ρσ2+(1−ρ)σ2/B\rho\sigma^{2}+(1-\rho)\sigma^{2}/Bρσ2+(1−ρ)σ2/B. What is it? Give three decimals.

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

What does averaging many different trees mainly reduce?

7Match10 ptsNot answered

Match each term with its meaning.

Pick the partner of each item on the left.

Leaf
Gini impurity
Bootstrap sample
Depth
8Explain in words15 ptsNot answered

A forest has an in-sample information coefficient of 0.1020.1020.102 but −0.008-0.008−0.008 in walk-forward tests, while a ridge regression has 0.0330.0330.033 in both. In one or two sentences, explain why.

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