TDR is an acceptance/rejection method that uses the concavity of a transformed
density to construct a hat function and squeezes automatically. Such PDFs are
called T-concave. Currently the following transformations are implemented:
c=0:T(x)c=−0.5:T(x)=log(x)=x1 (Default)
In addition to the PDF, it also requires the derivative of the PDF w.r.t x
(i.e. the variate). These functions must be present as methods of a Python
object which can then be passed to the generators to instantiate their object.
The variant that is implemented uses squeezes proportional to hat function ([1]).
An example of using this method is shown below:
>>> importnumpyasnp>>> fromscipy.stats.samplingimportTransformedDensityRejection>>> fromscipy.statsimportnorm>>>>>> classStandardNormal:... defpdf(self,x):... # note that the normalization constant is not required... returnnp.exp(-0.5*x*x)... defdpdf(self,x):... return-x*np.exp(-0.5*x*x)...>>> dist=StandardNormal()>>>>>> urng=np.random.default_rng()>>> rng=TransformedDensityRejection(dist,random_state=urng)>>> rng.rvs()-1.526829048388144
In the above example, we have used the TDR method to sample from the standard
normal distribution. Note that we can drop the normalization constant while
computing the PDF. This usually helps speed up the sampling stage. Also, note
that the PDF doesn’t need to be vectorized. It should accept and return a
scalar.
It is also possible to evaluate the inverse of the CDF of the hat distribution
directly using the ppf_hat method.
Apart from the PPF method, other attributes can be accessed
to see how well the generator fits the given distribution. These are:
‘squeeze_hat_ratio’: (area below squeeze) / (area below hat) for the generator. It
is a number between 0 and 1. Closer to 1 means that the hat and the squeeze
functions tightly envelop the distribution and fewer PDF evaluations are
required to generate samples. The expected number of evaluations of the
density is bounded by (1/squeeze_hat_ratio)-1 per sample. By default, it is
kept above 0.99 but that can be changed by passing a max_squeeze_hat_ratio
parameter.
‘hat_area’: area below the hat for the generator.
‘squeeze_area’: area below the squeeze for the generator.
Let’s see how this affects the callbacks to the PDF method of the
distribution:
>>> fromcopyimportcopy>>> classStandardNormal:... def__init__(self):... self.callbacks=0... defpdf(self,x):... self.callbacks+=1... returnnp.exp(-0.5*x*x)... defdpdf(self,x):... return-x*np.exp(-0.5*x*x)...>>> dist1=StandardNormal()>>> urng1=np.random.default_rng()>>> urng2=copy(urng1)>>> rng1=TransformedDensityRejection(dist1,random_state=urng1)>>> dist1.callbacks# evaluations during setup139>>> dist1.callbacks=0# don't consider evaluations during setup>>> rvs=rng1.rvs(100000)>>> dist1.callbacks# evaluations during sampling527>>> dist2=StandardNormal()>>> # use the same stream of uniform random numbers>>> rng2=TransformedDensityRejection(dist2,max_squeeze_hat_ratio=0.999,... random_state=urng2)>>> dist2.callbacks# evaluations during setup467>>> dist2.callbacks=0# don't consider evaluations during setup>>> rvs=rng2.rvs(100000)>>> dist2.callbacks# evaluations during sampling84 # may vary
As we can see, far fewer PDF evaluations are required during sampling when
we increase the squeeze_hat_ratio. The PPF-hat function is also more accurate:
Though, notice that this comes at the cost of increased PDF evaluations
during setup.
For densities with modes not close to 0, it is suggested to set either the
mode or the center of the distribution by passing mode or center
parameters. The latter is the approximate location of the mode or the mean
of the distribution. This location provides some information about the main
part of the PDF and is used to avoid numerical problems.
>>> # mode = 0 for our distribution>>> # if exact mode is not available, pass 'center' parameter instead>>> rng=TransformedDensityRejection(dist,mode=0.)
By default, the method uses 30 construction points to construct the hat.
This can be changed by passing a construction_points parameter which
can either be an array of construction points or an integer representing
the number of construction points to use.
This method accepts many other set-up parameters. See the documentation for
an exhaustive list. More information on the parameters and the method can be
found in Section 5.3.16 of the UNU.RAN user manual.
Please see [1] and [2] for more details on this method.