Solver for simple probability evaluation I'm looking into a simple probabilistic evaluation that goes like this:
Given an unknown probability $p$, $0 < p < 1$, which is assumed to have the same value for each event, a known count $n+1$ and a probability $0 \lt C \lt 1$.
What is the value of $p$ given that after $n+1$ times the probability of a successful event is $\gt C$?
As I write in in equation form:
$$p \cdot (1-p)^n \ge C$$
When I try to solve this with newton raphson I set
$x = 0.1$
Then for some iteration count:
$x = x - \frac{f(x)}{f^´(x)}$
$$
f(x) = x \cdot (1-x)^n - C \\
f^´(x) = (1-x)^{n-1} \cdot (1 - x \cdot (n+1))
$$
Writing a solver for this using Newton-Raphson and setting $n=2$ I get the solution $p=1.6511...$ which is not what I wanted. I wanted to $p$ to be constrained to $0 \lt p \lt 1$.
Fair enough though Newton-Raphson finds a solution when $p$ is unconstrained with some approximation. Another observation seems to indicate that this solver is highly unstable, for example setting N to odd numbers gives invalid solutions.
I assume I can't use newton-raphson since $p$ is constrained. Anyone can hint what solver I should use for this problem? I want to be able to model and write the whole solver code in my own c-program (roughly as the code below).
Here is the prototype solver in c/c++:
#include <stdio.h>
#include <math.h>

double N = 2;
double C = 0.7;
double f(double x) {
    return x * pow(1 - x, N) - C;
}
double fp(double x) {
    return pow(1 - x, N - 1) * (1 - x * (N + 1));
}

double solve(double x0, int nsteps) {
    int i;
    double x = x0;
    for (i = 0; i < nsteps; i++) {
        double f_value = f(x);
        double f_deriv = fp(x);
        x -= f_value / f_deriv;
    }
    return x;
}

int main(int, char**) {
  double p = solve(0.1, 1000);
  printf("p=%f, f(p)=%f\n", p, f(p));
  return 0;
}

Thanks for any feedback!
 A: Given integer $n\geq 1, C\in (0,1),$ you wish to solve the inequality  $$p\in (0,1): p(1-p)^n\geq C$$
or equivalently, letting $q=1-p,$
$$q\in (0,1): F(q)\equiv q^{n+1}-q^n+C\leq 0.$$
Observe $$F(0)=F(1)=C>0,\\F'(q)=q^{n-1}((n+1)q-n)\gtreqqless0\iff q\gtreqqless\frac{n}{n+1}=:q^*\in (0,1).$$ This tells us that for $q\in (0,1)$, $F$ first monotonically decreases, attains a relative minimum at $q^*$, and then monotonically increases. Thus, we have three cases:

*

*If $F(q^*)>0$, the inequality is never satisfied.


*If $F(q^*)=0$, the inequality is met uniquely at $q=q^*.$


*If $F(q^*)<0$, the inequality is met on the interval $(q_-,q_+)$ where $q_-,q_+$ are roots of $F$ that can be found numerically. Newton's method should get you each root if you choose a seed value close enough (see the conditions for quadratic convergence).
Note in your test example where $n=2,C=0.7$, the root you obtained numerically is correct, but there is no root in the unit interval since $F(q^*)\approx 0.55>0$ so your inequality is not met.
A: Note that $\ f'(x)>0\ $ for $\ x<\frac{1}{n+1}\ $ or $\ x>1\ $ when $\ n\ $ is even, $\ f'(x)=0\ $ when $\ x=\frac{1}{n+1}\ $ or $\ x=1\ $ and $\ n\ge2\ $, and $\ f'(x)<0\ $ when $\ \frac{1}{n+1}<x<1\ $, $\ x>1\ $ and $\ n\ $ is odd, or $\ x=n=1\ $.  Therefore $\ f(x)\ $ strictly increases from $\ 0\ $ to $\ \frac{n^n}{(n+1)^{n+1}}\ $ as $\ x\ $ increases from $\ 0\ $ to $\ \frac{1}{n+1}\ $, and strictly decreases from $\ \frac{n^n}{(n+1)^{n+1}}\ $ back down to $\ 0\ $ as $\ x\ $ increases from $\ \frac{1}{n+1}\ $ to $\ 1\ $.
If $\ n\ $ is even, $\ f(x)\ $ strictly increases from $\ 0\ $ to $\ \infty $ as $\ x\ $ increases from $\ 1\ $, and if $\ n\ $ is odd, it strictly decreases $\ 0\ $ to $\ -\infty $ as $\ x\ $ increases from $\ 1\ $.
It follows from all of this that if $\ C>\frac{n^n}{(n+1)^{n+1}}\ $ then the equation $\ f(x)=C\ $ has no real solutions when $\ n\ $ is odd or exactly one real solution when $\ n\ $ is even, and in the latter case the unique solution will always be strictly greater than $\ 1\ $.  In your example, where $\ x\approx1.6511\  $ and $\ n=4\ $, you must have $\ C\approx0.7>\frac{n^n}{(n+1)^{n+1}}=\frac{4}{27}\ $, so there must be a unique (real) solution to the equation, and it cannot lie in the interval $\ [0,1]\ $.
If $\ C=\frac{n^n}{(n+1)^{n+1}}\ $, then the equation $\ f(x)=C\ $ will have a unique solution $\ x= \frac{1}{n+1}\ $ within the interval $\ [0,1]\ $, and another one in the interval $\ (1,\infty)\ $ whenever $\ n\ $ is even.
If $\ 0<C<\frac{n^n}{(n+1)^{n+1}}\ $, then the equation $\ f(x)=C\ $ will have one solution in the interval $\ \left(0,\frac{1}{n+1}\right)\ $, a second in the interval $\ \left(\frac{1}{n+1},1\right)\ $, and a third in the interval $\ (1,\infty)\ $ whenever $\ n\ $ is even.  If you choose your starting point for your Newton-Raphson in the interval $\ \left(0,\frac{1}{n+1}\right)\ $, then it should converge to the unique solution of the equation lying within that interval, while if you choose your starting point in the interval $\ \left(\frac{1}{n+1},1\right)\ $, it should converge to the unique solution in that one.
