14

在 Mathematica 8 中,我想定义一个离散分布,其密度质量以列表形式给出。例如,

In[1] f = ProbabilityDistribution[{2/3, 1/3}[[x]], {x, 1, 2, 1}];

这似乎有效。但是,这发出了两次重复的警告:

"Part::pspec: Part specification x is neither an integer nor a list of integers." 

尽管如此, f 似乎工作正常。这个消息让我想到可能有更好的方法来定义相同的分布。如何使用列表定义离散分布但不调用警告?

4

2 回答 2

17

You may want to use EmpiricalDistribution when constructing a distribution from a list of values:

empiricalDistribution = EmpiricalDistribution[{2/3, 1/3} -> {1, 2}]

and you can then use this in other statistical and visualization functions:

Plot[CDF[empiricalDistribution][x], {x, 0, 4}]

The function ProbabilityDistribution is more appropriate when you have a pdf.

于 2011-11-10T18:44:25.323 回答
13

可以将权重列表转换为分段,并将其提供给 ProbabilityDistribution。

wts = {2/3, 1/3};
toPiecewise[wts_, x_] := 
 Piecewise[MapIndexed[{#1, x == #2[[1]]} &, wts]]

In[178]:= f = 
 ProbabilityDistribution[toPiecewise[wts, x], {x, 1, 2, 1}]

Out[178]= ProbabilityDistribution[
 Piecewise[{{2/3, \[FormalX] == 1}, {1/3, \[FormalX] == 2}}, 0], 
   {\[FormalX], 1, 2, 1}]

丹尼尔·利赫特布劳

于 2011-11-10T17:46:18.453 回答