Regression
Support Vector Regression, SVR
2023-12-19
In SVM, it is often used as an analogy that the largest possible margin, which is the widest road, is placed between the two classes. In SVR, the goal is to determine the coefficients of the regression in such a way that as many samples as possible fall within the given size of the road. Here, the width of the road is defined as ε. For the samples within the width of the road, no cost is incurred due to errors. However, for the samples outside the road, a penalty is imposed based on the distance from the regression function.
The shape of the loss function is as follows: (in the case of linear) it is linear outside the ε-insensitive region and 0 inside it.
if \(|y-f(x,w)|\le \epsilon,\) \(L_\epsilon(y,f(x,w))=0\)
otherwise, \(L_\epsilon(y,f(x,w))=|y-f(x,w)|-\epsilon\)

The mathematical formulation of the objective function and constraints are in a similar fashion with Soft-Margin SVM. A set of slack variables, ξ*, ξ, are introduced to define the range for handling outliers outside the boundaries.

The objective function to find the slope, w is as follows:
\(\min_w\dfrac{1}{2}\parallel w\parallel ^2+C\sum _i^{\ }(\xi_i+\xi_i^*)\)
Likewise Soft margin SVM, if C is small, the minimization is toward the widest road between two hyperplanes allowing more mistakes (i.e. more outliers). If C is large, the optimization tries to shrink the slack variables (i.e. less outliers)
The constraints are:
All the measurements, y should be less than the prediction wTx (the center line) + ε + ξ*, i.e.
\(y_i\le w^Tx_i+\epsilon+\xi_i^*\text{ (i=1,...,n)}\), then rearrange it to
\(y_i-w^Tx_i-\epsilon-\xi_i^* \le 0\)
All the measurements, y should be greater than the prediction wTx - ε - ξ, i.e.
\(y_i \ge w^Tx_i- \epsilon-\xi_i\text{ (i=1,...,n)}\), then rearrange it to
\(-y_i + w^Tx_i-\epsilon-\xi_i \le 0\)
For all samples,
\(\xi_i,\xi_i^*\ge 0\text{ (i=1,...,n)}\)
Introduce the Lagrange multiplier αi, and α*i to all samples to formulate Lagrange dual problem,
\(\max_w\dfrac{1}{2}w^Tw+C\sum_i^{\ }(\xi_i+\xi^*_i)+\sum_i^{\ }\alpha^*_i(y_i-w^Tx_i-\epsilon-\xi^*_i )+\sum_i^{\ }\alpha_i(-y_i+w^Tx_i-\epsilon-\xi_i)\)
\(=\max_w \dfrac{1}{2}w^Tw+\sum_i^{\ }(C-\alpha_i)\xi_i+\sum_i^{\ }(C-\alpha^*_i)\xi^*_i-\sum_i^{\ }(\alpha^*_i+\alpha_i)\epsilon+\sum_i^{\ }(\alpha^*_i-\alpha_i)y_i+\sum_i^{\ }(\alpha_i-\alpha^*_i)w^Tx_i\)
Take a derivative w.r.t. w to maximize the objective, L
\(\partial L / \partial w = w + \sum_i^{\ }(\alpha_i-\alpha^*_i)x_i = 0\)
Then, we got the w as follows:
\(w=\sum_i^{\ }(\alpha^*_i-\alpha_i)x_i\)
Substituting the w into the Lagrange dual formulation,
\(\max_{\alpha,\alpha^*}^{\ }\dfrac{1}{2}w^Tw+\sum_i^{\ }(C-\alpha_i)\xi_i+\sum_i^{\ }(C-\alpha^*_i)\xi^*_i-\sum_i^{\ }(\alpha^*_i+\alpha_i)\epsilon+\sum_i^{\ }(\alpha^*_i-\alpha_i)y_i-w^Tw\)
\(=\max_{\alpha,\alpha^*}^{\ }-\dfrac{1}{2}w^Tw+\sum_i^{\ }(C-\alpha_i)\xi_i+\sum_i^{\ }(C-\alpha^*_i)\xi^*_i-\sum_i^{\ }(\alpha^*_i+\alpha_i)\epsilon+\sum_i^{\ }(\alpha^*_i-\alpha_i)y_i\)
If we constraint all the Lagrange multipliers, αi, and α*i are less than equal to C, the hyperparameter,
\(C \ge \alpha^*_i\)
\(C \ge \alpha_i\)
Then, the below objective function also true for the same optimal values since all the slack variables are assumed to be positive.
\(\max_{\alpha,\alpha^*}^{\ }-\dfrac{1}{2}w^Tw-\sum_i^{\ }(\alpha^*_i+\alpha_i)\epsilon+\sum_i^{\ }(\alpha^*_i-\alpha_i)y_i\)
where \(w=\sum _i^{\ }(\alpha^*_i-\alpha_i)x_i\)
The constraints are
\(\sum _i^{\ }(\alpha_i^*-\alpha_i)=0\)
\(0\le \alpha_i^*\le C\)
\(0\le \alpha_i\le C\)
Note that the samples sitting outside of the hyperplanes are having the Lagrange multiplier (αi, α*i) greater than zero.
The bias, b, for each sample can be found as follows:
For the samples with \(\alpha_i \ge 0, b = y_i-w^Tx_i-\epsilon\)
For the samples with \(\alpha^*_i \ge 0, b = -y_i+w^Tx_i-\epsilon\)
For convenience and without loss of generosity, we take
\(b=\overline {y}-w^T\overline {x}\)
The steps for implementing the algorithm are as follows:
(step 1) Set the hyperparameters, C and ε
(step 2) Allocate two Lagrange multipliers α & α* at every sample. They must be randomized with any positive numbers.
(step 3) Calculate α*-α, α*+α.
(step 4) Calculate w
\(w=\sum _{i}^{\ }(\alpha_i^*-\alpha_i)x_i\)
(step 5) Calculate the objective function, L
\(-\dfrac{1}{2}w^Tw+\sum _{i}^{\ }(\alpha_i^*-\alpha_i)y_i-\epsilon\sum _{i}^{\ }(\alpha_i+\alpha_i^*)\)
(step 6) Compute the constraints
\(\sum _{i}^{\ }(\alpha_i^*-\alpha_i)=0\) which should be satisfied during the optimization
\(\max _{\ }^{ }(\alpha^*)\le C \) which is to ensure that all α* ≤ C
\(\max _{\ }^{ }(\alpha)\le C\) which is to ensure that all α ≤ C
(step 7) Perform the optimization w.r.t the α and α* for the objective to be maximized with all of the constraints.
(step 8) Calculate the bias, b
\(b=\overline {y}-w^T\overline {x}\)
To illustrate SVR in Excel, we would like to run SVR using the number of rooms (RM) as x, and the median home value (MEDV) as y from the Boston Housing dataset.
(Step 1) Data preparation. In order for a random sampling from the Boston Housing data, add a column with RAND() function, and extract the first 100 data points after sorting them in ascending order. Let
Column A: RM,
Column B: MEDV.
(step 2) Add two Lagrange multiplier α* and α and randomize with positive values by calling RAND()
Column C: a*
Column D: a
(step 3) Calculate α*-α, α*+α
Column E: α*-α
Column F: α*+α
(step 4) Calculate w
Cell L7: w =SUMPRODUCT(A2:A101,E2:E101)
(step 5) Set the hyperparameters, ε=5 and C=3. Calculate the objective and the constraints as inputs for the Solver.
Cell L4: ε=5
Cell L5: The cost = -0.5* L7^2+SUMPRODUCT(B2:B101,E2:E101)-L4*SUM(F2:F101)
Cell L6: The constraint = SUM(E:E101) which should be zero
Cell L8: max(α*) which should be less than equal to C
Cell L9: max(α) which should be less than equal to C

(step 6) Run the Solver with the following settings. You may have to repeat a few times to converge.
- The Changing Variable Cells are all the multipliers.
- Need to check the box for Non-Negative to ensure all the multipliers are positive.
- The two of the constraints include the setting C=3.

(step 7) After the completion of the Solver, calculate the bias, b, the predicted, y^, and the hyperplanes.
\(b=\overline {y}-w^T\overline {x}\)
\(\hat {y} = w^Tx+b\) which is shown in red in the plot below.
\(+e = \hat {y}+\epsilon\) which is shown in blue.
\(-e = \hat {y}-\epsilon\) which is shown in blue.
