Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

iphone - Cumulative Normal Distribution function in objective C

Anyone know of a good implementation of this whose license is compatible with non-free iPhone apps?

As suggested in this question, Boost looks absolutely wonderful. But as best I can tell, it is only available in C++.

Cumulative Normal Distribution Function in C/C++

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No need for anything fancy. Any platform with a good C99 math library (like the iphone) already has everything you need -- specifically the erfc function, which is a slight variant:

#import <math.h>

double cumulativeNormal(double x) {
    return 0.5 * erfc(-x * M_SQRT1_2);
}

Note this is for the standard normal distribution (i.e. mean = 0, variance = 1), you'll need to use the appropriate scaling -- so for some mean and variance, it will be:

return 0.5 * erfc(((mean - x)/sqrt(variance)) * M_SQRT1_2);

If you don't need double precision, change the doubles to floats, use the erfcf function, and change the literals to single-precision literals.

Remember, Objective-C is an extremely light extension to C. Every C function is an Objective-C function, with no wrapping or other shenanigans required.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...