Skip to content

Expose a rust lib in addition to the python lib #17

@peterschutt

Description

@peterschutt

I've now got a version of this library that can also be used in rust/cargo projects. You can cargo add it, and use it like this:

let odds = [2.0, 3.5, 4.0];

// Simple usage
let result = implied_probabilities(&odds)
    .solve()
    .unwrap();
let probabilities = &result.probs;

// With custom parameters
let result = implied_probabilities(&odds)
    .with_max_iterations(1000)
    .with_convergence_threshold(1e-12)
    .solve()
    .unwrap();

// Accessing full details
println!("Probabilities: {:?}", result.probs);
println!("Iterations: {}", result.iterations);
println!("Delta: {}", result.delta);
println!("Z value: {}", result.z);

To do this, I've pushed all of the logic of calculate_implied_probabilities() except for the odds mapping input conversion logic down into the rust implementation. So the python now looks like this:

...
from .shin import calculate_implied_probabilities as _calculate_implied_probabilities

def calculate_implied_probabilities(
    odds: Sequence[float] | Mapping[T, float],
    *,
    max_iterations: int = 1000,
    convergence_threshold: float = 1e-12,
    full_output: bool = False,
) -> (
    ShinOptimisationDetails[list[float]]
    | ShinOptimisationDetails[dict[T, float]]
    | list[float]
    | dict[T, float]
):
    odds_seq = list(odds.values()) if isinstance(odds, Mapping) else odds

    res = _calculate_implied_probabilities(
        odds_seq,
        max_iterations=max_iterations,
        convergence_threshold=convergence_threshold,
    )

    if isinstance(odds, Mapping):
        d = dict(zip(odds, res.probs, strict=False))
        if full_output:
            return ShinOptimisationDetails(
                implied_probabilities=d,
                iterations=res.iterations,
                delta=res.delta,
                z=res.z,
            )
        return d

    if full_output:
        return ShinOptimisationDetails(
            implied_probabilities=res.probs,
            iterations=res.iterations,
            delta=res.delta,
            z=res.z,
        )
    return res.probs

Note that I've removed the pure python optimizer functionality, but that could still be retained if you'd like.

Any interest in making similar changes here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions