Skip to content

Instantly share code, notes, and snippets.

View boennecd's full-sized avatar
💭
Coding

boennecd

💭
Coding
View GitHub Profile
# computes the log Cholesky decomposition
log_chol <- function (x) {
L <- chol(x)
diag(L) <- log(diag(L))
L[upper.tri(L, TRUE)]
}
# computes the inverse of the log Cholesky decomposition
log_chol_inv <- function(x) {
n <- round((sqrt(8 * length(x) + 1) - 1)/2)
@boennecd
boennecd / README.md
Last active April 14, 2021 13:39
Provides a version of kinship2::kinship which returns a sparse matrix even when there is only one family.

Provides a version of kinship2::kinship that returns a sparse matrix even when there is only one family. A full dense matrix is never stored. The script assigns two functions to the global enviroment when Rcpp::sourceCpp'ed. These are kinship_sparse_R and kinship_sparse which is an only R and mainly C++ implementation, respectilvy. The function can be sourced by calling

download.file("https://gist.githubusercontent.com/boennecd/b51550a8bec2fde6d48f8f3c2d1f69a1/raw",
 cpp_file &lt;- paste0(tempfile(), ".cpp"))
@boennecd
boennecd / README.md
Last active April 14, 2021 06:28
Provides a faster version of the kinship2::makefamid.

Provides a faster version of the kinship2::makefamid function if sourced using Rcpp::sourceCpp. It also provides a new implementation and quite different implementation based largely on C++ called makefamid_new. You can try the functions by calling

download.file("https://gist.githubusercontent.com/boennecd/d42fc0ced67033c222ddc8c95945af00/raw/makefamid-fast.cpp", 
              src_file <- paste0(tempfile(), ".cpp"))
Rcpp::sourceCpp(src_file)
# assign function to simulate a number of independent pedigrees along with
# the observed survival times for the last generation simulated from a
# mixed generalized survival model.
#
# Args:
# max_depth: maximum depth of the families (number of generations less
# one).
# max_members: roughly maximum number of members in each pedigree in each
# family.
# sds: standard deviation of the genetic effect.
/// tmb_includes.h
#ifndef TMB_INCLUDES_H
#define TMB_INCLUDES_H
/* TMB */
#include <TMB.hpp>
namespace CppAD {
inline double Value(double const x){
return x;
@boennecd
boennecd / fast-commutation.cpp
Last active August 5, 2020 13:45
Computes the commutation matrix fast-ish.
#include <Rcpp.h>
#include <memory.h>
std::unique_ptr<size_t[]> get_commutation_unequal_vec
(unsigned const n, unsigned const m, bool const transpose){
unsigned const nm = n * m,
nnm_p1 = n * nm + 1L,
nm_pm = nm + m;
std::unique_ptr<size_t[]> out(new size_t[nm]);
size_t * const o_begin = out.get();
## Essentially identical to
## https://github.com/r-hub/rhub-linux-builders/blob/7af50594ab7a3076adb6e52cce5a912d513a9faa/fedora/Dockerfile
##
## in the start
FROM fedora:30
## Set a default user. Available via runtime flag
RUN useradd docker
RUN dnf install -y \
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
/* d vech(chol(X)) / d vech(X). See mathoverflow.net/a/232129/134083
*
* Args:
* X: symmetric positive definite matrix.
* upper: logical for whether vech denotes the upper triangular part.
*/
// [[Rcpp::export]]
---
title: Faster Multivariate Normal densities with RcppArmadillo and OpenMP
author: Nino Hardt, Dicko Ahmadou
license: GPL (>= 2)
tags: armadillo openmp featured
summary: Fast implementation of Multivariate Normal density using RcppArmadillo and OpenMP.
---
The Multivariate Normal density function is used frequently
for a number of problems. Especially for MCMC problems, fast
# probit-issue-simulate.R
library(Rcpp)
# 'Weibull_mix.cpp' is at
# https://gist.github.com/boennecd/770cd7db6f0fa1b6829311ef005436b7
Rcpp::sourceCpp("Weibull_mix.cpp")
# Simulate potentially right censored outcomes from a generalized survival
# model with a given link function and a mixed weibull distribution as the
# baseline.
#