_statistics.py 4.99 KB
Newer Older
root's avatar
root committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from cupy import _core


logit_definition = """
template <typename T>
static __device__ T logit(T x) {
    x /= 1 - x;
    return log(x);
}

"""


logit = _core.create_ufunc(
    'cupy_logit',
    ('e->f', 'f->f', 'd->d'),
    'out0 = logit(in0)',
    preamble=logit_definition,
    doc='''Logit function.

    Args:
        x (cupy.ndarray): input data

    Returns:
        cupy.ndarray: values of logit(x)

    .. seealso:: :data:`scipy.special.logit`

    ''')


expit_definition = """
template <typename T>
static __device__ T expit(T x) {
    return 1 / (1 + exp(-x));
}

"""


expit = _core.create_ufunc(
    'cupy_expit',
    ('e->f', 'f->f', 'd->d'),
    'out0 = expit(in0)',
    preamble=expit_definition,
    doc='''Logistic sigmoid function (expit).

    Args:
        x (cupy.ndarray): input data (must be real)

    Returns:
        cupy.ndarray: values of expit(x)

    .. seealso:: :data:`scipy.special.expit`

    .. note::
        expit is the inverse of logit.

    ''')


# log_expit implemented based on log1p as in SciPy's scipy/special/_logit.h

log_expit_definition = """
template <typename T>
static __device__ T log_expit(T x)
{
    if (x < 0.0) {
        return x - log1p(exp(x));
    } else {
        return -log1p(exp(-x));
    }
}

"""


log_expit = _core.create_ufunc(
    'cupy_log_expit',
    ('e->f', 'f->f', 'd->d'),
    'out0 = log_expit(in0)',
    preamble=log_expit_definition,
    doc='''Logarithm of the logistic sigmoid function.

    Args:
        x (cupy.ndarray): input data (must be real)

    Returns:
        cupy.ndarray: values of log(expit(x))

    .. seealso:: :data:`scipy.special.log_expit`

    .. note::
        The function is mathematically equivalent to ``log(expit(x))``, but
        is formulated to avoid loss of precision for inputs with large
        (positive or negative) magnitude.
    ''')


boxcox_definition = """
static __device__ double boxcox(double x, double lmbda) {
    // if lmbda << 1 and log(x) < 1.0, the lmbda*log(x) product can lose
    // precision, furthermore, expm1(x) == x for x < eps.
    // For doubles, the range of log is -744.44 to +709.78, with eps being
    // the smallest value produced.  This range means that we will have
    // abs(lmbda)*log(x) < eps whenever abs(lmbda) <= eps/-log(min double)
    // which is ~2.98e-19.
    if (fabs(lmbda) < 1e-19) {
        return log(x);
    } else {
        return expm1(lmbda * log(x)) / lmbda;
    }
}

"""


boxcox = _core.create_ufunc(
    'cupy_boxcox',
    ('ee->f', 'ff->f', 'dd->d'),
    'out0 = out0_type(boxcox(in0, in1))',
    preamble=boxcox_definition,
    doc='''Compute the Box-Cox transformation.

    Args:
        x (cupy.ndarray): input data (must be real)

    Returns:
        cupy.ndarray: values of boxcox(x)

    .. seealso:: :data:`scipy.special.boxcox`

    ''')


boxcox1p_definition = """
static __device__ double boxcox1p(double x, double lmbda) {
    // The argument given above in boxcox applies here with the modification
    // that the smallest value produced by log1p is the minimum representable
    // value, rather than eps.  The second condition here prevents underflow
    // when log1p(x) is < eps.
    double lgx = log1p(x);
    if ((fabs(lmbda) < 1e-19)
        || ((fabs(lgx) < 1e-289) && (fabs(lmbda) < 1e273))) {
        return lgx;
    } else {
        return expm1(lmbda * lgx) / lmbda;
    }
}

"""


boxcox1p = _core.create_ufunc(
    'cupy_boxcox1p',
    ('ee->f', 'ff->f', 'dd->d'),
    'out0 = out0_type(boxcox1p(in0, in1))',
    preamble=boxcox1p_definition,
    doc='''Compute the Box-Cox transformation op 1 + `x`.

    Args:
        x (cupy.ndarray): input data (must be real)

    Returns:
        cupy.ndarray: values of boxcox1p(x)

    .. seealso:: :data:`scipy.special.boxcox1p`

    ''')


inv_boxcox_definition = """
static __device__ double inv_boxcox(double x, double lmbda) {
    if (lmbda == 0.0) {
        return exp(x);
    } else {
        return exp(log1p(lmbda * x) / lmbda);
    }
}

"""


inv_boxcox = _core.create_ufunc(
    'cupy_inv_boxcox',
    ('ee->f', 'ff->f', 'dd->d'),
    'out0 = out0_type(inv_boxcox(in0, in1))',
    preamble=inv_boxcox_definition,
    doc='''Compute the Box-Cox transformation.

    Args:
        x (cupy.ndarray): input data (must be real)

    Returns:
        cupy.ndarray: values of inv_boxcox(x)

    .. seealso:: :data:`scipy.special.inv_boxcox`

    ''')


inv_boxcox1p_definition = """
static __device__ double inv_boxcox1p(double x, double lmbda) {
    if (lmbda == 0.0) {
        return expm1(x);
    } else if (fabs(lmbda * x) < 1e-154) {
        return x;
    } else {
        return expm1(log1p(lmbda * x) / lmbda);
    }
}

"""


inv_boxcox1p = _core.create_ufunc(
    'cupy_inv_boxcox1p',
    ('ee->f', 'ff->f', 'dd->d'),
    'out0 = out0_type(inv_boxcox1p(in0, in1))',
    preamble=inv_boxcox1p_definition,
    doc='''Compute the Box-Cox transformation op 1 + `x`.

    Args:
        x (cupy.ndarray): input data (must be real)

    Returns:
        cupy.ndarray: values of inv_boxcox1p(x)

    .. seealso:: :data:`scipy.special.inv_boxcox1p`
''')