Hi everyone,
How do I calculate the roots of a function in JSL for confidence intervals?
I have a special case, where I need to calculate confidence Intervals which aren't implemented in JMP.
Using the Wilks theorem, I want to calculate confidence intervalls by calculating the roots of the following function:
f(x)=-2log(Likelihood(x|Data))-ChiSquare Quantile( 1-alpha, 1).
Where x is a real number and the Data is a univariate sample
Is there any build in JSL function I can use? Or at least something similar?
I couldn`t find anything on the web or in the scripting index.
I would really appreciate some suggestions.
Thanks in advance
See Help about the Minimize() function. Your expression is Abs( -2log(Likelihood(x|Data))-ChiSquare Quantile( 1-alpha, 1) ). This numerical optimization should be able to solve for the roots. The easiest way would be to use a Script Editor. Select File > New > Script. Right-click and select Show Embedded Log. This way the result of the function call will be shown in the same window.
See Help about the Minimize() function. Your expression is Abs( -2log(Likelihood(x|Data))-ChiSquare Quantile( 1-alpha, 1) ). This numerical optimization should be able to solve for the roots. The easiest way would be to use a Script Editor. Select File > New > Script. Right-click and select Show Embedded Log. This way the result of the function call will be shown in the same window.
Thanks for the answer,
the Minimize function seems to be seems to be a little unstable in extreme situations.
However, your tip gave me a good starting point to come up with a solution.
Minimize(Abs(function)) can behave badly in some cases. JMP should really add a bracketed root solver, along the lines of uniroot() in R. Until that happens, Ridders' method ( Ridders' method - Wikipedia ) can be programmed in jsl. It is nearly as efficient as Brent's method ( Brent's method - Wikipedia ), but easier to program. Numerical Recipes includes an implementation.
Why don't you add this suggestion to the Wish List discussion? Search first to see if anyone else has already suggested it and vote for it (kudo). If no one else has, then add it for consideration. Thanks!
Done last week add function to find a root (zero) of a function
@DunnTiger764 Did you end up finding a solution to this problem?
Hello, @olithun -san, @rcast15 -san and @DunnTiger764 -san,
I am Yusuke Ono, Senior Tester at JMP Japan. I apologize for my poor English in advance.
dt = New Table( "Binomial Data",
New Column( "k", Values( [10] )),
New Column( "n", Values( [500] )),
New Column( "Negative Loglikehood",
Formula( Parameter( {p = 0.5}, -:k * Log( p ) - (:n - :k) * Log( 1 - p ) ) ),
)
);
dt << Nonlinear(
Loss( :Negative Loglikehood ),
CL Limit( 1e-15 ),
Loss is Neg LogLikelihood( 1 ),
Newton,
Finish,
Plot( 0 ),
Confidence Limits
);
p0 = 0.0348142211885808;
p1 = 0.02;
Show(ChisquareQuantile(0.95,1));
Show(2 *(Log(Binomial Probability(p1,500,10)) - Log(Binomial Probability(p0,500,10))));
/*
ChiSquare Quantile(0.95, 1) = 3.84145882069412;
2 * (Log(Binomial Probability(p1, 500, 10)) - Log(Binomial Probability(p0, 500, 10))) = 3.84145882069407;
*/
Names Default To Here(1);
/* Regula Falsi method */
/* https://en.wikipedia.org/wiki/Regula_falsi */
Regula Falsi = Function({f, data, x0, x2, tol=1e-8, niter = 1000}, {Default Local},
fx0 = f(x0, data);
fx2 = f(x2, data);
If(fx0 * fx2>0, Write("f(pl)*f(pu) should be less than zero"); Return(.));
For(i = 1, i <= niter, i++,
If(Abs(fx0)<tol, Return(x0),
Abs(fx2)<tol, Return(x2)
);
x1 = (fx0*x2 - fx2*x0)/(fx0 - fx2);
fx1 = f(x1, data);
If(fx1*fx0>0, fx0=fx1; x0=x1,
fx1*fx2>0, fx2=fx1; x2=x1,
Return(x1)
);
);
Return(.);
);
/* Ridders' method */
/* https://en.wikipedia.org/wiki/Ridders%27_method */
Ridders = Function({f, data, x0, x2, tol=1e-8, niter = 1000}, {Default Local},
fx0 = f(x0, data);
fx2 = f(x2, data);
If(fx0 * fx2>0, Write("f(pl)*f(pu) should be less than zero"); Return(.));
For(i = 1, i <= niter, i++,
If(Abs(fx0)<tol, Return(x0),
Abs(fx2)<tol, Return(x2)
);
x1 = (x0 + x2) / 2;
fx1 = f(x1, data);
sign fx0 = If(fx0>0,1,fx0<0,-1, Retrun(x0));
x3 = x1 + (x1-x0) * sign fx0 * fx1 /Sqrt(fx1^2 - fx0*fx2);
fx3 = f(x3, data);
If( fx1*fx3 < 0,
x0 = x1; fx0 = fx1;
x2 = x3; fx2 = fx3;
,
fx3*fx0>0, fx0=fx3; x0=x3,
fx3*fx2>0, fx2=fx3; x2=x3,
Return(x3)
);
);
Return(.);
);
/* Solve for x to satisfy f(x, data) = 0 */
f = Function({p, data}, {Default Local},
k = data[1];
n = data[2];
chi = data[3];
p1 = k/n;
2*(k*Log(p1/p)+(n-k)*Log((1-p1)/(1-p))) - chi
);
k = 10;
n = 500;
chi = Chisquare Quantile(1-0.05, 1);
data = k||n||chi;
/* Upper Confidence Limit */
pl = k/n;
pu = 0.99999;
ci up rf= Regula Falsi(NameExpr(f), data, pl, pu);
ci up = Ridders(NameExpr(f), data, pl, pu);
/* Lower Confidence Limit */
pl = 0.00001;
pu = k/n;
ci low rf= Regula Falsi(NameExpr(f), data, pl, pu);
ci low = Ridders(NameExpr(f), data, pl, pu);
Show(ci low rf||ci up rf);
Show(ci low || ci up);