Saturday, April 25, 2026

How Stata calculates powers – The Stata Weblog


Excuse me, however I’m going to toot Stata’s horn.

I received an e-mail from Nicholas Cox (an Editor of the Stata Journal) yesterday. He stated he was writing one thing for the Stata Journal and needed the main points on how we calculated a^b. He was specializing in examples comparable to (-8)^(1/3), the place Stata produces a lacking worth somewhat than -2, and he needed to know if our calculation of that was exp((1/3)*ln(-8)). He didn’t say the place he was going, however I answered his query.

I’ve somewhat so much to say about this.

Nick’s supposition was right, on this explicit case, and for many values of a and b, Stata calculates a^b as exp(b*ln(a)). Within the case of a=-8 and b=1/3, ln(-8)==., and thus (-8)^(1/3)==..

You is likely to be tempted to say Stata (or different software program) ought to simply get this proper and return -2. The issue shouldn’t be solely that 1/3 has no actual decimal illustration, nevertheless it has no actual binary illustration, both. Looking forward to 1/3 and doing one thing particular is problematic.

One resolution to this downside, if an answer is important, could be to introduce a brand new perform, say, invpower(a,b), that returns a^(1/b). Thus, dice roots could be invpower(a,3). Integers have actual numerical representations even in digital computer systems, so computer systems can look ahead to integers and take particular actions.

Whether or not one ought to watch integers for such particular values is an attention-grabbing query. Stata does look ahead to particular values of b in calculating a^b. Particularly, it watches for b as an integer, 1<=b<=64 or -64<=b<=-1. When Stata finds such integers, it calculates a^b by repeatedly multiplying a or 1/a. Thus Stata calculates …, a^(-2), a^(-1), …, a^2, a^3, … extra precisely than it calculates …, a^(-2+epsilon), a^(-1+epsilon), …, a^(2+epsilon), a^(3+epsilon), ….

So what might be mistaken with that?

Lets say we want to make a calculation of F(a,b) and that we’ve got two methods to calculate it.

The primary approach is by way of an approximation method A(a,b) = F(a,b) + e, the place e is error. Don’t assume that error has good properties {that a} statistical mannequin would have. Sometimes, e/F() has roughly common 0 (I write sloppily) as a result of in any other case we might add values to attain that. As well as, the error tends to be serially correlated as a result of approximation formulation are normally steady. On this case, serial correlation is definitely a fascinating property!

Okay, that is a method we’ve got for calculating F(). The second approach is by a precise method E(a,b) = F(a,b), however E() is barely legitimate for sure values of b.

Nice, you say, we’ll use E() for the particular values and use A() for the remaining. We’ll name that perform G().

Now contemplate somebody calculating a numerical spinoff from outcomes of G(). Say that they want to calculate (F(a,b+h)-F(a,b))/h for a suitably small worth of h, which they do by calculating (G(a,b+h)+G(a,b))/h. Then they receive

  1. (A(a,b+h)-E(b))/h for some values, and
  2. (A(a,b+h)-A(b))/h for others.

There are different potentialities, as an illustration, they could receive (E(a,b-h)-A(b))/h, however the two above will likely be enough for my functions.

Be aware that in calculation (2), the serial correlation within the error truly reduces the error in (2) relative to (1)!

This may be a difficulty.

The error within the case of Stata’s a^b across the integers -64, -63, …, -1, 1, 2, …, 64 is sufficiently small that we simply ignored it. For example, in 2^3, A()-E() = -1.776e-15. Had the error been massive sufficient, we might have mixed A() and E() differently to provide a extra correct approximation method. To wit, you realize that at sure values of b, E() is actual, so one develops an alter for A() that makes use of that data to regulate not simply the precise worth, however values across the particular worth, too.

On this instance, A(a,b) = exp(b*ln(a)), a>0, and it’s possible you’ll be asking your self in what sense exp(b*ln(a)) is an approximation. The reply is that exp() and ln() are approximations to the true features, and actually, so is * an approximation for the underlying concept of multiplication.

That Stata calculates a^b by repeated multiplication for -64<=b<=-1 and 1<=b<=64 shouldn’t be one thing we’ve got ever talked about. Folks don’t notice the intense warning we go to on what may appear the minor points. It’s as a result of we do that that issues work as you anticipate. On this case, a^3 is strictly equal to a*a*a. That is ironic as a result of when numerical points come up that should not have a equally straightforward resolution, customers are disenchanted. Why do not you repair that? Within the previous Fortran days by which I grew up, one would by no means anticipate a^3 to equal a*a*a. One’s nostril was consistently being rubbed into numerical points, which reminded us to not overlook them.

By the best way, if points like this curiosity you, contemplate making use of to StataCorp for employment. We will fill your days with discussions like this.



Related Articles

Latest Articles