1

I use the LearnDistribution function to learn the distribution of some data. Then, I want to export some code to sample from that learned distribution. I tried to use Compile as follows

compFun=Compile[{}, RandomVariate[ld]]

where ld is a LearnedDistribution. However, this fails and the function cannot be compiled and exported. Is there any way to export a function to sample from a LearnedDistribution so that it can be used in another language, like Python?

sepehr78
  • 67
  • 6

1 Answers1

5

If you use the option Method -> "KernelDensityEstimation" in LearnDistribution, then the estimated bandwidth can be extracted. Call that value bw. (And I'm assuming that a Gaussian kernel will be used.)

(* Generate some data *)
SeedRandom[12345];
data = RandomVariate[NormalDistribution[0, 1], 100]

(* Use LearnDistribution *) ld = LearnDistribution[data, Method -> "KernelDensityEstimation"]

Based on the answer from @MarcoB one can obtain the bandwidth of the Gaussian kernel with

bw = First[ld]["Model", "KernelSize"]
(* 0.510338 *)

Now armed with the bandwidth (i.e., "KernelSize") to obtain a random sample from that estimated distribution one first randomly selects an observation from the original data. Then select a random sample from a normal distribution with a mean of the random observation and a standard deviation of bw. (Or equivalently, select a random sample from a normal distribution with mean 0 and standard deviation bw and then add in a random selection from the original data.)

RandomVariate[NormalDistribution[data[[RandomInteger[{1, Length[data]}]]], bw]]

It might even make more sense to write code in the external application that just uses the data, the estimated bandwidth, and whatever random functions are available especially if multiple random samples are contemplated.

I'll ignore the use of Compile (another subject I know little about).

For a much, much better description of this process see @whuber's answer at CrossValidated.

JimB
  • 41,653
  • 3
  • 48
  • 106