Skip to content

Instantly share code, notes, and snippets.

View bicycle1885's full-sized avatar

Kenta Sato bicycle1885

  • Karolinska Institute
  • Stockholm, Sweden
View GitHub Profile
@bicycle1885
bicycle1885 / dihedral.jl
Last active July 4, 2023 04:20
Dihedral angle
# Public Domain
using LinearAlgebra: cross, dot, norm
"""
dihedral(r1, r2, r3, r4)
Compute the dihedral angle of two planes defined by (`r1`, `r2`, `r3`) and
(`r2`, `r3`, `r4`).
function f1()
function g()
x = 1
return () -> x
end
x = 0
h = g()
return h()
end
@bicycle1885
bicycle1885 / approxsol.jl
Last active November 30, 2021 07:55
Sparse inverse covariance estimation (graphical lasso)
# Fattahi, Salar, and Somayeh Sojoudi. 2019. “Graphical Lasso and Thresholding:
# Equivalence and Closed-Form Solutions.” Journal of Machine Learning Research:
# JMLR. https://www.jmlr.org/papers/volume20/17-501/17-501.pdf.
function approxsol(Σ, λ)
# Residue of Σ relative to λ (see def. 11).
Σres = zeros(size(Σ))
for j in axes(Σ, 2), i in axes(Σ, 1)
if i ≠ j && abs(Σ[i,j]) > λ
Σres[i,j] = Σ[i,j] - λ*sign(Σ[i,j])
end
@bicycle1885
bicycle1885 / genx2cdf.jl
Created November 6, 2021 19:24
Approximation of Generalized Chi-Squared Distribution
using LinearAlgebra
using Distributions
# Liu, Huan, Yongqiang Tang, and Hao Helen Zhang. 2009. “A New Chi-Square
# Approximation to the Distribution of Non-Negative Definite Quadratic Forms in
# Non-Central Normal Variables.” Computational Statistics & Data Analysis 53
# (4): 853–56.
function genx2cdf(A, μ, Σ, t)
# k = 1
AΣᵏ = A
@bicycle1885
bicycle1885 / GillespieAlgorithm.ipynb
Last active February 24, 2021 17:00
Gillespie simulation
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bicycle1885
bicycle1885 / staticlowrankdowndate.jl
Created January 19, 2021 02:20
Static version of lowrankdowndate
# Static version of lowrankdowndate.
# The source code is derived from stdlib/LinearAlgebra/src/cholesky.jl.
# License is MIT (https://julialang.org/license)
using StaticArrays: SMatrix
using LinearAlgebra: LowerTriangular, PosDefException
"""
lowrankdowndate(L, v)
@bicycle1885
bicycle1885 / MultivariateSkewNormal.ipynb
Last active January 6, 2021 10:03
Multivariate skew normal distribution
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bicycle1885
bicycle1885 / channel-mt.jl
Last active January 4, 2021 22:38
Multithreaded channel demonstration
using Base.Threads: @spawn, nthreads, threadid
N = 100
chan = Channel(nthreads()) do chan
for val in 1:N
put!(chan, val)
println("T$(threadid()) => put $(val)")
end
end
@bicycle1885
bicycle1885 / potts.jl
Last active July 25, 2020 15:21
[THIS IS WRONG] Optimized 2D Potts model code (derived from https://nbviewer.jupyter.org/gist/genkuroki/d586036fc52dfbc41d5f7e5b25ec0e4a)
using Random: default_rng, seed!, rand!
using RandomNumbers
using StaticArrays
#using Plots
#using BenchmarkTools
β_crit(q) = log(1 + √q)
rand_potts2d(q, m, n=m, rng=default_rng()) = rand(rng, Base.OneTo(Int8(q)), m, n)
function potts2d_ifelse!(q, s, β, niters, rng)
using LinearAlgebra
function main()
# input data
train_data = read_data("./ml-100k/u.data")
n_user = length(unique([t[1] for t in train_data]))
n_item = length(unique([t[2] for t in train_data]))
# parameters
P, Q = fit(n_user, n_item, train_data)