Rcpp Version 1.0.9
logis.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // logis.h: Rcpp R/C++ interface class library --
4 //
5 // Copyright (C) 2010 - 2016 Douglas Bates, Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__stats__logis_h
23 #define Rcpp__stats__logis_h
24 
25 namespace Rcpp{
26 namespace stats{
27 
28 inline double dlogis_0(double x /*, double location [=0.0], double scale [=1.0] */, int give_log){
29  double e, f;
30 #ifdef IEEE_754
31  if (ISNAN(x))
32  return x + 1.0;
33 #endif
34 
35  e = ::exp(-::fabs(x));
36  f = 1.0 + e;
37  return give_log ? -(x + ::log(f * f)) : e / (f * f);
38 }
39 
40 inline double dlogis_1(double x, double location /*, double scale [=1.0] */, int give_log){
41  double e, f;
42 #ifdef IEEE_754
43  if (ISNAN(x) || ISNAN(location))
44  return x + location + 1.0;
45 #endif
46 
47  x = ::fabs((x - location));
48  e = ::exp(-x);
49  f = 1.0 + e;
50  return give_log ? -(x + ::log(f * f)) : e / (f * f);
51 }
52 
53 
54 inline double plogis_0(double x /*, double location [=0.0] , double scale [=1.0] */,
55  int lower_tail, int log_p) {
56 #ifdef IEEE_754
57  if (ISNAN(x))
58  return x + 1.0;
59 #endif
60 
61  if (ISNAN(x)) return R_NaN;
63 
64  x = ::exp(lower_tail ? -x : x);
65  return (log_p ? -::log1p(x) : 1 / (1 + x));
66 }
67 
68 
69 inline double plogis_1(double x, double location /*, double scale [=1.0] */,
70  int lower_tail, int log_p) {
71 #ifdef IEEE_754
72  if (ISNAN(x) || ISNAN(location))
73  return x + location + 1.0;
74 #endif
75 
76  x = (x - location);
77  if (ISNAN(x)) return R_NaN;
79 
80  x = ::exp(lower_tail ? -x : x);
81  return (log_p ? -::log1p(x) : 1 / (1 + x));
82 }
83 
84 inline double qlogis_0(double p /*, double location [=0.0], double scale [=1.0] */,
85  int lower_tail, int log_p) {
86 #ifdef IEEE_754
87  if (ISNAN(p))
88  return p + 1.0;
89 #endif
91 
92  /* p := logit(p) = log(p / (1. - p)) : */
93  if (log_p) {
94  if (lower_tail)
95  p = p - ::log1p(- ::exp(p));
96  else
97  p = ::log1p(- ::exp(p)) - p;
98  }
99  else
100  p = ::log(lower_tail ? (p / (1. - p)) : ((1. - p) / p));
101 
102  return p;
103 }
104 
105 
106 inline double qlogis_1(double p, double location /*, double scale [=1.0] */,
107  int lower_tail, int log_p) {
108 #ifdef IEEE_754
109  if (ISNAN(p) || ISNAN(location))
110  return p + location + 1.0;
111 #endif
113 
114  /* p := logit(p) = log(p / (1. - p)) : */
115  if (log_p) {
116  if (lower_tail)
117  p = p - ::log1p(- ::exp(p));
118  else
119  p = ::log1p(- ::exp(p)) - p;
120  }
121  else
122  p = ::log(lower_tail ? (p / (1. - p)) : ((1. - p) / p));
123 
124  return location + p;
125 }
126 
127 }
128 }
129 
132 RCPP_DPQ_2(logis,::Rf_dlogis,::Rf_plogis,::Rf_qlogis)
133 
134 #endif
135 
#define RCPP_DPQ_2(__NAME__, __D__, __P__, __Q__)
Definition: dpq.h:363
#define RCPP_DPQ_1(__NAME__, __D__, __P__, __Q__)
Definition: dpq.h:340
#define RCPP_DPQ_0(__NAME__, __D__, __P__, __Q__)
Definition: dpq.h:318
double log1p(double x)
void exp(InputIterator begin, InputIterator end, OutputIterator out)
Definition: algorithm.h:474
void log(InputIterator begin, InputIterator end, OutputIterator out)
Definition: algorithm.h:469
double plogis_1(double x, double location, int lower_tail, int log_p)
Definition: logis.h:69
double plogis_0(double x, int lower_tail, int log_p)
Definition: logis.h:54
double qlogis_1(double p, double location, int lower_tail, int log_p)
Definition: logis.h:106
double dlogis_1(double x, double location, int give_log)
Definition: logis.h:40
double dlogis_0(double x, int give_log)
Definition: logis.h:28
double qlogis_0(double p, int lower_tail, int log_p)
Definition: logis.h:84
Rcpp API.
Definition: algo.h:28
#define R_P_bounds_Inf_01(x)
Definition: macros.h:107
#define R_Q_P01_boundaries(p, _LEFT_, _RIGHT_)
Definition: macros.h:84
#define give_log
Definition: macros.h:24
#define ML_POSINF
Definition: stats.h:27
#define ML_NEGINF
Definition: stats.h:28