Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / NHPP.R
Created November 12, 2018 02:49
simulation of inhomogeneous poisson process
# Simulation of NHPP (CTDE style)
# based on: https://freakonometrics.hypotheses.org/724
# analytical functions from Mathematica
rm(list=ls());gc()
# intensity function
lambda <- function(x){
100*(sin(x*pi)+1)
}
@slwu89
slwu89 / RcppFns.cpp
Created October 16, 2018 20:21
useful functions in Rcpp
/* the "vals" part of R's rle (run-length encoding) */
Rcpp::IntegerVector rle_vals(const Rcpp::IntegerVector& x){
int n = x.size();
/* y */
Rcpp::IntegerVector head = x[Rcpp::seq(1,n-1)];
Rcpp::IntegerVector tail = x[Rcpp::seq(0,n-2)];
Rcpp::LogicalVector y = head != tail;
@slwu89
slwu89 / Rstats.R
Last active October 19, 2018 22:13
kernel smoothing given a set of points (produces ECDF, PMF, smoothed CDF and differentiated smoothed PDF) , ... and other useful R stats-y stuff
smooth_kernels <- function(distances, tol = .Machine$double.eps^0.75){
d_ecdf <- stats::ecdf(distances)
d_knots <- stats::knots(d_ecdf)
d_pmf <- vapply(d_knots, function(x,tol){
d_ecdf(x+.Machine$double.eps^0.75) - d_ecdf(x-.Machine$double.eps^0.75)
}, numeric(1), tol = tol)
# might want to check into isotonic regression here to force monotonic increasing fn for smooth CDF
@slwu89
slwu89 / klemens.R
Last active October 3, 2018 01:43
examples for Klemens paper in R
################################################################################
# Definition 4
################################################################################
norm_for_Lp <- function(x,p){
dnorm(x = x,mean = p,sd = 1,log = FALSE)
}
Lp <- function(p){
integrate(f = norm_for_Lp,lower = -Inf,upper = Inf,p=p)$value
@slwu89
slwu89 / release.cpp
Last active July 24, 2018 22:57
mgdrive releases without reallocating memory
//
// main.cpp
// main_only_test
//
// Created by Sean Wu on 7/24/18.
// Copyright © 2018 Sean Wu. All rights reserved.
//
#include <iostream>
#include <memory>
@slwu89
slwu89 / sortVecOfPointers.cpp
Created July 13, 2018 23:27
how to sort a vector of pointers to template class
/*
* still need to figure out how to more the lambda function into the class as a friend or something,
* it will be annoying to write lambdas all over the code...
*/
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
@slwu89
slwu89 / templateConsOverload.cpp
Created July 9, 2018 22:19
constructor overloading for template classes
// Example program
#include <iostream>
#include <string>
/* Release event */
template <typename T>
class release {
public:
release(T x); /* constructor */