1010
1111# multiplier stepdown
1212function 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
1616end
1717
1818function 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
2424end
2525
26+
2627function 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.)
4748end
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
7173end
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 ))
7577end
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
116119end
117120
118- function isotonic_regression (y:: Vector{Float64 } )
121+ function isotonic_regression {T<:AbstractFloat} (y:: Vector{T } )
119122 isotonic_regression (y, ones (y))
120123end
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
139142end
140-
0 commit comments