Skip to content

Instantly share code, notes, and snippets.

@slwu89
slwu89 / stochasticEuler.R
Created November 16, 2018 16:22
time changes for continuous time vs. Euler approximation of stochastic jump processes
# continuous time rate
R <- 1/5
# mean time to next event in continuous time
mean(rexp(n = 1e6,rate = R))
# get a discrete time rate to plug into Euler approximation of continuous time process
get_r <- function(R,dt){
log(1 + R*dt) / dt
}
@slwu89
slwu89 / parameters.cpp
Created November 20, 2018 02:17
stupid simple way to store heterogeneous parameters for c++ projects.
/* standard includes */
#include <stdio.h>
#include <iostream>
/* hash-table */
#include <unordered_map>
#include <string> /* for keys */
class parameters {
public:
@slwu89
slwu89 / better_logger.cpp
Last active December 17, 2018 21:08
flexible logger class
//
// main.cpp
// test2
// a slightly less horrible logging class
//
// Created by Sean Wu on 12/17/18.
// Copyright © 2018 Sean Wu. All rights reserved.
//
/* ######################################################################
@slwu89
slwu89 / llist-attr.cpp
Created April 25, 2019 17:38
store humans in linked-list and some attributes of them outside, delete multiple at once
//
// main.cpp
// test_humans
//
// Created by Sean Wu on 5/31/18.
// Copyright © 2018 Sean Wu. All rights reserved.
//
#include <iostream>
#include <string>
@slwu89
slwu89 / widgetlist.cpp
Last active April 30, 2019 03:19
unique_ptr<widget> in a list, modify them
// Example program
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <memory>
typedef struct widget {
size_t i;
@slwu89
slwu89 / mvhyper.cpp
Created May 28, 2019 21:24
multivar hypergeometric in rcpp
// destination: array to fill the drawn "balls"
// source: number of "balls" in each "urn"
// n: number of draws to take
// k: number of "urns"
void rmhyper(int* destination, int const* source, int n, int k){
int sum, x, y;
size_t i;
if(n < 0 || k < 0){Rcpp::stop("Invalid parameters of distribution");}
// total number of "balls"
@slwu89
slwu89 / melancholia_i.R
Created August 8, 2019 02:04
Melancholia I has a matrix on the wall. This finds its stationary distribution. (https://en.wikipedia.org/wiki/Melencolia_I)
xx <- matrix(c(16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1),nrow = 4,ncol = 4,byrow = T)
xxx <- xx/rowSums(xx)
xeig <- eigen(xxx)
xvecs <- xeig$vectors
lvecs <- MASS::ginv(xvecs)
pi_eq <- lvecs[1,]/sum(lvecs[1,])
pi_eq
@slwu89
slwu89 / bridge1.cpp
Created August 20, 2019 01:01
bridge-ish pattern with c++ when you want to have the different implementations store variable data using void*
#include <iostream>
#include <iomanip>
#include <string>
#include <string>
#include <memory>
class impl_base {
public:
/* ctor & dtor */
@slwu89
slwu89 / shared_pointer_test.cpp
Created December 4, 2019 20:32
checking that shared pointers are working right when mtple objects are sharing one resource, even when the first goes out of scope
//
// main.cpp
// test_simple
//
// Created by Sean Wu on 4/10/19.
// Copyright © 2019 Sean Wu. All rights reserved.
//
#include <memory>
#include <functional>
@slwu89
slwu89 / unique_array.cpp
Created March 12, 2020 19:43
how to get unique elements and how many times they appear in an array of ints (for C, but compiled with C++)
// Example program
#include <iostream>
#include <string>
int main(void) {
int arr[13] = {1, 2, 2, 123121, 123121, 3, 5, 6 , 7, 7, 14, 2, 16};
int len = 13;
int unique[len];