(This question may be suited for https://stats.stackexchange.com/, but I'm thinking it's just how you calculate what I want in R
that is my question).
(这个问题可能适用于https://stats.stackexchange.com/,但我认为这就是你如何计算我想要的R,这是我的问题)。
I'm trying to add multiple distributions together, and then look at the resulting distribution. I'll illustrate my problem with a simple example using normally distributed random variables, p1
and p2
.
我正在尝试将多个发行版添加到一起,然后查看生成的发行版。我将用一个使用正态分布随机变量p1和p2的简单例子来说明我的问题。
set.seed(21)
N <- 1000
p1 <- rnorm(N, mean = 0, sd = 1)
p2 <- rnorm(N, mean = 10, sd = 1)
Which we can plot:
我们可以绘制:
data.frame(p1, p2) %>%
gather(key="dist", value="value") %>%
ggplot(aes(value, color=dist)) + geom_density()
I can add these distributions together using convolve
. Okay so that's fine. But what I can't figure out, is how to plot the summation of the distributions with the appropriate x-values. In the examples I've seen, it looks like the x-values are manually added in a way that doesn't seem "accurate" for lack of better work. See this Example.
我可以使用convolve将这些发行版添加到一起。好的,没关系。但我无法弄清楚的是,如何用适当的x值绘制分布的总和。在我看过的例子中,看起来x值是以缺乏更好的工作似乎“准确”的方式手动添加的。见这个例子。
I can "add" them together and plot:
我可以将它们“添加”在一起并绘制:
pdf.c <- convolve(pdf1.y, pdf2.y, type = "open")
plot(pdf.c, type="l")
My question is how to get the corresponding x-values of the new distribution. I'm sure I'm missing something from a foundational statistics point of view.
我的问题是如何获得新分布的相应x值。从基础统计的角度来看,我确信我遗漏了一些东西。
Appendix for pdf1
and pdf2
:
pdf1和pdf2的附录:
set.seed(21)
N <- 1000
p1 <- rnorm(N, mean = 0, sd = 1)
p2 <- rnorm(N, mean = 10, sd = 1)
pdf1.x <- density(p1)$x
pdf2.x <- density(p2)$x
pdf1.y <- density(p1)$y / sum(density(p1)$y)
pdf2.y <- density(p2)$y / sum(density(p2)$y)
df1 <- data.frame(pdf.x = pdf1.x, pdf.y = pdf1.y, dist = "1", stringsAsFactors = FALSE)
df2 <- data.frame(pdf.x = pdf2.x, pdf.y = pdf2.y, dist = "2", stringsAsFactors = FALSE)
df <- bind_rows(df1, df2)
1 个解决方案
#1
0
Assuming that p1 and p2 are discretized uniformly, with the same interval dx between successive x values. (I see that you have discretized p1 and p2 at random points -- that's not the same, and, without thinking about it some more, I don't have an answer for that.) Let x1 = x1_0 + (k - 1) times dx, k = 1, 2, 3, ..., n1 be the points at which p1 is discretized, and x2 = x2_0 + (k - 1) times dx, k = 1, 2, 3, ..., n2 be the points at which p2 is discretized.
假设p1和p2均匀离散,在连续的x值之间具有相同的间隔dx。 (我看到你在随机点上已经离散了p1和p2 - 这是不一样的,并且,如果不考虑它,我没有答案。)让x1 = x1_0 +(k - 1)时间dx,k = 1,2,3,...,n1是p1离散化的点,x2 = x2_0 +(k-1)乘以dx,k = 1,2,3,..., n2是p2离散化的点。
Each point xi_k = xi_0 + (k - 1) times dx represents the center point of a bar which has width dx and height pi(xi_k), i = 1, 2. Thus the mass of the bar is dx times pi(xi_k), and the total mass for all bars approaches 1 as dx approaches 0. These masses are the values which are convolved. If the discretized masses are normalized to 1, then their convolution will also be normalized to 1.
每个点xi_k = xi_0 +(k-1)乘以dx表示条的中心点,其具有宽度dx和高度pi(xi_k),i = 1,2。因此条的质量是dx乘以pi(xi_k)当dx接近0时,所有条的总质量接近1.这些质量是卷积的值。如果将离散化的质量归一化为1,则它们的卷积也将归一化为1。
To be very careful, the range over which the distributions are discretized are xi_0 - dx/2 to xi_0 + (ni - 1) times dx + dx/2. After computing the convolution, the range for the result is likewise -dx/2 and +dx/2 the first and last points, respectively.
要非常小心,分布离散化的范围是xi_0 - dx / 2到xi_0 +(ni - 1)乘以dx + dx / 2。在计算卷积之后,结果的范围同样分别是-dx / 2和+ dx / 2的第一个和最后一个点。
The convolution has n = n1 + n2 - 1 points, namely x1_0 + x2_0 + (k - 1) times dx, k = 1, 2, 3, ..., n1 + n2 - 1. The first point is x1_0 + x2_0 (i.e. first point for p1 plus first point for p2) and the last point is x1_0 + x2_0 + (n1 + n2 - 2) times dx = (x1_0 + (n1 - 1) times dx) + (x2_0 + (n2 - 1) times dx) (i.e. last point for p1 plus last point for p2). From this you can construct x values corresponding to the convolution via the seq
function or something like that.
卷积有n = n1 + n2 - 1个点,即x1_0 + x2_0 +(k - 1)乘以dx,k = 1,2,3,...,n1 + n2 - 1.第一个点是x1_0 + x2_0 (即p1的第一个点加上p2的第一个点),最后一个点是x1_0 + x2_0 +(n1 + n2 - 2)次dx =(x1_0 +(n1 - 1)乘以dx)+(x2_0 +(n2 - 1) )次dx)(即p1的最后一点加上p2的最后一点)。从这里你可以通过seq函数或类似的东西构造对应于卷积的x值。
#1
0
Assuming that p1 and p2 are discretized uniformly, with the same interval dx between successive x values. (I see that you have discretized p1 and p2 at random points -- that's not the same, and, without thinking about it some more, I don't have an answer for that.) Let x1 = x1_0 + (k - 1) times dx, k = 1, 2, 3, ..., n1 be the points at which p1 is discretized, and x2 = x2_0 + (k - 1) times dx, k = 1, 2, 3, ..., n2 be the points at which p2 is discretized.
假设p1和p2均匀离散,在连续的x值之间具有相同的间隔dx。 (我看到你在随机点上已经离散了p1和p2 - 这是不一样的,并且,如果不考虑它,我没有答案。)让x1 = x1_0 +(k - 1)时间dx,k = 1,2,3,...,n1是p1离散化的点,x2 = x2_0 +(k-1)乘以dx,k = 1,2,3,..., n2是p2离散化的点。
Each point xi_k = xi_0 + (k - 1) times dx represents the center point of a bar which has width dx and height pi(xi_k), i = 1, 2. Thus the mass of the bar is dx times pi(xi_k), and the total mass for all bars approaches 1 as dx approaches 0. These masses are the values which are convolved. If the discretized masses are normalized to 1, then their convolution will also be normalized to 1.
每个点xi_k = xi_0 +(k-1)乘以dx表示条的中心点,其具有宽度dx和高度pi(xi_k),i = 1,2。因此条的质量是dx乘以pi(xi_k)当dx接近0时,所有条的总质量接近1.这些质量是卷积的值。如果将离散化的质量归一化为1,则它们的卷积也将归一化为1。
To be very careful, the range over which the distributions are discretized are xi_0 - dx/2 to xi_0 + (ni - 1) times dx + dx/2. After computing the convolution, the range for the result is likewise -dx/2 and +dx/2 the first and last points, respectively.
要非常小心,分布离散化的范围是xi_0 - dx / 2到xi_0 +(ni - 1)乘以dx + dx / 2。在计算卷积之后,结果的范围同样分别是-dx / 2和+ dx / 2的第一个和最后一个点。
The convolution has n = n1 + n2 - 1 points, namely x1_0 + x2_0 + (k - 1) times dx, k = 1, 2, 3, ..., n1 + n2 - 1. The first point is x1_0 + x2_0 (i.e. first point for p1 plus first point for p2) and the last point is x1_0 + x2_0 + (n1 + n2 - 2) times dx = (x1_0 + (n1 - 1) times dx) + (x2_0 + (n2 - 1) times dx) (i.e. last point for p1 plus last point for p2). From this you can construct x values corresponding to the convolution via the seq
function or something like that.
卷积有n = n1 + n2 - 1个点,即x1_0 + x2_0 +(k - 1)乘以dx,k = 1,2,3,...,n1 + n2 - 1.第一个点是x1_0 + x2_0 (即p1的第一个点加上p2的第一个点),最后一个点是x1_0 + x2_0 +(n1 + n2 - 2)次dx =(x1_0 +(n1 - 1)乘以dx)+(x2_0 +(n2 - 1) )次dx)(即p1的最后一点加上p2的最后一点)。从这里你可以通过seq函数或类似的东西构造对应于卷积的x值。