看板 R_Language 關於我們 聯絡資訊
[問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: 入門(寫過其他程式,只是對語法不熟悉) [問題敘述]: 大家好 我現在遇到一個問題 有4個藍色玩具 B1 B2 B3 B4 另有6個紅色玩具 R1 R2 ... R6 現在要看隨機抽出三個玩具 顏色相同的機率 我的做法是 (2C1*4C3 + 2C1*6C3)/10C3 不知道這樣對嗎? 然後現在我遇到一個問題是 write a piece of R code to empirically compute the prob. of the three toys in the same color, let n be the number of random samples in the bag. 我用的R code如下 當n=50,000 n <- 50000 f <- 0 for(i in 1:n) f <- f + ifelse((sample(3)[1]==sample(3)[1]),1,0) print(f/n) 但現在問題要我 Plot your estimated probabilities as a function of n (log scale again here, please) 根據 n=50,100,200,.....,50000 用R繪製出機率的圖表 我在想這是不是要先把取出三種同顏色的玩具的C.D.F.先求出來 再用plot C.D.F的方式去繪製呢? 想了兩天都還是沒頭緒 老師有給蒙地卡羅模擬的參考資料 但我覺得這好像不是 另外還有buffon needle probability的參考資料 但我都無法refer 這些資料 又覺得並沒有我想的複雜 但還是目前無法動彈 不知道有沒有大大願意給小魯一點指點 感謝不盡~ 也希望大家颱風天都一切平安 [關鍵字]: 選擇性,也許未來有用 -- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 8.41.66.215 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1537055950.A.AD8.html
andrew43: sample(3)[1]==sample(3)[1] 是{1,2,3}抽出且放回二次的 09/16 08:33
andrew43: sample(3)[1]==sample(3)[1] 的意義和三選一獨立 09/16 08:35
andrew43: 進行二次,看二次的結果是否相同。我看不懂這和問題的關 09/16 08:35
andrew43: 係是什麼... 09/16 08:35
AmigoSafin: 啊是這樣嗎?謝謝您~我再來改改 09/16 10:19
AmigoSafin: 因為我是找老師的code來改 我也不甚理解其中的意義.. 09/16 10:33
july81212: 用排列組合方式算完再丟不就沒training 的意義嗎…… 09/19 22:30
july81212: 原本只是要你們製造一個機率函數 然後用seed去餵n次 09/19 22:33
july81212: 再來filtering 相同出現的機率吧 09/19 22:33
大大真是高手 我回去找老師給的資料 有看到以下說明: # The randomness generated a computer is not entirely random indeed # It is referred to as pseudo-randomness set.seed(8262009) 這就是您說的嗎? 其實我還是不甚明白啊 因為我沒有學過這個/_\ 那就藉此再問一下 Compute Pr[1 X 3] where X FX(x) with fX(x) = 3x2 in [0,1]. 我用的code: n <- 10000 z <- rnorm(n) proba <- length(which(abs(z)<1/4))/n print(proba) 根據的是老師給的資料 Probability as expectation (因為Monte Carlo Simulation有兩種 Probability as expectation/ Probability using the uniform density as the weighting/importance function 我用的是前者 不知道這樣對嗎? 謝謝大家~~ ※ 編輯: AmigoSafin (8.41.66.215), 09/21/2018 10:27:39
AmigoSafin: 他應該是要我們先算theoretical再算empirical但我真的 09/21 10:28
AmigoSafin: 很難推敲出R code要怎寫 無奈 09/21 10:29
andrew43: 你下的nrorm是常態mu=0,s=1亂數。uniform用runif 09/21 14:56
andrew43: rnorm 09/21 14:56
AmigoSafin: 那方便問一下a大 如何判斷這函數uniform distribution 09/21 19:41
andrew43: 不懂你的問題。 09/21 21:22
AmigoSafin: 就是為何要用uniform的function做這題呢?不好意思>"< 09/21 22:24
andrew43: 喔,我以為這題就是要用uniform來做 09/21 23:52
andrew43: 建議你一次提一個問題,這樣別人要回答的話較方便 09/21 23:54
andrew43: 不然你覺得什麼要用normal? 09/22 00:04
AmigoSafin: 我後來看了一下資料 這是一個multivariate 用MC的uni 09/22 08:34
AmigoSafin: form去approxiate 這是我的理解~不好意思思緒有點亂 09/22 08:35