Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Optimization In R

4,788 views

Published on

  • Be the first to comment

Optimization In R

  1. 1. Optimization in R id:syou6162 Saturday, March 27, 2010
  2. 2. Agenda • • GSoC Tsukuba.R • Optimization in R(R ) • • R • Saturday, March 27, 2010
  3. 3. • (id:syou6162 @syou6162) • http://syou6162.sakura.ne.jp/ • => • & Saturday, March 27, 2010
  4. 4. Google Summer of Code • 3/29 • NoSQL interface for R • Dirk Eddelbuettel • Saturday, March 27, 2010
  5. 5. Tsukuba.R • ( ) R • R • (Tokyo Osaka Nagoya) • 2008 7 #6 Saturday, March 27, 2010
  6. 6. (1) • • plot • • R • • apply family • ggplot2 animation Saturday, March 27, 2010
  7. 7. (2) • • R C • openCV C R • R • • Twitter Saturday, March 27, 2010
  8. 8. Tsukuba.R Saturday, March 27, 2010
  9. 9. Saturday, March 27, 2010
  10. 10. Saturday, March 27, 2010
  11. 11. Saturday, March 27, 2010
  12. 12. Saturday, March 27, 2010
  13. 13. Saturday, March 27, 2010
  14. 14. • Tsukuba.R • Saturday, March 27, 2010
  15. 15. Optimization in R id:syou6162 Saturday, March 27, 2010
  16. 16. R • 2 !! • optim optimize • optim optimize 1 • ? Saturday, March 27, 2010
  17. 17. ? •R • => !! • • ? Saturday, March 27, 2010
  18. 18. : (= ) !! http://ja.wikipedia.org/wiki/ Saturday, March 27, 2010
  19. 19. • ( ) • • • !! Saturday, March 27, 2010
  20. 20. > f <- function (x) (x - 1 / 3)^2 > (xmin <- optimize(f, c(0, 1), tol = 0.0001) $minimum [1] 0.3333333 $objective [1] 0 Saturday, March 27, 2010
  21. 21. • • • ( ) • Saturday, March 27, 2010
  22. 22. R • •R log_likelihood_for_norm <- function(x){ return(function(par){ mu <- par[1] sigma2 <- par[2] - length(x) / 2 * log(sigma2) - 1 / 2 * sum((x - mu)^2) / sigma2 }) (x) μ σ } Saturday, March 27, 2010
  23. 23. : •R • • •R : apply family • apply tapply sapply mapply rapply eapply lapply Saturday, March 27, 2010
  24. 24. : sapply&rapply > invisible(sapply(1:3, function(x) { cat("x is", x, fill=TRUE) })) x is 1 x is 2 x is 3 > rapply(list(list(c("a", "b"), list(1:10)), matrix(1:10, 2, 5)), + function(x) { + class(x) + } + ) [1] "character" "integer" "matrix" Saturday, March 27, 2010
  25. 25. R • •R log_likelihood_for_norm <- function(x){ return(function(par){ mu <- par[1] sigma2 <- par[2] - length(x) / 2 * log(sigma2) - 1 / 2 * sum((x - mu)^2) / sigma2 }) (x) μ σ } Saturday, March 27, 2010
  26. 26. optim • => cars$speed > optim(par = c(5, 10), fn = log_likelihood_for_norm(cars$speed), control = list(fnscale = -1)) $par [1] 15.39812 27.40184 ( ) $value [1] -107.7636 $counts function gradient 75 NA optim $convergence fnscale=-1 [1] 0 $message NULL Saturday, March 27, 2010
  27. 27. ? > optim(par = c(5, 10), fn = log_likelihood_for_norm(cars$speed), control = list(fnscale = -1)) $par [1] 15.39812 27.40184 $value [1] -107.7636 > mean(cars$speed) [1] 15.4 $counts > var(cars$speed) function gradient [1] 27.95918 75 NA $convergence [1] 0 !! $message NULL Saturday, March 27, 2010
  28. 28. !! result <- lm(dist~speed, data=cars) plot(cars) coef <- result$coef abline(coef[1], coef[2], col="red") Saturday, March 27, 2010
  29. 29. > obj_func <- function(x, y) { + function(par) { + a <- par[1]; b <- par[2] + sum((y - (a + b * x))^2) + } + } > > result <- optim(par = c(0, 5), fn = obj_func(cars$speed, cars $dist)) > result$par [1] -17.579625 3.932217 > lm(dist~speed, data=cars)$coef (Intercept) speed !! -17.579095 3.932409 Saturday, March 27, 2010
  30. 30. ... • optim method • CG SANN B BFGS Nelder-Mead L-BFGS- • Saturday, March 27, 2010
  31. 31. CG( ) • • 2 • • n 2 n • CG • http://ja.wikipedia.org/wiki/ Saturday, March 27, 2010
  32. 32. BFGS • <= • => 2 => • • => 2 ( ) • • Saturday, March 27, 2010
  33. 33. Nelder-Mead • N N+1 • • • • http://ibisforest.org/index.php?Nelder-Mead Saturday, March 27, 2010
  34. 34. Nelder-Mead optim (method ) Saturday, March 27, 2010
  35. 35. R • optim • etc • Nelder-Mead SANN • BFGS CG Saturday, March 27, 2010
  36. 36. FAQ Saturday, March 27, 2010
  37. 37. R ? • • • C C++ • Tsukuba.R#4 • http://d.hatena.ne.jp/ syou6162/20090301/1235890688 Saturday, March 27, 2010
  38. 38. ? • CG BFGS • ( ) • optim • Saturday, March 27, 2010
  39. 39. • • Writing R Extentions !! Saturday, March 27, 2010
  40. 40. • • GSoC Tsukuba.R • Optimization in R(R ) • • R • Saturday, March 27, 2010

×