Skip to content

Instantly share code, notes, and snippets.

View baggepinnen's full-sized avatar
🌴

Fredrik Bagge Carlson baggepinnen

🌴
View GitHub Profile
function testsleep()
sleeptimes = logspace(-3,1,50)
actual_sleeptimes = map(sleeptimes) do st
@elapsed sleep(st)
end
sleeptimes, actual_sleeptimes
end
sleeptimes, actual_sleeptimes = testsleep()
diff = actual_sleeptimes-sleeptimes
plot(sleeptimes, [diff diff./sleeptimes], xlabel="Sleeptimes", ylabel=["Absolute difference" "Relative difference"],
using ControlSystems
G = tf(1,[1,1])
bodeplot(G)
nyquistplot(G)
nicholsplot(G, linewidth=5)
lsimplot(G, 5+0.1randn(100), 0:0.1:9.9)
pzmap(G)
gangoffourplot(G,G)
G = tf(1,[1,1])
@baggepinnen
baggepinnen / flux_juno_segfault.jl
Created November 2, 2017 13:19
This script causes a segfault, sometimes it as to be run twice to cause it. Julia v0.6.1
using Flux
using Flux: back!, truncate!, treelike, train!, mse
N = 200
n = 2
function generate_data()
A = randn(n,n)
A = expm(A - A')
A = 0.999A
A = [0.999 1; 0 0.8]
@baggepinnen
baggepinnen / runmany.jl
Last active November 10, 2017 13:25
A nice way to run many experiments in parallel
@sync begin # Enusres that both operations below are finished before leaving this scope
@async begin # Enusres that the last running job does not stop the next parallel operation from starting
info("Launching forward only")
result1 = pmap(35:-2:5) do it # Start heaviest job (35) first
do_something(it) # Execution time proportional to it
end
end
@async begin
info("Launching forward inverse")
@baggepinnen
baggepinnen / mapif.jl
Last active May 29, 2019 10:50
Compiler pass to translate branching code to map over function
#I would like to use Cassette.jl to implement a compiler pass. The task is to (conditioned on types) translate code that contains a branch into a map over a function that contains the branch. An example of the transformation I would like to do is from
code = Meta.@lower if x > 0
return x^2
else
return -x^2
end
#to
code2 = Meta.@lower map(x.particles) do x
if x > 0
return x^2
@baggepinnen
baggepinnen / AlphaStable.jl
Created December 27, 2019 08:48
An implementation of an alpha stable distribution in Julia
using StatsBase, Distributions
Base.@kwdef struct AlphaStable{T} <: Distributions.ContinuousUnivariateDistribution
α::T = 1.5
β::T = 0.0
scale::T = 1.0
location::T = 0.0
end
@baggepinnen
baggepinnen / exploding_ram.jl
Created March 10, 2020 10:17
Filed edited while ram consumption exploded
"""
γ, u, v = sinkhorn(C, a, b; β=1e-1, iters=1000)
The Sinkhorn algorithm. `C` is the cost matrix and `a,b` are vectors that sum to one. Returns the optimal plan and the dual potentials. See also [`IPOT`](@ref).
"""
function sinkhorn(C, a, b; β=1e-1, iters=1000)
K = exp.(.-C ./ β)
v = one.(b)
u = a ./ (K * v)
v = b ./ (K' * u)
@baggepinnen
baggepinnen / covid.jl
Last active April 9, 2020 00:23
Covid model
cd(@__DIR__)
using Pkg
pkg"activate ."
# pkg"add Turing, NamedTupleTools, MonteCarloMeasurements, StatsPlots"
# pkg"dev Turing2MonteCarloMeasurements"
using Turing, Turing2MonteCarloMeasurements, NamedTupleTools, MonteCarloMeasurements
## Some helper functions stolen from Soss ======================================
@baggepinnen
baggepinnen / mcmodel.jl
Created April 9, 2020 01:00
Soss Markov Chain model
using Soss
struct MarkovChain{P,D}
pars :: P
dist :: D
end
function Distributions.logpdf(chain::MarkovChain, x::AbstractVector{X}) where {X}
@inbounds x1 = (pars=chain.pars,state=x[1])
length(x) == 1 && return logpdf(chain.dist, x1.state)
@baggepinnen
baggepinnen / walk_julia_files.jl
Created April 23, 2020 06:48
Search all julia files for regexes
using Printf, Base.Iterators
root = "/home/$(ENV["USER"])/.julia/dev/"
# root = "/home/$(ENV["USER"])/.julia/packages/"
const regs = [
r"(\w+)\s?->\w+\(\1\)[,\b]", # Finds x->f(x)
r"\w+\s?==\s?(true|false)", # Finds x == true
]
function findem(root, regs; extension=".jl", pad=80)