Count fraction: private API

PortfolioOptimisers.count_or_fraction — Function
count_or_fraction(x::Option{<:UnresolvedValue})
count_or_fraction(x::Integer)
count_or_fraction(x::Real)

Return the AbstractCountOrFraction that a field of a part of a set holds, or the value that the field holds in its place.

The constructor of each such field calls this function, so a plain number becomes a CountOf or a FractionOf, and each reader dispatches on the two members alone. A plain number is the shorthand of AbstractCountOrFraction: the type of the number selects the member.

The function is a repair at a constructor, so it returns unchanged each value that needs no repair: nothing, a member of the family, a rule or a function that computes the part, and a TimeDependent schedule. Each of these is an UnresolvedValue or nothing. Each other type is a MethodError.

Algorithm

  1. Return nothing, a member of the family, or another UnresolvedValue unchanged.
  2. Return CountOf(x) for an Integer. Bool is an Integer, so true is a count of one.
  3. Refuse x == 1 for any other real number. Return FractionOf(x) otherwise.

Validation

  • A plain real number that is not an Integer and equals one throws a DomainError. A search grid promotes the count 1 to 1.0, and the whole set is a different part. State CountOf(1) or FractionOf(1.0).
  • The constructors of CountOf and FractionOf check the range.

Returns

  • x::Option{<:UnresolvedValue}: A CountOf for an Integer, a FractionOf for each other real number, and x unchanged otherwise.

Examples

julia> PortfolioOptimisers.count_or_fraction(3)CountOf  val ┴ Int64: 3julia> PortfolioOptimisers.count_or_fraction(0.2)FractionOf  val ┴ Float64: 0.2

Related

source