简体   繁体   English

硬币翻转的3d直方图产生R

[英]3d histogram of coin flips results in R

My goal is to generate a 3d histogram of the probability of obtaining a certain number of heads in a first and second sequence of 4 coin flips each. 我的目标是生成一个3d直方图,以3d直方图的形式显示在第一个和第二个序列(分别有4个硬币翻转)中获得一定数量的正面的情况。

My idea is simple: 我的想法很简单:

  • use expand.grid to take the cartesian product of the probabilities of a certain number of heads in the first and second sequence 使用expand.grid在第一和第二序列中取一定数量的头的概率的笛卡尔积

  • apply a product operation to each item in the cartesian product to obtain the probability of having that many heads in the first AND that many heads in the second. 对笛卡尔乘积中的每个项目应用乘积运算,以获得在第一个中具有那么多的头并在第二个中具有那么多的头的概率。 I think the trouble is in this step. 我认为麻烦在于这一步。

  • show them in a 3d histogram. 以3d直方图显示。

But I get an extremely confusing output I cannot comprehend: 但是我得到了一个非常令人困惑的输出,我无法理解:

奇怪的输出

I would expect and output with bars lower at the extremes (hard to get 0 head in the first AND in the second) and high in the middle (easier to get 2 heads in the first AND in the second). 我期望并输出的条形在极端处较低(在第一个AND中很难获得0头),在中间较高(在第二个AND中更容易获得2头)。

require("plot3D")

x <- c(1, 4, 6, 4, 1)/8
y <- c(1, 4, 6, 4, 1)/8

prod <- function( arr ) { return (arr[1]*arr[1])}

z <- as.matrix(apply(expand.grid(x,y), c(1,2), prod))

print(z)
##  Plot as a 3D histogram:
hist3D(z=z, border="black")

Also the output of expand.grid looks too linear, instead of a table as a cartesian product should: 此外, expand.grid的输出看起来也太线性了,而不是像笛卡尔积那样使用表:

    Var1  Var2
1  0.125 0.125
2  0.375 0.125
3  0.375 0.125
4  0.125 0.125
5  0.125 0.375
6  0.375 0.375
7  0.375 0.375
8  0.125 0.375
9  0.125 0.375
10 0.375 0.375
11 0.375 0.375
12 0.125 0.375
13 0.125 0.125
14 0.375 0.125
15 0.375 0.125
16 0.125 0.125

Given x and y are the number of heads in 1st and 2nd sequence of coin flips respectively coming from independent binomial distributions . 给定xy是硬币翻转的第一和第二序列的正面数目,分别来自独立的二项分布

You need to evaluate z as a joint probability distribution over the grid of x and y , where x, y € {0,1,2,3,4} 您需要将z评估为 xy 网格上的联合概率分布 ,其中x,y€{0,1,2,3,4}

require("plot3D")

x_val <- 0:4                    # No of heads in 1st sequence of flips
y_val <- 0:4                    # No of heads in 2nd sequence of flips

px <- c(1, 4, 6, 4, 1)/16       # probability vector for no of flips 
py <- c(1, 4, 6, 4, 1)/16       # in 1st and 2nd sequence of flips

grid_prob <- mesh(px, py)
z  <- with(grid_prob, x*y)      # sum(z) should equal 1 to be a pdf

# Plot as a 3D histogram:
hist3D(z=z, border="black", x = x_val , y = y_val)

x和y的联合pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM