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
//! This file defines `TotalBoost` based on the paper
//! "Totally Corrective Boosting Algorithms that Maximize the Margin"
//! by Warmuth et al.
//!
use crate::;
use ControlFlow;
/// The TotalBoost algorithm proposed in the following paper:
/// [Manfred K. Warmuth, Jun Liao, and Gunnar Rätsch - Totally corrective boosting algorithms that maximize the margin](https://dl.acm.org/doi/10.1145/1143844.1143970)
///
/// Given a set `{(x_{1}, y_{1}), (x_{2}, y_{2}), ..., (x_{m}, y_{m})}`
/// of training examples,
/// [`TotalBoost`] aims to find an optimal solution of
/// the hard-margin optimization problem:
///
/// ```txt
/// max ρ
/// ρ,w
/// s.t. y_{i} Σ_{h ∈ Δ_{H}} w_{h} h(x_{i}) ≥ ρ, for all i ∈ [m],
/// w ∈ Δ_{H}
/// ```
///
/// # Convergence rate
/// Assume that there exists a convex combination of hypotheses
/// that perfectly classifies the training examples:
///
/// ```txt
/// ∃ w ∈ Δ_{h},
/// ∀ (x, y) in training examples,
/// y Σ_{h ∈ H} w_{h} h( x ) > 0.
/// ```
///
/// Given a set of training examples of size `m > 0`
/// and an accuracy parameter `ε > 0`,
/// `TotalBoost` finds an `ε`-approximate solution of
/// the hard-margin optimization problem
/// in `o( ln(m) / ε² )` iterations.
///
/// # Related information
/// - [`TotalBoost`] is a special case of [`SoftBoost`].
/// That is, `TotalBoost` restricts [`SoftBoost::nu`] as `1.0`.
/// For this reason, [`TotalBoost`] is
/// just a wrapper of [`SoftBoost`].
///
///
/// # Example
/// The following code shows
/// a small example for running [`TotalBoost`].
///
///
/// ```no_run
/// use miniboosts::prelude::*;
///
/// // Read the training sample from the CSV file.
/// // We use the column named `class` as the label.
/// let sample = SampleReader::new()
/// .file(path_to_file)
/// .has_header(true)
/// .target_feature("class")
/// .read()
/// .unwrap();
///
///
/// // Get the number of training examples.
/// let n_sample = sample.shape().0 as f64;
///
/// // Initialize `TotalBoost` and set the tolerance parameter as `0.01`.
/// // This means `booster` returns a hypothesis whose training error is
/// // less than `0.01` if the traing examples are linearly separable.
/// let mut booster = TotalBoost::init(&sample)
/// .tolerance(0.01);
///
/// // Set the weak learner with setting parameters.
/// let weak_learner = DecisionTreeBuilder::new(&sample)
/// .max_depth(2)
/// .criterion(Criterion::Entropy)
/// .build();
///
/// // Run `TotalBoost` and obtain the resulting hypothesis `f`.
/// let f = booster.run(&weak_learner);
///
/// // Get the predictions on the training set.
/// let predictions = f.predict_all(&sample);
///
/// // Calculate the training loss.
/// let target = sample.target();
/// let training_loss = target.into_iter()
/// .zip(predictions)
/// .map(|(&y, fx)| if y as i64 == fx { 0.0 } else { 1.0 })
/// .sum::<f64>()
/// / n_sample;
///
///
/// println!("Training Loss is: {training_loss}");
/// ```