Custom Theano Op to do numerical integration

前端 未结 3 2036
抹茶落季
抹茶落季 2021-01-12 17:34

I\'m attempting to write a custom Theano Op which numerically integrates a function between two values. The Op is a custom likelihood for PyMC3 which involves the numerical

3条回答
  •  一整个雨季
    2021-01-12 18:17

    I always use the following code where I generate B = 10000 samples of n = 30 observations from a normal distribution with µ = 1 and σ 2 = 2.25. For each sample, the parameters µ and σ are estimated and stored in a matrix. I hope this can help you.

    loglik <- function(p,z){
    sum(dnorm(z,mean=p[1],sd=p[2],log=TRUE))
    }
    set.seed(45)
    n <- 30
    x <- rnorm(n,mean=1,sd=1.5)
    optim(c(mu=0,sd=1),loglik,control=list(fnscale=-1),z=x)
    
    B <- 10000
    bootstrap.results <- matrix(NA,nrow=B,ncol=3)
    colnames(bootstrap.results) <- c("mu","sigma","convergence")
    for (b in 1:B){
    sample.b <- rnorm(n,mean=1,sd=1.5)
    m.b <- optim(c(mu=0,sd=1),loglik,control=list(fnscale=-1),z=sample.b)
    bootstrap.results[b,] <- c(m.b$par,m.b$convergence)
    }
    

    One can also obtain the ML estimate of λ and use the bootstrap to estimate the bias and the standard error of the estimate. First calculate the MLE of λ Then, we estimate the bias and the standard error of λˆ by a nonparametric bootstrap.

    B <- 9999
    lambda.B <- rep(NA,B)
    n <- length(w.time)
    for (b in 1:B){
    b.sample <- sample(1:n,n,replace=TRUE)
    lambda.B[b] <- 1/mean(w.time[b.sample])
    }
    bias <- mean(lambda.B-m$estimate)
    sd(lambda.B)
    

    In the second part we calculate a 95% confidence interval for the mean time between failures.

    n <- length(w.time)
    m <- mean(w.time)
    se <- sd(w.time)/sqrt(n)
    interval.1 <- m + se * qnorm(c(0.025,0.975))
    interval.1
    

    But we can also use the the assumption that the data are from an exponential distribution. In that case we have varX¯ = 1/(nλ^2) = θ^{2}/n which can be estimated by X¯^{2}/n.

    sd.m <- sqrt(m^2/n)
    interval.2 <- m + sd.m * qnorm(c(0.025,0.975))
    interval.2
    

    We can also estimate the standard error of ˆθ by means of a boostrap procedure. We use the nonparametric bootstrap, that is, we sample from the original sample with replacement.

    B <- 9999
    m.star <- rep(NA,B)
    for (b in 1:B){
    m.star[b] <- mean(sample(w.time,replace=TRUE))
    }
    sd.m.star <- sd(m.star)
    interval.3 <- m + sd.m.star * qnorm(c(0.025,0.975))
    interval.3
    An interval not based on the assumption of normality of ˆθ is obtained by the percentile method:
    
    interval.4 <- quantile(m.star, probs=c(0.025,0.975))
    interval.4
    

提交回复
热议问题