Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
peterschutt opened this issue Apr 4, 2025 · 0 comments
Open

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

peterschutt opened this issue Apr 4, 2025 · 0 comments

Comments

@peterschutt
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant