Skip to content

Instantly share code, notes, and snippets.

@swist
Created February 6, 2015 17:14
Show Gist options
  • Save swist/5666c5bfe4a0fda7717d to your computer and use it in GitHub Desktop.
Save swist/5666c5bfe4a0fda7717d to your computer and use it in GitHub Desktop.
s7.r
# question 3
linear_discriminant <- function(x, mean, covariance, prior){
posterior = dmvnorm(x, mean, covariance)
g_i <- log(prior) + log(posterior)
g_i
}
quadratic_discriminant <- function(x, mean, covariance, prior)
{
W_i <- (-1/2) * solve(covariance)
w_i <- solve(covariance) %*% mean
w_i0 <- (-1/2) * t(mean) *solve(covariance) * mean - (1/2) * log(abs(det(cov)) + log(prior)
g_i <- t(x) * W_i * x + t(w_i) * x + w_i0
g_i
}
prior.c1 = 0.25
prior.c2 = 0.75
p <- sample(seq(10,15),1)
u1 <- rep(0,p)
u2 <- runif(p, 0,1)
cov.c1 <- diag(p)
cov.c2 <- 0.5*diag(p)
cov.c2[1,3] = cov.c2[3,1] = -0.3
cov.c2[2,3] = cov.c2[3,2] = 0.2
cov.c2[4,8] = cov.c2[8,4] = 0.1
for (n in seq(100,1000,by=100)) {
training.c1 = rmvnorm(prior.c1 * n, u1, cov.c1)
training.c2 = rmvnorm(prior.c2 * n, u2, cov.c2)
test.c1 = rmvnorm(prior.c1 * 4000, u1, cov.c1)
test.c2 = rmvnorm(prior.c2 * 4000, u2, cov.c2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment