Skip to content

Instantly share code, notes, and snippets.

View mschauer's full-sized avatar

Moritz Schauer mschauer

View GitHub Profile
@mschauer
mschauer / hiddenmarkov.jl
Created March 28, 2021 10:15
Parameter inference for discrete hidden Markov model using autodial
using Random, LinearAlgebra, Distributions, StatsBase
using StaticArrays
# use representation x*exp(l) to compute the likelihood without underflow
struct Exp{T} <: Number
x::T
l::T
end
Exp(x::T) where {T<:Real} = Exp(x, zero(x))
@mschauer
mschauer / sparsezigzag.jl
Last active March 16, 2021 11:09
Simplistic ZigZag for Soss
using Soss: logdensity, xform, ConditionalModel
using ZigZagBoomerang
using ForwardDiff
using ForwardDiff: gradient!
using LinearAlgebra
using SparseArrays
using StructArrays
using TransformVariables
@mschauer
mschauer / chain.jl
Created March 10, 2021 10:25
Sketch BFFG markov chain
xnew = sample(A[x, :])
struct TransitionKernel
A::Matrix
end
(kappa::TransitionKernel)(x) = kappa.A[x, :]
A = [0.5 0.5 0
0 0.3 0.7
@mschauer
mschauer / basemeasure.jl
Last active March 7, 2021 23:41
Get reference measure
export basemeasure
import MeasureTheory
function MeasureTheory.basemeasure(c::ConditionalModel{A,B,M}, x=NamedTuple()) where {A,B,M}
_basemeasure(M, Model(c), argvals(c), observations(c), x)
end
export sourceBasemeasure
@mschauer
mschauer / condbin.jl
Last active March 3, 2021 15:27
Guided processes for conditional mixed Bernoulli
using Mitosis
using MeasureTheory
using Random
using Test
n = 30
ρ = rand(n)
struct PlusBernoulli{S,T} <: MeasureTheory.AbstractMeasure
x::S
@mschauer
mschauer / zigzagregression.jl
Created February 19, 2021 13:00
1 Million data points
using ZigZagBoomerang
using Distributions
using SparseArrays
using LinearAlgebra
using Random
Random.seed!(1)
# Million data points, 5 parameters + intercept
N = 1000000
k = 5;
X = randn(N,k);
@mschauer
mschauer / bigirds.jl
Created January 11, 2021 10:40
High precision IDR(s)
using IterativeSolvers
using LinearAlgebra
A = rand(-10:10, 16, 16) + 5*LinearAlgebra.I
x = rand(-10:10, 16)
b = A*x
y = idrs(BigFloat.(A), BigFloat.(b), s=4, tol=1e-100, maxiter=100, verbose=true)
using Distributions, Statistics, LinearAlgebra
## simulate sample trajectory
μ = [1.0, 0.0]
Σ = [0.5 0.0
0.0 0.5]
x0 = rand(MultivariateNormal(μ, Σ)) #start point
β = [0.0, 0.0]
@mschauer
mschauer / georgia.jl
Last active May 20, 2022 16:03
Will Georgia become blue (Brownian semimartingale model for vote difference vs percentage)
# Fit a Brownian motion with drift to and scale to
# Georgia election data:
# fraction of total count of votes vs difference in votes
data = [0.9222672139181572 103896.10389610389
0.9267802009311443 87662.33766233765
0.9277217593727027 85714.28571428571
0.9285683655966674 83116.88311688312
0.9299797843665768 79220.77922077922
0.9310169076206811 78571.42857142857
@mschauer
mschauer / lecture11.jl
Created September 30, 2020 10:24
One sided test for hypothesis about proportion
#=
Newborn babies are more likely to be boys than girls.
A random sample found 13 173 boys were born among 25 468
newborn children. Population: All children born that time period. Alternatively,
the unknown p could be the unknown probability.
=#
b = 13173
n = 25486