4
4
#
5
5
#
6
6
# ###########################################################################################
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
+
8
16
9
17
# ###############
10
18
# Means
@@ -20,11 +28,6 @@ function vtmean(f, A; dims=:)
20
28
end
21
29
vtmean (A; dims= :) = vtmean (identity, A, dims= dims)
22
30
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
28
31
function vgeomean (A; dims= :)
29
32
c = 1 / _denom (A, dims)
30
33
vmapreducethen (log, + , x -> exp (c * x), A, dims= dims)
@@ -33,14 +36,55 @@ function vgeomean(f::F, A; dims=:) where {F}
33
36
c = 1 / _denom (A, dims)
34
37
vmapreducethen (x -> log (f (x)), + , x -> exp (c * x), A, dims= dims)
35
38
end
36
-
37
39
function vharmmean (A; dims= :)
38
40
c = 1 / _denom (A, dims)
39
41
vmapreducethen (inv, + , x -> inv (c * x), A, dims= dims)
40
42
end
41
43
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
44
88
45
89
# ###############
46
90
# Entropies
0 commit comments