Miscellaneous tools
Real-indexed vector
This is a simple implementation of a vector that can be accessed with real (floating point) sub-indices. It does simply a linear binning of a specified (semi-open) interval $[min,max)$ on the real line, and provides a convenient Vector-like syntax to access elements. The stored values can be of any type, but the interval and number of bins are fixed at construction.
julia> A = BinnedVector{String}(5,min=0.,max=1.)5-element BinnedVector{String}: #undef #undef #undef #undef #undefjulia> A[0.1]="hello""hello"julia> A[0.2]="bye bye""bye bye"julia> A5-element BinnedVector{String}: "hello" "bye bye" #undef #undef #undef
To initialise elements with a default value, give function with a signature like zeros:
B = BinnedVector{Rational{Int64}}(10,min=2.,max=8.,init=zeros)
B[2.1]=3//5
B[5.3]=4//1
B10-element BinnedVector{Rational{Int64}}:
3//5
0//1
0//1
0//1
0//1
4//1
0//1
0//1
0//1
0//1Alternatively, it is possible to construct specifying bin width and the interval. In this case, one of the values will need to be rounded to get an integer number of bins:
julia> C = BinnedVector{Int}(Δ=0.15,min=5.,max=10.,round_Δ=RoundUp); (C.Δ, interval(C))(0.15151515151515152, (5.0, 10.0))julia> D = BinnedVector{Int}(Δ=0.15,min=5.,max=10.,round_max=RoundUp); (D.Δ, interval(D))(0.15, (5.0, 10.1))julia> E = BinnedVector{Int}(Δ=0.15,min=5.,max=10.,round_min=RoundUp); (E.Δ, interval(E))(0.15, (5.05, 10.0))
RoundDown is also recognised.
Indexing with integers refers to bin numbers.
julia> A[1:2]2-element Vector{String}: "hello" "bye bye"julia> B[1:5]5-element Vector{Rational{Int64}}: 3//5 0//1 0//1 0//1 0//1
Outliers are mapped to bin 0 if below range, or -1 if above range.
julia> B[0.]=10//3 ; B[40.]=99//2;julia> B[0]10//3julia> B[-1]99//2
The Histogram type is implemented using BinnedVector.
API
BioStatPhys.BinnedVector — Typemutable struct BinnedVector{T}Simple real-indexed vector. Maps a real interval to an integer range 1:nbins then used to index a vector of arbitrary type. Can be used e.g. to build histograms. The real interval and number of bins nbins are fixed at the outset.
Example
Create with
A = BinnedVector{Int}(nbins,min=1.,max=10.,init=zeros)for fixed number of bins, or
A = BinnedVector{Int}(;Δ=0.1,min=1.,max=10.init=zeros,round_Δ=RoundUp)
A = BinnedVector{Int}(;Δ=0.1,min=1.,max=10.init=zeros,round_min=RoundUp)
A = BinnedVector{Int}(;Δ=0.1,min=1.,max=10.init=zeros,round_max=RoundUp)to get fixed (min, max), (Δ,max) or (min,Δ) respectively (rounding mode RoundDown is also recognised).
init is optional, defaults to leave elements undefined.
Access or write as a vector:
A[5.2] += 2Numbers above and below range map to two special bins.
If indexed with integers, these are interpreted as bin numbers. A[0] and A[-1] are the outlier bins (below and above, respectively).
BioStatPhys.interval — Functioninterval(A::BinnedVector{T})Return tuple (min,max) giving the extrema of the real interval mapped to the array bins.
BioStatPhys.delta — Functiondelta(A::BinnedVector{T})Return the with of the bins of A
BioStatPhys.nbins — Functionnbins(A::BinnedVector{T})Return number of bins of A
BioStatPhys.bin — Functionbin(A::BinnedVector{T},x::Float64) where {T}Map real value x to bin number. Return 0 if below range, or -1 if above range.
BioStatPhys.binc — Functionbinc(A::BinnedVector{T},i::Int) where {T}Return center of bin i, which must be in range 1:size(A,1). Does not perform range check.
Base.range — Methodrange(start[, stop]; length, stop, step=1)Given a starting value, construct a range either by length or from start to stop, optionally with a given step (defaults to 1, a UnitRange). One of length or stop is required. If length, stop, and step are all specified, they must agree.
If length and stop are provided and step is not, the step size will be computed automatically such that there are length linearly spaced elements in the range.
If step and stop are provided and length is not, the overall range length will be computed automatically such that the elements are step spaced.
Special care is taken to ensure intermediate values are computed rationally. To avoid this induced overhead, see the LinRange constructor.
stop may be specified as either a positional or keyword argument.
stop as a positional argument requires at least Julia 1.1.
Examples
julia> range(1, length=100)
1:100
julia> range(1, stop=100)
1:100
julia> range(1, step=5, length=100)
1:5:496
julia> range(1, step=5, stop=100)
1:5:96
julia> range(1, 10, length=101)
1.0:0.09:10.0
julia> range(1, 100, step=5)
1:5:96Base.range(A::BinnedVector{T}) where{T}Return range spanning bin centers of A. With r=collect(range(A)) one obtains a Vector such that r[i]=binc(A,i)
Distance binning
The distance_binning function takes a series of points in (2- or 3-$d$) space and creates a BinnedVector that classifies the pairs according to their Euclidean distance.
BioStatPhys.DistanceBinning — TypeAlias for a BinnedVector of Vector{Tuple{Int,Int}}
BioStatPhys.distance_binning — Functiondistance_binning(pos,Δr;rmin=0.,rmax=nothing)Create a BinnedVector of tuples (i,j) with i!=j, where each bin contains all pairs of positions in pos whose distance is within the bin extrema.
pos is expected to be a Matrix where each row pos[i,:] is a 2-d or 3-d position vector.