Introduction
This guide provides a simple formula to determine the optimal strategy that maximizes the expected candies gained from catching a raid boss.
Conclusion

The optimal strategy is always using Pinap Berry first and Golden Razz Berry (GRB) last.

The number of balls left where you switch to GRB is independent of the number of balls you receive
Let

$p_{1}$ be the catch rate of using GRB

$c_{1}$ be the number of candies gained of using GRB

$p_{2}$ be the catch rate of using Pinap

$c_{2}$ be the number of candies gained of using Pinap
Where $0 < p_{2} < p_{1} < 1$ and $0 < c_{1} < c_{2}$. Then:

If $p_{1} c_{1} > p_{2} c_{2} $, then the optimal strategy is using GRB in the last $k$ balls, where $$ k = \lceil \log_{1  p_{1}} ( \frac{ c_{2}  c_{1} }{ c_{1} } \frac{ p_{2} }{ p_{1}  p_{2} } ) \rceil $$

If $p_{1} c_{1} \leq p_{2} c_{2} $, then it is optimal to always use Pinap.
For the bosses with base catch rate of 2% (including the current Eon Duo), assuming you have golden badges, and you’ll transfer the boss if caught (and get one candy, during noncandyevent), the optimal strategy is:
Excellent Curve  Great Curve  Nice Curve  

Nonboosted  
Weather Boosted 
The result above uses the Grand Unified Catch Theory. From it, we calculate the catch rates on a single throw in all cases:
Excellent Curve  Great Curve  Nice Curve  

Nonboosted  14.668% / 6.154%  12.166% / 5.054%  9.837% / 4.054% 
Weather Boosted  13.207% / 5.514%  11.167% / 4.626%  8.832% / 3.628% 
Derivation
In any case (using GRB or Pinap), denote the catch rate as $p$ and the number of candies gained as $c$.
Let $X_{n}$ be the number of candies gained with $n$ balls to throw. $X_{n}$ takes two possible values:
\[ X_{n} = \begin{cases} c & \quad \text{with the probability of } p \\ X_{n1} & \quad \text{with the probability of } 1p \end{cases} \]Therefore:
$$E[X_{n}] = pc + (1p)E[X_{n1}] $$At the $n$th last ball, use GRB if and only if
$$ p_{1}c_{1} + (1p_{1})E[X_{n1}] > p_{2}c_{2} + (1p_{2})E[X_{n1}] $$That is (with the assumption that $p_{1} > p_{2}$):
$$ E[X_{n1}] < \frac { p_{1}c_{1}  p_{2}c_{2} } { p_{1}  p_{2} } $$If $p_{1} c_{1} \leq p_{2} c_{2} $, then it is never optimal to use GRB (hence use Pinap only) since
$$E[X_{n}] \geq 0 \geq \frac { p_{1}c_{1}  p_{2}c_{2} } { p_{1}  p_{2} }$$If $p_{1} c_{1} > p_{2} c_{2} $, then the proof continues.
Note that $E[X_{n}]$ increases in $n$. Hence, if it is better to use GRB at the $n$th last ball, then it must be also better to use GRB at the $(n1)$th last, at the $(n2)$th last, ..., and at the last ball, since:
$$ 0 = E[X_{0}] < E[X_{1}] < \dots < E[X_{n1}] < \frac { p_{1}c_{1}  p_{2}c_{2} } { p_{1}  p_{2} } $$Likewise, if it is better to use Pinap at the $(n+1)$th last ball, then it must be also better to use Pinap at the at the $(n+2)$th last, ..., and at the first ball:
$$ \frac { p_{1}c_{1}  p_{2}c_{2} } { p_{1}  p_{2} } \leq E[X_{n}] < E[X_{n+1}] < \dots $$Suppose using GRB in the last $k$ balls is the optimal strategy. That is, it is better to use GRB at the $k$th last ball and to use Pinap at the $(k+1)$th last ball. Thus:
$$ E[X_{k1}] < \frac { p_{1}c_{1}  p_{2}c_{2} } { p_{1}  p_{2} } \leq E[X_{k}] $$Since it is always using GRB from the $k$th last ball to the very last ball, we have:
\[ \begin{cases} E[X_{k1}] = (1  (1p_{1}) ^ {k1} ) c_{1} \\ E[X_{k}] = (1  (1p_{1}) ^ {k} ) c_{1} \end{cases} \]Plug in and solve the inequality with respect to $k$, and it will give:
$$ k = \lceil \log_{1  p_{1}} ( \frac{ c_{2}  c_{1} }{ c_{1} } \frac{ p_{2} }{ p_{1}  p_{2} } ) \rceil $$
Results have been independently confirmed by reddit user u/ZicNik in this thread.