Compute integral for expected value in R.
From a previous assignment, I found that the extinction probability of of a branching process is given by solving the equation
$$G(s)=s,$$
where $G(s)=e^{lambda(s-1)}$ is the PGF and noting the smallest root in terms of $s$. The solution to the equation
$$e^{lambda(s-1)}-s=0$$
will be a function of $lambda$, namley $s(lambda)$ which can be computed numerically for a fixed $lambda.$
Now I'm asked to numerically find the expected value of this probability given that $lambdasimGamma(15,9)$. Let's for convention now use the function $s(Lambda)$, where $LambdasimGamma(15,9)$, I know that the expected value of a function of a random variable $X$ is given by
$$mathbb{E}[g(X)]=int_{infty}^{infty}g(x)cdotpi(x)dx,$$
where $pi(x)$ is the pdf of $X$. So in my case I get
$$mathbb{E}[s(Lambda)]=int_{0}^{infty}s(lambda)pi(lambda)dlambda=int_0^{infty}s(lambda)frac{9^{15}}{Gamma(15)}lambda^{15-1}e^{-9lambda}dlambda.$$
Since $9^{15}/Gamma(15)approx 2361.7$ we have that the integral to be numerically computed in R is
$$2361.7int_{0}^{infty}s(lambda)lambda^{14}e^{-9lambda}.$$
I figured, since we don't have a closed form expression for $s(lambda)$, that I need nestled functions, first express the solution of $G(s)-s=0$ in one function depending on $s$, keeping $lambda$ constant and then wrap this with another function only depending on $lambda,$ like this:
fun = function(lambda){
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2] # since this root is the smallest one.
integrand = probExtinction*exp((-9)*lambda)*lambda^(14)
return(integrand)
}
ExpectedProb = 2361.7*integrate(fun,0,Inf)$value
But here is where trouble starts. I get the error below. I understand what the error means, it's that if the function values at the endpoints are not oposite, then the function never crosses the x-axis, thus no roots will be found. However, plotting the function $f(x)=e^{a(x-1)}-x$ I see that the function dramatically changes behavior when $a$ passes $1$. I don't see how to fix my code in order to deal with this issue.
Error in uniroot(f, lower = xseq[i], upper = xseq[i + 1], ...) :
f() values at end points not of opposite sign
In addition: Warning messages:
1: In lambda * (s - 1) :
longer object length is not a multiple of shorter object length
2: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
the condition has length > 1 and only the first element will be used
3: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
the condition has length > 1 and only the first element will be used
Whats weird is that when I only run this code I don't get the uniroot-error:
lambda = 2
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2])
Anyone knows how I can fix this?
probability integration probability-theory expected-value
add a comment |
From a previous assignment, I found that the extinction probability of of a branching process is given by solving the equation
$$G(s)=s,$$
where $G(s)=e^{lambda(s-1)}$ is the PGF and noting the smallest root in terms of $s$. The solution to the equation
$$e^{lambda(s-1)}-s=0$$
will be a function of $lambda$, namley $s(lambda)$ which can be computed numerically for a fixed $lambda.$
Now I'm asked to numerically find the expected value of this probability given that $lambdasimGamma(15,9)$. Let's for convention now use the function $s(Lambda)$, where $LambdasimGamma(15,9)$, I know that the expected value of a function of a random variable $X$ is given by
$$mathbb{E}[g(X)]=int_{infty}^{infty}g(x)cdotpi(x)dx,$$
where $pi(x)$ is the pdf of $X$. So in my case I get
$$mathbb{E}[s(Lambda)]=int_{0}^{infty}s(lambda)pi(lambda)dlambda=int_0^{infty}s(lambda)frac{9^{15}}{Gamma(15)}lambda^{15-1}e^{-9lambda}dlambda.$$
Since $9^{15}/Gamma(15)approx 2361.7$ we have that the integral to be numerically computed in R is
$$2361.7int_{0}^{infty}s(lambda)lambda^{14}e^{-9lambda}.$$
I figured, since we don't have a closed form expression for $s(lambda)$, that I need nestled functions, first express the solution of $G(s)-s=0$ in one function depending on $s$, keeping $lambda$ constant and then wrap this with another function only depending on $lambda,$ like this:
fun = function(lambda){
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2] # since this root is the smallest one.
integrand = probExtinction*exp((-9)*lambda)*lambda^(14)
return(integrand)
}
ExpectedProb = 2361.7*integrate(fun,0,Inf)$value
But here is where trouble starts. I get the error below. I understand what the error means, it's that if the function values at the endpoints are not oposite, then the function never crosses the x-axis, thus no roots will be found. However, plotting the function $f(x)=e^{a(x-1)}-x$ I see that the function dramatically changes behavior when $a$ passes $1$. I don't see how to fix my code in order to deal with this issue.
Error in uniroot(f, lower = xseq[i], upper = xseq[i + 1], ...) :
f() values at end points not of opposite sign
In addition: Warning messages:
1: In lambda * (s - 1) :
longer object length is not a multiple of shorter object length
2: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
the condition has length > 1 and only the first element will be used
3: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
the condition has length > 1 and only the first element will be used
Whats weird is that when I only run this code I don't get the uniroot-error:
lambda = 2
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2])
Anyone knows how I can fix this?
probability integration probability-theory expected-value
In general, if you getthe condition has length > 1 and only the first element will be used
that means your function is not suitable for vector arguments. Either uselapply
,sapply
or just do a loop so the argument is not a vector
– Yuriy S
Dec 4 '18 at 11:27
@YuriyS - Loop with what index? I don't understand where to place the loop. Can you elaborate?
– Parseval
Dec 4 '18 at 11:29
add a comment |
From a previous assignment, I found that the extinction probability of of a branching process is given by solving the equation
$$G(s)=s,$$
where $G(s)=e^{lambda(s-1)}$ is the PGF and noting the smallest root in terms of $s$. The solution to the equation
$$e^{lambda(s-1)}-s=0$$
will be a function of $lambda$, namley $s(lambda)$ which can be computed numerically for a fixed $lambda.$
Now I'm asked to numerically find the expected value of this probability given that $lambdasimGamma(15,9)$. Let's for convention now use the function $s(Lambda)$, where $LambdasimGamma(15,9)$, I know that the expected value of a function of a random variable $X$ is given by
$$mathbb{E}[g(X)]=int_{infty}^{infty}g(x)cdotpi(x)dx,$$
where $pi(x)$ is the pdf of $X$. So in my case I get
$$mathbb{E}[s(Lambda)]=int_{0}^{infty}s(lambda)pi(lambda)dlambda=int_0^{infty}s(lambda)frac{9^{15}}{Gamma(15)}lambda^{15-1}e^{-9lambda}dlambda.$$
Since $9^{15}/Gamma(15)approx 2361.7$ we have that the integral to be numerically computed in R is
$$2361.7int_{0}^{infty}s(lambda)lambda^{14}e^{-9lambda}.$$
I figured, since we don't have a closed form expression for $s(lambda)$, that I need nestled functions, first express the solution of $G(s)-s=0$ in one function depending on $s$, keeping $lambda$ constant and then wrap this with another function only depending on $lambda,$ like this:
fun = function(lambda){
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2] # since this root is the smallest one.
integrand = probExtinction*exp((-9)*lambda)*lambda^(14)
return(integrand)
}
ExpectedProb = 2361.7*integrate(fun,0,Inf)$value
But here is where trouble starts. I get the error below. I understand what the error means, it's that if the function values at the endpoints are not oposite, then the function never crosses the x-axis, thus no roots will be found. However, plotting the function $f(x)=e^{a(x-1)}-x$ I see that the function dramatically changes behavior when $a$ passes $1$. I don't see how to fix my code in order to deal with this issue.
Error in uniroot(f, lower = xseq[i], upper = xseq[i + 1], ...) :
f() values at end points not of opposite sign
In addition: Warning messages:
1: In lambda * (s - 1) :
longer object length is not a multiple of shorter object length
2: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
the condition has length > 1 and only the first element will be used
3: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
the condition has length > 1 and only the first element will be used
Whats weird is that when I only run this code I don't get the uniroot-error:
lambda = 2
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2])
Anyone knows how I can fix this?
probability integration probability-theory expected-value
From a previous assignment, I found that the extinction probability of of a branching process is given by solving the equation
$$G(s)=s,$$
where $G(s)=e^{lambda(s-1)}$ is the PGF and noting the smallest root in terms of $s$. The solution to the equation
$$e^{lambda(s-1)}-s=0$$
will be a function of $lambda$, namley $s(lambda)$ which can be computed numerically for a fixed $lambda.$
Now I'm asked to numerically find the expected value of this probability given that $lambdasimGamma(15,9)$. Let's for convention now use the function $s(Lambda)$, where $LambdasimGamma(15,9)$, I know that the expected value of a function of a random variable $X$ is given by
$$mathbb{E}[g(X)]=int_{infty}^{infty}g(x)cdotpi(x)dx,$$
where $pi(x)$ is the pdf of $X$. So in my case I get
$$mathbb{E}[s(Lambda)]=int_{0}^{infty}s(lambda)pi(lambda)dlambda=int_0^{infty}s(lambda)frac{9^{15}}{Gamma(15)}lambda^{15-1}e^{-9lambda}dlambda.$$
Since $9^{15}/Gamma(15)approx 2361.7$ we have that the integral to be numerically computed in R is
$$2361.7int_{0}^{infty}s(lambda)lambda^{14}e^{-9lambda}.$$
I figured, since we don't have a closed form expression for $s(lambda)$, that I need nestled functions, first express the solution of $G(s)-s=0$ in one function depending on $s$, keeping $lambda$ constant and then wrap this with another function only depending on $lambda,$ like this:
fun = function(lambda){
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2] # since this root is the smallest one.
integrand = probExtinction*exp((-9)*lambda)*lambda^(14)
return(integrand)
}
ExpectedProb = 2361.7*integrate(fun,0,Inf)$value
But here is where trouble starts. I get the error below. I understand what the error means, it's that if the function values at the endpoints are not oposite, then the function never crosses the x-axis, thus no roots will be found. However, plotting the function $f(x)=e^{a(x-1)}-x$ I see that the function dramatically changes behavior when $a$ passes $1$. I don't see how to fix my code in order to deal with this issue.
Error in uniroot(f, lower = xseq[i], upper = xseq[i + 1], ...) :
f() values at end points not of opposite sign
In addition: Warning messages:
1: In lambda * (s - 1) :
longer object length is not a multiple of shorter object length
2: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
the condition has length > 1 and only the first element will be used
3: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
the condition has length > 1 and only the first element will be used
Whats weird is that when I only run this code I don't get the uniroot-error:
lambda = 2
sLambda = function(s){exp(lambda*(s-1))-s}
roots = uniroot.all(sLambda,c(0,1))
probExtinction = roots[2])
Anyone knows how I can fix this?
probability integration probability-theory expected-value
probability integration probability-theory expected-value
edited Dec 4 '18 at 11:07
Parseval
asked Dec 4 '18 at 10:59
ParsevalParseval
2,7771718
2,7771718
In general, if you getthe condition has length > 1 and only the first element will be used
that means your function is not suitable for vector arguments. Either uselapply
,sapply
or just do a loop so the argument is not a vector
– Yuriy S
Dec 4 '18 at 11:27
@YuriyS - Loop with what index? I don't understand where to place the loop. Can you elaborate?
– Parseval
Dec 4 '18 at 11:29
add a comment |
In general, if you getthe condition has length > 1 and only the first element will be used
that means your function is not suitable for vector arguments. Either uselapply
,sapply
or just do a loop so the argument is not a vector
– Yuriy S
Dec 4 '18 at 11:27
@YuriyS - Loop with what index? I don't understand where to place the loop. Can you elaborate?
– Parseval
Dec 4 '18 at 11:29
In general, if you get
the condition has length > 1 and only the first element will be used
that means your function is not suitable for vector arguments. Either use lapply
, sapply
or just do a loop so the argument is not a vector– Yuriy S
Dec 4 '18 at 11:27
In general, if you get
the condition has length > 1 and only the first element will be used
that means your function is not suitable for vector arguments. Either use lapply
, sapply
or just do a loop so the argument is not a vector– Yuriy S
Dec 4 '18 at 11:27
@YuriyS - Loop with what index? I don't understand where to place the loop. Can you elaborate?
– Parseval
Dec 4 '18 at 11:29
@YuriyS - Loop with what index? I don't understand where to place the loop. Can you elaborate?
– Parseval
Dec 4 '18 at 11:29
add a comment |
1 Answer
1
active
oldest
votes
The solution of
$$e^{lambda(s-1)}-s=0$$ is given in terms of Lambert function and it is
$$s=-frac{Wleft(-lambda, e^{-lambda } right)}{lambda }$$ This function is available in R (have a look here).
When plotted, the integrand looks very nice but you still need numerical integration. If I did it properly, the result is $approx 0.4002$.
Thanks, I know that it can be expressed in the Lambert function, I did that too and got the same answer but since we have not covered this Lambert function, we are not supposed to ue it. Is there a way to do this without using that function?
– Parseval
Dec 4 '18 at 11:27
1
@Parseval. No idea ! Sorry for that. Cheers :-(
– Claude Leibovici
Dec 4 '18 at 11:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3025426%2fcompute-integral-for-expected-value-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution of
$$e^{lambda(s-1)}-s=0$$ is given in terms of Lambert function and it is
$$s=-frac{Wleft(-lambda, e^{-lambda } right)}{lambda }$$ This function is available in R (have a look here).
When plotted, the integrand looks very nice but you still need numerical integration. If I did it properly, the result is $approx 0.4002$.
Thanks, I know that it can be expressed in the Lambert function, I did that too and got the same answer but since we have not covered this Lambert function, we are not supposed to ue it. Is there a way to do this without using that function?
– Parseval
Dec 4 '18 at 11:27
1
@Parseval. No idea ! Sorry for that. Cheers :-(
– Claude Leibovici
Dec 4 '18 at 11:30
add a comment |
The solution of
$$e^{lambda(s-1)}-s=0$$ is given in terms of Lambert function and it is
$$s=-frac{Wleft(-lambda, e^{-lambda } right)}{lambda }$$ This function is available in R (have a look here).
When plotted, the integrand looks very nice but you still need numerical integration. If I did it properly, the result is $approx 0.4002$.
Thanks, I know that it can be expressed in the Lambert function, I did that too and got the same answer but since we have not covered this Lambert function, we are not supposed to ue it. Is there a way to do this without using that function?
– Parseval
Dec 4 '18 at 11:27
1
@Parseval. No idea ! Sorry for that. Cheers :-(
– Claude Leibovici
Dec 4 '18 at 11:30
add a comment |
The solution of
$$e^{lambda(s-1)}-s=0$$ is given in terms of Lambert function and it is
$$s=-frac{Wleft(-lambda, e^{-lambda } right)}{lambda }$$ This function is available in R (have a look here).
When plotted, the integrand looks very nice but you still need numerical integration. If I did it properly, the result is $approx 0.4002$.
The solution of
$$e^{lambda(s-1)}-s=0$$ is given in terms of Lambert function and it is
$$s=-frac{Wleft(-lambda, e^{-lambda } right)}{lambda }$$ This function is available in R (have a look here).
When plotted, the integrand looks very nice but you still need numerical integration. If I did it properly, the result is $approx 0.4002$.
answered Dec 4 '18 at 11:23
Claude LeiboviciClaude Leibovici
119k1157132
119k1157132
Thanks, I know that it can be expressed in the Lambert function, I did that too and got the same answer but since we have not covered this Lambert function, we are not supposed to ue it. Is there a way to do this without using that function?
– Parseval
Dec 4 '18 at 11:27
1
@Parseval. No idea ! Sorry for that. Cheers :-(
– Claude Leibovici
Dec 4 '18 at 11:30
add a comment |
Thanks, I know that it can be expressed in the Lambert function, I did that too and got the same answer but since we have not covered this Lambert function, we are not supposed to ue it. Is there a way to do this without using that function?
– Parseval
Dec 4 '18 at 11:27
1
@Parseval. No idea ! Sorry for that. Cheers :-(
– Claude Leibovici
Dec 4 '18 at 11:30
Thanks, I know that it can be expressed in the Lambert function, I did that too and got the same answer but since we have not covered this Lambert function, we are not supposed to ue it. Is there a way to do this without using that function?
– Parseval
Dec 4 '18 at 11:27
Thanks, I know that it can be expressed in the Lambert function, I did that too and got the same answer but since we have not covered this Lambert function, we are not supposed to ue it. Is there a way to do this without using that function?
– Parseval
Dec 4 '18 at 11:27
1
1
@Parseval. No idea ! Sorry for that. Cheers :-(
– Claude Leibovici
Dec 4 '18 at 11:30
@Parseval. No idea ! Sorry for that. Cheers :-(
– Claude Leibovici
Dec 4 '18 at 11:30
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3025426%2fcompute-integral-for-expected-value-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In general, if you get
the condition has length > 1 and only the first element will be used
that means your function is not suitable for vector arguments. Either uselapply
,sapply
or just do a loop so the argument is not a vector– Yuriy S
Dec 4 '18 at 11:27
@YuriyS - Loop with what index? I don't understand where to place the loop. Can you elaborate?
– Parseval
Dec 4 '18 at 11:29