Skip to content

Instantly share code, notes, and snippets.

View mschauer's full-sized avatar

Moritz Schauer mschauer

View GitHub Profile
@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
@mschauer
mschauer / kalman.jl
Created September 4, 2020 12:30
Differentiable Kalman filter
using GaussianDistributions
using GaussianDistributions: correct, ⊕
using LinearAlgebra
using Statistics
lchol(a) = cholesky(a).L
llikelihood(yres, S) = GaussianDistributions.logpdf(Gaussian(zero(yres), Symmetric(S)), yres)
@mschauer
mschauer / sampler.jl
Created August 12, 2020 14:24
Linear forward filtering backward smoothing/sampling with Zygote
# Linear state space system forward filtering backward sampling with Zygote
using LinearAlgebra
using GaussianDistributions
using GaussianDistributions: ⊕, logpdf
using StaticArrays
using Statistics
using Zygote
@mschauer
mschauer / partial.jl
Created July 5, 2020 17:39
Partial derivatives
using ForwardDiff: derivative
partiali(f, x, i) = ForwardDiff.partials(f([Dual{}(x[j], 1.0*(i==j)) for j in eachindex(x)]))[]
struct ∂{k, F} <: Function
f::F
∂{k}(f::F) where {k, F} = new{k, F}(f)
end
function (pd::∂{k})(args...) where {k}
@mschauer
mschauer / gammabridge.jl
Last active June 10, 2020 07:46
Sample a Gamma bridge with guided proposals
using Distributions
using Makie
using Random
g(x, α, β) = pdf(Gamma(α, 1/β), x)
lg(x, α, β) = logpdf(Gamma(α, 1/β), x)
Random.seed!(1)
T = 100
@mschauer
mschauer / animate.jl
Last active June 1, 2020 08:52
SPDE menetekel
# ] add https://github.com/mschauer/BridgeSPDE.jl
# will run in the parent directory
# of the script location and create output folder there
cd(joinpath(@__DIR__, ".."))
using SparseArrays
using BridgeSPDE
using FileIO
@mschauer
mschauer / zagboom.jl
Last active July 2, 2020 07:57
Zig zag and Boomerang reference implementation
# Zig zag and Boomerang reference implementation
"""
poisson_time(a,b,u)
obtaining waiting time for inhomogeneous Poisson Process
with rate of the form λ(t) = (a + b*t)^+, `a`,`b` ∈ R, `u` uniform random variable
"""
function poisson_time(a,b,u)
if b > 0
if a < 0