@@ -30,29 +30,40 @@ kurtosis(m) # throws error because its above 2nd moment
3030```
3131"""
3232abstract type AbstractMoments{N} end
33- n_moments (:: Type{<:AbstractMoments{N}} ) where N = N
34- n_moments (m:: AbstractMoments{N} ) where N = N
35- Distributions. mean (m:: AbstractMoments ) = n_moments (m) >= 1 ? m[1 ] :
33+ n_moments (:: Type{<:AbstractMoments{N}} ) where {N} = N
34+ n_moments (m:: AbstractMoments{N} ) where {N} = N
35+ function Distributions. mean (m:: AbstractMoments )
36+ n_moments (m) >= 1 ? m[1 ] :
3637 error (" mean not recorded" )
37- Distributions. var (m:: AbstractMoments ) = n_moments (m) >= 2 ? m[2 ] :
38+ end
39+ function Distributions. var (m:: AbstractMoments )
40+ n_moments (m) >= 2 ? m[2 ] :
3841 error (" variance not recorded" )
39- Distributions. std (m:: AbstractMoments ) = n_moments (m) >= 2 ? sqrt (m[2 ]) :
42+ end
43+ function Distributions. std (m:: AbstractMoments )
44+ n_moments (m) >= 2 ? sqrt (m[2 ]) :
4045 error (" std not recorded" )
41- Distributions. skewness (m:: AbstractMoments ) = n_moments (m) >= 3 ? m[3 ] :
46+ end
47+ function Distributions. skewness (m:: AbstractMoments )
48+ n_moments (m) >= 3 ? m[3 ] :
4249 error (" skewness not recorded" )
43- Distributions. kurtosis (m:: AbstractMoments ) = n_moments (m) >= 4 ? m[4 ] :
50+ end
51+ function Distributions. kurtosis (m:: AbstractMoments )
52+ n_moments (m) >= 4 ? m[4 ] :
4453 error (" kurtosis not recorded" )
54+ end
4555
46-
47- struct Moments{N,T} <: AbstractMoments{N}
48- all:: SVector{N,T}
56+ struct Moments{N, T} <: AbstractMoments{N}
57+ all:: SVector{N, T}
4958end
5059Moments (x... ) = Moments (SVector {length(x)} (promote (x... )))
5160Moments () = Moments (SA[])
52- Base. getindex (m:: Moments , i) = n_moments (m) >= i ? m. all[i] :
61+ function Base. getindex (m:: Moments , i)
62+ n_moments (m) >= i ? m. all[i] :
5363 error (" $(i) th moment not recorded." )
64+ end
5465Base. convert (:: Type{AbstractArray} , m:: Moments ) = m. all
55- Base. eltype (:: Moments{N,T} ) where {N,T} = T
66+ Base. eltype (:: Moments{N, T} ) where {N, T} = T
5667
5768"""
5869 moments(D, ::Val{N} = Val(2))
@@ -67,15 +78,15 @@ moments(LogNormal(), Val(4)) # first four moments
6778moments(Normal()) # mean and variance
6879```
6980"""
70- function moments (d:: Distribution , :: Val{N} = Val (2 )) where N
81+ function moments (d:: Distribution , :: Val{N} = Val (2 )) where {N}
7182 N isa Integer || error (" N must be a positive Integer" )
72- N > 4 && error (" Getting moments above 4 not yet implemented for " *
73- " distribution $(typeof (d)) ." )
74- N == 4 && return (Moments (mean (d), var (d), skewness (d), kurtosis (d)))
75- N == 3 && return (Moments (mean (d), var (d), skewness (d)))
76- N == 2 && return (Moments (mean (d), var (d)))
77- N == 1 && return (Moments (mean (d)))
78- N == 0 && return (Moments ())
83+ N > 4 && error (" Getting moments above 4 not yet implemented for " *
84+ " distribution $(typeof (d)) ." )
85+ N == 4 && return (Moments (mean (d), var (d), skewness (d), kurtosis (d)))
86+ N == 3 && return (Moments (mean (d), var (d), skewness (d)))
87+ N == 2 && return (Moments (mean (d), var (d)))
88+ N == 1 && return (Moments (mean (d)))
89+ N == 0 && return (Moments ())
7990 error (" N must be a positive Integer." )
8091end
8192
@@ -113,9 +124,9 @@ plot(d, label = "lognormal", ylab="probability density")
113124plot!(Normal(3,1.2), label = "normal")
114125```
115126"""
116- fit (:: Type{D} , m:: AbstractMoments ) where {D<: Distribution } =
127+ function fit (:: Type{D} , m:: AbstractMoments ) where {D <: Distribution }
117128 error (" fitting to moments not implemented for distribution of type $D " )
118-
129+ end
119130
120131"""
121132 QuantilePoint
@@ -143,53 +154,68 @@ There are macros/functions for some commonly used sets of QuantilePoints: 90% an
143154- `@qs_cf90(q0_05,q0_95)`
144155- `@qs_cf95(q0_025,q0_975)` -> `Set([QuantilePoint(q0_025,0.025),QuantilePoint(q0_975,0.975)]))`
145156"""
146- struct QuantilePoint{TQ,TP}
157+ struct QuantilePoint{TQ, TP}
147158 q:: TQ
148159 p:: TP
149- QuantilePoint {TQ,TP} (q,p) where {TQ,TP} = 0 < p < 1 ? new (q,p) :
160+ function QuantilePoint {TQ, TP} (q, p) where {TQ, TP}
161+ 0 < p < 1 ? new (q, p) :
150162 error (" p must be in (0,1)" )
163+ end
151164end
152- QuantilePoint (q,p) = QuantilePoint {typeof(q),typeof(p)} (q,p)
165+ QuantilePoint (q, p) = QuantilePoint {typeof(q), typeof(p)} (q, p)
153166
154- QuantilePoint (qp:: QuantilePoint ; q = qp. q, p = qp. p) = QuantilePoint (q,p)
167+ QuantilePoint (qp:: QuantilePoint ; q = qp. q, p = qp. p) = QuantilePoint (q, p)
155168Base. show (io:: IO , qp:: QuantilePoint ) = print (io, " QuantilePoint($(qp. q) ,$(qp. p) )" )
156- function Base. isless (x:: QuantilePoint ,y:: QuantilePoint )
169+ function Base. isless (x:: QuantilePoint , y:: QuantilePoint )
157170 is_equal_q = (x. q == y. q)
158171 ((x. p == y. p) && ! is_equal_q) && error (" incompatible: $x ,$y " )
159172 isless = (x. q < y. q)
160173 # for different p, q needs to be different
161174 (isless && (x. p > y. p)) && error (" incompatible: $(x) ,$(y) " )
162- (! isless && ! is_equal_q && (x. p < y. p)) && error (" incompatible: $x ,$y " )
163- return (isless)
175+ (! isless && ! is_equal_q && (x. p < y. p)) && error (" incompatible: $x ,$y " )
176+ return (isless)
164177end
165178
166- macro qp (q,p) :(QuantilePoint ($ (esc (q)), $ (esc (p)))) end
167- macro qp_ll (q0_025) :(QuantilePoint ($ (esc (q0_025)),0.025 )) end
168- macro qp_l (q0_05) :(QuantilePoint ($ (esc (q0_05)),0.05 )) end
169- macro qp_m (median) :(QuantilePoint ($ (esc (median)),0.5 )) end
170- macro qp_u (q0_95) :(QuantilePoint ($ (esc (q0_95)),0.95 )) end
171- macro qp_uu (q0_975) :(QuantilePoint ($ (esc (q0_975)),0.975 )) end
179+ macro qp (q, p)
180+ :(QuantilePoint ($ (esc (q)), $ (esc (p))))
181+ end
182+ macro qp_ll (q0_025)
183+ :(QuantilePoint ($ (esc (q0_025)), 0.025 ))
184+ end
185+ macro qp_l (q0_05)
186+ :(QuantilePoint ($ (esc (q0_05)), 0.05 ))
187+ end
188+ macro qp_m (median)
189+ :(QuantilePoint ($ (esc (median)), 0.5 ))
190+ end
191+ macro qp_u (q0_95)
192+ :(QuantilePoint ($ (esc (q0_95)), 0.95 ))
193+ end
194+ macro qp_uu (q0_975)
195+ :(QuantilePoint ($ (esc (q0_975)), 0.975 ))
196+ end
172197
173- macro qs_cf90 (q0_05,q0_95)
174- :(Set ([QuantilePoint ($ (esc (q0_05)),0.05 ),QuantilePoint ($ (esc (q0_95)),0.95 )])) end
175- macro qs_cf95 (q0_025,q0_975)
176- :(Set ([QuantilePoint ($ (esc (q0_025)),0.025 ),QuantilePoint ($ (esc (q0_975)),0.975 )])) end
198+ macro qs_cf90 (q0_05, q0_95)
199+ :(Set ([QuantilePoint ($ (esc (q0_05)), 0.05 ), QuantilePoint ($ (esc (q0_95)), 0.95 )]))
200+ end
201+ macro qs_cf95 (q0_025, q0_975)
202+ :(Set ([QuantilePoint ($ (esc (q0_025)), 0.025 ), QuantilePoint ($ (esc (q0_975)), 0.975 )]))
203+ end
177204
178205# The non-macro versions return percentile whose type matches that of the argument
179- qp_ll (q0_025:: T ) where T = QuantilePoint (q0_025, T (0.025 ))
180- qp_l (q0_05:: T ) where T = QuantilePoint (q0_05, T (0.05 ))
181- qp_m (median:: T ) where T = QuantilePoint (median, T (0.5 ))
182- qp_u (q0_95:: T ) where T = QuantilePoint (q0_95, T (0.95 ))
183- qp_uu (q0_975:: T ) where T = QuantilePoint (q0_975, T (0.975 ))
184-
185- function qs_cf90 (q0_05:: T ,q0_95:: T ) where T
186- Set ([QuantilePoint (q0_05,T (0.05 )),QuantilePoint (q0_95,T (0.95 ))])
206+ qp_ll (q0_025:: T ) where {T} = QuantilePoint (q0_025, T (0.025 ))
207+ qp_l (q0_05:: T ) where {T} = QuantilePoint (q0_05, T (0.05 ))
208+ qp_m (median:: T ) where {T} = QuantilePoint (median, T (0.5 ))
209+ qp_u (q0_95:: T ) where {T} = QuantilePoint (q0_95, T (0.95 ))
210+ qp_uu (q0_975:: T ) where {T} = QuantilePoint (q0_975, T (0.975 ))
211+
212+ function qs_cf90 (q0_05:: T , q0_95:: T ) where {T}
213+ Set ([QuantilePoint (q0_05, T (0.05 )), QuantilePoint (q0_95, T (0.95 ))])
187214end
188- function qs_cf95 (q0_025:: T ,q0_975:: T ) where T
189- Set ([QuantilePoint (q0_025,T (0.025 )),QuantilePoint (q0_975,T (0.975 ))])
215+ function qs_cf95 (q0_025:: T , q0_975:: T ) where {T}
216+ Set ([QuantilePoint (q0_025, T (0.025 )), QuantilePoint (q0_975, T (0.975 ))])
190217end
191218
192-
193219"""
194220 fit(D, lower::QuantilePoint, upper::QuantilePoint)
195221
@@ -208,12 +234,12 @@ quantile.(d, [0.5, 0.975]) ≈ [3,5]
208234true
209235```
210236"""
211- function fit (:: Type{D} , lower:: QuantilePoint , upper:: QuantilePoint ) where D<: Distribution
237+ function fit (:: Type{D} ,
238+ lower:: QuantilePoint ,
239+ upper:: QuantilePoint ) where {D <: Distribution }
212240 error (" fitting to two quantile points not implemented for distribution of type $D " )
213241end
214242
215-
216-
217243"""
218244 fit(D, val, qp, ::Val{stats} = Val(:mean))
219245
@@ -240,25 +266,31 @@ d = fit(LogNormal, 5.0, @qp_uu(14), Val(:mode));
240266(true, true)
241267```
242268"""
243- function fit (:: Type{D} , val, qp:: QuantilePoint , :: Val{stats} = Val (:mean )) where {D<: Distribution , stats}
244- stats == :mean && return (fit_mean_quantile (D, val, qp))
245- stats == :mode && return (fit_mode_quantile (D, val, qp))
246- stats == :median && return (fit_median_quantile (D, val, qp))
269+ function fit (:: Type{D} ,
270+ val,
271+ qp:: QuantilePoint ,
272+ :: Val{stats} = Val (:mean )) where {D <: Distribution , stats}
273+ stats == :mean && return (fit_mean_quantile (D, val, qp))
274+ stats == :mode && return (fit_mode_quantile (D, val, qp))
275+ stats == :median && return (fit_median_quantile (D, val, qp))
247276 error (" unknown stats: $stats " )
248277end ,
249- function fit_median_quantile (:: Type{D} , median, qp:: QuantilePoint ) where {D <: Distribution }
250- return (fit (D, @qp_m (median), qp))
278+ function fit_median_quantile (:: Type{D} ,
279+ median,
280+ qp:: QuantilePoint ) where {D <: Distribution }
281+ return (fit (D, @qp_m (median), qp))
251282end ,
252- function fit_mean_quantile (:: Type{D} , mean:: Real , qp:: QuantilePoint ) where D<: Distribution
283+ function fit_mean_quantile (:: Type{D} ,
284+ mean:: Real ,
285+ qp:: QuantilePoint ) where {D <: Distribution }
253286 error (" fit_mean_quantile not yet implemented for Distribution of type: $D " )
254287end ,
255- function fit_mode_quantile (:: Type{D} , mode:: Real , qp:: QuantilePoint ) where D<: Distribution
288+ function fit_mode_quantile (:: Type{D} ,
289+ mode:: Real ,
290+ qp:: QuantilePoint ) where {D <: Distribution }
256291 error (" fit_mode_quantile not yet implemented for Distribution of type: $D " )
257292end
258293
259294# function fit_mean_quantile(d::Type{D}, mean::Real, qp::QuantilePoint) where D<:Distribution
260295# error("fit_mean_quantile not yet implemented for Distribution of type: $D")
261296# end
262-
263-
264-
0 commit comments