Skip to content

Instantly share code, notes, and snippets.

@smc77
smc77 / gist:8277196
Last active January 2, 2016 08:29
Grid World - Value Iteration
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)
}
@smc77
smc77 / gist:8277155
Last active May 19, 2016 02:56
Grid World - Setup
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
@smc77
smc77 / normal_equation.jl
Created April 22, 2012 17:05
Normal Equation (with Julia)
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
@smc77
smc77 / regularization.R
Created November 17, 2011 03:47
Regularization
#
# 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))
@smc77
smc77 / polynomial_generalization.R
Created November 9, 2011 03:22
Generalization
#
# 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))
@smc77
smc77 / overfitting.R
Created November 4, 2011 02:05
Overfitting
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))
@smc77
smc77 / logistic_regression_multi.R
Created October 28, 2011 03:28
Multiclass Logistic Regression
# 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)
@smc77
smc77 / logistic_gradient_descent.R
Created October 28, 2011 03:14
Logistic Regression with Gradient Descent
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
@smc77
smc77 / logistic_regression.R
Created October 26, 2011 01:45
Logistic Regression
# 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)])
@smc77
smc77 / normal_equation.R
Created October 24, 2011 00:11
Normal Equation
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