-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrandom-forest.typ
279 lines (257 loc) · 10.1 KB
/
random-forest.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#import "@preview/cetz:0.3.4": canvas, draw
#import draw: line, content, circle, rect, group, set-style, on-layer
#set page(width: auto, height: auto, margin: 8pt)
// TODO add arrows next to each connecting line between red nodes
// Define styles and constants
#let arrow-style = (
mark: (end: "stealth", fill: black, scale: 0.5),
stroke: 0.5pt,
)
#let line-style = (stroke: 0.5pt)
#let red-arrow-style = (
stroke: red,
fill: red,
mark: (end: "stealth", scale: 0.6, offset: 0.2, start: (symbol: "|", width: 1pt)),
stroke-width: 1.2pt,
)
#let node-box-style = (
stroke: 0.3pt,
fill: white,
radius: 3pt,
)
#let tree-box-style = (
stroke: 0.3pt,
fill: none,
radius: 3pt,
)
#canvas({
// Set global content frame style
set-style(
content: (
frame: "rect",
stroke: 0.1pt,
fill: white,
inset: 3pt,
radius: 3pt,
padding: (3pt, 5pt, 2pt),
),
)
// Helper function for tree nodes
let tree-node(position, fill: blue.lighten(40%), name: none) = {
circle(position, radius: 0.2, fill: fill, stroke: 0.3pt, name: name)
}
// Helper function to draw a complete tree
let draw-tree(x-position, tree-name, path-nodes: ()) = {
let y-position = -2
let level-spacing = 1.2 // Vertical spacing between levels
let node-spacing = 1.0 // Base horizontal spacing between nodes
let arrow-offset = 0.15 // Offset for red arrows
group(
name: tree-name,
{
// Root node (level 0)
tree-node(
(x-position, y-position),
name: "root",
fill: if "root" in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
// Level 1 nodes (2 nodes, evenly spaced)
tree-node(
(x-position - node-spacing, y-position - level-spacing),
name: "left",
fill: if "left" in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
tree-node(
(x-position + node-spacing, y-position - level-spacing),
name: "right",
fill: if "right" in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
// Connect nodes with black lines
line("root", "left", ..line-style)
line("root", "right", ..line-style)
// Add red arrows for paths
if "root" in path-nodes and "left" in path-nodes {
line(
(x-position - arrow-offset, y-position),
(x-position - node-spacing - arrow-offset, y-position - level-spacing),
..red-arrow-style,
)
}
if "root" in path-nodes and "right" in path-nodes {
line(
(x-position + arrow-offset, y-position),
(x-position + node-spacing + arrow-offset, y-position - level-spacing),
..red-arrow-style,
)
}
// Level 2 nodes - different structure for each tree
if tree-name == "tree1" {
// Tree 1: left node splits into 3, right into 1
let left-spacing = node-spacing * 0.67 // Tighter spacing for 3 nodes
for (node-idx, offset-factor) in (-1, 0, 1).enumerate() {
let child-x = x-position - node-spacing + offset-factor * left-spacing
let child-y = y-position - 2 * level-spacing
let node-name = "l" + str(node-idx)
tree-node(
(child-x, child-y),
name: node-name,
fill: if node-name in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
// Black line
line("left", node-name, ..line-style)
// Red arrow if in path
if "left" in path-nodes and node-name in path-nodes {
// Determine offset direction based on node position
let offset-direction = if offset-factor < 0 { -arrow-offset } else { arrow-offset }
line(
(x-position - node-spacing + offset-direction, y-position - level-spacing),
(child-x + offset-direction, child-y),
..red-arrow-style,
)
}
}
// Right side
let right-child-x = x-position + node-spacing
let right-child-y = y-position - 2 * level-spacing
tree-node(
(right-child-x, right-child-y),
name: "r0",
fill: if "r0" in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
line("right", "r0", ..line-style)
if "right" in path-nodes and "r0" in path-nodes {
line(
(x-position + node-spacing + arrow-offset, y-position - level-spacing),
(right-child-x + arrow-offset, right-child-y),
..red-arrow-style,
)
}
} else if tree-name == "tree2" {
// Tree 2: left node splits into 2, right into 2
let side-spacing = node-spacing * 0.5 // Half spacing for 2 nodes each side
for (node-idx, offset-factor) in (-1, 1).enumerate() {
let child-x = x-position - node-spacing + offset-factor * side-spacing
let child-y = y-position - 2 * level-spacing
let node-name = "l" + str(node-idx)
tree-node(
(child-x, child-y),
name: node-name,
fill: if node-name in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
// Black line
line("left", node-name, ..line-style)
// Red arrow if in path
if "left" in path-nodes and node-name in path-nodes {
// Determine offset direction based on node position
let offset-direction = if offset-factor < 0 { -arrow-offset } else { arrow-offset }
line(
(x-position - node-spacing + offset-direction, y-position - level-spacing),
(child-x + offset-direction, child-y),
..red-arrow-style,
)
}
}
for (node-idx, offset-factor) in (-1, 1).enumerate() {
let child-x = x-position + node-spacing + offset-factor * side-spacing
let child-y = y-position - 2 * level-spacing
let node-name = "r" + str(node-idx)
tree-node(
(child-x, child-y),
name: node-name,
fill: if node-name in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
// Black line
line("right", node-name, ..line-style)
// Red arrow if in path
if "right" in path-nodes and node-name in path-nodes {
// Determine offset direction based on node position
let offset-direction = if offset-factor < 0 { -arrow-offset } else { arrow-offset }
line(
(x-position + node-spacing + offset-direction, y-position - level-spacing),
(child-x + offset-direction, child-y),
..red-arrow-style,
)
}
}
} else {
// Tree 3: left node splits into 1, right into 3
let left-child-x = x-position - node-spacing
let left-child-y = y-position - 2 * level-spacing
tree-node(
(left-child-x, left-child-y),
name: "l0",
fill: if "l0" in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
line("left", "l0", ..line-style)
if "left" in path-nodes and "l0" in path-nodes {
line(
(x-position - node-spacing - arrow-offset, y-position - level-spacing),
(left-child-x - arrow-offset, left-child-y),
..red-arrow-style,
)
}
let right-spacing = node-spacing * 0.67 // Tighter spacing for 3 nodes
for (node-idx, offset-factor) in (-1, 0, 1).enumerate() {
let child-x = x-position + node-spacing + offset-factor * right-spacing
let child-y = y-position - 2 * level-spacing
let node-name = "r" + str(node-idx)
tree-node(
(child-x, child-y),
name: node-name,
fill: if node-name in path-nodes { red.lighten(30%) } else { blue.lighten(40%) },
)
// Black line
line("right", node-name, ..line-style)
// Red arrow if in path
if "right" in path-nodes and node-name in path-nodes {
// Determine offset direction based on node position
let offset-direction = if offset-factor < 0 { -arrow-offset } else { arrow-offset }
line(
(x-position + node-spacing + offset-direction, y-position - level-spacing),
(child-x + offset-direction, child-y),
..red-arrow-style,
)
}
}
}
// Tree box and label (made slightly wider)
rect(
(x-position - 2.6, y-position - 2.6 * level-spacing),
(x-position + 2.6, y-position + 0.5),
stroke: 0.3pt,
fill: none,
radius: 3pt,
name: tree-name,
)
content(
(x-position - 1.7, y-position + 0.1),
[Tree #if tree-name == "tree3" { $n$ } else { tree-name.last() }],
)
},
)
}
// Draw main nodes
content((0, 0), [Training Data], name: "training")
content((0, 1), [Sample and Feature Bagging], name: "bagging")
// Draw trees with different paths highlighted
draw-tree(-6, "tree1", path-nodes: ("root", "left", "l1"))
draw-tree(0, "tree2", path-nodes: ("root", "right", "r0"))
draw-tree(6.75, "tree3", path-nodes: ("root", "right", "r2"))
// Add dots between trees
content((3.375, -3.5), text(size: 1.2em)[$dots.c$])
// Draw mean/prediction nodes
content(
(0, -6.5),
align(center)[Mean in regression or\ majority vote in classification],
name: "mean",
padding: (5pt, 5pt, 1.5pt),
)
content((0, -8), [Prediction], name: "pred", padding: (4pt, 5pt, 3pt))
// Connect everything
for tree-name in ("tree1", "tree2", "tree3") {
line("training", tree-name + ".north", ..arrow-style)
line(tree-name + ".south", "mean", ..arrow-style)
}
line("bagging", "training", ..arrow-style)
line("mean", "pred", ..arrow-style)
})