Skip to content

Instantly share code, notes, and snippets.

@vankesteren
vankesteren / sparsecoding.jl
Created November 13, 2019 07:46
Sparse coding for MNIST feature extraction using autodiff
# Sparse coding for MNIST feature extraction using autodiff
using Zygote: gradient
using MLDatasets: MNIST
using LinearAlgebra: Diagonal
using ImageCore
# Goodfellow page 629, equation 19.16, but per pixel
function loss(H::Matrix{Float64}, W::Matrix{Float64})
(sum(abs.(H)) + sum((X - H*W).^2)) / L
end
@vankesteren
vankesteren / mscomputer.md
Last active October 14, 2024 08:52
Documentation for using the Methods & Statistics department compute server
@vankesteren
vankesteren / all_pdf_to_eps.sh
Last active October 8, 2019 14:18
convert all pdfs in a folder to eps
mkdir eps; for file in $(ls | grep .pdf); do pdf2ps $file eps/${file%.*}.eps; done
@vankesteren
vankesteren / mv-norm-sampling.R
Last active July 18, 2019 12:51
Sampling from multivariate distribution & population analysis
# Covariance matrix and random normal data
Sigma <- matrix(c(1, .5, .5, 1), 2)
X <- matrix(rnorm(1000), ncol = 2)
S <- cov(X)
# random multivariate normal sample
R_mvn <- chol(Sigma)
X_mvn <- X %*% R_mvn
cov(X_mvn)
@vankesteren
vankesteren / get_the_correlation.js
Last active December 23, 2020 10:04
Cheat on guessthecorrelation.com
// cheat on guessthecorrelation.com
// don't actually do this to get the high-score. it would be sad.
function get_the_cor() {
// Get points from html
var points = document.getElementsByClassName("nv-group")[0]
// create dataset from transform attribute on the svg points
var transs = Array.from(points.children).map(p => p.getAttribute("transform"))
var data = transs.map(x => x.split(",").map(y => y.match(/[\d\.]+/g)).map(Number))
var tdata = data[0].map((col, i) => data.map(row => row[i]));
@vankesteren
vankesteren / tree_print.R
Last active July 7, 2019 12:07
Pretty print R list structures
# Pretty print list structures with the unicode stuff
# using https://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure
str2 <- function(x, name = NULL, max.depth = 10L, indent = NULL, depth = 0L, last = TRUE) {
cat(indent)
if (last) {
cat("\u2514\u2500")
indent <- paste0(indent, " ")
} else {
cat("\u251c\u2500")
indent <- paste0(indent, "\u2502 ")
@vankesteren
vankesteren / ad_linreg.jl
Last active September 24, 2022 13:22
Gradient descent for linear regression using Zygote AutoDiff
# Gradient descent with autodiff for linear regression
using Zygote
# Data
X = randn(1000, 10)
b = (1:10)
y = X * b + randn(1000)
# MSE for linear model
function mse(bhat)
@vankesteren
vankesteren / convolutions.R
Created March 19, 2019 10:57
Time series convolutions
# convolutions
convolve <- function(x, kernel) {
size <- length(kernel)
x_pad <- c(rep(0, size - 1), x)
out <- rep(0, length(x))
for (i in 1:length(x)) {
out[i] <- x_pad[i:(i + (size - 1))] %*% kernel
}
return(out)
}
@vankesteren
vankesteren / regression.R
Last active March 11, 2021 08:28
All the interesting regression quantities
# Randomly generate some data
S <- rWishart(1, 10, diag(10))[,,1] / 10
X <- MASS::mvrnorm(100, rep(0, 10), S)
b <- runif(10, -1, 1)
y <- X %*% b + rnorm(100, sd = sqrt(b %*% S %*% b))
# all the interesting regression quantities!
n <- nrow(X) # sample size
p <- ncol(X) # parameters in model matrix
@vankesteren
vankesteren / water_NL.R
Last active January 28, 2019 22:53
Plotting 3d maps of the Netherlands with different water levels
# Use the great rayshader package (www.rayshader.com)
library(rayshader)
# Download data
zip_loc <- tempfile()
download.file("http://geodata.nationaalgeoregister.nl/ahn1/extract/ahn1_100m/ahn1_100.tif.zip",
zip_loc)
local_tif <- raster::raster(unzip(zip_loc, "ahn_100.tif"))
unlink(zip_loc)