본문 바로가기

Programming/R Script

3D plot using rgl package in R to describe probabilty of coin tosses

3D plot using rgl package in R to describe probabilty of coin tosses


정리가 아주 잘되어 있는 링크를 발견하고 따라서 돌려봤다.


http://alphaism.wordpress.com/2012/04/20/exploring-optimal-f/


지금 joint probability에 관한 내용을 하고 있고, 그림도 삼차원으로 나타내려 했는데, 적절한 예제인 듯하다.


3D window를 통해서 마우스로 회전/확대 등의 조작도 가능하다. 좋은 세상이구만; 내가 뒤쳐져 있지만.


<Figure generated with R>




<R scripts>


# Two components function

TCOpt.f <- function(win = 2, loss = -1, outcome = 4, lev = 1, obs = 100, plays = 1) {

    f1 <- seq(0, lev, 1/obs)

    f2 <- seq(0, lev, 1/obs)

    rets <- matrix(0, length(f1), length(f2))     

    for (i in 1:length(f1)) {

        for (j in 1:length(f2)) {            

            s1 <- (1 + (f1[i] * win + f2[j] * loss) / -loss) ^ (1 / outcome)            

            s2 <- (1 + (f1[i] * win + f2[j] * win) / -loss ) ^ (1 / outcome)            

            s3 <- (1 + (f1[i] * loss + f2[j] * win) / -loss ) ^ (1 / outcome)            

            s4 <- (1 + (f1[i] * loss + f2[j] * loss) / -loss ) ^ (1 / outcome)            

            rets[i, j] <- (s1 * s2 * s3 * s4) ^ plays        

        }    

    }   

    rets[is.na(rets)] <- 0

    mat <- matrix(f1, nrow = length(f1), ncol=length(f1))    

    list(xmat = mat, ymat = t(mat), 'rets' = rets)


require(rgl)

plays5 <- TCOpt.f(plays=5)

x <- plays5$xmat

y <- plays5$ymat

z <- plays5$rets

col <- c('white','blue')[ z+1 ]

persp3d(x, y, z, color=col, alpha=.6, xlab = 'coin 1', ylab = 'coin 2', zlab = 'returns')

grid3d(c('x', 'y', 'z'))

title3d("Two Components Coin Tossing", line=5)

 



'Programming > R Script' 카테고리의 다른 글

How to show the symbol of Angstrom  (0) 2014.04.04
R - Regular Expression : gsub  (1) 2014.02.05
[R] Apply Family  (0) 2013.12.20
[R] Adjust P-value for Multiple Comparisons  (0) 2013.12.18
[R] Correlation Test / Export to Excel File  (0) 2013.12.18