r2rs_nmath/distribution/logis/r.rs
1// Translation of nmath's rlogis
2//
3// Copyright (C) 2020 neek-sss
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18use num_traits::Float;
19use strafe_type::{FloatConstraint, Rational64, Real64};
20
21use crate::traits::RNG;
22
23pub fn rlogis<RE: Into<Real64>, RA: Into<Rational64>, R: RNG>(
24 location: RE,
25 scale: RA,
26 rng: &mut R,
27) -> Real64 {
28 let location = location.into().unwrap();
29 let scale = scale.into().unwrap();
30
31 if !scale.is_finite() {
32 return f64::nan().into();
33 }
34
35 if !location.is_finite() {
36 location
37 } else {
38 let u = rng.unif_rand();
39 location + scale * (u / (1.0 - u)).ln()
40 }
41 .into()
42}