opts_chunk$set(external = TRUE, cache = FALSE, cache.path = "myers-cache/", warning=FALSE) #read_chunk('gaussian-process-control.R') library(knitcitations)
require(pdgControl) require(nonparametricbayes) require(reshape2) require(ggplot2) require(data.table) require(tgp) library(kernlab) require(MCMCpack) require(plyr)
opts_knit$set(upload.fun = socialR::notebook.url) #opts_chunk$set(dev = 'Cairo_pdf', dev.args=list("")) opts_chunk$set(dev="png", dev.args=list(bg="transparent")) opts_chunk$set(comment=NA, tidy=FALSE) theme_set(theme_bw(base_size=16)) theme_update(panel.background = element_rect(fill = "transparent",colour = NA), plot.background = element_rect(fill = "transparent",colour = NA)) cbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
Fixed priors on hyperparameters, fixed model type.
s2.p <- c(50,50) # inverse gamma mean is tau2.p <- c(20,1) d.p = c(10, 1/0.01, 10, 1/0.01) nug.p = c(10, 1/0.01, 10, 1/0.01) # gamma mean s2_prior <- function(x) dinvgamma(x, s2.p[1], s2.p[2]) tau2_prior <- function(x) dinvgamma(x, tau2.p[1], tau2.p[2]) d_prior <- function(x) dgamma(x, d.p[1], scale = d.p[2]) + dgamma(x, d.p[3], scale = d.p[4]) nug_prior <- function(x) dgamma(x, nug.p[1], scale = nug.p[2]) + dgamma(x, nug.p[3], scale = nug.p[4]) beta0_prior <- function(x, tau) dnorm(x, 0, tau) beta = c(0) priors <- list(s2 = s2_prior, tau2 = tau2_prior, beta0 = dnorm, nug = nug_prior, d = d_prior, ldetK = function(x) 0)
profit = function(x,h) pmin(x, h) delta <- 0.01 OptTime = 20 # stationarity with unstable models is tricky thing reward = 0 xT <- 0 z_g = function() rlnorm(1, 0, sigma_g) z_m = function() 1+(2*runif(1, 0, 1)-1) * sigma_m
f <- RickerAllee p <- c(1.1, 10, 5) K <- 10 allee <- 5
sigma_g <- 0.05 sigma_m <- 0.02 x_grid <- seq(0, 1.5 * K, length=101) h_grid <- x_grid
With parameters r p
.
require(snowfall) sfInit(par=TRUE, cpu=8) sfExportAll() sfLibrary(pdgControl) sfLibrary(nonparametricbayes) sfLibrary(reshape2) sfLibrary(ggplot2) sfLibrary(data.table) sfLibrary(tgp) sfLibrary(kernlab) sfLibrary(MCMCpack) sfLibrary(plyr)
seed <- round(runif(32) * 1e6) seed[1] <- 1 seed yields <- lapply(seed, function(seed_i){ Xo <- allee + x_grid[10] # observations start from x0 <- Xo # simulation under policy starts from obs <- sim_obs(Xo, z_g, f, p, Tobs=40, seed = seed_i) est <- par_est(obs) gp <- bgp(X=obs$x, XX=x_grid, Z=obs$y, verb=0, meanfn="constant", bprior="b0", BTE=c(2000,16000,2), m0r1=FALSE, corr="exp", trace=FALSE, beta = beta, s2.p = s2.p, d.p = d.p, nug.p = nug.p, tau2.p = tau2.p, s2.lam = "fixed", d.lam = "fixed", nug.lam = "fixed", tau2.lam = "fixed") gp_plot(gp, f, p, est$f_alt, est$p_alt, x_grid, obs, seed_i) # posteriors_plot(gp, priors) # needs trace=TRUE! OPT <- optimal_policy(gp, f, est$f_alt, p, est$p_alt, x_grid, h_grid, sigma_g, est$sigma_g_alt, delta, xT, profit, reward, OptTime) dt <- simulate_opt(OPT, f, p, x_grid, h_grid, x0, z_g, profit) sim_plots(dt, seed=seed_i) profits_stats(dt) })
yields <- melt(yields, id=c("method", "V1", "sd")) ggplot(yields) + geom_histogram(aes(V1)) + facet_wrap(~method, scale='free') yields ```` ```r bibliography("html")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.