atanh

Returns the inverse hyperbolic tangent of a number, defined for values between -1 and 1.

Signature
atanh(float $num): float
  • Added in PHP 4.1.0

Parameters

ParameterTypeDescription
$numfloatThe argument to process. Must be between -1 and 1.

Return value

floatThe inverse hyperbolic tangent of num. Returns INF or -INF at 1 and -1, and NAN outside that range.

Errors

Throws a TypeError if num cannot be coerced to float. Out-of-domain values return NAN instead of throwing.

atanh() is the inverse of tanh(). It equals 0.5 * log((1 + $num) / (1 - $num)), and it maps the open interval -1 to 1 onto the whole number line, stretching values near the ends towards infinity.

When to reach for it

Its signature use in statistics is the Fisher z-transform: correlation coefficients are bounded between -1 and 1 and their sampling distribution is skewed near the ends, so you apply atanh() to make them roughly normal, average or build a confidence interval there, then map back with tanh(). Averaging raw correlations without that step understates the result.

The same trick applies anywhere you need to turn a bounded score — a similarity value, a normalised rating, a probability rescaled to -1..1 — into an unbounded one so a linear model or a plain arithmetic mean behaves sensibly.

What trips people up

The endpoints are not errors: atanh(1) is INF and atanh(-1) is -INF. That means a perfect correlation of 1.0 produces an infinity that quietly turns a subsequent average into INF or NAN, and json_encode() refuses to serialise it. Clamp inputs slightly inside the boundary before transforming.

Beyond the endpoints you get NAN, which must be detected with is_nan() because comparisons against it are always false.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Average several correlation coefficients correctly using the Fisher z-transform.
  • Build a confidence interval for a Pearson correlation.
  • Turn a bounded -1 to 1 similarity score into an unbounded feature for a linear model.

Frequently asked questions

Why does atanh(1) return INF instead of an error?

Because tanh() approaches 1 only as its input goes to infinity, so infinity is the mathematically correct answer. PHP returns INF rather than throwing, which means you have to guard against it yourself.

What is the difference between atanh() and atan()?

atan() inverts the circular tangent, accepts any real number and returns an angle between -90 and 90 degrees. atanh() inverts the hyperbolic tangent, accepts only values between -1 and 1, and returns an unbounded hyperbolic angle.

How do I average correlation coefficients in PHP?

Transform each one with atanh(), average the transformed values, then map back with tanh(). A plain arithmetic mean of the raw coefficients underestimates the result because the -1 to 1 scale is not linear.

More examples

Fisher z-transform to average correlations

The naive mean is 0.7233, but averaging in z-space and mapping back with tanh() gives 0.7847. The difference comes from the strong 0.92 carrying more weight once the bounded scale is stretched out.

Example · php
<?php
$r = [0.80, 0.92, 0.45];
$z = array_map('atanh', $r);
$meanZ = array_sum($z) / count($z);
printf("naive mean r = %.4f\n", array_sum($r) / count($r));
printf("fisher mean r = %.4f\n", tanh($meanZ));

Clamping away the infinity

The raw average is float(INF) because atanh(1.0) is infinite and infinity dominates the sum. Clamping to 0.999999 turns that infinity into 7.2543, so the average becomes a finite 3.9018 instead, which can be stored and serialised.

Example · php
<?php
$scores = [0.5, 1.0];
$raw = array_map('atanh', $scores);
var_dump(array_sum($raw) / count($raw));
$safe = array_map(fn($s) => atanh(max(-0.999999, min(0.999999, $s))), $scores);
printf("%.4f\n", array_sum($safe) / count($safe));

Discussion

  • Be the first to comment on this lesson.