generated from slds-lmu/lecture_template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added two illustrations to robust loss chunk
- Loading branch information
1 parent
a2a2014
commit 4f60468
Showing
8 changed files
with
237 additions
and
13 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
### This script produces a plot illustrating the ERM under pinball losses | ||
### Data: simulated univariate heteroskedastic regression y=theta0+theta1*x+eps(x) | ||
### eps(x) is normally distributed with mean 0 and standard deviation 0.5+0.5*x | ||
### x is chosen uniformly in [0,10], theta0=1, theta1=0.2, n=200 samples | ||
|
||
rm(list=ls()) | ||
library(quantreg) | ||
library(ggplot2) | ||
|
||
# Data simulation | ||
set.seed(123) | ||
n <- 200 | ||
x <- runif(n, 0, 10) | ||
theta0 <- 1 | ||
theta1 <- 0.2 | ||
eps <- rnorm(n, mean = 0, sd = 0.5+0.5*x) | ||
y <- theta0 + theta1 * x + eps | ||
simu.dat <- data.frame(x = x, y = y) | ||
|
||
# Pinball loss for 0.05 quantile | ||
mod.5 <- rq(formula = as.formula("y ~ x"), tau=0.05, data=simu.dat) | ||
res.5 <- mod.5$coefficients | ||
resids.5 <- mod.5$residuals | ||
preds.5 <- predict(mod.5) | ||
# Pinball loss for 0.5 quantile | ||
mod.50 <- rq(formula = as.formula("y ~ x"), tau=0.5, data=simu.dat) | ||
res.50 <- mod.50$coefficients | ||
resids.50 <- mod.50$residuals | ||
preds.50 <- predict(mod.50) | ||
# Pinball loss for 0.95 quantile | ||
mod.95 <- rq(formula = as.formula("y ~ x"), tau=0.95, data=simu.dat) | ||
res.95 <- mod.95$coefficients | ||
resids.95 <- mod.95$residuals | ||
preds.95 <- predict(mod.95) | ||
|
||
# Aggregate results | ||
res.dat <- simu.dat | ||
res.dat$resid_5 <- resids.5 | ||
res.dat$resid_50 <- resids.50 | ||
res.dat$resid_95 <- resids.95 | ||
res.dat$pred_5 <- preds.5 | ||
res.dat$pred_50 <- preds.50 | ||
res.dat$pred_95 <- preds.95 | ||
|
||
# Calculate true conditional mean | ||
res.dat$true_mean <- theta0 + theta1 * res.dat$x | ||
|
||
|
||
p <- ggplot(res.dat, aes(x = x)) + | ||
geom_line(aes(y = pred_5, color = "q=0.05"), size = 1.6, alpha=1) + | ||
geom_line(aes(y = pred_50, color = "q=0.5"), size = 1.6, alpha=1) + | ||
geom_line(aes(y = pred_95, color = "q=0.95"), size = 1.6, alpha=1) + | ||
geom_line(aes(y = true_mean, linetype = "True mean"), color = "black", size = 1.6) + | ||
geom_point(aes(y = y), color = "darkgrey", size = 2, alpha=1) + | ||
labs(y = "y", x = "x", color = "Quantile", linetype = "") + | ||
scale_linetype_manual(values = c("True mean" = "dashed")) + | ||
theme_minimal() + | ||
theme(text = element_text(size = 12), | ||
axis.title = element_text(size = rel(2)), | ||
axis.text = element_text(size = rel(2)), | ||
legend.title = element_text(size = rel(1.5)), | ||
legend.text = element_text(size = rel(1.3))) + | ||
guides(color = guide_legend(order = 1), linetype = guide_legend(order = 2)) | ||
# Print plot | ||
print(p) | ||
|
||
# Save figure | ||
ggsave(filename = paste0("../figure/quantile-regression.pdf"), plot = p) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
### This script produces a plot comparing various loss functions for robust regression | ||
### Dataset: telephone data (number of calls per year in Belgium) | ||
### Losses: L2, L1, log-cosh, Huber loss | ||
|
||
rm(list=ls()) | ||
|
||
library(Rfit) | ||
library(hqreg) | ||
library(MASS) | ||
library(quantreg) | ||
library(CVXR) | ||
library(ggplot2) | ||
|
||
set.seed(123) | ||
|
||
# Data | ||
tel.dat <- Rfit::telephone | ||
tel.dat$year <- as.numeric(tel.dat$year) | ||
|
||
# L2 loss (least-squares) | ||
fm0 <- lm(calls ~ year, tel.dat) | ||
X <- model.matrix(fm0) | ||
res.l2 <- fm0$coefficients | ||
resids.l2 <- fm0$residuals | ||
preds.l2 <- predict(fm0) | ||
|
||
# Log-cosh regression | ||
f <- function(b) with(tel.dat, sum(log(cosh(calls - X %*% b)))) | ||
res.optim <- optim(coef(fm0), f, method = "Nelder-Mead", control=list(alpha=1.0, beta=0.5, gamma=2.0)) | ||
res.logcosh <- res.optim$par | ||
resids.logcosh <- with(tel.dat, calls - X %*% res.logcosh) | ||
preds.logcosh <- as.numeric(with(tel.dat, X %*% res.logcosh)) | ||
|
||
# L1 loss robust regression | ||
#mod.l1 <- rq(formula = as.formula("calls ~ year"), tau=0.5, data=tel.dat) | ||
#res.l1 <- mod.l1$coefficients | ||
#resids.l1 <- mod.l1$residuals | ||
#preds.l1 <- predict(mod.l1) | ||
|
||
# L1 loss robust regression (manual optimization) | ||
f.l1 <- function(b) with(tel.dat, sum(abs(calls - X %*% b))) | ||
res.optim.l1 <- optim(coef(fm0), f.l1, method = "Nelder-Mead", control=list(alpha=1.0, beta=0.5, gamma=2.0)) | ||
res.l1.manual <- res.optim.l1$par | ||
resids.l1.manual <- with(tel.dat, calls - X %*% res.l1.manual) | ||
preds.l1.manual <- as.numeric(with(tel.dat, X %*% res.l1.manual)) | ||
|
||
# Huber loss robust regression using CVXR | ||
beta <- Variable(2) | ||
obj <- sum(CVXR::huber(tel.dat$calls - X %*% beta, 1)) | ||
prob <- Problem(Minimize(obj)) | ||
result <- solve(prob) | ||
res.huber <- result$getValue(beta) | ||
names(res.huber) <- names(res.l2) | ||
resids.huber <- with(tel.dat, calls - X %*% res.huber) | ||
preds.huber <- as.numeric(with(tel.dat, X %*% res.huber)) | ||
|
||
# aggregate results data | ||
res.tel <- tel.dat | ||
res.tel$resid_l2 <- resids.l2 | ||
#res.tel$resid_l1 <- resids.l1 | ||
res.tel$resid_l1_manual <- resids.l1.manual | ||
res.tel$resid_huber <- resids.huber | ||
res.tel$resid_logcosh <- resids.logcosh | ||
res.tel$pred_l2 <- preds.l2 | ||
#res.tel$pred_l1 <- preds.l1 | ||
res.tel$pred_l1_manual <- preds.l1.manual | ||
res.tel$pred_huber <- preds.huber | ||
res.tel$pred_logcosh <- preds.logcosh | ||
|
||
#coef.b0 <- c(res.l2[1], res.l1[1], res.huber[1], res.logcosh[1]) | ||
#coef.b1 <- c(res.l2[2], res.l1[2], res.huber[2], res.logcosh[2]) | ||
|
||
# Plot results for telephone data | ||
p <- ggplot(res.tel, aes(x = year)) + | ||
geom_line(aes(y = pred_l2, color = "L2 (OLS)"), size = 1.6, alpha=1) + | ||
#geom_line(aes(y = pred_l1, color = "L1"), size = 1.4) + | ||
geom_line(aes(y = pred_l1_manual, color = "L1 (man)"), size = 1.6, alpha=1) + | ||
geom_line(aes(y = pred_huber, color = "Huber"), size = 1.6, alpha=1) + | ||
geom_line(aes(y = pred_logcosh, color = "Log-Cosh"), size = 1.6, alpha=1) + | ||
geom_point(aes(y = calls), color = "black", size = 4, alpha=1) + | ||
labs(y = "Calls (in mio.)", x = "Year", color = "Loss") + | ||
theme_minimal() + | ||
theme(text = element_text(size = 12), | ||
axis.title = element_text(size = rel(2)), | ||
axis.text = element_text(size = rel(2)), | ||
legend.title = element_text(size = rel(1.5)), | ||
legend.text = element_text(size = rel(1.3))) | ||
|
||
# Print plot | ||
print(p) | ||
|
||
# Save figure | ||
ggsave(filename = paste0("../figure/telephone-data.pdf"), plot = p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters