MTH408 Machine - Learning - Logistic - Regression
MTH408 Machine - Learning - Logistic - Regression
1
Dr. David Liu
Department of Financial and Actuarial Mathematics
School of Science, Xi’an Jiaotong-Liverpool University
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x0 ⋯ x1
x= ⋮ ⋱ ⋮
x0 ⋯ x𝑚
Note: x0 = 1 because a
constant term is required.
18
ln
ln
Note: The above result is consistent with the negative log likelihood of the problem. This turns out to be a
convex function, so minimization can be achieved.
19
20
21
22
23
24
%% % Logistic regression using CostFunction.m
clc
clear all
load Commodity.mat
closep = Close(:,1); % the first column is the soybean prices.
LogRet = log( closep(2:end) ./ closep(1:end-1) ); % calculate log-returns
tmp1 = LogRet(6:end,:) ; % the day's return if we count from 6
Y = zeros(length(tmp1),1);
tmp2 = find( tmp1 > 0 ); % find positive returns
Y(tmp2) = 1;
ONEs = ones(length(Y),1); % for the constant term
X = [ones(length(Y),1), tmp1];
[rr,cc]=size(X);
initial_theta = zeros(cc, 1);
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, Y)), initial_theta, options)Ahmet
25
26
Example: Linear regression
ℎ𝜃 𝑥 = 𝜃0 + 𝜃1 𝑥 ℎ𝜃 𝑥 = 𝜃0 + 𝜃1 𝑥 + 𝜃2 𝑥 2 ℎ𝜃 𝑥 = 𝜃0 + 𝜃1 𝑥 + 𝜃2 𝑥 2 +
𝜃3 𝑥 3 + 𝜃4 𝑥 4 + ⋯
Under-fitting Just right Overfitting
27
• If we have too many features (i.e. complex model), the learned hypothesis
may fit the training set very well
𝑚
1 𝑖 𝑖 2
𝐽 𝜃 = ℎ𝜃 𝑥 −𝑦 ≈0
2𝑚
𝑖=1
28
Price ($) Price ($)
in 1000’s in 1000’s
ℎ𝜃 𝑥 = 𝜃0 + 𝜃1 𝑥 + 𝜃2 𝑥 2 ℎ𝜃 𝑥 = 𝜃0 + 𝜃1 𝑥 + 𝜃2 𝑥 2 + 𝜃3 𝑥 3 + 𝜃4 𝑥 4
Under-fitting Overfitting
30
31
32
33
34
Binary classification Multiclass classification
𝑥2 𝑥2
𝑥1 𝑥1
35
𝑥2
ℎ𝜃1 𝑥
𝑥1
𝑥2
2 𝑥2
ℎ𝜃 𝑥
𝑥1 𝑥1
Class 1:
Class 2: 3 𝑥2
Class 3: ℎ𝜃 𝑥
𝑖
ℎ𝜃 𝑥 = 𝑃 𝑦 = 𝑖 𝑥; 𝜃 (𝑖 = 1, 2, 3) 36
𝑥1
37
38
39
40
:,
41
42
43