Làm thế nào để tính sai số tương đối khi giá trị thực bằng 0?


32

Làm cách nào để tính sai số tương đối khi giá trị thực bằng 0?

Nói rằng tôi có xtrue=0xtest . Nếu tôi xác định lỗi tương đối là:

relative error=xtruextestxtrue

Sau đó, lỗi tương đối luôn luôn không được xác định. Nếu thay vào đó tôi sử dụng định nghĩa:

relative error=xtruextestxtest

Sau đó, lỗi tương đối luôn là 100%. Cả hai phương pháp dường như vô dụng. Có cách nào khác không?


I had the exact same question regarding parameter bias in Monte Carlo simulations, using your first definition. One of my parameter values was 0, so I didn't calculate parameter bias for this particular parameter...
Patrick Coulombe

2
The solution is to not use relative error in this case.
Marc Claesen

2
One option, which responds to the intent if not the letter of your question, is to use a slightly different measure that closely agrees with relative error when the relative error is small, such as 2(xtruextest)/(|xtrue|+|xtest|). (Use 0 when xtrue=xtest=0.) This particular solution is universal in that it is invariant under a change in the unit of measure (because it involves no arbitrary constants).
whuber

@whuber I think you should consider posting that comment as an answer, since it seems superior to the existing ones.
Silverfish

@Silver You're right--I apologize for posting an answer as a comment. I have therefore slightly expanded that comment into an answer.
whuber

Câu trả lời:


39

There are many alternatives, depending on the purpose.


A common one is the "Relative Percent Difference," or RPD, used in laboratory quality control procedures. Although you can find many seemingly different formulas, they all come down to comparing the difference of two values to their average magnitude:

d1(x,y)=xy(|x|+|y|)/2=2xy|x|+|y|.

This is a signed expression, positive when x exceeds y and negative when y exceeds x. Its value always lies between 2 and 2. By using absolute values in the denominator it handles negative numbers in a reasonable way. Most of the references I can find, such as the New Jersey DEP Site Remediation Program Data Quality Assessment and Data Usability Evaluation Technical Guidance, use the absolute value of d1 because they are interested only in the magnitude of the relative error.


A Wikipedia article on Relative Change and Difference observes that

d(x,y)=|xy|max(|x|,|y|)

is frequently used as a relative tolerance test in floating point numerical algorithms. The same article also points out that formulas like d1 and d may be generalized to

df(x,y)=xyf(x,y)

where the function f depends directly on the magnitudes of x and y (usually assuming x and y are positive). As examples it offers their max, min, and arithmetic mean (with and without taking the absolute values of x and y themselves), but one could contemplate other sorts of averages such as the geometric mean |xy|, the harmonic mean 2/(1/|x|+1/|y|) and Lp means ((|x|p+|y|p)/2)1/p. (d1 corresponds to p=1 and d corresponds to the limit as p.) One might choose an f based on the expected statistical behavior of x and y. For instance, with approximately lognormal distributions the geometric mean would be an attractive choice for f because it is a meaningful average in that circumstance.


Most of these formulas run into difficulties when the denominator equals zero. In many applications that either is not possible or it is harmless to set the difference to zero when x=y=0.

Note that all these definitions share a fundamental invariance property: whatever the relative difference function d may be, it does not change when the arguments are uniformly rescaled by λ>0:

d(x,y)=d(λx,λy).

It is this property that allows us to consider d to be a relative difference. Thus, in particular, a non-invariant function like

d(x,y)=? |xy|1+|y|

simply does not qualify. Whatever virtues it might have, it does not express a relative difference.


The story does not end here. We might even find it fruitful to push the implications of invariance a little further.

The set of all ordered pairs of real numbers (x,y)(0,0) where (x,y) is considered to be the same as (λx,λy) is the Real Projective Line RP1. In both a topological sense and an algebraic sense, RP1 is a circle. Any (x,y)(0,0) determines a unique line through the origin (0,0). When x0 its slope is y/x; otherwise we may consider its slope to be "infinite" (and either negative or positive). A neighborhood of this vertical line consists of lines with extremely large positive or extremely large negative slopes. We may parameterize all such lines in terms of their angle θ=arctan(y/x), with π/2<θπ/2. Associated with every such θ is a point on the circle,

(ξ,η)=(cos(2θ),sin(2θ))=(x2y2x2+y2,2xyx2+y2).

Any distance defined on the circle can therefore be used to define a relative difference.

As an example of where this can lead, consider the usual (Euclidean) distance on the circle, whereby the distance between two points is the size of the angle between them. The relative difference is least when x=y, corresponding to 2θ=π/2 (or 2θ=3π/2 when x and y have opposite signs). From this point of view a natural relative difference for positive numbers x and y would be the distance to this angle:

dS(x,y)=|2arctan(yx)π/2|.

To first order, this is the relative distance |xy|/|y|--but it works even when y=0. Moreover, it doesn't blow up, but instead (as a signed distance) is limited between π/2 and π/2, as this graph indicates:

Figure

This hints at how flexible the choices are when selecting a way to measure relative differences.


Thanks for the comprehensive answer, what do you think is the best reference for this line : "is frequently used as a relative tolerance test in floating point numerical algorithms. The same article also points out that formulas like d1d1 and d∞d∞ may be generalized to"
Hammad Haleem

1
btw, nevermind I found an academic reference for this :) tandfonline.com/doi/abs/10.1080/00031305.1985.10479385
Hammad Haleem

4
Why has this not been selected as the answer? (sorry if this is not an appropriate comment, but this is the better answer by far)
Brash Equilibrium

2
@Brash I appreciate the sentiment. Acceptance is uniquely the province of the original proposer: nobody can override that (except by deleting the accepted post). On some occasions when I feel as you do, I post comments that point out explicitly how and why I think some answers are better or more noteworthy than others. Even if that fails to change anything, such comments may make the material a little more useful or understandable to future readers: and that, ultimately, is the point of our work on this site.
whuber

1
@KutalmisB Thank you for noticing that: the "min" doesn't belong there at all. It looks like it may have been a vestige of a more complex formula that handled all possible signs of x and y that I later simplified. I have removed it.
whuber

11

First, note that you typically take the absolute value in computing the relative error.

A common solution to the problem is to compute

relative error=|xtruextest|1+|xtrue|.

3
This is problematic in that it varies depending on the units of measure chosen for the values.
whuber

1
That's absolutely true. This isn't a perfect solution to the problem, but it is a common approach that works reasonably well when x is well scaled.
Brian Borchers

Could you elaborate in your answer on what you mean by "well scaled"? For instance, suppose the data arise from calibration of an aqueous chemical measurement system designed for concentrations between 0 and 0.000001 moles/liter which can achieve a precision of, say, three significant digits. Your "relative error" would therefore be constantly zero except for obviously erroneous measurements. In light of this, how exactly would you rescale such data?
whuber

1
Your example is one where the variable isn't well scaled. By "well scaled", I mean that that variable is scaled so that it takes on values in a small range (of e.g. a couple of orders of magnitude) near 1. If your variable takes on values over many orders of magnitude than you've got more serious scaling issues and this simple approach isn't going to be adequate.
Brian Borchers

2
Any reference for this approach? The name of this method? Thank you.
CroCo

0

I was a bit confused on this for a while. In the end, its because if you are trying to measure relative error with respect to zero then you are trying to force something that simply does not exist.

If you think about it, you're comparing apples to oranges when you compare relative error to the error measured from zero, because the error measured from zero is equivalent to the measured value (that's why you get 100% error when you divide by the test number).

For example, consider measuring error of gauge pressure (the relative pressure from atmospheric) vs absolute pressure. Say that you use an instrument to measure the gauge pressure at perfect atmospheric conditions, and your device measured atmospheric pressure spot on so that it should record 0% error. Using the equation you provided, and first assuming we used the measured gauge pressure, to calculate relative error:

relative error=Pgauge,truePgauge,testPgauge,true
Then Pgauge,true=0 and Pgauge,test=0 and you do not get 0% error, instead it is undefined. That is because the actual percent error should be using the absolute pressure values like this:
relative error=Pabsolute,truePabsolute,testPabsolute,true
Now Pabsolute,true=1atm and Pabsolute,test=1atm and you get 0% error. This is the proper application of relative error. The original application that used gauge pressure was more like "relative error of the relative value" which is a different thing than "relative error". You need to convert the gauge pressure to absolute before measuring the relative error.

The solution to your question is to make sure you are dealing with absolute values when measuring relative error, so that zero is not a possibility. Then you are actually getting relative error, and can use that as an uncertainty or a metric of your real percent error. If you must stick with relative values, than you should be using absolute error, because the relative (percent) error will change depending on your reference point.

It's hard to put a concrete definition on 0... "Zero is the integer denoted 0 that, when used as a counting number, means that no objects are present." - Wolfram MathWorld http://mathworld.wolfram.com/Zero.html

Feel free to nit pick, but zero essentially means nothing, it is not there. This is why it does not make sense to use gauge pressure when calculating relative error. Gauge pressure, though useful, assumes there is nothing at atmospheric pressure. We know this is not the case though, because it has an absolute pressure of 1 atm. Thus, the relative error with respect to nothing, just does not exist, it's undefined.

Feel free to argue against this, simply put: any quick fixes, such as adding one to the bottom value, are faulty and not accurate. They can be still be usefully if you are simply trying to minimize error. If you are trying to make accurate measurements of uncertainty though, not so much...


0

MAPE Formula

Finding MAPE,

It is very debatable topic and many opensource contributors have discussed on the above topic. The most efficient approach till now is followed by the developers. Please refer to this PR to know more.

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.