dpqr 0.0.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dpqr/__init__.py ADDED
@@ -0,0 +1,16 @@
1
+ """
2
+ R style distribution and random variable functions.
3
+
4
+ This module provides R style distribution functions.
5
+
6
+ Currently is mainly a wrapper for SciPy distribution functions,
7
+ eventually may convert to Boost C++ functions directly for improved
8
+ vec_recycling performance.
9
+ """
10
+
11
+ from .vec_recycle import *
12
+ from .beta import *
13
+ from .gamma import *
14
+ from .invgamma import *
15
+ from .norm import *
16
+
dpqr/beta.py ADDED
@@ -0,0 +1,73 @@
1
+ import numpy as np
2
+ from scipy.stats import beta
3
+
4
+ from .vec_recycle import vec_recycle_common
5
+
6
+ ###############################################################################
7
+ # Beta
8
+
9
+ # TODO: ncp version not yet implemented
10
+ # dbeta(x, shape1, shape2, ncp = 0, log = FALSE)
11
+ # pbeta(q, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE)
12
+ # qbeta(p, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE)
13
+ # rbeta(n, shape1, shape2, ncp = 0)
14
+
15
+ def dbeta(x, shape1, shape2, log = False):
16
+ """
17
+ Density function for the Beta distribution.
18
+ dbeta(x, shape1, shape2, log = False)
19
+ """
20
+
21
+ x, shape1, shape2 = vec_recycle_common(x, shape1, shape2)
22
+ if log:
23
+ return beta.logpdf(x, a=shape1, b=shape2)
24
+ else:
25
+ return beta.pdf(x, a=shape1, b=shape2)
26
+
27
+ # pbeta(0,1,1,lower_tail=False,log_p=True)
28
+ def pbeta(q, shape1, shape2, lower_tail = True, log_p = False):
29
+ """
30
+ Cumulative Density Function (CDF) for the Beta distribution.
31
+ pbeta(q, shape1, shape2, lower_tail = True, log_p = False):
32
+ """
33
+
34
+ q, shape1, shape2 = vec_recycle_common(q, shape1, shape2)
35
+
36
+ if lower_tail:
37
+ if log_p:
38
+ return beta.logcdf(q, a=shape1, b=shape2)
39
+ else:
40
+ return beta.cdf(q, a=shape1, b=shape2)
41
+ else:
42
+ cdf = np.subtract(1.,beta.cdf(q, a=shape1, b=shape2))
43
+ if log_p:
44
+ with np.errstate(divide='ignore', invalid='ignore'):
45
+ return np.log( cdf )
46
+ else:
47
+ return cdf
48
+
49
+
50
+ def qbeta(p, shape1, shape2, lower_tail = True, log_p = False):
51
+ """
52
+ Inverse Cumulative Density Function (CDF^-1) for the Beta distribution.
53
+ qbeta(p, shape1, shape2, lower_tail = True, log_p = False)
54
+ """
55
+
56
+ if log_p:
57
+ p = np.exp(p)
58
+ if not lower_tail:
59
+ p = np.subtract(1.,p)
60
+ p, shape1, shape2 = vec_recycle_common(p, shape1, shape2)
61
+ q = beta.ppf(p, shape1, shape2)
62
+ return q
63
+
64
+
65
+ def rbeta(n, shape1, shape2):
66
+ """
67
+ Random number generator for the Beta distribution.
68
+ rbeta(n, shape1, shape2)
69
+ """
70
+
71
+ shape1, shape2 = vec_recycle_common(shape1, shape2, n=n)
72
+ return beta.rvs(shape1, shape2, size=n)
73
+
dpqr/gamma.py ADDED
@@ -0,0 +1,77 @@
1
+ #from operator import ne
2
+
3
+ import numpy as np
4
+ from scipy.stats import gamma
5
+
6
+ from .vec_recycle import vec_recycle_common
7
+
8
+ ###############################################################################
9
+ # Gamma
10
+ # TODO: add shape + scale paramaterization
11
+
12
+ # dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE)
13
+ # pgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE)
14
+ # qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE)
15
+ # rgamma(n, shape, rate = 1, scale = 1/rate)
16
+
17
+ def dgamma(x, shape = 1., rate = 1., log = False):
18
+ """
19
+ Density function for the Gamma distribution.
20
+ dgamma(x, shape, rate, log = False)
21
+ """
22
+
23
+ x, shape, scale = vec_recycle_common(x, shape, 1./rate)
24
+ if log:
25
+ x = gamma.logpdf(x, a=shape, scale=scale)
26
+ return np.nan_to_num(x,nan=-np.inf,posinf=np.inf,neginf=-np.inf )
27
+ else:
28
+ x = gamma.pdf(x, a=shape, scale=scale)
29
+ return np.nan_to_num(x,nan=0,posinf=np.inf,neginf=-np.inf )
30
+
31
+
32
+ def pgamma(q, shape=1., rate=1., lower_tail = True, log_p = False):
33
+ """
34
+ Cumulative Density Function (CDF) for the Gamma distribution.
35
+ pgamma(q, shape1, shape2, lower_tail = True, log_p = False):
36
+ """
37
+
38
+ q, shape, scale = vec_recycle_common(q, shape, 1./rate)
39
+ if lower_tail:
40
+ if log_p:
41
+ with np.errstate(divide='ignore', invalid='ignore'):
42
+ return gamma.logcdf(q, a=shape, scale=scale)
43
+ else:
44
+ return gamma.cdf(q, a=shape, scale=scale)
45
+ else:
46
+ cdf = np.subtract(1.,gamma.cdf(q, a=shape, scale=scale))
47
+ if log_p:
48
+ with np.errstate(divide='ignore', invalid='ignore'):
49
+ return np.log( cdf )
50
+ else:
51
+ return cdf
52
+
53
+
54
+ def qgamma(p, shape=1., rate=1., lower_tail = True, log_p = False):
55
+ """
56
+ Inverse Cumulative Density Function (CDF^-1) for the Gamma distribution.
57
+ qgamma(p, shape, rate, lower_tail = True, log_p = False)
58
+ """
59
+
60
+ if log_p:
61
+ p = np.exp(p)
62
+ if not lower_tail:
63
+ p = np.subtract(1.,p)
64
+ p, shape, scale = vec_recycle_common(p, shape, 1./rate)
65
+ q = gamma.ppf(p, shape, scale=scale)
66
+ return q
67
+
68
+
69
+ def rgamma(n, shape, rate=1.):
70
+ """
71
+ Random number generator for the Gamma distribution.
72
+ rgamma(n, shape, rate)
73
+ """
74
+
75
+ shape, scale = vec_recycle_common(shape, 1./rate, n=n)
76
+ return gamma.rvs(shape, scale=scale, size=n)
77
+
dpqr/invgamma.py ADDED
@@ -0,0 +1,80 @@
1
+ import numpy as np
2
+ from scipy.stats import invgamma
3
+
4
+ from .vec_recycle import vec_recycle_common
5
+
6
+ ###############################################################################
7
+ # Beta
8
+
9
+ # note, invgamma not part of the base R definitions.
10
+ # using paramaterization from R library MCMCpack, not invgamma
11
+ # dinvgamma(x, shape, scale = 1, log = FALSE)
12
+ # pinvgamma(q, shape, scale = 1, lower.tail = TRUE, log.p = FALSE)
13
+ # qinvgamma(p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE)
14
+ # rinvgamma(n, shape, scale = 1)
15
+
16
+ # only scale or rate can be defined.
17
+ #scale = 1., rate = 1./scale
18
+ # or just skip rate definition
19
+ #def dinvgamma(x, shape = 1., scale = None, rate = None, log = False):
20
+ def dinvgamma(x, shape = 1., scale = 1., log = False):
21
+ """
22
+ Density function for the Inverse Gamma distribution.
23
+ dinvgamma(x, shape, scale, log = False)
24
+ """
25
+
26
+ x, shape, scale = vec_recycle_common(x, shape, scale)
27
+ if log:
28
+ x = invgamma.logpdf(x, a=shape, scale=scale)
29
+ return np.nan_to_num(x, posinf=np.inf,neginf=-np.inf )
30
+ else:
31
+ x = invgamma.pdf(x, a=shape, scale=scale)
32
+ return np.nan_to_num(x, posinf=np.inf,neginf=-np.inf )
33
+
34
+
35
+ def pinvgamma(q, shape=1., scale=1., lower_tail = True, log_p = False):
36
+ """
37
+ Cumulative Density Function (CDF) for the Inv Gamma distribution.
38
+ pinvgamma(q, shape, scale, lower_tail = True, log_p = False):
39
+ """
40
+
41
+ q, shape, scale = vec_recycle_common(q, shape, scale)
42
+ if lower_tail:
43
+ if log_p:
44
+ return invgamma.logcdf(q, a=shape, scale=scale)
45
+ else:
46
+ return invgamma.cdf(q, a=shape, scale=scale)
47
+ else:
48
+
49
+ cdf = 1 - invgamma.cdf(q, a=shape, scale=scale)
50
+ if log_p:
51
+ with np.errstate(divide='ignore', invalid='ignore'):
52
+ return np.log( cdf )
53
+ else:
54
+ return cdf
55
+
56
+
57
+ def qinvgamma(p, shape=1., scale=1., lower_tail = True, log_p = False):
58
+ """
59
+ Inverse Cumulative Density Function (CDF^-1) for the Inv Gamma distribution.
60
+ qinvgamma(p, shape, scale, lower_tail = True, log_p = False)
61
+ """
62
+
63
+ if log_p:
64
+ p = np.exp(p)
65
+ if not lower_tail:
66
+ p = np.subtract(1., p)
67
+ p, shape, scale = vec_recycle_common(p, shape, scale)
68
+ q = invgamma.ppf(p, shape, scale=scale)
69
+ return q
70
+
71
+
72
+ def rinvgamma(n, shape, scale):
73
+ """
74
+ Random number generator for the Inv Gamma distribution.
75
+ rinvgamma(n, shape, scale)
76
+ """
77
+
78
+ shape, scale = vec_recycle_common(shape, scale, n=n)
79
+ return invgamma.rvs(shape, scale=scale, size=n)
80
+
dpqr/norm.py ADDED
@@ -0,0 +1,66 @@
1
+ import numpy as np
2
+ from scipy.stats import norm
3
+
4
+ from .vec_recycle import vec_recycle_common
5
+
6
+ ###############################################################################
7
+ # Normal
8
+
9
+ def dnorm(x, mean = 0., sd = 1., log = False):
10
+ """
11
+ Density function for the normal distribution.
12
+ dnorm(x, mean = 0., sd = 1., log = False)
13
+ """
14
+
15
+ x, mean, sd = vec_recycle_common(x, mean, sd)
16
+ if log:
17
+ return norm.logpdf(x, loc=mean, scale=sd)
18
+ else:
19
+ return norm.pdf(x, loc=mean, scale=sd)
20
+
21
+
22
+ def pnorm(q, mean = 0., sd = 1., lower_tail = True, log_p = False):
23
+ """
24
+ Cumulative Density Function (CDF) for the normal distribution.
25
+ pnorm(q, mean = 0., sd = 1., lower_tail = True, log_p = False)
26
+ """
27
+
28
+ q, mean, sd = vec_recycle_common(q, mean, sd)
29
+ if lower_tail:
30
+ if log_p:
31
+ return norm.logcdf(q, loc=mean, scale=sd)
32
+ else:
33
+ return norm.cdf(q, loc=mean, scale=sd)
34
+ else:
35
+ cdf = np.subtract(1., norm.cdf(q, loc=mean, scale=sd) )
36
+ if log_p:
37
+ with np.errstate(divide='ignore', invalid='ignore'):
38
+ return np.log( cdf )
39
+ else:
40
+ return cdf
41
+
42
+
43
+ def qnorm(p, mean = 0., sd = 1., lower_tail = True, log_p = False):
44
+ """
45
+ Inverse Cumulative Density Function (CDF^-1) for the normal distribution.
46
+ qnorm(p, mean = 0, sd = 1, lower_tail = True, log_p = False)
47
+ """
48
+
49
+ if log_p:
50
+ p = np.exp(p)
51
+ if not lower_tail:
52
+ p = np.subtract(1., p)
53
+ p, mean, sd = vec_recycle_common(p, mean, sd)
54
+ q = norm.ppf(p, loc=mean, scale=sd)
55
+ return q
56
+
57
+
58
+ def rnorm(n=None, mean = 0., sd = 1.):
59
+ """
60
+ Random number generator for the normal distribution.
61
+ rnorm(n=None, mean = 0, sd = 1)
62
+ """
63
+
64
+ mean, sd = vec_recycle_common(mean, sd, n=n)
65
+ return norm.rvs(loc=mean, scale=sd, size=n)
66
+
dpqr/py.typed ADDED
File without changes
dpqr/vec_recycle.py ADDED
@@ -0,0 +1,35 @@
1
+ import numpy as np
2
+ from collections.abc import Sized
3
+
4
+ ###############################################################################
5
+ # common functions
6
+
7
+ def vec_recycle(x, n):
8
+ """
9
+ Recycle vector values to a specified length.
10
+ """
11
+
12
+ # check if resize is needed.
13
+ if isinstance(x, Sized):
14
+ if len(x) != n:
15
+ x = np.resize(x, n)
16
+ else:
17
+ if n > 1:
18
+ x = np.resize(x, n)
19
+ return x
20
+
21
+ def vec_recycle_common( *xs, n=None):
22
+ """
23
+ Recycle multiple vectors to a common length.
24
+ """
25
+
26
+ # ensure all scalars or vectors.
27
+ if any( [isinstance(x, Sized) for x in xs] ):
28
+ xs = np.atleast_1d(*xs)
29
+
30
+ # if n is missing, calculate it.
31
+ if n is None:
32
+ n = max([ len(x) if isinstance(x, Sized) else 1 for x in xs] )
33
+
34
+ return tuple([vec_recycle(x, n) for x in xs])
35
+
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.4
2
+ Name: dpqr
3
+ Version: 0.0.0.dev0
4
+ Summary: R style dpqr distribution functions
5
+ Keywords: R,cran,distribution,dpqr,rnorm
6
+ Author: Jacob Colvin
7
+ Author-email: Jacob Colvin <28549119+jbcolvin0@users.noreply.github.com>
8
+ License-Expression: GPL-3.0-or-later
9
+ Requires-Dist: numpy
10
+ Requires-Dist: scipy
11
+ Requires-Python: >=3.10
12
+ Project-URL: Homepage, https://github.com/jbcolvin0/dpqr/
13
+ Project-URL: Repository, https://github.com/jbcolvin0/dpqr.git
14
+ Description-Content-Type: text/markdown
15
+
16
+ # dpqr
17
+ R style dpqr distribution functions.
18
+
19
+ # NOTE: this is still experimental / in development.
20
+ version 0.0.0dev0
21
+
@@ -0,0 +1,10 @@
1
+ dpqr/__init__.py,sha256=fhevHeukwLP2wyjIu5IRIJRvKmZq3FumbCSNO0WkgPI,388
2
+ dpqr/beta.py,sha256=1P17lP-G9qcoXlYV3pHEVYYnNDVA57rUCiLSo1vJYNM,2147
3
+ dpqr/gamma.py,sha256=ZmIZc_YbZuAYeOpj-EwOFFyFm-uWkVEt6aPKBGPYmag,2406
4
+ dpqr/invgamma.py,sha256=RBttPoZ1YaVIk80nPOoy4oRP-z-HHqg1Rx75-vnR2NY,2552
5
+ dpqr/norm.py,sha256=E2EwbUeznYSdCX70P0zU7IFmskJ5CWsxsg-h0g9zLRE,1854
6
+ dpqr/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ dpqr/vec_recycle.py,sha256=RdbWgNIgV_oBO7n6nneLthHC1aNu9qsbScAQyviyxnk,845
8
+ dpqr-0.0.0.dev0.dist-info/WHEEL,sha256=f5fWSvWsg5Knq5GWa6t1nJIug0Tqo69GqAWD_9LbBKw,81
9
+ dpqr-0.0.0.dev0.dist-info/METADATA,sha256=0vRfXgPxKG8uOa6h0VCePFy4STMXASYAPX1mM7mCibY,615
10
+ dpqr-0.0.0.dev0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.11.16
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any