Skip to content

Instantly share code, notes, and snippets.

View cscherrer's full-sized avatar

Chad Scherrer cscherrer

View GitHub Profile
@Hirrolot
Hirrolot / tagless-final.rs
Last active April 27, 2024 04:30
Tagless-final encoding of a simple arithmetic language in Rust
trait Interp {
type Repr<T>;
fn lit(i: i32) -> Self::Repr<i32>;
fn add(a: Self::Repr<i32>, b: Self::Repr<i32>) -> Self::Repr<i32>;
}
struct Eval;
impl Interp for Eval {
@mschauer
mschauer / nonparabayes.jl
Last active October 13, 2021 13:07
Non-parametric Bayesian regression in Fourier domain
n = 1000
x = range(0, 1, length=n)
ς = 1.5 # noise level
μ = 3*x.*sin.(2pi*x) # periodic signal in time domain
#μ = 6*sqrt.(abs.(x .- 0.5)) # this one is difficult to estimate
# Model: Signal distorted by white noise
y = μ + ς*randn(n)
@mschauer
mschauer / bouncy.jl
Last active April 3, 2021 10:54
Linear regression, p = 5, n = 10_000_000, with subsampling and approx ML estimate as control variate
using ZigZagBoomerang
using StaticArrays
using LinearAlgebra
using SparseArrays
using Random
using Test
using Statistics
Random.seed!(2)
using StaticArrays
@cscherrer
cscherrer / Julia____-packages.txt
Created February 7, 2020 16:09
A list of Julia packages that are "branded" in the sense of being hosted in a Julia____ organization on Github
JuliaActuary/MortalityTables.jl
JuliaActuary/DynamicPolynomials.jl
JuliaActuary/FixedPolynomials.jl
JuliaActuary/MultivariateBases.jl
JuliaActuary/MultivariateMoments.jl
JuliaActuary/MultivariatePolynomials.jl
JuliaActuary/SemialgebraicSets.jl
JuliaActuary/StaticPolynomials.jl
JuliaActuary/TypedPolynomials.jl
@mschauer
mschauer / example.jl
Last active October 2, 2020 17:20
Stochastic process example with Girsanov likelihood
struct StoProMeasure
θ::Float64
end
struct WienerMeasure
end
function logdensity(P::StoProMeasure, tr, ::WienerMeasure)
som = 0.0
t, x = tr
@cscherrer
cscherrer / logpdfs.jl
Created January 10, 2020 03:45
Log-densities with less code, using MacroTools and SymPy
import PyCall,SymPy
using MLStyle
stats = PyCall.pyimport_conda("sympy.stats", "sympy");
SymPy.import_from(stats)
sym(x) = SymPy.symbols(x);
macro ℓ(expr)
args = @match expr begin
@jiahao
jiahao / aaism.jl
Last active September 24, 2020 04:43
Implementation of the stabilized Type-I Anderson acceleration (AA-I-S-m) algorithm of Zhang, O'Donoghue and Boyd (2018). This implementation solves g(x) = 0 as opposed to f(x) = x, which you obtain from g(x) = f(x) - x.
using Dates: now
using DataFrames
import Base: *, push!
mutable struct AAUpdate{Tu,Tv} # Matrix-free representation of the H matrix
m::Int #:: Size of the AA subspace
u::Vector{Tu} #:: The quantities s-Hỹ (note typo in paper)
v::Vector{Tv} #:: The quantities (H'ŝ)/(ŝ'Hŷ)
end
@mschauer
mschauer / poisson.jl
Last active October 2, 2020 17:20
Inhomog poisson
function sample_poisson(T, λ, λmax)
t = 0.0
tt = zeros(0)
while t <= T
t = t - log(rand())/λmax
if rand() ≤ λ(t)/λmax
push!(tt, t)
end
end
@mschauer
mschauer / treeiter.jl
Last active December 15, 2020 16:13
Graph-topological iterators
using LightGraphs
using LightGraphs.SimpleGraphs: fadj
using BenchmarkTools
using Test
macro twice(body)
quote $(esc(body));$(esc(body)) end
end
struct Done
end
@mschauer
mschauer / mcmc.jl
Created April 11, 2019 13:00
RS after MCMC
using Distributions
K = 10^5
μ1 = 1.0
μ2 = -1.0
σ1 = σ2 = 0.3
args = (μ1 = μ1, σ1 = σ1, μ2 = μ2, σ2 = σ2)
lp(x, args) = log(pdf(Normal(args.μ1, args.σ2), x) + pdf(Normal(args.μ2, args.σ2), x))