sample_beta_with_mean#

bamengine.utils.sample_beta_with_mean(mean, n=1, low=None, high=None, concentration=12.0, *, relative_margin=0.5, rng=None)[source]#

Draw n samples from a Beta distribution scaled to [low, high), such that the scaled mean is approximately ``mean``.

Parameters:
  • mean (float) – Desired mean of the returned samples. Can be any positive value.

  • n (int, default 1) – Number of samples to draw.

  • low (float or None, optional) –

    Bounds of the target interval. If either is None, it is derived as:

    low = mean * (1 - relative_margin)
    high = mean * (1 + relative_margin)
    

    making the mean the midpoint of the interval. A tiny eps is added when needed to guarantee low < mean < high.

  • high (float or None, optional) –

    Bounds of the target interval. If either is None, it is derived as:

    low = mean * (1 - relative_margin)
    high = mean * (1 + relative_margin)
    

    making the mean the midpoint of the interval. A tiny eps is added when needed to guarantee low < mean < high.

  • concentration (float, default 12) – Total pseudo–sample size of the Beta (a+b). Larger values concentrate the draws more tightly around mean.

  • relative_margin (float, default 0.50) – Half-width of the automatically generated interval as a fraction of mean. Set 0.25 for ±25 %, 1.0 for ±100 %, etc.

  • rng (np.random.Generator, optional) – Random number generator (falls back to default_rng()).

Returns:

n samples if n > 1; otherwise a scalar.

Return type:

float or ndarray

Notes

  • If mean is very close to zero, the automatically chosen low may be negative; it is then clipped to zero and an eps-wide gap is kept so that mean stays strictly inside the interval.

  • The function raises ValueError for invalid arguments.