Skip to content

Instantly share code, notes, and snippets.

@slwu89
slwu89 / categoricalMLE.R
Created May 25, 2017 00:36
how to do MLE of multinomial distribution for categorical data
###############################################################
#
# MLE example with categorical data
#
###############################################################
# make up some 'data'; c(0.1,0.2,0.3,0.4) are the 'real' parameters
data = rmultinom(n = 40,size = 4,prob = c(0.1,0.2,0.3,0.4))
# the log-likelihood of the first sample
@slwu89
slwu89 / discourse_jump.jl
Last active December 8, 2023 19:45
acset vs dicts for jump model generation
# from question: https://discourse.julialang.org/t/speeding-up-jump-model-creation-with-sets-that-depend-on-other-indexes/107333
using Catlab, JuMP
using BenchmarkTools
@present NetSch(FreeSchema) begin
(V,E)::Ob
src::Hom(E,V)
tgt::Hom(E,V)
TimeType::AttrType
@slwu89
slwu89 / stochAD.jl
Last active July 11, 2023 19:29
sandbox for SIR particle filter
using StochasticAD, Distributions, StaticArrays, Plots
using Zygote, ForwardDiff
# map rates to probabilities
function rate_to_proportion(r, t)
1-exp(-r*t)
end
# dynamic part of SIR model
function sir_dyn_mod(x, u0, p, δt)
@slwu89
slwu89 / pwexp.R
Created March 16, 2023 18:27
piecewise exp firing time sampling
rpwexp <- function(n, failRates) {
end_time <- cumsum(failRates$duration)
n_rate <- nrow(failRates)
t <- 0
tau <- Tk <- rep(0, n)
Pk <- rexp(n = n)
notfired <- rep(TRUE, n)
for (i in 1:n_rate) {
delta_tk <- (Pk[notfired] - Tk[notfired]) / failRates$rate[i]
tau[notfired] <- t + delta_tk
@slwu89
slwu89 / counting_mc.jl
Created February 26, 2023 18:51
counting up and down in the most extravagant way possible
using MeasureTheory
using Plots
using Random
function counting(x)
Dirac([x[1]+1, x[2]-1])
end
mc = Chain(x -> counting(x), Dirac([0,0]))
r = rand(mc)
@slwu89
slwu89 / secrets.jl
Created January 17, 2023 02:25
julia tricks and useful things
# 1. ----------
# "guess what an appropriate container eltype would be for storing results of `f`..."
Base.promote_op
f() = true
Base.promote_op(f)
# 2. ----------
# take apart whatever comes in
dump(f)
@slwu89
slwu89 / pf_fsm.tex
Created May 9, 2018 20:25
how to make finite state machine in tikz (latex)
\documentclass[12pt]{article}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
semithick]
@slwu89
slwu89 / urchins.jl
Last active December 12, 2022 20:15
the Simon Wood urchin example in Julia; using the Laplace approximation to fit models with random effects, needs Julia version >= 1.8 for typed globals (https://julialang.org/blog/2022/08/julia-1.8-highlights/#typed_globals)
using Distributions
using ForwardDiff
using Optim
using LineSearches # https://github.com/JuliaNLSolvers/Optim.jl/issues/713
# logdet
using LinearAlgebra
# get the data
@slwu89
slwu89 / foward_ad.R
Last active October 19, 2022 18:11
foward mode autodiff in R, completing the unfinished example in Simon Wood's "Core Statistics" 5.5.3
rm(list=ls());gc()
ad <- function(x,diff = c(1,1)) {
## create class "ad" object. diff[1] is length of grad
## diff[2] is element of grad to set to 1.
grad <- rep(0,diff[1])
if (diff[2]>0 && diff[2]<=diff[1]) grad[diff[2]] <- 1
attr(x,"grad") <- grad
class(x) <- "ad"
x
@slwu89
slwu89 / cubicspline.jl
Last active October 15, 2022 23:17
penalized cubic splines in julia
using Random, Distributions
using LinearAlgebra
using Statistics
using Plots
using JuMP, Ipopt
using RCall
# initial fixup and optional (not here) scaling https://github.com/cran/mgcv/blob/a534170c82ce06ccd8d76b1a7d472c50a2d7bbd2/R/smooth.r#L1602
# scaling here: https://github.com/cran/mgcv/blob/a534170c82ce06ccd8d76b1a7d472c50a2d7bbd2/R/smooth.r#L3757
function penalty_cr(D,B)