Coin tossing and generating graphs

坚强是说给别人听的谎言 提交于 2021-02-11 14:10:51

问题


Could someone generate those graphs or at least help me with that ? I want to generate that probability of getting heads and tails in a undetermined number of flips. Actually its not like that but consider pthetaGivenData like this and check my code. Probably you´ll know what I want to get. This is what I´ve done so far:

The while wasnt working. I couldnt store my results of each coin result in add, so then, pthetaGivenData in storage as well. I was trying to generate the first five data graphs and later I would do the others and then make the graphs in a matrix. The while was just in case we get repete results in the firsts flips like TTT, or HHHHH.

The main formula is pthetaGivenData = theta^nHeads * (1-theta)^nTails

theta = 1
storage = numeric()
add = numeric()
y = 0
nHeads = 0
nTails = 0
z = 0

while(nHeads == 0 || nTails == 0){
  
    Data = sample(c("H", "T"), 1)
    if(Data == "H"){
      x=1
    }else{
      x=0
    }
    append(add, x)
  
nHeads = sum( add == 1 )
nTails = sum( add == 0 )
    
    y <- 0
    while(nTails==0 && y==0){
      pthetaGivenData = (theta)^nHeads
    
      append(storage, pthetaGivenData)
      theta = pthetaGivenData
      
      y <- y+1
    }
    
    while(nHeads==0 && y==0){
      pthetaGivenData = (1-theta)^nTails
    
      append(storage, pthetaGivenData)
      theta = pthetaGivenData
      
      y <- y+1
    }
  z <- z+1  
  }
  
for(i in z+1:5){  

  pthetaGivenData = theta^nHeads * (1-theta)^nTails
   
  storage[i] <- pthetaGivenData[i]
  theta = pthetaGivenData
}

来源:https://stackoverflow.com/questions/64358679/coin-tossing-and-generating-graphs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!