4. Given that $a, b$ are positive real numbers, $n \in \mathbf{N}_{+}$. Then the function
$$
f(x)=\frac{\left(x^{2 n}-a\right)\left(b-x^{2 n}\right)}{\left(x^{2 n}+a\right)\left(b+x^{2 n}\right)}
$$

has a maximum value of $\qquad$ .
4. $\left(\frac{\sqrt{a}-\sqrt{b}}{\sqrt{a}+\sqrt{b}}\right)^{2}$.

Let $t=x^{2 n}$. Then $t \geqslant 0$, H
$$
\begin{aligned}
f & =\frac{(t-a)(b-t)}{(t+a)(b+t)} \\
& =\frac{-t^{2}+(a+b) t-a b}{t^{2}+(a+b) t+a b} \\
& =-1+\frac{2(a+b) t}{t^{2}+(a+b) t+a b} .
\end{aligned}
$$

When $t=0$, $f=-1$;
When $t>0$,
$$
\begin{aligned}
f & =-1+\frac{2(a+b)}{(a+b)+t+\frac{a b}{t}} \\
& \leqslant-1+\frac{2(a+b)}{(a+b)+2 \sqrt{a b}}=\left(\frac{\sqrt{a}-\sqrt{b}}{\sqrt{a}+\sqrt{b}}\right)^{2} .
\end{aligned}
$$

The equality holds if and only if $t=\sqrt{a b}$.
Therefore, the maximum value of $f(x)$ is $\left(\frac{\sqrt{a}-\sqrt{b}}{\sqrt{a}+\sqrt{b}}\right)^{2}$.