Skip to content

Instantly share code, notes, and snippets.

Avatar

Jiahao Chen jiahao

View GitHub Profile
@jiahao
jiahao / aaism.jl
Last active Sep 24, 2020
Implementation of the stabilized Type-I Anderson acceleration (AA-I-S-m) algorithm of Zhang, O'Donoghue and Boyd (2018). This implementation solves g(x) = 0 as opposed to f(x) = x, which you obtain from g(x) = f(x) - x.
View aaism.jl
using Dates: now
using DataFrames
import Base: *, push!
mutable struct AAUpdate{Tu,Tv} # Matrix-free representation of the H matrix
m::Int #:: Size of the AA subspace
u::Vector{Tu} #:: The quantities s-Hỹ (note typo in paper)
v::Vector{Tv} #:: The quantities (H'ŝ)/(ŝ'Hŷ)
end
@jiahao
jiahao / sparsematrixiterator.jl
Last active Dec 4, 2019
Iterate over SparseMatrixCSC stored entries. Implements Julia's new iterator protocol (new as of v0.7) https://julialang.org/blog/2018/07/iterators-in-julia-0.7
View sparsematrixiterator.jl
# Iterate over SparseMatrixCSC stored entries, ignoring stored zeros and
# missing values.
#
# Implements Julia's new iterator protocol (new as of v0.7)
# Ref: https://julialang.org/blog/2018/07/iterators-in-julia-0.7
#
# Jiahao Chen 2019-12-03
#
# MIT License available upon request
#
@jiahao
jiahao / sparselogisticpca.jl
Created Nov 22, 2019
Sparse logistic PCA in Julia - translated from @andland 's implementation https://github.com/andland/SparseLogisticPCA
View sparselogisticpca.jl
using LinearAlgebra
using StatsBase
using StatsFuns
using NaNMath
"x->2x-1 in place"
function twoxm1!(dat; val=0.0)
@inbounds for (i,x) in enumerate(dat)
dat[i] = ifelse(isnan(x), val, 2x-1)
end
View gist:c662f104b1c9319058fe1e564ca54373
### Keybase proof
I hereby claim:
* I am jiahao on github.
* I am jiahao (https://keybase.io/jiahao) on keybase.
* I have a public key ASD8uXwFCTxC_HNYH0M6m_5niip3vql6gQ9nqYuUWnkiiQo
To claim this, I am signing this object:
@jiahao
jiahao / naivebayes.jl
Last active Apr 28, 2019
Multinomial naive Bayes in Julia, allowing for generic numeric types for the conditional probabilities. When using rational numbers, you can calculate exact probabilities without roundoff error.
View naivebayes.jl
struct MultinomialNaiveBayes{T, V<:AbstractVector}
feature_ratios::V
prior_ratio::T
end
"""
fit(MultinomialNaiveBayes, [T,] features, labels, α = 1) -> MNB
fits a `MultinomialNaiveBayes` classifier `MNB` using the
`features` matrix and `labels` vector of `Bool`s.
@jiahao
jiahao / naivebayes.jl
Created Apr 27, 2019
Multinomial naive Bayes in Julia, allowing for generic numeric types for the conditional probabilities. (including rational numbers) that allow you to calculate exact probabilities.
View naivebayes.jl
struct NaiveBayes{T, V<:AbstractVector, M<:AbstractMatrix}
probabilities::M
priors::V
end
train(::Type{NaiveBayes}, T::Type{R}, features, labels, α = 1) where R<:Real =
train(NaiveBayes{T, Vector{T}, Matrix{T}}, features, labels, α)
for (typ, op) in ((Rational, ://), (Real, :/)) @eval begin
function train(::Type{NaiveBayes{T, S, R}},
View atiyah.jl
# Compute the nth elementary symmetric function (elementary symmetric polynomial) of a a 1D iterable x
# This is the THIRD esf
function esf(n, x)
summand = zero(eltype(x))
for j1 = 1:n, j2 = 1:j1-1, j3 = 1:j2-1
summand += x[j1]*x[j2]*x[j3]
end
return summand
end
@jiahao
jiahao / ipm.jl
Created Jul 7, 2016
A simple interior point method implementation. Ref: http://www.maths.ed.ac.uk/~gondzio/reports/mfCS.pdf
View ipm.jl
#Problem-specific thing
dualitygap(x...) = nothing
solveeq(x...) = rand()
############################
#Simple backtracking line search
function linesearch(v, dv, α₀= 1.0, δ = 0.6)
α = α₀
while v + α*dv 0
@jiahao
jiahao / movielens-20m-dl.jl
Created May 15, 2016
Julia script for downloading the MovieLens 20M dataset from http://grouplens.org/datasets/movielens/
View movielens-20m-dl.jl
using CSV
using Nettle
using ZipFile
zfilename = download("http://files.grouplens.org/datasets/movielens/ml-20m.zip")
#TODO check hashes
#md5 = readchomp(open(download("http://files.grouplens.org/datasets/movielens/ml-20m.zip.md5")))
#md5dl = open(zfilename) do f hexdigest(readall(f)) end
@jiahao
jiahao / m012.jl
Last active May 21, 2017
Matvecs with matrices in PLINK v1 BEM format
View m012.jl
import Base: convert, rand, size, getindex, A_mul_B!
immutable PLINK1Matrix <: AbstractMatrix{UInt8}
m :: Int
n :: Int
data :: Array{UInt8}
end
size(M::PLINK1Matrix, i::Int) = i==1 ? M.m : i==2 ? M.n : error()
size(M::PLINK1Matrix) = (M.m, M.n)
You can’t perform that action at this time.