@@ -11,14 +11,19 @@ struct Semicircle{T<:Real} <: ContinuousUnivariateDistribution
1111 radius:: T
1212 Semicircle {T} (μ:: T ,r:: T ) where T = new (μ,r)
1313end
14- Semicircle {T<:Real} (μ:: T = 0.0 , r:: T = 2.0 ) = r > 0 ? Semicircle {T} (μ, r) :
14+ Semicircle (μ:: T = 0.0 , r:: T = 2.0 ) where {T <: Real } = r > 0 ? Semicircle {T} (μ, r) :
1515 throw (ArgumentError (" radius r must be positive, got $r " ))
16+
17+ Semicircle {T} (d:: Semicircle ) where T = Semicircle {T} (convert (T, d. mean), convert (T, d. radius))
18+ convert (:: Type{Semicircle{T}} , d:: Semicircle{T} ) where T = d
19+ convert (:: Type{Semicircle} , d:: Semicircle ) = d
20+ convert (:: Type{Semicircle{T}} , d:: Semicircle ) where T = Semicircle {T} (convert (T, d. mean), convert (T, d. radius))
1621
1722# Distribution function methods
1823# ##############################
1924
2025# cumulative distribution function
21- function cdf {T<:Real} (d:: Semicircle{T} , x:: T )
26+ function cdf (d:: Semicircle{T} , x:: T ) where {T <: Real }
2227 a, r = d. mean, d. radius
2328 if insupport (d, x)
2429 return 0.5 + (x- a)/ (π* r^ 2 ) * √ (r^ 2 - (x- a)^ 2 ) + asin ((x- a)/ r)/ π
@@ -30,7 +35,7 @@ function cdf{T<:Real}(d::Semicircle{T}, x::T)
3035end
3136
3237# probability density function
33- function pdf {T<:Real} (d:: Semicircle{T} , x:: T )
38+ function pdf (d:: Semicircle{T} , x:: T ) where {T <: Real }
3439 a, r = d. mean, d. radius
3540 if insupport (d, x)
3641 return 2 / (π* r^ 2 ) * √ (r^ 2 - (x- a)^ 2 )
@@ -40,7 +45,24 @@ function pdf{T<:Real}(d::Semicircle{T}, x::T)
4045end
4146
4247# predicate is x in the support of the distribution?
43- insupport {T<:Real} (d:: Semicircle{T} , x:: T )= abs (x)< d. radius
48+ insupport {T<:Real} (d:: Semicircle{T} , x:: T ) = abs (x- d. mean) < d. radius
49+
50+ function cdf (X:: Semicircle{T} , x:: V ) where {T<: Real ,V<: Real }
51+ TV = promote_type (T,V)
52+ cdf (convert (Semicircle{TV},X), convert (TV,x))
53+ end
54+
55+ # probability density function
56+ function pdf (X:: Semicircle{T} , x:: V ) where {T<: Real ,V<: Real }
57+ TV = promote_type (T,V)
58+ pdf (convert (Semicircle{TV},X), convert (TV,x))
59+ end
60+
61+ # predicate is x in the support of the distribution?
62+ function insupport (X:: Semicircle{T} , x:: V ) where {T<: Real ,V<: Real }
63+ TV = promote_type (T,V)
64+ insupport (convert (Semicircle{TV},X), convert (TV,x))
65+ end
4466
4567# Entropy methods
4668# ###############
@@ -73,7 +95,7 @@ std(X::Semicircle)=X.radius/2
7395var (X:: Semicircle )= std (X)^ 2
7496
7597# moment of distribution
76- function moment {T<:Real} (X:: Semicircle{T} , order:: Integer )
98+ function moment (X:: Semicircle{T} , order:: Integer ) where {T <: Real }
7799 a, r = X. mean, X. radius
78100 if X. mean != 0
79101 return a^ n* hypergeom ([(1 - n)/ 2 , - n/ 2 ], 2 , (r/ a)^ 2 )
@@ -85,7 +107,7 @@ function moment{T<:Real}(X::Semicircle{T}, order::Integer)
85107end
86108
87109# cumulant of distribution
88- function cumulant {T<:Real} (d:: Semicircle{T} , order:: Integer )
110+ function cumulant (d:: Semicircle{T} , order:: Integer ) where {T <: Real }
89111 if d. mean != 0
90112 throw (ArgumentError (" Non-central Semicircle not supported" ))
91113 end
@@ -100,7 +122,7 @@ function cumulant{T<:Real}(d::Semicircle{T}, order::Integer)
100122end
101123
102124# free cumulant of distribution
103- function freecumulant {T<:Real} (d:: Semicircle{T} , order:: Int )
125+ function freecumulant (d:: Semicircle{T} , order:: Int ) where {T <: Real }
104126 if order == 0
105127 return one (T)
106128 elseif order == 1
0 commit comments