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
bellman.update <- function(action, state, values, gamma=1) { | |
state.transition.prob <- transition[[action]] | |
q <- rep(0, length(state.transition.prob)) | |
for(i in 1:length(state.transition.prob)) { | |
new.state <- act(names(state.transition.prob)[i], state) | |
q[i] <- (state.transition.prob[i] * (rewards[state["y"], state["x"]] + (gamma * values[new.state["y"], new.state["x"]]))) | |
} | |
sum(q) | |
} |
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
actions <- c("N", "S", "E", "W") | |
x <- 1:4 | |
y <- 1:3 | |
rewards <- matrix(rep(0, 12), nrow=3) | |
rewards[2, 2] <- NA | |
rewards[1, 4] <- 1 | |
rewards[2, 4] <- -1 |
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
A = [1 1; 1 2; 1 3; 1 4]; b = [2 1 1 1]'; | |
[m, n] = size(A); | |
C = A' * A; c = A' *b; bb = b'*b; | |
Cbar = [C c; c' bb]; | |
Gbar = chol(Cbar)'; | |
G = Gbar(1:2, 1:2); z = Gbar(3, 1:2)'; rho = Gbar(3, 3); | |
x = G'\z |
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
# | |
# Let's look at how the different models generalize between different datasets | |
# | |
n.training <- 10 | |
n.test <- 100 | |
error.function <- function(y, y.pred) sum((y.pred - y)^2) / 2 | |
e.rms <- function(y, y.pred) sqrt(2 * error.function(y=y, y.pred=y.pred) / length(y)) |
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
# | |
# Let's look at how the different models generalize between different datasets | |
# | |
n.training <- 10 | |
n.test <- 100 | |
error.function <- function(y, y.pred) sum((y.pred - y)^2) / 2 | |
e.rms <- function(y, y.pred) sqrt(2 * error.function(y=y, y.pred=y.pred) / length(y)) |
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
library(PolynomF) | |
n <- 10 | |
f <- function(x) sin(2 * pi * x) | |
x <- seq(0, 1, length=n) | |
y <- f(x) + rnorm(n, sd=0.2) | |
plot(data.frame(x, y)) |
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
# Plot the data | |
pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species", pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)]) | |
# Use linear discriminant analysis | |
iris.lda <- lda(Species ~ ., data = iris) | |
summary(iris.lda) | |
# Use a multinomial logistic regression model | |
library(VGAM) | |
iris.vglm <- glm(Species ~ , family=multinomial, data=iris) |
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
num.iterations <- 1000 | |
# Download South African heart disease data | |
sa.heart <- read.table("http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data", sep=",",head=T,row.names=1) | |
x <- sa.heart[,c("age", "ldl")] | |
y <- sa.heart$chd | |
plot(x, pch=21, bg=c("red","green")[factor(y)]) | |
# Function to standardize input values |
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
# Plot the sigmoid function | |
library(ggplot2) | |
qplot(-10:10, 1/(1 + exp(-(-10:10))), geom="line", xlab="z", ylab="sigmoid function") | |
# Download South African heart disease data | |
sa.heart <- read.table("http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data", sep=",",head=T,row.names=1) | |
# Pretty plot | |
pairs(sa.heart[1:9],pch=21,bg=c("red","green")[factor(sa.heart$chd)]) |
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
data <- read.csv("http://www.statalgo.com/wp-content/uploads/2011/10/housing.csv") | |
x <- as.matrix(cbind(intercept=rep(1, m), data[, c("area", "bedrooms")])) | |
theta <- solve(t(x) %*% x) %*% t(x) %*% y |
NewerOlder