discretes discretes website

Codecov test coverage License: MIT Lifecycle: stable Netlify Status R-CMD-check

The discretes package provides a framework for representing numeric series that may be finite or infinite. Think 1:Inf, without storing all the values explicitly.

Series can be traversed, checked for which values are in them, and queried for limit points (“sinks”). They can be manipulated to create new series, such as by transforming or combining. Rules for doing so are delegated to base R whenever possible to maintain congruence with numeric vectors.

The name “discretes” reflects the original use case of representing the support of discrete probability distributions like the Poisson or Geometric, which are often infinite but enumerable. The package is not limited to probability applications: it is designed as a general-purpose tool for working with enumerable numeric series.

Installation

Install discretes from CRAN with:

install.packages("discretes")

Examples

library(discretes)

While vectors in R must be finite, this is not a problem for discretes.

natural0()
#> Integer series of length Inf:
#> 0, 1, 2, 3, 4, 5, ...

These objects are referred to as “numeric series”, and have class “discretes”. Their members are referred to as “discrete values”.

What’s the next discrete value after 10 in the series of integers? Or the previous five values from 1.3?

next_discrete(integers(), from = 10)
#> [1] 11
prev_discrete(integers(), from = 1.3, n = 5)
#> [1]  1  0 -1 -2 -3

Test whether values are discrete values in a series.

has_discretes(natural1(), c(0, 1))
#> [1] FALSE  TRUE

Perform arithmetic operations on series.

1 / 2^integers()
#> Reciprocal series of length Inf:
#> Loading required namespace: testthat
#> ..., 0.25, 0.5, 1, 2, 4, 8, ...

A new series is created after a base series gets modified. See the Creating numeric series vignette for what’s allowed (e.g. monotonic transformations) and how to use dsct_transform() for custom transformations.

Sinks

That last series above, 1 / 2^integers(), has a sink1 at 0 (approached from the right) and a sink at infinity, best seen by plotting:

x <- 1 / 2^integers()
plot(x)

Notice that there are infinitely many discrete values close to 0.

num_discretes(x, from = 0, to = 1)
#> [1] Inf

There is no such thing as a “next” value when looking left of the sink.

next_discrete(x, from = -1)
#> numeric(0)

You can ask whether a sink exists directly.

has_sink_in(x, from = 0, to = 1)
#> [1] TRUE
has_sink_at(x, 0, dir = "right")
#> [1] TRUE

Vignettes

There are two main vignettes explaining how to use this package.

In addition, there are two technical vignettes for those interested in more details.

Limitations

The series supported by the package include arithmetic series like integers, finite series from a numeric vector, and series created from them. Specialized series like the Fibonacci numbers are not explicitly supported. Dense countable sets like the rational numbers are also not supported because they do not have a well-defined notion of local successor/predecessor.

Code of Conduct

Please note that the discretes project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Similar Packages

Acknowledgements

Development of this package would not have been possible without the funding and support of the European Space Agency, BGC Engineering Inc., and the Politecnico di Milano. The need for this package arose from work on the probaverse project, which aims to provide tools for probabilistic modeling and inference in R.


  1. Or more precisely, a limit point.↩︎