我有一个问题,涉及到一组连续概率分布函数,其中大部分是经验确定的(例如:出发时间、运输时间)。我需要的是找到一种方法,能够对两个这样的PDF进行算术计算。例如,如果我从PDF X中取得两个值x和y,那么我需要得到(x+y)或任何其他操作f(x,y)的PDF。
由于不可能存在解析解,因此我正在寻找一些能够处理这些操作的PDF表示方式。一个显而易见(但计算复杂度较高)的解决方案是通过蒙特卡罗方法:生成大量的x和y值,然后测量f(x, y)。但这需要太多的CPU时间。
我曾想过将PDF表示为一系列范围的列表,其中每个范围具有大致相等的概率,以有效地将PDF表示为一系列均匀分布的并集。但我无法看到如何将它们结合起来。
是否有人有解决这个问题的好方法?
编辑:目标是创建一个用于操作PDF的小型语言(也称为特定领域语言)。但首先我需要解决底层表示和算法。
编辑2:dmckee提出了一种直方图实现方法。这就是我通过均匀分布列表所得到的结果。但我看不出如何将它们结合起来以创建新的分布。最终,我需要找到像P(x < y)这样的东西,在这种情况下可能非常小。
编辑3:我有一堆直方图。它们不是均匀分布的,因为我从发生数据中生成它们。因此,如果我有100个样本,并且我想在直方图中有十个点,则我会将每个条分配10个样本,并使条形变量宽度但面积恒定。
我已经弄清楚了如何添加PDF,即对它们进行卷积,我已经学习了相关数学知识。当您对两个均匀分布进行卷积时,您将得到一个新的分布,其中包含三个部分:较宽的均匀分布仍然存在,但在较窄的分布两侧各有一个三角形。因此,如果我将X和Y的每个元素进行卷积,我将得到许多这样的重叠部分。现在我正在努力弄清楚如何对它们求和,然后得到最佳近似的直方图。
我开始怀疑蒙特卡罗并不是一个坏主意。
编辑4: 这篇论文详细讨论了均匀分布的卷积问题。总的来说,你会得到一个“梯形”分布。由于直方图中的每个“柱子”都是均匀分布,因此我希望这个问题可以通过对这些列进行卷积并对结果求和来解决。
然而,结果比输入复杂得多,并且还包括三角形。但是,如果将这些梯形近似为具有相同面积的矩形,则会得到正确的答案,并且减少结果中的矩形数量
-- | Continuous distributions of scalars are represented as a
-- | histogram where each bar has approximately constant area but
-- | variable width and height. A histogram with N bars is stored as
-- | a list of N+1 values.
data Continuous = C {
cN :: Int,
-- ^ Number of bars in the histogram.
cAreas :: [Double],
-- ^ Areas of the bars. @length cAreas == cN@
cBars :: [Double]
-- ^ Boundaries of the bars. @length cBars == cN + 1@
} deriving (Show, Read)
{- | Add distributions. If two random variables @vX@ and @vY@ are
taken from distributions @x@ and @y@ respectively then the
distribution of @(vX + vY)@ will be @(x .+. y).
This is implemented as the convolution of distributions x and y.
Each is a histogram, which is to say the sum of a collection of
uniform distributions (the "bars"). Therefore the convolution can be
computed as the sum of the convolutions of the cross product of the
components of x and y.
When you convolve two uniform distributions of unequal size you get a
trapezoidal distribution. Let p = p2-p1, q - q2-q1. Then we get:
> | |
> | ______ |
> | | | with | _____________
> | | | | | |
> +-----+----+------- +--+-----------+-
> p1 p2 q1 q2
>
> gives h|....... _______________
> | /: :\
> | / : : \ 1
> | / : : \ where h = -
> | / : : \ q
> | / : : \
> +--+-----+-------------+-----+-----
> p1+q1 p2+q1 p1+q2 p2+q2
However we cannot keep the trapezoid in the final result because our
representation is restricted to uniform distributions. So instead we
store a uniform approximation to the trapezoid with the same area:
> h|......___________________
> | | / \ |
> | |/ \|
> | | |
> | /| |\
> | / | | \
> +-----+-------------------+--------
> p1+q1+p/2 p2+q2-p/2
-}
(.+.) :: Continuous -> Continuous -> Continuous
c .+. d = C {cN = length bars - 1,
cBars = map fst bars,
cAreas = zipWith barArea bars (tail bars)}
where
-- The convolve function returns a list of two (x, deltaY) pairs.
-- These can be sorted by x and then sequentially summed to get
-- the new histogram. The "b" parameter is the product of the
-- height of the input bars, which was omitted from the diagrams
-- above.
convolve b c1 c2 d1 d2 =
if (c2-c1) < (d2-d1) then convolve1 b c1 c2 d1 d2 else convolve1 b d1
d2 c1 c2
convolve1 b p1 p2 q1 q2 =
[(p1+q1+halfP, h), (p2+q2-halfP, (-h))]
where
halfP = (p2-p1)/2
h = b / (q2-q1)
outline = map sumGroup $ groupBy ((==) `on` fst) $ sortBy (comparing fst)
$ concat
[convolve (areaC*areaD) c1 c2 d1 d2 |
(c1, c2, areaC) <- zip3 (cBars c) (tail $ cBars c) (cAreas c),
(d1, d2, areaD) <- zip3 (cBars d) (tail $ cBars d) (cAreas d)
]
sumGroup pairs = (fst $ head pairs, sum $ map snd pairs)
bars = tail $ scanl (\(_,y) (x2,dy) -> (x2, y+dy)) (0, 0) outline
barArea (x1, h) (x2, _) = (x2 - x1) * h
其他运算符留给读者作为练习。
priorMeasure >>= likelihoodMeasure
产生了后验预测度量。 - jtobinpriorMeasure >>= likelihoodMeasure
产生了先验预测分布。当您传入后验时,后验预测自然会跟随,但>>=
不会自动处理后验。 - jtobin