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
//! This file defines `TotalBoost` based on the paper
//! "Totally Corrective Boosting Algorithms that Maximize the Margin"
//! by Warmuth et al.
//!
use crate::;
/// `TotalBoost`.
/// This algorithm is originally invented in this paper:
/// [Totally corrective boosting algorithms that maximize the margin](https://dl.acm.org/doi/10.1145/1143844.1143970)
/// by Manfred K. Warmuth, Jun Liao, and Gunnar Rätsch.
/// `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 [`SoftBoost`](SoftBoost).
/// See also:
/// - [`SoftBoost`]
/// - [`DTree`]
/// - [`DTreeClassifier`]
/// - [`CombinedHypothesis<F>`]
///
/// [`SoftBoost`]: SoftBoost
/// [`DTree`]: crate::weak_learner::DTree
/// [`DTreeClassifier`]: crate::weak_learner::DTreeClassifier
/// [`CombinedHypothesis<F>`]: crate::hypothesis::CombinedHypothesis
///
///
/// ```no_run
/// use miniboosts::prelude::*;
///
/// // Read the training sample from the CSV file.
/// // We use the column named `class` as the label.
/// let has_header = true;
/// let mut sample = Sample::from_csv(path_to_csv_file, has_header)
/// .unwrap()
/// .set_target("class");
///
///
/// // Get the number of training examples.
/// let n_sample = sample.shape().0 as f64;
///
/// // Initialize `LPBoost` 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.
/// // Note that the default tolerance parameter is set as `1 / n_sample`,
/// // where `n_sample = sample.shape().0` is
/// // the number of training examples in `sample`.
/// let booster = LPBoost::init(&sample)
/// .tolerance(0.01);
///
/// // Set the weak learner with setting parameters.
/// let weak_learner = DecisionTree::init(&sample)
/// .max_depth(2)
/// .criterion(Criterion::Edge);
///
/// // Run `LPBoost` and obtain the resulting hypothesis `f`.
/// let f: CombinedHypothesis<DTreeClassifier> = booster.run(&weak_learner);
///
/// // Get the predictions on the training set.
/// let predictions: Vec<i64> = 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}");
/// ```