Skip to content

Commit

Permalink
call coskewness and cokurtosis without weights
Browse files Browse the repository at this point in the history
  • Loading branch information
tinybike committed Dec 14, 2014
1 parent e3e6004 commit 2b81a39
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Use:
-1.62089 -0.44919 1.20592
-1.06458 -0.68214 -1.12841 ];

julia> weights = [1.0, 0.1, 0.5];
Third and fourth joint central moment tensors:

julia> coskew(data)
3x3x3 Array{Float64,3}:
Expand All @@ -38,9 +38,6 @@ Use:
0.350696 0.156275 0.131448
0.451934 0.131448 -0.645484

julia> coskewness(data, weights)
0.467586

julia> cokurt(data)
3x3x3x3 Array{Float64,4}:
[:, :, 1, 1] =
Expand Down Expand Up @@ -88,19 +85,46 @@ Use:
0.97585 0.58726 1.73728
2.69607 1.73728 5.85635

Statistics:

julia> coskewness(data)
0.2838850631006579

julia> cokurtosis(data)
0.8916763961210045

`coskewness` and `cokurtosis` can use an optional weight vector, which assigns a weight to each column of the data matrix:

julia> weights = [1.0, 0.1, 0.5];

julia> coskewness(data, weights)
0.46758589701357833

julia> cokurtosis(data, weights)
1-element Array{Float64,1}:
1.32039
1.3203902349727108

The `coskew` and `cokurt` functions can also return flattened/unfolded versions of the cumulant tensors:
The `coskew` and `cokurt` functions can also return flattened/unfolded tensors:

julia> coskew(data, flatten=true)
3x9 Array{Float64,2}:
0.294091 0.26697 0.773618 0.26697 0.162051 0.350696 0.773618 0.350696 0.451934
0.26697 0.162051 0.350696 0.162051 0.0852269 0.156275 0.350696 0.156275 0.131448
0.773618 0.350696 0.451934 0.350696 0.156275 0.131448 0.451934 0.131448 -0.645484

julia> cokurt(data,flatten=true)
3x27 Array{Float64,2}:
2.12678 1.11885 0.474782 1.11885 1.12294 0.187331 0.474782 0.187331 1.15524 1.11885 … 0.276558 0.474782 0.187331 1.15524 0.187331 -0.0266349 0.276558 1.15524 0.276558 0.178083
1.11885 1.12294 0.187331 1.12294 1.40462 -0.0266349 0.187331 -0.0266349 0.276558 1.12294 0.779221 0.187331 -0.0266349 0.276558 -0.0266349 -0.517198 0.779221 0.276558 0.779221 0.218732
0.474782 0.187331 1.15524 0.187331 -0.0266349 0.276558 1.15524 0.276558 0.178083 0.187331 0.218732 1.15524 0.276558 0.178083 0.276558 0.779221 0.218732 0.178083 0.218732 5.98947

The `coskew`, `cokurt`, `coskewness`, and `cokurtosis` functions have `standardize` and `bias` keyword arguments. Setting `standardize=true` standardizes the elements of the joint moment tensors (divides by the standard deviation). Setting `bias=1` uses Bessel's correction (divides by `N-1` instead of `N`).

### Tests

Unit tests can be run from the command line:

$ julia test/runtests.jl

Or from the Julia prompt:

julia> Pkg.test("JointMoments")
10 changes: 8 additions & 2 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ function coskewness{T<:Real}(data::Array{T}, w::Array{T}; standardize=false, bia
w = normalize(w)
end
length(w) == size(data, 2) || throw(DimensionMismatch("Inconsistent array lengths."))
w' * coskew(data, standardize=standardize, flatten=true, bias=bias) * kron(w, w)
first(w' * coskew(data, standardize=standardize, flatten=true, bias=bias) * kron(w, w))
end

coskewness{T<:Real}(data::Array{T}; standardize=false, bias=0) =
coskewness(data, ones(size(data, 2)), standardize=standardize, bias=bias)

function cokurtosis{T<:Real}(data::Array{T}, w::Array{T}; standardize=false, bias=0)
if sum(w) != 1
w = normalize(w)
end
length(w) == size(data, 2) || throw(DimensionMismatch("Inconsistent array lengths."))
w' * cokurt(data, standardize=standardize, flatten=true, bias=bias) * kron(kron(w, w), w)
first(w' * cokurt(data, standardize=standardize, flatten=true, bias=bias) * kron(kron(w, w), w))
end

cokurtosis{T<:Real}(data::Array{T}; standardize=false, bias=0) =
cokurtosis(data, ones(size(data, 2)), standardize=standardize, bias=bias)
6 changes: 6 additions & 0 deletions test/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ end

@test_approx_eq_eps coskewness(data, wt, standardize=true) 0.148687894 ε
@test_approx_eq_eps cokurtosis(data, wt, standardize=true) 0.664085775 ε

@test_approx_eq_eps coskewness(data) 0.081011176 ε
@test_approx_eq_eps cokurtosis(data) 0.571876949 ε

@test_approx_eq_eps coskewness(data, bias=1) 0.084533401 ε
@test_approx_eq_eps cokurtosis(data, bias=1) 0.596741164 ε

0 comments on commit 2b81a39

Please sign in to comment.