Skip to content

Commit fa8509f

Browse files
Add threaded variants; move things around
1 parent 7e5b111 commit fa8509f

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

src/vstats.jl

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
#
55
#
66
############################################################################################
7-
# Statistical things
7+
# Assorted functions from StatsBase, plus some of my own
8+
9+
################
10+
# Utility functions
11+
_xlogx(x::T) where {T} = ifelse(iszero(x), zero(T), x * log(x))
12+
_xlogy(x::T, y::T) where {T} = ifelse(iszero(x) & !isnan(y), zero(T), x * log(y))
13+
# _xlogxdy(x::T, y::T) where {T} = _xlogy(x, ifelse(iszero(x) & iszero(y), zero(T), x / y))
14+
_klterm(x::T, y::T) where {T} = _xlogy(x, x) - _xlogy(x, y)
15+
816

917
################
1018
# Means
@@ -20,11 +28,6 @@ function vtmean(f, A; dims=:)
2028
end
2129
vtmean(A; dims=:) = vtmean(identity, A, dims=dims)
2230

23-
# Naturally, faster than the overflow/underflow-safe logsumexp, but if one can tolerate it...
24-
vlse(A; dims=:) = vmapreducethen(exp, +, log, A, dims=dims)
25-
vtlse(A; dims=:) = vtmapreducethen(exp, +, log, A, dims=dims)
26-
27-
# Assorted functions from StatsBase
2831
function vgeomean(A; dims=:)
2932
c = 1 / _denom(A, dims)
3033
vmapreducethen(log, +, x -> exp(c * x), A, dims=dims)
@@ -33,14 +36,55 @@ function vgeomean(f::F, A; dims=:) where {F}
3336
c = 1 / _denom(A, dims)
3437
vmapreducethen(x -> log(f(x)), +, x -> exp(c * x), A, dims=dims)
3538
end
36-
3739
function vharmmean(A; dims=:)
3840
c = 1 / _denom(A, dims)
3941
vmapreducethen(inv, +, x -> inv(c * x), A, dims=dims)
4042
end
4143

42-
_xlogx(x::T) where {T} = ifelse(iszero(x), zero(T), x * log(x))
43-
_xlogy(x::T, y::T) where {T} = ifelse(iszero(x) & !isnan(y), zero(T), x * log(y))
44+
function vtgeomean(A; dims=:)
45+
c = 1 / _denom(A, dims)
46+
vtmapreducethen(log, +, x -> exp(c * x), A, dims=dims)
47+
end
48+
function vtgeomean(f::F, A; dims=:) where {F}
49+
c = 1 / _denom(A, dims)
50+
vtmapreducethen(x -> log(f(x)), +, x -> exp(c * x), A, dims=dims)
51+
end
52+
function vtharmmean(A; dims=:)
53+
c = 1 / _denom(A, dims)
54+
vtmapreducethen(inv, +, x -> inv(c * x), A, dims=dims)
55+
end
56+
57+
# Mean on the log scaley
58+
function vmean_log(f, A; dims=:)
59+
c = log(_denom(A, dims))
60+
vmapreducethen(f, +, x -> log(x) - c, A, dims=dims)
61+
end
62+
63+
################
64+
# logsumexp (the naive and unsafe version)
65+
# Naturally, faster than the overflow/underflow-safe logsumexp, but if one can tolerate it...
66+
vlse(A; dims=:) = vmapreducethen(exp, +, log, A, dims=dims)
67+
vtlse(A; dims=:) = vtmapreducethen(exp, +, log, A, dims=dims)
68+
vlse(f, A; dims=:) = vmapreducethen(x -> exp(f(x)), +, log, A, dims=dims)
69+
vtlse(f, A; dims=:) = vtmapreducethen(x -> exp(f(x)), +, log, A, dims=dims)
70+
71+
function vlse_mean(A; dims=:)
72+
c = log(_denom(A, dims))
73+
vmapreducethen(exp, +, x -> log(x) - c, A, dims=dims)
74+
end
75+
function vlse_mean(f, A; dims=:)
76+
c = log(_denom(A, dims))
77+
vmapreducethen(x -> exp(f(x)), +, x -> log(x) - c, A, dims=dims)
78+
end
79+
80+
function vtlse_mean(A; dims=:)
81+
c = log(_denom(A, dims))
82+
vtmapreducethen(exp, +, x -> log(x) - c, A, dims=dims)
83+
end
84+
function vtlse_mean(f, A; dims=:)
85+
c = log(_denom(A, dims))
86+
vtmapreducethen(x -> exp(f(x)), +, x -> log(x) - c, A, dims=dims)
87+
end
4488

4589
################
4690
# Entropies

0 commit comments

Comments
 (0)