# [R] optimize simultaneously two binomials inequalities using nlm( ) or optim( )

From: <emir.toktar_at_gmail.com>
Date: Mon, 04 Aug 2008 23:55:18 -0300

I´m trying to optimize simultaneously two binomials inequalities (used in acceptance sampling) which are nonlinear solution, so there is no simple direct solution. Please, let me explain shortly the the problem and the question as following.

The objective is to obtain the smallest value of 'n' (sample size) satisfying both inequalities:

(1-alpha) <= pbinom(c, n, p1) && pbinom(c, n, p2) <= beta

Where p1 (AQL) and p2 (LTPD) are probabilities parameters (Consumer and Producer), the alpha and beta are conumer and producer risks, and finally, the 'n' represents the sample size and the 'c' an acceptance number or maximum number of defects (nonconforming) in sample size.

Considering that the 'n' and 'c' values are integer variables, it is commonly not possible to derive an OC curve including the both (p1,1-alpha) and (p2,beta) points. Some adjacency compromise is commonly required, achieved by searching a more precise OC curve with respect to one of the points.

I´m using Mathematica 6 but it is a Trial, so I would like use R intead (or better, I need it)!

To exemplify, In Mathematica I call the function using NMinimize passing the restriction and parameters:

restriction = (1 - alpha) <= CDF[BinomialDistribution[sample_n, p1], c]

```		&&  betha >= CDF[BinomialDistribution[sample_n, p2], c]
&&   0 < alpha < alphamax && 0 < betha < bethamax
&&   1 < sample_n <= lot_Size   &&   0 <= c < lot_size
&&  p1 < p2 < p2max ;

fcost = 	sample_n/lot_Size;

```

result = NMinimize[{fcost, restriction}, {sample_n, c, alpha, betha, p2max}, Method -> "NelderMead", AccuracyGoal -> 10];

/* Calling the function findOpt */
findOpt[p1=0.005, lot_size=1000, alphamax=0.05, bethamax =0.05, p2max = 0.04]

/* and I got the return of values of; minimal "n", "c", "alpha", "betha" and
the "p2" or (LTPD) were computed */ {0.514573, {alpha\$74 -> 0.0218683, sample_n\$74 -> 155.231, betha\$74 -> 0.05, c\$74 -> 2, p2\$74 -> 0.04}}

Now, using R, I would define the "pbinom(c, n, prob)" given only the minimum and maximum values to "n" and "c" and limits to p1 and p2 probabilities (Consumer and Producer), similar on the example above.

I found some examples to optimize equations in R and some tips, but I not be able to define the sintaxe to use with that functions. Among the functions that could be used to resolve the problem presented, I found the function optim() that it is used for unconstrained optimization and the nlm() which is used for solving nonlinear unconstrained minimization problems. May I wrong, but the nlm() function would be appropriate to solve this problem, is it right?

Can I get a pointer to solve this problem using the nlm() function or where could I get some tips/example to help me, please?

// (1-alpha) <= pbinom(c, n, p1) && pbinom(c, n, p2) <= beta It was used "betha" parameter name to avoid the 'beta' function used in Mathematica...

findS <- function(p1='numeric', lot_size='numeric', alphamax='numeric', bethamax ='numeric', p2max ='numeric')
{

(1 - alpha) <= pbinom(c, sample_n, p1) && betha >= pbinom(c, sample_n, p2)

```	&&   0 < alpha < alphamax && 0 < betha < bethamax
&&   1 < sample_n <= lot_Size   &&   0 <= c < lot_size
&&  p1 < p2 < p2max ;
```

}

Parameters:

```	p1=0.005,
lot_size=1000,
alphamax=0.05,
bethamax =0.05,
p2max = 0.04

Minimize results should return/printing the following values:
sample_n, 	(minimal sample size)
c , 		(critical level of defectives)
alpha , 	(producer's risk)
betha , 	(consumer's risk)
p2max	(consumer's probability p2)

```

Could one help me understand how can desing the optimize nonlinear function using R for two binomials or point me some tips?