Quantile#

class openstef_core.types.Quantile(value: float)[source]#

Bases: float

A float subclass representing a quantile value between 0 and 1.

Uses Decimal arithmetic internally for all conversions so that values like Quantile(0.999) round-trip exactly through format() / parse() and to_percentile() / from_percentile().

Example

Creating and using quantiles

>>> q50 = Quantile(0.5)  # Median
>>> q50.format()
'quantile_P50'
>>> q95 = Quantile(0.95)  # 95th percentile
>>> q95.format()
'quantile_P95'
>>> # Parse from string
>>> Quantile.parse('quantile_P25')
0.25
>>> # Non-integer quantiles
>>> Quantile(0.025).format()
'quantile_P2.5'
>>> # Percentile conversion
>>> Quantile.from_percentile(99.9).format()
'quantile_P99.9'
>>> # Complementary quantiles
>>> Quantile(0.1).complementary()
0.9
Parameters:

value (float)

static __new__(cls, value: float) → Self[source]#

Create a new Quantile instance with validation.

Parameters:

value (float) – Float value between 0 and 1.

Return type:

Self

Returns:

New Quantile instance.

Raises:

ValueError – If value is not between 0 and 1.

classmethod __get_pydantic_core_schema__(source_type: Any, handler: GetCoreSchemaHandler) → InvalidSchema | AnySchema | NoneSchema | BoolSchema | IntSchema | FloatSchema | DecimalSchema | FractionSchema | StringSchema | BytesSchema | DateSchema | TimeSchema | DatetimeSchema | TimedeltaSchema | LiteralSchema | MissingSentinelSchema | EllipsisSchema | EnumSchema | IsInstanceSchema | IsSubclassSchema | CallableSchema | ListSchema | DequeSchema | TupleSchema | SetSchema | FrozenSetSchema | GeneratorSchema | DictSchema | FrozenDictSchema | OrderedDictSchema | CounterSchema | AfterValidatorFunctionSchema | BeforeValidatorFunctionSchema | WrapValidatorFunctionSchema | PlainValidatorFunctionSchema | WithDefaultSchema | NullableSchema | UnionSchema | TaggedUnionSchema | ChainSchema | LaxOrStrictSchema | JsonOrPythonSchema | TypedDictSchema | ModelFieldsSchema | ModelSchema | DataclassArgsSchema | DataclassSchema | NamedTupleSchema | ArgumentsSchema | ArgumentsV3Schema | CallSchema | CustomErrorSchema | JsonSchema | UrlSchema | MultiHostUrlSchema | DefinitionsSchema | DefinitionReferenceSchema | UuidSchema | ComplexSchema[source]#

Define Pydantic validation and serialization behavior.

Return type:

InvalidSchema | AnySchema | NoneSchema | BoolSchema | IntSchema | FloatSchema | DecimalSchema | FractionSchema | StringSchema | BytesSchema | DateSchema | TimeSchema | DatetimeSchema | TimedeltaSchema | LiteralSchema | MissingSentinelSchema | EllipsisSchema | EnumSchema | IsInstanceSchema | IsSubclassSchema | CallableSchema | ListSchema | DequeSchema | TupleSchema | SetSchema | FrozenSetSchema | GeneratorSchema | DictSchema | FrozenDictSchema | OrderedDictSchema | CounterSchema | AfterValidatorFunctionSchema | BeforeValidatorFunctionSchema | WrapValidatorFunctionSchema | PlainValidatorFunctionSchema | WithDefaultSchema | NullableSchema | UnionSchema | TaggedUnionSchema | ChainSchema | LaxOrStrictSchema | JsonOrPythonSchema | TypedDictSchema | ModelFieldsSchema | ModelSchema | DataclassArgsSchema | DataclassSchema | NamedTupleSchema | ArgumentsSchema | ArgumentsV3Schema | CallSchema | CustomErrorSchema | JsonSchema | UrlSchema | MultiHostUrlSchema | DefinitionsSchema | DefinitionReferenceSchema | UuidSchema | ComplexSchema

Returns:

Core schema for Pydantic validation.

Parameters:
  • source_type (Any)

  • handler (GetCoreSchemaHandler)

format() → str[source]#

Instance method to format the quantile as a string.

Return type:

str

Returns:

Formatted quantile string in ‘quantile_PXX’ format.

classmethod parse(quantile_str: str) → Self[source]#

Parse a quantile string back to a Quantile object.

Parameters:

quantile_str (str) – String in ‘quantile_PXX’ format.

Return type:

Self

Returns:

Parsed Quantile object.

Raises:

ValueError – If the string format is invalid.

static is_valid_quantile_string(quantile_str: str) → bool[source]#

Check if a string is a valid quantile representation.

Parameters:

quantile_str (str) – String to check.

Return type:

bool

Returns:

True if the string is a valid quantile representation, False otherwise.

classmethod from_percentile(percentile: float) → Self[source]#

Create a Quantile from a percentile value.

Parameters:

percentile (float) – Percentile value between 0 and 100.

Return type:

Self

Returns:

Quantile corresponding to the given percentile.

Raises:

ValueError – If the percentile is not between 0 and 100.

to_percentile() → float[source]#

Convert this Quantile to a percentile value.

Return type:

float

Returns:

Percentile value corresponding to this Quantile.

complementary() → Self[source]#

Return the complementary quantile (1 - self).

Return type:

Self