In

http://nbviewer.ipython.org/gist/dmbates/9746197

I describe randomization tests for a completely randomized design and how 
the 'combinations' iterator makes evaluating the complete randomization 
distribution so easy, up to the point where the combinatorial complexity 
makes things too slow.

Another type of randomization test applied to a paired design is based on 
the sum of the observed differences between pairs.  The observed difference 
is compared to the population of sums obtained by assigning arbitrary signs 
to the observed differences.  If the means of the two groups were equal the 
sum of the observed differences should look similar to this population of 
sums because the labels of the two groups would be arbitrary.

To enumerate the sums of k differences with all possible combinations of 
signs I need to iterate over the 2^k possible subsets of signs to flip.  My 
guess is that simply iterating over unsigned ints  from 0 to 2^k-1 and 
using the bit patterns in these numbers is the way to go.  Is there a good 
idiom for doing this?  Currently I use shifting and masking to determine 
the bit pattern

function signedsum(v::Vector, j::Uint)
    s = zero(eltype(v))
    for i in 1:length(v)
        s += (j & one(Uint)) == zero(Uint) ? v[i] : -v[i]
        j >>= 1
    end
    s
end
function sdifsums(v::Vector)
    [signedsum(v,j) for j in uint(0:(2^k - 1))]
end

A test case is

julia> srand(1234321)

julia> v = randn(6)
6-element Array{Float64,1}:
 -0.160767
 -1.48275 
 -0.384419
 -1.3541  
  1.03842 
 -1.16268 

julia> sdifsums(v)
64-element Array{Float64,1}:
 -3.5063  
 -3.18477 
 -0.540799
 -0.219266
 -2.73746 
 -2.41593 
  0.228038
  0.549572
 -0.798091
 -0.476557
  ⋮       
  0.476557
  0.798091
 -0.549572
 -0.228038
  2.41593 
  2.73746 
  0.219266
  0.540799
  3.18477 
  3.5063  


Are there any suggestions for a faster or cleaner approach?

Reply via email to