integral

Monte Carlo integration using importance sampling given a proposal function

痞子三分冷 提交于 2019-12-01 14:11:37
Given a Laplace Distribution proposal: g(x) = 1/2*e^(-|x|) and sample size n = 1000 , I want to Conduct the Monte Carlo (MC) integration for estimating θ: via importance sampling. Eventually I want to calculate the mean and standard deviation of this MC estimate in R once I get there. Edit (arrived late after the answer below) This is what I have for my R code so far: library(VGAM) n = 1000 x = rexp(n,0.5) hx = mean(2*exp(-sqrt(x))*(sin(x))^2) gx = rlaplace(n, location = 0, scale = 1) Now we can write a simple R function to sample from Laplace distribution: ## `n` is sample size rlaplace <-

Monte Carlo integration using importance sampling given a proposal function

十年热恋 提交于 2019-12-01 10:31:23
问题 Given a Laplace Distribution proposal: g(x) = 1/2*e^(-|x|) and sample size n = 1000 , I want to Conduct the Monte Carlo (MC) integration for estimating θ: via importance sampling. Eventually I want to calculate the mean and standard deviation of this MC estimate in R once I get there. Edit (arrived late after the answer below) This is what I have for my R code so far: library(VGAM) n = 1000 x = rexp(n,0.5) hx = mean(2*exp(-sqrt(x))*(sin(x))^2) gx = rlaplace(n, location = 0, scale = 1) 回答1:

Integrate over an integral in R

杀马特。学长 韩版系。学妹 提交于 2019-12-01 05:52:02
I want to solve the following in R: ∫ 0 H [π( t ) ∫ t H A ( x ) dx ] dt Where π(t) is the prior and A(x) is the A function defined below. prior <- function(t) dbeta(t, 1, 24) A <- function(x) dbeta(x, 1, 4) expected_loss <- function(H){ integrand <- function(t) prior(t) * integrate(A, lower = t, upper = H)$value loss <- integrate(integrand, lower = 0, upper = H)$value return(loss) } Since π(t), A(x) > 0, expected_loss(.5) should be less than expected_loss(1). But this is not what I get: > expected_loss(.5) [1] 0.2380371 > expected_loss(1) [1] 0.0625 I'm not sure what I'm doing wrong. In your

Integrate over an integral in R

依然范特西╮ 提交于 2019-12-01 04:40:00
问题 I want to solve the following in R: ∫ 0 H [π( t ) ∫ t H A ( x ) dx ] dt Where π(t) is the prior and A(x) is the A function defined below. prior <- function(t) dbeta(t, 1, 24) A <- function(x) dbeta(x, 1, 4) expected_loss <- function(H){ integrand <- function(t) prior(t) * integrate(A, lower = t, upper = H)$value loss <- integrate(integrand, lower = 0, upper = H)$value return(loss) } Since π(t), A(x) > 0, expected_loss(.5) should be less than expected_loss(1). But this is not what I get: >

Using scipy to perform discrete integration of the sample

﹥>﹥吖頭↗ 提交于 2019-12-01 03:39:29
问题 I am trying to port from labview to python. In labview there is a function "Integral x(t) VI" that takes a set of samples as input, performs a discrete integration of the samples and returns a list of values (the areas under the curve) according to Simpsons rule. I tried to find an equivalent function in scipy, e.g. scipy.integrate.simps, but those functions return the summed integral across the set of samples, as a float. How do I get the list of integrated values as opposed to the sum of

infinite integration with matlab

夙愿已清 提交于 2019-12-01 00:34:23
I want to compute the following type of integrals in Matlab. It is the integral of function e^-(u)*u and the boundaries are zero and infinity. This integral should return 1. How can I do this in Matlab? And if you don't have the symbolic toolbox, or want more speed, quadgk supports infinite limits: f = @(x) x.*exp(-x); a = quadgk(f, 0, inf) a = 1.000000000000000e+00 Symbolic toolbox. syms u int(exp(-u)*u, u, 0, inf) 来源: https://stackoverflow.com/questions/11839394/infinite-integration-with-matlab

infinite integration with matlab

拟墨画扇 提交于 2019-11-30 19:11:57
问题 I want to compute the following type of integrals in Matlab. It is the integral of function e^-(u)*u and the boundaries are zero and infinity. This integral should return 1. How can I do this in Matlab? 回答1: And if you don't have the symbolic toolbox, or want more speed, quadgk supports infinite limits: f = @(x) x.*exp(-x); a = quadgk(f, 0, inf) a = 1.000000000000000e+00 回答2: Symbolic toolbox. syms u int(exp(-u)*u, u, 0, inf) 来源: https://stackoverflow.com/questions/11839394/infinite

Explicit integral could not be found

不想你离开。 提交于 2019-11-30 19:10:19
问题 I am getting a well-known error of "Explicit integral could not be found" if I try to evaluate following integral syms z; funz=1./(1+exp((z*z-0.5)/0.1)); Integ2=int(funz,z,0,inf) I get the warning: Warning: Explicit integral could not be found. Integ2 = int(1/(exp(10*z^2 - 5) + 1), z == 0..Inf) Mathematica evaluates this integral to 0.693 . I have tried replacing lower integration limit to some small finite number (0.001) but that doesn't help. Please help in identifying the fix for this

R plotting integral

痴心易碎 提交于 2019-11-29 15:34:30
I'm having some problems with integration function in R. I'm trying to plot the integral vo but it seems I'm not doing correctly. t <- seq(0, 0.04, 0.0001) vi <- function(x) {5 * sin(2 * pi * 50 * x)} vo <- function(x) {integrate(vi, lower=0, upper=x)$value} test_vect = Vectorize(vo, vectorize.args='x') plot(t, vo(t)) # should be a cosine wave plot(t, vi(t)) # sine wave vo should be a sine wave but using test_vect gives me wrong plot and using vo directly gives error 'x' and 'y' lengths differ . Can anyone, please, help me on this matter? You are already there. Just use plot(t, test_vect(t)) .

double integral in R

大兔子大兔子 提交于 2019-11-28 12:14:23
I'm wondering how to code that takes double integrals in R. I already referred two similar questions. calculating double integrals in R quickly double integration in R with additional argument But I'm still confused how I can get my question from those answers. My question is following. I would like to code this calculations in R. From my hand and Wolfram alpha calculation, it becomes 16826.4. I know how to take a integral if both integrals are from exact numbers using adaptIntegrate(). But I'm not sure how to do in my case. Could you guys help me? Thank you so much in advance. Let me start