pglpm / bayes_nonparametric_inference

Software development for "Bayesian nonparametric population inference". In other words, just the direct application of probability theory to get the most general, principled, model-free inference we can have.
Creative Commons Zero v1.0 Universal
4 stars 1 forks source link

Recycle values in samplesFDistribution.R #27

Open choisant opened 3 weeks ago

choisant commented 3 weeks ago

Can you explain to me what this does and why? I will add the answers to the code.

pglpm commented 3 weeks ago

Recycling is a general feature of R, present in all its functions: see §2.2 in the intro (or a text search on the whole manual). So it often appears naturally as a by-product when writing functions in R. This is the main reason.

It is also useful: for instance if we need the probability of several values of Y conditional on one particular value of X (as it happens when we want to plot P(Y|X) for Y), then thanks to recycling we only need to give one Y argument and a vector of X arguments. Vice versa too, if we need P(Y|X) for one specific value of Y and several conditional values of X. By recycling is also possible to build a grid of values to represent P(X,Y). In base R functions this leads to fast computations because the operation is vectorized internally.

Unfortunately this built-in vectorization doesn't apply to samplesFDistribution – I didn't find a way to exploit R's built-in recycling capabilities for the actual computation there (which is performed in parallel by foreach). But an R user that builds scripts with R's characteristic recycling in mind would be happy that samplesFDistribution respects it.

(If this explains it, feel free to close the issue :) )