Skip to content

Commit

Permalink
Fix 0.4 deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
simonster committed Oct 6, 2015
1 parent 682be7b commit 95d0672
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/FusedLasso.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module FusedLassoMod
using StatsBase
import Base: +, -, *
export FusedLasso

# Implements the algorithm described in Johnson, N. A. (2013). A
Expand Down
42 changes: 22 additions & 20 deletions src/Lasso.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Lasso
using Compat
module Util
# Extract fields from object into function locals
# See https://github.com/JuliaLang/julia/issues/9755
Expand Down Expand Up @@ -39,6 +40,19 @@ function Base.A_mul_B!{T}(out::Vector, X::Matrix, coef::SparseCoefficients{T})
out
end

function Base.A_mul_B!{T}(out::Vector, X::SparseMatrixCSC, coef::SparseCoefficients{T})
@extractfields X colptr rowval nzval
fill!(out, zero(eltype(out)))
@inbounds for icoef = 1:nnz(coef)
ipred = coef.coef2predictor[icoef]
c = coef.coef[icoef]
@simd for i = colptr[ipred]:colptr[ipred+1]-1
out[rowval[i]] += c*nzval[i]
end
end
out
end

function Base.dot{T}(x::Vector{T}, coef::SparseCoefficients{T})
v = 0.0
@inbounds @simd for icoef = 1:nnz(coef)
Expand Down Expand Up @@ -109,7 +123,7 @@ if VERSION >= v"0.4-dev+1915"
# iteration quickly.
immutable RandomCoefficientIterator
rng::MersenneTwister
rg::Base.Random.RangeGeneratorInt{Int,Uint}
rg::Base.Random.RangeGeneratorInt{Int,@compat UInt}
coeforder::Vector{Int}
end
const RANDOMIZE_DEFAULT = true
Expand All @@ -123,7 +137,7 @@ else
const RANDOMIZE_DEFAULT = false
end

typealias CoefficientIterator Union(UnitRange{Int}, RandomCoefficientIterator)
typealias CoefficientIterator @compat Union{UnitRange{Int},RandomCoefficientIterator}

# Iterate over coefficients in random order
function Base.start(x::RandomCoefficientIterator)
Expand All @@ -147,7 +161,7 @@ addcoef(x::UnitRange{Int}, icoef::Int) = 1:length(x)+1

## LASSO PATH

type LassoPath{S<:Union(LinearModel, GeneralizedLinearModel),T} <: RegressionModel
type LassoPath{S<:@compat(Union{LinearModel,GeneralizedLinearModel}),T} <: RegressionModel
m::S
nulldev::T # null deviance
nullb0::T # intercept of null model, if one was fit
Expand Down Expand Up @@ -196,14 +210,14 @@ function computeλ(Xy, λminratio, α, nλ)
λ = exp(linspace(logλmax, logλmax + log(λminratio), nλ))
end

function StatsBase.fit{T<:FloatingPoint,V<:FPVector}(::Type{LassoPath},
function StatsBase.fit{T<:AbstractFloat,V<:FPVector}(::Type{LassoPath},
X::AbstractMatrix{T}, y::V, d::UnivariateDistribution=Normal(),
l::Link=canonicallink(d);
wts::Union(FPVector, Nothing)=ones(T, length(y)),
wts::@compat(Union{FPVector,Void})=ones(T, length(y)),
offset::V=similar(y, 0),
α::Number=one(eltype(y)), nλ::Int=100,
λminratio::Number=ifelse(size(X, 1) < size(X, 2), 0.01, 1e-4),
λ::Union(Vector,Nothing)=nothing, standardize::Bool=true,
λ::@compat(Union{Vector,Void})=nothing, standardize::Bool=true,
intercept::Bool=true,
naivealgorithm::Bool=(!isa(d, Normal) || !isa(l, IdentityLink) || size(X, 2) > 5*size(X, 1)),
dofit::Bool=true,
Expand Down Expand Up @@ -280,20 +294,8 @@ function StatsBase.fit{T<:FloatingPoint,V<:FPVector}(::Type{LassoPath},
nullb0 = zero(T)
end

mu = mustart(d, y, wts)
eta = linkfun!(l, similar(mu), mu)
if !isempty(off)
@simd for i = 1:length(eta)
@inbounds eta[i] -= off[i]
end
end

if length(GlmResp.parameters) == 3
# a hack so this works with my hacked up working copy of GLM
rr = GlmResp{typeof(y),typeof(d),typeof(l)}(y, d, l, eta, mu, offset, wts)
else
rr = GlmResp{typeof(y)}(y, d, l, eta, mu, offset, wts)
end
eta = GLM.initialeta!(d, l, similar(y), y, wts, off)
rr = GlmResp{typeof(y),typeof(d),typeof(l)}(y, d, l, eta, similar(eta), offset, wts)
model = GeneralizedLinearModel(rr, cd, false)
end

Expand Down
1 change: 1 addition & 0 deletions src/TrendFiltering.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module TrendFiltering
using StatsBase, ..FusedLassoMod, ..Util
import Base: +, -, *
export TrendFilter

# Implements the algorithm from Ramdas, A., & Tibshirani, R. J. (2014).
Expand Down
10 changes: 5 additions & 5 deletions test/lasso.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ randdist(::Binomial, x) = rand(Bernoulli(x))
randdist(::Poisson, x) = rand(Poisson(x))
function genrand(T::DataType, d::Distribution, l::GLM.Link, nsamples::Int, nfeatures::Int, sparse::Bool)
X, coef = makeX(0.0, nsamples, nfeatures, sparse)
y = linkinv!(l, Array(T, nsamples), X*coef)
y = X*coef
for i = 1:length(y)
y[i] = randdist(d, y[i])
y[i] = randdist(d, linkinv(l, y[i]))
end
(X, y)
end
Expand Down Expand Up @@ -82,14 +82,14 @@ facts("LassoPath") do
# rd = (l.b0 - g.a0)./g.a0
# rd[!isfinite(rd)] = 0
# println(" b0 adiff = $(maxabs(l.b0 - g.a0)) rdiff = $(maxabs(rd))")
@fact l.coefs => roughly(gbeta, 2e-7)
@fact l.b0 => roughly(g.a0, 2e-7)
@fact l.coefs --> roughly(gbeta, 5e-7)
@fact l.b0 --> roughly(g.a0, 2e-5)

# Ensure same number of iterations with all algorithms
if niter == 0
niter = l.niter
else
@fact l.niter => niter
@fact l.niter --> niter
end
end
end
Expand Down

0 comments on commit 95d0672

Please sign in to comment.