Skip to content

Commit

Permalink
correction of type parametrisation
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Sep 13, 2017
1 parent 37ac57f commit 820391a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ to install the files. Julia 0.6 is required.
### Moment

```julia
julia> moment{T <: AbstractFloat}(data::Matrix{T}, m::Int, b::Int = 2)
julia> moment(data::Matrix{T}, m::Int, b::Int = 2) where T<: AbstractFloat
```

Returns a `SymmetricTensor{T, m}` of the moment of order `m` of multivariate data represented by a `t` by `n` matrix, i.e. data with `n` marginal variables and `t` realisations. The argument `b` with defalt value `2`, is an optional `Int` that determines the size
Expand Down Expand Up @@ -75,7 +75,7 @@ julia> convert(Array, m)
### Cumulants

```julia
julia> cumulants{T <: AbstractFloat}(data::Matrix{T}, m::Int = 4, b::Int = 2)
julia> cumulants(data::Matrix{T}, m::Int = 4, b::Int = 2) where T<: AbstractFloat
```

Returns a vector of `SymmetricTensor{T, i}` `i = 1,2,3,...,m` of cumulants of
Expand Down Expand Up @@ -133,7 +133,7 @@ julia> @everywhere using Cumulants
Naive algorithms of moment and cumulant tensors calculations are also available.

```julia
julia> {T <: AbstractFloat}naivemoment(data::Matrix{T}, m::Int = 4)
julia> naivemoment(data::Matrix{T}, m::Int = 4) where T<: AbstractFloat
```
Returns array{T, m} of the m'th moment of data. calculated using a naive algorithm.

Expand All @@ -158,7 +158,7 @@ julia> naivemoment(data, 3)
```

```julia
julia> naivecumulant{T <: AbstractFloat}(data::Matrix{T}, m::Int = 4)
julia> naivecumulant(data::Matrix{T}, m::Int = 4) where T<: AbstractFloat
```
Returns `Array{T, m}` of the `m`'th cumulant of data, calculated using a naive algorithm. Works for `1 <= m , 7`, for `m >= 7` throws exception.

Expand Down
34 changes: 15 additions & 19 deletions src/cumulant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ julia> mom_el(M, (1,1), (2,2), 2)
"""

function blockel{T <: AbstractFloat}(data::Matrix{T}, mi::Tuple, mj::Tuple, b::Int)
function blockel(data::Matrix{T}, mi::Tuple, mj::Tuple, b::Int) where T <: AbstractFloat
ret = 0.
t = size(data, 1)
for l in 1:t
Expand Down Expand Up @@ -51,10 +51,8 @@ julia> momentblock(M, (1,1), (2,2), 2)
```
"""

function momentblock{T <: AbstractFloat}(X::Matrix{T},
j::Tuple,
dims::Tuple,
b::Int)
function momentblock(X::Matrix{T}, j::Tuple, dims::Tuple,
b::Int) where {T <: AbstractFloat}
ret = zeros(T, dims)
for ind = 1:(prod(dims))
i = ind2sub(dims, ind)
Expand Down Expand Up @@ -82,7 +80,7 @@ Returns: SymmetricTensor{Float, m}, a tensor of the m'th moment of X, where b
is a block size. Uses 1 core implementation
"""

function moment1c{T <: AbstractFloat}(X::Matrix{T}, m::Int, b::Int=2)
function moment1c(X::Matrix{T}, m::Int, b::Int=2) where T <: AbstractFloat
n = size(X, 2)
sizetest(n, b)
nbar = mod(n,b)==0 ? n÷b : n÷b + 1
Expand All @@ -102,7 +100,7 @@ Returns: SymmetricTensor{Float, m}, a tensor of the m'th moment of X, where b
is a block size. Uses multicore parallel implementation via pmap()
"""

function momentnc{T <: AbstractFloat}(x::Matrix{T}, m::Int, b::Int = 2)
function momentnc(x::Matrix{T}, m::Int, b::Int = 2) where T <: AbstractFloat
t = size(x, 1)
f(z::Matrix{T}) = moment1c(z, m, b)
k = length(workers())
Expand All @@ -120,7 +118,7 @@ Returns: SymmetricTensor{Float, m}, a tensor of the m'th moment of X, where b
is a block size. Calls 1 core or multicore moment function.
"""

moment{T <: AbstractFloat}(X::Matrix{T}, m::Int, b::Int=2) =
moment(X::Matrix{T}, m::Int, b::Int=2) where T <: AbstractFloat =
(nworkers()>1)? momentnc(X, m, b):moment1c(X, m, b)

# ---- following code is used to caclulate cumulants in SymmetricTensor form----
Expand Down Expand Up @@ -197,9 +195,8 @@ Array{Float64,N}[
[1.0 2.0; 2.0 4.0]]
```
"""
function accesscum{T <: AbstractFloat}(mulind::Tuple,
part::IndexPart,
cum::SymmetricTensor{T}...)
function accesscum(mulind::Tuple, part::IndexPart,
cum::SymmetricTensor{T}...) where T <: AbstractFloat
blocks = Array{Array{T}}(part.npart)
sq = cum[1].sqr || !(cum[1].bln in mulind)
for k in 1:part.npart
Expand Down Expand Up @@ -246,8 +243,8 @@ julia> outprodblocks(IndexPart(Array{Int64,1}[[1,2],[3,4]],[2,2],4,2), blocks)
```
"""

function outprodblocks{T <: AbstractFloat}(inp::IndexPart,
blocks::Vector{Array{T}})
function outprodblocks(inp::IndexPart,
blocks::Vector{Array{T}}) where T <: AbstractFloat
b = size(blocks[1], 1)
block = zeros(T, fill(b, inp.nind)...)
for i = 1:(b^inp.nind)
Expand Down Expand Up @@ -286,10 +283,9 @@ Nullable{Array{Float64,4}}[[9.0 18.0; 18.0 36.0]
Nullable{Array{Float64,4}}[[23.0 46.0; 46.0 92.0] [45.0; 90.0]; #NULL [75.0]],2,2,3,false)
```
"""
function outerprodcum{T <: AbstractFloat}(retd::Int,
npart::Int,
cum::SymmetricTensor{T}...;
exclpartlen::Int = 1)
function outerprodcum(retd::Int, npart::Int,
cum::SymmetricTensor{T}...;
exclpartlen::Int = 1) where T <: AbstractFloat
parts = indpart(retd, npart, exclpartlen)
prodcum = NullableArray(Array{T, retd}, fill(cum[1].bln, retd)...)
for muli in indices(retd, cum[1].bln)
Expand All @@ -315,7 +311,7 @@ Returns: SymmetricTensor{Float, m}, a tensor of the m'th cumulant of X, given Ve
of cumulants of order 2, ..., m-2
"""

function cumulant{T <: AbstractFloat}(X::Matrix{T}, cum::SymmetricTensor{T}...)
function cumulant(X::Matrix{T}, cum::SymmetricTensor{T}...) where T <: AbstractFloat
m = length(cum) + 2
ret = moment(X, m, cum[1].bls)
for sigma in 2:div(m, 2)
Expand Down Expand Up @@ -345,7 +341,7 @@ julia> convert(Array, cumulants(M, 3)[3])
0.0 0.0
```
"""
function cumulants{T <: AbstractFloat}(X::Matrix{T}, m::Int = 4, b::Int = 2)
function cumulants(X::Matrix{T}, m::Int = 4, b::Int = 2) where T <: AbstractFloat
cvec = Array{SymmetricTensor{T}}(m)
cvec[1] = moment1c(X, 1, b)
X = X .- mean(X, 1)
Expand Down
16 changes: 8 additions & 8 deletions src/mom2cum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Return z - Vector{Float} , vectorsed outer/kroneker product o vectors a and b
Auxiliary function for rawmoment
"""
function outer!{T <: AbstractFloat}(z::Vector{T}, a::Vector{T}, b::Vector{T})
function outer!(z::Vector{T}, a::Vector{T}, b::Vector{T}) where T<: AbstractFloat
sa = size(a,1)
sb = size(b,1)
m = 1
Expand All @@ -21,7 +21,7 @@ end
Returns updated Vector{Float} A, by adding elementwisely Vector{Float} B
Auxiliary function for rawmoment
"""
function updvec!{T<: AbstractFloat}(A::Vector{T}, B::Vector{T})
function updvec!(A::Vector{T}, B::Vector{T}) where T<: AbstractFloat
n = size(A, 1)
for i=1:n
@inbounds A[i] += B[i]
Expand All @@ -38,7 +38,7 @@ pyramid structures and blocks
Returns Array{Float, m}, the m'th moment's tensor
"""

function rawmoment{T <: AbstractFloat}(X::Matrix{T}, m::Int = 4)
function rawmoment(X::Matrix{T}, m::Int = 4) where T<: AbstractFloat
t,n = size(X)
if m == 1
return mean(X, 1)[1,:]
Expand All @@ -63,7 +63,7 @@ end
Returns [Array{Float, 1}, ..., Array{Float, k}] noncentral moment tensors of
order 1, ..., k
"""
raw_moments_upto_k{T <: AbstractFloat}(X::Matrix{T}, k::Int = 4) =
raw_moments_upto_k(X::Matrix{T}, k::Int = 4) where T<: AbstractFloat =
[rawmoment(X, i) for i in 1:k]

"""
Expand All @@ -76,7 +76,7 @@ c_{ijkl} = m_{ijkl} - m_{ijk} m_{l} [4] - m_{ij} m_{kl} [3] + 2 m_{ij} m_{k} m_{
-6 m_{i} m_{j} m_{k} m_{l}
"""

function cumulants_from_moments{T <: AbstractFloat}(raw::Vector{Array{T}})
function cumulants_from_moments(raw::Vector{Array{T}}) where T<: AbstractFloat
k = length(raw)
cumarr = Array{Array{Float64}}(k)
for j in 1:k
Expand Down Expand Up @@ -104,8 +104,8 @@ dpp - vector of a factor for each product of moments (a factorial factor).
Returns Array{Float, n} the n'th cumulant tensor
"""
function onecumulant{T <: AbstractFloat}(ind::Tuple, raw::Vector{Array{T}},
spp::Vector, sppl::Vector{Vector{Int}}, dpp::Vector{Int})
function onecumulant(ind::Tuple, raw::Vector{Array{T}}, spp::Vector,
sppl::Vector{Vector{Int}}, dpp::Vector{Int}) where T<: AbstractFloat
ret = zero(T)
for i in 1:length(spp)
part = spp[i]
Expand All @@ -126,5 +126,5 @@ end
Returns a vector of 1,2, .., k dims Arrays{Float} of cumulant tensors
"""
mom2cums{T <: AbstractFloat}(x::Matrix{T}, k::Int) =
mom2cums(x::Matrix{T}, k::Int) where T<: AbstractFloat =
cumulants_from_moments(raw_moments_upto_k(x, k))
8 changes: 4 additions & 4 deletions src/naivecumulants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ julia> momel(M, (1,1,1,1))
```
"""

momel{T <: AbstractFloat}(X::Matrix{T}, multind::Tuple) = blockel(X, multind, multind, 0)
momel(X::Matrix{T}, multind::Tuple) where T<: AbstractFloat = blockel(X, multind, multind, 0)

"""
Expand All @@ -37,7 +37,7 @@ julia> naivemoment(M, 3)
```
"""
function naivemoment{T<:AbstractFloat}(X::Matrix{T}, m::Int = 4)
function naivemoment(X::Matrix{T}, m::Int = 4) where T<: AbstractFloat
n = size(X, 2)
moment = zeros(T, fill(n, m)...)
for i = 1:(n^m)
Expand Down Expand Up @@ -66,7 +66,7 @@ mixel(M, (1,1,1,1,1,1))
```
"""

function mixel{T<:AbstractFloat}(X::Matrix{T}, i::Tuple)
function mixel(X::Matrix{T}, i::Tuple) where T<: AbstractFloat
a = zero(T)
if length(i) == 4
a -= momel(X, (i[1],i[2]))*momel(X, (i[3],i[4]))
Expand Down Expand Up @@ -131,7 +131,7 @@ julia> naivecumulant(M, 3)
```
"""
function naivecumulant{T<:AbstractFloat}(X::Matrix{T}, m::Int = 4)
function naivecumulant(X::Matrix{T}, m::Int = 4) where T<: AbstractFloat
m < 7 || throw(AssertionError("naive implementation of $m cumulant not supported"))
if m == 1
return naivemoment(X,m)
Expand Down
10 changes: 5 additions & 5 deletions src/pyramidcumulants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ julia> pyramidmoment(M, 3)
-0.0407653 0.0120729
```
"""
function pyramidmoment{T<:AbstractFloat}(data::Matrix{T}, m::Int)
function pyramidmoment(data::Matrix{T}, m::Int) where T<: AbstractFloat
n = size(data,2)
ret = zeros(T, fill(n, m)...)
for ind in indices(m, n)
Expand All @@ -66,8 +66,8 @@ end
Returns Float, element of the sum of products of lower cumulants at given
multi-index and set of its partitions
"""
function mixedel{T<:AbstractFloat}(cum::Vector{Array{T}}, mulind::Tuple,
parts::Vector{Vector{Vector{Int}}})
function mixedel(cum::Vector{Array{T}}, mulind::Tuple,
parts::Vector{Vector{Vector{Int}}}) where T<: AbstractFloat
sum = 0.
for k = 1:length(parts)
prod = 1.
Expand All @@ -85,7 +85,7 @@ end
Returns Array{Float, m}, the mixed array (sum of products of lower cumulants)
"""
function mixedarr{T<:AbstractFloat}(cumulants::Vector{Array{T}}, m::Int)
function mixedarr(cumulants::Vector{Array{T}}, m::Int) where T<: AbstractFloat
n = size(cumulants[1], 1)
sumofprod = zeros(fill(n, m)...)
parts = part(m)
Expand Down Expand Up @@ -119,7 +119,7 @@ julia> pyramidcumulants(M, 3)[2]
0.0 0.0
```
"""
function pyramidcumulants{T<:AbstractFloat}(X::Matrix{T}, m::Int = 4)
function pyramidcumulants(X::Matrix{T}, m::Int = 4) where T<: AbstractFloat
cumulants = Array{T}[]
push!(cumulants, pyramidmoment(X, 1))
X = X .- mean(X, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/comptimes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ function main(args)
savecomptime(m, t, n)
end

main(ARGS)
main(ARGS)
2 changes: 1 addition & 1 deletion test/gendata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ function main()
save("data/datafortests.jld", d)
end

main()
main()

0 comments on commit 820391a

Please sign in to comment.