Erik-Jan van Kesteren
In this document, I create a small R function for computing the
Kullback-Leibler divergence between two (multivariate) normal (Gaussian)
distributions. The general formula for the divergence is as follows:
| library(lavaan) | |
| # sim_dat function from my blog post | |
| # https://erikjanvankesteren.nl/blog/multivariate_regression | |
| sim_dat <- function(N = 100, P = 5, Q = 2, varprop = 0.5, rho_X = 0, rho_B = 0, rho_E = 0, exact = TRUE) { | |
| # Create X matrix | |
| X <- matrix(rnorm(N*P), N) | |
| RX <- matrix(rho_X, P, P) | |
| diag(RX) <- 1 | |
| CRX <- chol(RX) |
| import gzip | |
| x1 = "Japan’s Seiko Epson Corp. has developed a 12-gram flying microrobot." | |
| x2 = "The latest tiny flying robot has been unveiled in Japan." | |
| x3 = "Michael Phelps won the gold medal in the 400 individual medley." | |
| def ncd(x: str, y: str) -> float: | |
| ''' | |
| Returns the normalized compression distance between two strings. |
| # density ratio estimation using ulsif with laplace kernel | |
| library(kernlab) | |
| library(rmutil) | |
| set.seed(45) | |
| # two samples | |
| x <- matrix(rlaplace(10000)) | |
| y <- matrix(rnorm(10000, sd = 0.707)) | |
| # kernel stuff |
| # Figuring out whether this logspline thing is any good | |
| library(logspline) | |
| # mixture | |
| d1 <- function(x) 0.6*dnorm(x) + 0.3*dlnorm(x, sdlog = 0.1) + 0.1*dnorm(x, 2) | |
| r1 <- function(n) sample(c(rnorm(round(n*.6)), rlnorm(round(n*.3), sdlog = 0.1), rnorm(round(n*.1), 2))) | |
| set.seed(123) | |
| x <- r1(100) | |
| dd <- density(x, n = 1000) |
| library(torch) | |
| tot_obs <- 1000 | |
| x <- c(rnorm(.2*tot_obs), rnorm(.2*tot_obs, 5, 2), rnorm(.3*tot_obs), rnorm(.1*tot_obs, 5, 2), rnorm(.2*tot_obs)) | |
| x_torch <- torch_tensor(matrix(x)) | |
| plot(x, type = "l") | |
| # Create a very simple lstm, with only one hidden node and linear activation for the output | |
| SimpleLSTM <- nn_module("simplelstm", | |
| initialize = function(obs_size, hidden_size) { |
| # setup with seeds for reproducibility | |
| library(torch) | |
| set.seed(45) | |
| torch_manual_seed(45) | |
| # True distribution is normal(1, 3), we sample 10000 points | |
| N <- 500 | |
| y <- rnorm(N, 1, 3) | |
| # we need to create torch tensor of this data to use in torch |
| #include <RcppArmadillo.h> | |
| // [[Rcpp::depends(RcppArmadillo)]] | |
| // [[Rcpp::export]] | |
| double rbf_kern(const arma::vec xi, const arma::vec xj, const double& gamma) { | |
| return exp(-gamma*norm(xi-xj)); | |
| } | |
| // [[Rcpp::export]] | |
| arma::mat rbf_gram_cpp(const arma::mat& X, const double& gamma) { |
| # Plot & interpolate BBB vote percentage in Utrecht | |
| # using open data from data.utrecht.nl | |
| # last edited 20230320 by | |
| library(tidyverse) | |
| library(sf) | |
| library(curl) | |
| library(glue) | |
| library(jsonlite) | |
| library(pbapply) | |
| library(ggspatial) |
| Sector_score,LOCATION_ID,PARA_A,Score_A,Risk_A,PARA_B,Score_B,Risk_B,TOTAL,numbers,Score_B,Risk_C,Money_Value,Score_MV,Risk_D,District_Loss,PROB,RiSk_E,History,Prob,Risk_F,Score,Inherent_Risk,CONTROL_RISK,Detection_Risk,Audit_Risk,Risk | |
| 3.89,23,4.18,0.6,2.508,2.5,0.2,0.5,6.68,5,0.2,1,3.38,0.2,0.676,2,0.2,0.4,0,0.2,0,2.4,8.574,0.4,0.5,1.7148,1 | |
| 3.89,6,0,0.2,0,4.83,0.2,0.966,4.83,5,0.2,1,0.94,0.2,0.188,2,0.2,0.4,0,0.2,0,2,2.554,0.4,0.5,0.5108,0 | |
| 3.89,6,0.51,0.2,0.102,0.23,0.2,0.046,0.74,5,0.2,1,0,0.2,0,2,0.2,0.4,0,0.2,0,2,1.548,0.4,0.5,0.3096,0 | |
| 3.89,6,0,0.2,0,10.8,0.6,6.48,10.8,6,0.6,3.6,11.75,0.6,7.05,2,0.2,0.4,0,0.2,0,4.4,17.53,0.4,0.5,3.506,1 | |
| 3.89,6,0,0.2,0,0.08,0.2,0.016,0.08,5,0.2,1,0,0.2,0,2,0.2,0.4,0,0.2,0,2,1.416,0.4,0.5,0.2832,0 | |
| 3.89,6,0,0.2,0,0.83,0.2,0.166,0.83,5,0.2,1,2.95,0.2,0.59,2,0.2,0.4,0,0.2,0,2,2.156,0.4,0.5,0.4312,0 | |
| 3.89,7,1.1,0.4,0.44,7.41,0.4,2.964,8.51,5,0.2,1,44.95,0.6,26.97,2,0.2,0.4,0,0.2,0,3.2,31.774,0.4,0.5,6.3548,1 | |
| 3.89,8,8.5,0.6,5.1,12.03,0.6,7.218,20.53,5.5,0.4,2.2,7.79,0.4,3.116,2,0 |