Skip to content

Commit a5efd5d

Browse files
Merge pull request #17 from juliangehring/cleanup
Minor formatting and syntax changes
2 parents c0f3ee2 + 527fe86 commit a5efd5d

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

src/pi0-estimators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868
function bootstrap_pi0{T<:AbstractFloat,S<:AbstractFloat}(pValues::Vector{T}, lambda::Vector{S} = [0.05:0.05:0.95;], q::S = 0.1)
6969
#validPValues(pValues)
7070
n = length(pValues)
71-
w = Int[sum(pValues .>= l) for l in lambda] ## TODO: check >= or >
71+
w = [sum(pValues .>= l) for l in lambda] # TODO: check if >= or >
7272
pi0 = w ./ n ./ (1. - lambda)
7373
min_pi0 = quantile(pi0, q)
7474
mse = (w ./ (n.^2 .* (1. - lambda).^2 )) .* (1. - w/n) + (pi0 - min_pi0).^2

src/utils.jl

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ end
1010

1111
# multiplier stepdown
1212
function stepdown!{T<:AbstractFloat}(sortedPValues::Vector{T}, multiplier::Function, n::Integer = length(sortedPValues))
13-
stepfun(p::T, i::Int, n::Int) = p*multiplier(i,n)
13+
stepfun(p::T, i::Int, n::Int) = p * multiplier(i, n)
1414
general_stepdown!(sortedPValues, stepfun, n)
1515
return sortedPValues
1616
end
1717

1818
function general_stepdown!{T<:AbstractFloat}(sortedPValues::Vector{T}, stepfun::Function, n::Integer = length(sortedPValues))
1919
sortedPValues[1] = stepfun(sortedPValues[1], 1, n)
2020
for i in 2:n
21-
sortedPValues[i] = max(sortedPValues[i-1], stepfun(sortedPValues[i],i, n))
21+
sortedPValues[i] = max(sortedPValues[i-1], stepfun(sortedPValues[i], i, n))
2222
end
2323
return sortedPValues
2424
end
2525

26+
2627
function reorder{T<:Number}(values::Vector{T})
2728
newOrder = sortperm(values)
2829
oldOrder = sortperm(newOrder)
@@ -47,34 +48,36 @@ function isin{T<:Real}(x::Vector{T}, lower::Real = 0., upper::Real = 1.)
4748
end
4849

4950

50-
function isotonicregression(y::Array{Float64,1},w::Array{Float64,1})
51-
#todo: ignore zero weights
52-
y=copy(y)
53-
w=copy(w)
54-
m = length(y)
55-
cnts = ones(Int64,m)
56-
i = 2
57-
# ... not most efficient way but could be fun to (ab)use iterator protocol
58-
while (!done(y,i))
59-
if y[i]<y[i-1]
60-
y[i-1]=(w[i]*y[i]+w[i-1]*y[i-1])/(w[i]+w[i-1])
61-
w[i-1]=w[i]+w[i-1]
62-
cnts[i-1] += cnts[i]
63-
deleteat!(y,i)
64-
deleteat!(w,i)
65-
deleteat!(cnts,i)
66-
i = max(i-2,1)
51+
function isotonic_regression_reference{T<:AbstractFloat}(y::Vector{T}, w::Vector{T})
52+
#todo: ignore zero weights
53+
y = copy(y)
54+
w = copy(w)
55+
m = length(y)
56+
cnts = ones(Int64, m)
57+
i = 2
58+
# ... not most efficient way but could be fun to (ab)use iterator protocol
59+
while !done(y, i)
60+
if y[i] < y[i-1]
61+
y[i-1] = (w[i]*y[i]+w[i-1]*y[i-1])/(w[i]+w[i-1])
62+
w[i-1] = w[i]+w[i-1]
63+
cnts[i-1] += cnts[i]
64+
deleteat!(y, i)
65+
deleteat!(w, i)
66+
deleteat!(cnts, i)
67+
i = max(i-2, 1)
68+
end
69+
i += 1
6770
end
68-
i += 1
69-
end
70-
yisotonic = vcat([y[idx]*ones(Float64,cnt) for (idx,cnt) in enumerate(cnts)]...)
71+
yisotonic = vcat([y[idx]*ones(Float64, cnt) for (idx, cnt) in enumerate(cnts)]...)
72+
return yisotonic
7173
end
7274

73-
function isotonicregression(y::Array{Float64,1})
74-
isotonicregression(y, ones(Float64, length(y)))
75+
function isotonic_regression_reference{T<:AbstractFloat}(y::Vector{T})
76+
isotonic_regression_reference(y, ones(y))
7577
end
7678

77-
function isotonic_regression(y::Vector{Float64}, weights::Vector{Float64})
79+
80+
function isotonic_regression{T<:AbstractFloat}(y::Vector{T}, weights::Vector{T})
7881
n = length(y)
7982
if n <= 1
8083
return y
@@ -115,12 +118,12 @@ function isotonic_regression(y::Vector{Float64}, weights::Vector{Float64})
115118
return y
116119
end
117120

118-
function isotonic_regression(y::Vector{Float64})
121+
function isotonic_regression{T<:AbstractFloat}(y::Vector{T})
119122
isotonic_regression(y, ones(y))
120123
end
121124

122125

123-
function grenander(pv::Vector{Float64})
126+
function grenander{T<:AbstractFloat}(pv::Vector{T})
124127
pv_sorted = sort(pv)
125128
## ecdf that handles duplicated values
126129
pv_sorted_unique, counts = rle(pv_sorted)
@@ -137,4 +140,3 @@ function grenander(pv::Vector{Float64})
137140

138141
return pv_sorted_unique, f, F
139142
end
140-

test/test-grenander.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ using Base.Test
3838
@testset "Isotonic regression" begin
3939

4040
#pooled adjacent violators example page 10 robertson
41-
isotonicregression = MultipleTesting.isotonicregression
42-
@test isotonicregression([22.5; 23.333; 20.833; 24.25], [3.0;3.0;3.0;2.0]) [22.222; 22.222; 22.222; 24.25]
41+
isotonic_regression_reference = MultipleTesting.isotonic_regression_reference
42+
@test isotonic_regression_reference([22.5; 23.333; 20.833; 24.25], [3.0;3.0;3.0;2.0]) [22.222; 22.222; 22.222; 24.25]
4343

4444
#if input already ordered, then output should be the same
45-
@test isotonicregression([1.0; 2.0; 3.0]) [1.0; 2.0; 3.0]
46-
@test isotonicregression([1., 41., 51., 1., 2., 5., 24.], [1., 2., 3., 4., 5., 6., 7.]) [1.0, 13.95, 13.95, 13.95, 13.95, 13.95, 24]
45+
@test isotonic_regression_reference([1.0; 2.0; 3.0]) [1.0; 2.0; 3.0]
46+
@test isotonic_regression_reference([1., 41., 51., 1., 2., 5., 24.], [1., 2., 3., 4., 5., 6., 7.]) [1.0, 13.95, 13.95, 13.95, 13.95, 13.95, 24.]
4747

4848
# single value or empty vector remains unchanged
4949
r = rand(1)

0 commit comments

Comments
 (0)