Skip to content

Instantly share code, notes, and snippets.

@tpapp
tpapp / DynamicHMC_DiffEq.jl
Created Aug 13, 2019
MWE for DiffEq AD mismatch
View DynamicHMC_DiffEq.jl
#####
##### 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 Nov 28, 2018
Pkg3 issue manifest and project files
View Manifest.toml
[[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 Oct 31, 2018
dynamicHMC with diffeq MWE
View code.jl
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 Sep 24, 2018
make-REQUIRE.jl
View 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 Sep 20, 2018
MWE for collecting using a functional style
View mycollect.jl
# 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)
View diffeqhmc.jl
# 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 Dec 14, 2017
upper triangle of matrix as a vector
View vectriu.jl
# 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]
View buffer.html
<!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 Sep 28, 2017
reading UInt8 lines from a gzip compressed file in Julia
View read_UInt8_lines.jl
######################################################################
# 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 Sep 13, 2017
log1p accuracy in Julia (compared with Base.Math.JuliaLibm)
View log1p_error.jl
# 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