Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tpapp
tpapp / DynamicHMC_DiffEq.jl
Created August 13, 2019 08:33
MWE for DiffEq AD mismatch
#####
##### Self-contained example to dissect broken tests in
##### https://github.com/JuliaDiffEq/DiffEqBayes.jl/blob/e033307768892e2f7242ae0aab3e09ec4819c11b/test/dynamicHMC.jl
#####
##### NOTE: you need DynamicHMC#master and LogDensityProblems#0.9.x for this to work
####
#### part that just relies on the DiffEq ecosystem
####
@tpapp
tpapp / Manifest.toml
Last active November 28, 2018 11:01
Pkg3 issue manifest and project files
[[AltDistributions]]
deps = ["ArgCheck", "Distributions", "DocStringExtensions", "ForwardDiff", "LinearAlgebra", "Parameters", "Random", "SpecialFunctions", "StatsFuns"]
path = "/home/tamas/code/julia/AltDistributions"
uuid = "875e7ca2-ab9d-5828-93ae-a098eac53262"
version = "0.1.0"
[[ArgCheck]]
deps = ["Random", "Test"]
git-tree-sha1 = "9e3bff61bc46d3420d903b53ad07f85325fddc28"
uuid = "dce04be8-c92d-5529-be00-80e4d2c0e197"
@tpapp
tpapp / code.jl
Created October 31, 2018 10:58
dynamicHMC with diffeq MWE
using OrdinaryDiffEq, DynamicHMC, TransformVariables, LogDensityProblems, MCMCDiagnostics,
Parameters, Distributions, LinearAlgebra
struct BayesianODEProblem{F, U, S, T, P, N, D}
f::F # ODE
u::U # initial value
timespan::S # timespan as a tuple
timepoints::T # evaluated at these points
logprior::P # callable, return the log prior of parameters
noise::N # a noise distribution for observations
@tpapp
tpapp / make-REQUIRE.jl
Created September 24, 2018 12:51
make-REQUIRE.jl
#!/usr/bin/env julia
######################################################################
# Overwrite REQUIRE using dependency information from Project.toml.
#
# Call from the root of the package repository.
#
# Has some basic sanity checks, but **use at your own risk**, `REQUIRE`
# will be overwritten.
#
# The purpose of this script is to appease attobot, until
@tpapp
tpapp / mycollect.jl
Last active September 20, 2018 07:56
MWE for collecting using a functional style
# define an iterator with unknown type and length
struct MyItr N::Int end
Base.IteratorSize(::Type{MyItr}) = Base.SizeUnknown()
Base.IteratorEltype(::Type{MyItr}) = Base.EltypeUnknown()
function Base.iterate(itr::MyItr, state = 1)
if state > itr.N
nothing
else
val = state == 200 ? missing : state
(val, state + 1)
# DiffEq tools
using DiffEqBayes, OrdinaryDiffEq, ParameterizedFunctions, RecursiveArrayTools
# clone from github.com/tpapp
using DynamicHMC, MCMCDiagnostics, DiffWrappers, ContinuousTransformations
using Parameters, Distributions, Optim
f1 = @ode_def_nohes LotkaVolterraTest1 begin
dx = a*x - b*x*y
@tpapp
tpapp / vectriu.jl
Created December 14, 2017 19:06
upper triangle of matrix as a vector
# see https://discourse.julialang.org/t/vector-of-upper-triangle/7764
function vec_triu(M::AbstractMatrix{T}) where T
m, n = size(M)
m == n || throw(error("not square"))
l = n*(n+1) ÷ 2
v = Vector{T}(l)
k = 0
for i in 1:n
v[k + (1:i)] .= M[1:i, i]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<!-- Created by htmlize-1.51 in inline-css mode. -->
<html>
<head>
<title>*scratch*</title>
</head>
<body style="color: #262626; background-color: #ded6c5;">
<pre>
<span style="color: #008b45;">;; </span><span style="color: #008b45;">This buffer is for text that is not saved, and for Lisp evaluation.
</span><span style="color: #008b45;">;; </span><span style="color: #008b45;">To create a file, visit it with &lt;open&gt; and enter text in its buffer.
@tpapp
tpapp / read_UInt8_lines.jl
Last active September 28, 2017 10:13
reading UInt8 lines from a gzip compressed file in Julia
######################################################################
# context: reading UInt8 lines from a gzipped stream
# - the real dataset has about 5e10 lines, this is a self-contaned MWE
# - the real dataset lines are then processed, this MEW is just about optimizing reading
# - line length can be bounded (relevant for buffered reading)
######################################################################
using CodecZlib
"""
@tpapp
tpapp / log1p_error.jl
Last active September 13, 2017 10:25
log1p accuracy in Julia (compared with Base.Math.JuliaLibm)
# consistent random numbers
srand(UInt32[0xfd909253, 0x7859c364, 0x7cd42419, 0x4c06a3b6])
"""
err(x, [prec])
Return two values, which are the log2 relative errors for calculating
`log1p(x)`, using `Base.log1p` and `Base.Math.JuliaLibm.log1p`.
The errors are calculated by compating to `BigFloat` calculations with the given