Skip to content

Commit

Permalink
Added corrected option to many stats methods.
Browse files Browse the repository at this point in the history
* Reverts many test changes from the last commit
* Changed a bunch of test cases to use `corrected=false` for now
* This included a lot of little changes to method definition and some resulting formatting changes where necessary
* Added a few test cases for the corrected variances
* Added bias correction for ProbabililtyWeights.
* Deprecated `WeightVec`
* Added a macro for easier creation of weight types.
* Renamed weight creation function to `fweights`, `pweights`, etc.
  • Loading branch information
rofinn committed Apr 26, 2017
1 parent bc7c554 commit 080fcb0
Show file tree
Hide file tree
Showing 11 changed files with 510 additions and 330 deletions.
12 changes: 8 additions & 4 deletions src/StatsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ module StatsBase

## weights
AbstractWeights, # the abstract type to represent any weight vector
Weights, # the default type for representing a weight vector
AnalyticWeights, # the default type for representing a analytic/precision/reliability weight vectors
FrequencyWeights, # the type for representing a frequency weight vectors
weights, # construct a weights vector
frequency, # construct a frequency weights vector
exponential, # construct a weights vector using a exponential smoothing schema
ProbabilityWeights,# the type for representing a probability/sampling weight vectors
ExponentialWeights,# the type for representing exponential weights
weights, # alias for aweights
aweights, # construct an AnalyticWeights vector
fweights, # construct a FrequencyWeights vector
pweights, # construct a ProbabilityWeights vector
eweights, # construct an ExponentialWeights vector
wsum, # weighted sum with vector as second argument
wsum!, # weighted sum across dimensions with provided storage
wmean, # weighted mean
Expand Down
32 changes: 18 additions & 14 deletions src/cov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ function mean_and_cov end
end

## weighted cov
Base.cov(x::DenseMatrix, wv::AbstractWeights; mean=nothing, vardim::Int=1) =
scale!(scattermat(x, wv; mean=mean, vardim=vardim), inv(bias(wv)))
function Base.cov(x::DenseMatrix, wv::AbstractWeights; mean=nothing, vardim::Int=1, corrected=true)
scale!(scattermat(x, wv; mean=mean, vardim=vardim), bias(wv, corrected))
end

function mean_and_cov(x::DenseMatrix; vardim::Int=1)
function mean_and_cov(x::DenseMatrix; vardim::Int=1, corrected=true)
m = mean(x, vardim)
return m, Base.covm(x, m; vardim=vardim)
return m, Base.covm(x, m; vardim=vardim, corrected=corrected)
end
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights; vardim::Int=1)
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights; vardim::Int=1, corrected=true)
m = mean(x, wv, vardim)
return m, Base.cov(x, wv; mean=m, vardim=vardim)
return m, Base.cov(x, wv; mean=m, vardim=vardim, corrected=corrected)
end
else
scattermatm(x::DenseMatrix, mean, vardim::Int=1) =
Expand All @@ -106,18 +107,21 @@ else
scattermatm(x, Base.mean(x, wv, vardim), wv, vardim)

## weighted cov
Base.covm(x::DenseMatrix, mean, wv::AbstractWeights, vardim::Int=1) =
scale!(scattermatm(x, mean, wv, vardim), inv(bias(wv)))
function Base.covm(x::DenseMatrix, mean, wv::AbstractWeights, vardim::Int=1, corrected::Bool=true)
scale!(scattermatm(x, mean, wv, vardim), bias(wv, corrected))
end

Base.cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int=1) =
Base.covm(x, Base.mean(x, wv, vardim), wv, vardim)
function Base.cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int=1; corrected=true)
Base.covm(x, Base.mean(x, wv, vardim), wv, vardim, corrected)
end

function mean_and_cov(x::DenseMatrix, vardim::Int=1)
function mean_and_cov(x::DenseMatrix, vardim::Int=1; corrected=true)
m = mean(x, vardim)
return m, Base.covm(x, m, vardim)
return m, Base.covm(x, m, vardim, corrected)
end
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int=1)

function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int=1; corrected=true)
m = mean(x, wv, vardim)
return m, Base.cov(x, wv, vardim)
return m, Base.cov(x, wv, vardim; corrected=corrected)
end
end
2 changes: 2 additions & 0 deletions src/deprecates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ findat(a::AbstractArray, b::AbstractArray) = findat!(Array{Int}(size(b)), a, b)

@deprecate df(obj::StatisticalModel) dof(obj)
@deprecate df_residual(obj::StatisticalModel) dof_residual(obj)

@deprecate WeightVec{S<:Real, V<:RealVector}(vs::V, s::S=sum(vs)) AnalyticWeights(vs, s)
Loading

0 comments on commit 080fcb0

Please sign in to comment.