Skip to content

Instantly share code, notes, and snippets.

View cscherrer's full-sized avatar

Chad Scherrer cscherrer

View GitHub Profile
@cscherrer
cscherrer / sobolrand.jl
Last active June 13, 2022 15:20
A Vector-of-vectors view of Sobol quasirandom Uniform and StdNormal draws
using Sobol, ArraysOfArrays
function sobolrand(n,k, extraskip::Int=0)
ω = SobolSeq(k)
Sobol.skip(ω, n) # recommended in the Sobol.jl README
for _ in 1:extraskip
Sobol.next!(ω)
end
x = Matrix{Float64}(undef, k, n)
@cscherrer
cscherrer / modified-gramschmidt.jl
Created November 30, 2021 00:03
modified GramSchmidt
using LinearAlgebra
# Modify column jᵥ to be orthogonal to column jᵤ
function ortho!(A::AbstractMatrix, jᵥ, jᵤ; startat=1)
# v ← v - projᵤ(v)
nrows = size(A,1)
u_v = 0.0
u_u = 0.0
@inbounds for i in startat:nrows
u_v += A[i,jᵤ] * A[i,jᵥ]
@cscherrer
cscherrer / method-wrappers.jl
Created November 22, 2021 19:09
Method wrappers, by Mason Protter
# Origin:
# https://julialang.zulipchat.com/#narrow/stream/274208-helpdesk-.28published.29/topic/Constraining.20method.20signatures/near/262358928
struct MethodWrapper{F, T <: Tuple, R}
f::F
end
MethodWrapper(f::F, Ts) where {F} = MethodWrapper{F, toTuple(Ts), Any}(f)
MethodWrapper(f::F, (Ts, R)::Pair) where {F} = MethodWrapper{F, toTuple(Ts), R}(f)
toTuple(::Type{T}) where {T} = Tuple{T}
@cscherrer
cscherrer / soss-zigzag.jl
Last active November 4, 2021 14:15
Summarizing ZigZag results
using Soss
using ZigZagBoomerang
using MeasureTheory
using Soss: logdensity, xform, ConditionalModel
using ZigZagBoomerang
using ForwardDiff
using ForwardDiff: gradient!
using LinearAlgebra
@cscherrer
cscherrer / soss-abstractgps.jl
Created October 19, 2021 16:10
Modified Soss tests from AbstractGPs.jl
using Soss, MeasureTheory, AbstractGPs, Test, SampleChainsDynamicHMC
@testset "Soss" begin
@testset "GP regression" begin
k = SqExponentialKernel()
y = randn(3)
X = randn(3, 1)
x = [rand(1) for _ in 1:3]
gp_regression = Soss.@model X begin
@cscherrer
cscherrer / mh.jl
Created October 6, 2021 20:15
Metropolis-Hastings as a Soss model
using Soss, MeasureTheory, Plots
# ℓ: log-posterior density we want to sample
# propose: the Markov kernel for the proposal
# x: current point
mhstep = @model ℓ,propose,x begin
y ~ propose(x)
u ~ Uniform()
accept = log(u) < ℓ(y) - ℓ(x)
return accept ? y : x
@cscherrer
cscherrer / online-gaussian.jl
Last active June 7, 2021 22:22
OnlineStat for log-weighetd Gaussian
using PositiveFactorizations
using LinearAlgebra
using StatsFuns
using Random
using OnlineStatsBase
using Statistics
const OSB = OnlineStatsBase
const bessel = OSB.bessel
@cscherrer
cscherrer / zigzag-soss.jl
Last active April 22, 2022 19:50
ZigZag sampler with a Soss model
using Soss
using ZigZagBoomerang
using Soss: logdensity, xform, ConditionalModel
using ForwardDiff
using ForwardDiff: gradient!
using LinearAlgebra
using SparseArrays
using StructArrays
using TransformVariables
using MeasureTheory
@cscherrer
cscherrer / tuplevectormaps.jl
Created April 20, 2021 22:18
A convenient way to create a new TupleVector from an existing one
using MacroTools: @q
using GeneralizedGenerated
struct TypelevelExpr{T}
expr::Expr
TypelevelExpr(expr::Expr) = new{to_type(expr)}(expr)
end
@gg function with(nt::NamedTuple{N,T}, ::TypelevelExpr{E}) where {N,T,E}
@cscherrer
cscherrer / hypercube.jl
Created April 20, 2021 21:33
`rand` method for "hypercubes", e.g. Sobol sequence
using Sobol
using MeasureTheory
abstract type Hypercube{k} end
struct SobolHypercube{k} <: Hypercube{k}
seq :: SobolSeq{k}
value :: Vector{Float64}
index :: Ref{Int} # start at zero