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

🐞use fractions.Fraction to avoid rounding error in probabilities sum #26

Merged
merged 3 commits into from
May 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/stochatreat/stochatreat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
===============================================================================
"""
from typing import List, Optional
from fractions import Fraction

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -98,7 +99,8 @@ def stochatreat(
probs_np = np.array([frac] * len(treatment_ids))
elif probs is not None:
probs_np = np.array(probs)
if probs_np.sum() != 1:
probs_sum = float(np.array([Fraction(f).limit_denominator() for f in probs]).sum())
if probs_sum != 1:
manmartgarc marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("The probabilities must add up to 1")

assertmsg = "length of treatments and probs must be the same"
Expand Down
Loading