0
$\begingroup$

I have a few data point, that I can interpolate into a distribution. I want to be able to generate points that follow this distribution, but Mathematica does not seem to be happy.

Here is my code:

data = {{-493.394, 2.68462}, {-439.841, 0.817059}, {-392.376, 
8.56566}, {-346.081, 9.54882}, {-292.772, 21.9888}, {-245.355, 
37.5084}, {-194.096, 55.3625}, {-147.557, 58.1818}, {-95.0778, 
87.6094}, {-49.8689, 85.9708}, {5.78225, 99.1785}, {51.3327, 
71.4299}, {103.873, 73.3692}, {151.79, 43.5556}, {203.562, 
32.7452}, {248.295, 13.67}, {304.263, 10.4961}, {348.899, 
5.15376}, {400.756, 4.98765}, {448.088, 0.95174}};

Then I interpolate it

interpolation = Interpolation[data]

Now, what I want is to define the distribution

pdf = ProbabilityDistribution[interpolation, {v, -480, 440}];

And finally generate the points:

newdata = RandomVariate[pdf, 1000]

Unfortunately, this gives this error.

InterpolatingFunction::nomthd: There is no method RandomType for InterpolatingFunction objects.

RandomVariate::udist: The specification 
InterpolatingFunction[{{-493.394,448.088}},{5,7,0,{20},{4},0,0,0,0,Automatic,
{},{},False},<<1>>,{Developer`PackedArrayForm,{0,<<20>>},
{2.68462,0.817059,8.56566,9.54882,21.9888,37.5084,
<<9>>,13.67,10.4961,5.15376,4.98765,0.95174}},{Automatic}] is not a random 
distribution recognized by the system.

What should I do? Just to clarify, I don't have the initial dataset, I just have the numerical values for the distribution.

Thanks,

Lina

$\endgroup$
  • $\begingroup$ Did you check the area under the curve? $\endgroup$ – J. M. will be back soon Sep 23 '17 at 0:10
  • $\begingroup$ @J.M. You mean whether it is normalized? It is not normalized, but generally "ProbabilityDistribution" deals with that. $\endgroup$ – Lina Sep 23 '17 at 0:15
5
$\begingroup$

You have a couple issues. First, you need to give the InterpolatingFunction generated by Interpolation an argument. Here is your interpolation:

i = Interpolation[data];
i //OutputForm

InterpolatingFunction[{{-493.394, 448.088}}, <>]

In order to extract values from an interpolating function, you need to give it an argument:

i[1]

99.152

ProbabilityDistribution is expecting an expression in the variable that is specified in the second argument, so instead of:

ProbabilityDistribution[i, {x, -493, 448}]

you need to use:

dist = ProbabilityDistribution[i[x], {x, -493, 448}, Method->"Normalize"];

ProbabilityDistribution::nvpdf: The PDF of the given distribution is not non-negative.

where I use the "Normalize" method because your data is not normalized. Here is where your second issue comes up. The default interpolation causes the pdf to be negative. Here is a plot of your pdf:

Plot[i[x],{x,-493,448}]

enter image description here

Notice that the curve drops below the x-axis around $x\approx -470$. The simplest way to fix this is to give the Interpolation function an InterpolationOrder option:

i = Interpolation[data, InterpolationOrder->1];
dist = ProbabilityDistribution[i[x], {x, -493, 448}, Method->"Normalize"];

This time, ProbabilityDistribution doesn't complain about the pdf. Now, we can use RandomVariate:

RandomVariate[dist, 10]

{85.6924, -314.322, 112.498, 53.6079, 5.81098, 12.4271, 124.443, 150.017, \ -30.7861, -41.2655}

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.