EDIT/UPDATE:Question How do I cut down the numberuse NIntegrate/NExpectation to integrate over a vector function for which each element is a function of parameters anda common solution to an underlying equation?
Background I know that NIntegrate can integrate vectors, but according to a comment to this question, the sizevector structure of the output must be particularly obvious to NIntegrate. The comment constrasts NIntegrate[{Sin[x],Cos[x]},{x,0,1}], which will work, with f[x_?NumericQ]:={Sin[x],Cos[x]}; NIntegrate[f[x],{x,0,1}], which does not work. In the example that works, the vector can be written in an obvious way because each element can be calculated completely separately from the others. I don't know how to make the code/example more "minimal"vector structure apparent to NIntegrate when each element of my vector function needs the root of an equation that I want avoid solving separately for each element of the vector.
Details To be more specific, I need to solve for the smallest root greater than zero of a function of the incomplete Beta function, its first derivative, and a few parametersparameter. $$x^*(k) = \min\{x: x> 0 \text{ and } B(1,2,2) - B(x,2,2) - kx(1-x) = 0\}$$ for which $B()$ is the incomplete beta function, and $B^{\prime}() = x(1-x)$ is its first derivative.
Then, using this solution, I calculate a vector of objects of interest (for simplicity, I'm focusing on just two elements in my example) $\{\frac{B(x^*(k),2,2)}{(B(1,2,2)}, \frac{B(x^*(k),3,2)}{B(x^*(k),2,2)}\}$
Finally, I want to give the parameter a distribution and integrate the vector of interest over that distribution.
Clear[f, xClear[x, impliedCapF0equation, startVals, modelOutcomes]; f[x_] = x*(1 - x); impliedCapF0[x_equation[x_?NumericQ, const_]param_] := Beta[2,2] - Beta[x, 2, 2] +- f[x]*const;x*(1 - x)*param; startVals = Range[1/10, 9/10, 1/10]; (*initial values for FindRoot*) modelOutcomes[cost_modelOutcomes[param_?NumericQ] := Module[ {capF0 = Beta[2, 2], xSoln, result1, result2}, (*find the smallest root greater than 0 *) xSoln = Min[ Part[ FindRoot[ capF0 - impliedCapF0[xValFindRoot[equation[xVal, cost/80000]param], {xVal, #, 0, 1}] & /@ startVals, All, 1, 2] ]; (*calculate results*) result1 = Beta[xSoln, 2, 2] /capF0; Beta[2, 2]; result2 = Beta[xSoln, 3, 2] / Beta[xSoln, 2, 2]; {result1, result2} ]; I want to give the parameters a joint density and integrate the vector of interest over that density.
mu = 8;1/10; sigma = 1/800;1000; NExpectation[modelOutcomes[z1] , z1 \[Distributed] LogNormalDistribution[mu, sigma]] (* Integrand modelOutcomes[E^8modelOutcomes[E^(1/10) z1] Piecewise[{{(400500 E^(-320000500000 Log[z1]^2) Sqrt[2/\[Pi]])/z1,z1>0}},0] is not numerical at {z1}={250.34090521920675`}*) Why am I being told the Integrand is not numerical? I'm using the ?NumericQ pattern test on the arguments. And ifSo as I plug in the value shown in the stack trace forhave written the error, namely 250.34090521920675*Exp[8] for z1code, the function has no problems producing a numerical result
modelOutcomes[250.34090521920675*Exp[8]] (*{0.000979518, 0.0121014}*) And NIntegrate doesn't seemNIntegrate is unable to havesee and expect a problem with the vector per seoutput from the Integrand, because it integrates a constant vector without problem
NExpectation[modelOutcomes[10000], z1 \[Distributed] LogNormalDistribution[mu, sigma]] (*{0.434068, 0.223295}*) What do I need to change sotelling me that NExpectation handles this without error?the output is "not numerical."
Update If If I alter modelOutcomes so that it returns a scalarjust result1, then NExpectation works. If I alter it to return a list of length 1, containing the same scalar{result1}, it gives me the same "not numerical" error. So I wonder if this is about the function returning a vector/list.
Update According to a comment to this question, NIntegrate needsproblem seems to know that the function produces a vector. The comment constrastsbe about getting NIntegrate[{Sin[x],Cos[x]},{x,0,1}], which will work, with f[x_?NumericQ]:={Sin[x],Cos[x]}; NIntegrate[f[x],{x,0,1}]NIntegrate, which does not work. The challenge in my application is that I need to find the root of an underlying function and use that result to calculate each element of the outcomes vector. If I treat each element of theexpect a vector completely separately I will have to find the root separately, which I need to avoid to keep computation time reasonableintegrand.