+*Let PHI(x) be the normal cdf. Suppose that Q calculates +*1.0 - PHI(x), and that QINV calculates QINV(p) for p in (0.0,.5]. +*Then for p .le. .5, x = PHIINV(p) = -QINV(p). +*For p .gt. .5, x = PHIINV(p) = QINV(1.0 - p). +*The formula for approximating QINV is taken from Abramowitz and Stegun, +*Handbook of Mathematical Functions, Dover, 9th printing, +*formula 26.2.3, page 933. The error in x is claimed to +*be less than 4.5e-4 in absolute value. +* +*@param p p must lie between 0 and 1. xnormi returns +* the normal cdf inverse evaluated at p. +* +*@author Steve Verrill +*@version .5 --- June 7, 1996 +* +*/ + +// FIX: Eventually I should build in a check that p lies in (0,1) + + public static double xnormi(double p) { + + double arg,t,t2,t3,xnum,xden,qinvp,x,pc; + + final double c[] = {2.515517, + .802853, + .010328}; + + final double d[] = {1.432788, + .189269, + .001308}; + + if (p <= .5) { + + arg = -2.0*Math.log(p); + t = Math.sqrt(arg); + t2 = t*t; + t3 = t2*t; + + xnum = c[0] + c[1]*t + c[2]*t2; + xden = 1.0 + d[0]*t + d[1]*t2 + d[2]*t3; + qinvp = t - xnum/xden; + x = -qinvp; + + return x; + + } + + else { + + pc = 1.0 - p; + arg = -2.0*Math.log(pc); + t = Math.sqrt(arg); + t2 = t*t; + t3 = t2*t; + + xnum = c[0] + c[1]*t + c[2]*t2; + xden = 1.0 + d[0]*t + d[1]*t2 + d[2]*t3; + x = t - xnum/xden; + + return x; + + } + + } + + +/** +* +*This method calculates the normal cumulative distribution function. +*
+*It is based upon algorithm 5666 for the error function, from:
+*
+* Hart, J.F. et al, 'Computer Approximations', Wiley 1968 +*+*
+*The FORTRAN programmer was Alan Miller. The documentation +*in the FORTRAN code claims that the function is "accurate +*to 1.e-15."
+*Steve Verrill +*translated the FORTRAN code (the March 30, 1986 version) +*into Java. This translation was performed on January 10, 2001. +* +*@param z The method returns the value of the normal +* cumulative distribution function at z. +* +*@version .5 --- January 10, 2001 +* +*/ + + +/* + +Here is a copy of the documentation in the FORTRAN code: + + SUBROUTINE NORMP(Z, P, Q, PDF) +C +C Normal distribution probabilities accurate to 1.e-15. +C Z = no. of standard deviations from the mean. +C P, Q = probabilities to the left & right of Z. P + Q = 1. +C PDF = the probability density. +C +C Based upon algorithm 5666 for the error function, from: +C Hart, J.F. et al, 'Computer Approximations', Wiley 1968 +C +C Programmer: Alan Miller +C +C Latest revision - 30 March 1986 +C + +*/ + + public static double normp(double z) { + + double zabs; + double p; + double expntl,pdf; + + final double p0 = 220.2068679123761; + final double p1 = 221.2135961699311; + final double p2 = 112.0792914978709; + final double p3 = 33.91286607838300; + final double p4 = 6.373962203531650; + final double p5 = .7003830644436881; + final double p6 = .3526249659989109E-01; + + final double q0 = 440.4137358247522; + final double q1 = 793.8265125199484; + final double q2 = 637.3336333788311; + final double q3 = 296.5642487796737; + final double q4 = 86.78073220294608; + final double q5 = 16.06417757920695; + final double q6 = 1.755667163182642; + final double q7 = .8838834764831844E-1; + + final double cutoff = 7.071; + final double root2pi = 2.506628274631001; + + zabs = Math.abs(z); + +// |z| > 37 + + if (z > 37.0) { + + p = 1.0; + + return p; + + } + + if (z < -37.0) { + + p = 0.0; + + return p; + + } + +// |z| <= 37. + + expntl = Math.exp(-.5*zabs*zabs); + + pdf = expntl/root2pi; + +// |z| < cutoff = 10/sqrt(2). + + if (zabs < cutoff) { + + p = expntl*((((((p6*zabs + p5)*zabs + p4)*zabs + p3)*zabs + + p2)*zabs + p1)*zabs + p0)/(((((((q7*zabs + q6)*zabs + + q5)*zabs + q4)*zabs + q3)*zabs + q2)*zabs + q1)*zabs + + q0); + + } else { + + p = pdf/(zabs + 1.0/(zabs + 2.0/(zabs + 3.0/(zabs + 4.0/ + (zabs + 0.65))))); + + } + + if (z < 0.0) { + + return p; + + } else { + + p = 1.0 - p; + + return p; + + } + + } + +} \ No newline at end of file diff --git a/tools/cooja/apps/arm/java/statistics/Gaussian.java b/tools/cooja/apps/arm/java/statistics/Gaussian.java new file mode 100644 index 000000000..4a302fb2b --- /dev/null +++ b/tools/cooja/apps/arm/java/statistics/Gaussian.java @@ -0,0 +1,63 @@ +package statistics; + +// Gaussian CDF Taylor approximation +// Code borrowed from http://www.cs.princeton.edu/introcs/21function/Gaussian.java.html 19/9 2006 + +/************************************************************************* +* Compilation: javac Gaussian.java +* Execution: java Gaussian x mu sigma +* +* Function to compute the Gaussian pdf (probability density function) +* and the Gaussian cdf (cumulative density function) +* +* % java Gaussian 820 1019 209 +* 0.17050966869132111 +* +* % java Gaussian 1500 1019 209 +* 0.9893164837383883 +* +* % java Gaussian 1500 1025 231 +* 0.9801220907365489 +* +*************************************************************************/ + +public class Gaussian { + + // return phi(x) = standard Gaussian pdf + public static double phi(double x) { + return Math.exp(-x*x / 2) / Math.sqrt(2 * Math.PI); + } + + // return phi(x) = Gaussian pdf with mean mu and stddev sigma + public static double phi(double x, double mu, double sigma) { + return phi((x - mu) / sigma) / sigma; + } + + // return Phi(z) = standard Gaussian cdf using Taylor approximation + public static double Phi(double z) { + if (z < -8.0) return 0.0; + if (z > 8.0) return 1.0; + double sum = 0.0, term = z; + for (int i = 3; sum + term != sum; i += 2) { + sum = sum + term; + term = term * z * z / i; + } + return 0.5 + sum * phi(z); + } + + + + // return Phi(z, mu, sigma) = Gaussian cdf with mean mu and stddev sigma + public static double Phi(double z, double mu, double sigma) { + return Phi((z - mu) / sigma); + } + + + public static void main(String[] args) { + double z = Double.parseDouble(args[0]); + double mu = Double.parseDouble(args[1]); + double sigma = Double.parseDouble(args[2]); + System.out.println(Phi(z, mu, sigma)); + } + +} diff --git a/tools/cooja/apps/arm/java/statistics/GaussianWrapper.java b/tools/cooja/apps/arm/java/statistics/GaussianWrapper.java new file mode 100644 index 000000000..5d196fa28 --- /dev/null +++ b/tools/cooja/apps/arm/java/statistics/GaussianWrapper.java @@ -0,0 +1,50 @@ +package statistics; + +public class GaussianWrapper { + + /** + * Returns standard Gaussian cdf approximation based on algortihm for error function. + * + * @param value Value + * @return Probability + */ + public static double cdfErrorAlgo(double value) { + return CDF_Normal.normp(value); + } + + /** + * Returns Gaussian cdf approximation based on algorithm for error function. + * + * @param value Value + * @param mean Mean value + * @param stdDev Standard deviance + * @return Probability + */ + public static double cdfErrorAlgo(double value, double mean, double stdDev) { + return CDF_Normal.normp((value - mean) / stdDev); + } + + /** + * Returns standard Gaussian cdf using Taylor approximation. + * + * @param value Value + * @return Probability + */ + public static double cdfTaylor(double value) { + return Gaussian.Phi(value); + } + + /** + * Returns Gaussian cdf using Taylor approximation . + * + * @param value Value + * @param mean Mean value + * @param stdDev Standard deviance + * @return Probability + */ + public static double cdfTaylor(double value, double mean, double stdDev) { + return Gaussian.Phi(value, mean, stdDev); + } + +} + diff --git a/tools/cooja/apps/arm/lib/arm.jar b/tools/cooja/apps/arm/lib/arm.jar new file mode 100644 index 000000000..a8d90ad19 Binary files /dev/null and b/tools/cooja/apps/arm/lib/arm.jar differ diff --git a/tools/cooja/apps/arm/lib/jcommon-1.0.0.jar b/tools/cooja/apps/arm/lib/jcommon-1.0.0.jar new file mode 100644 index 000000000..c5d23f4ae Binary files /dev/null and b/tools/cooja/apps/arm/lib/jcommon-1.0.0.jar differ diff --git a/tools/cooja/apps/arm/lib/jfreechart-1.0.1.jar b/tools/cooja/apps/arm/lib/jfreechart-1.0.1.jar new file mode 100644 index 000000000..6a015249b Binary files /dev/null and b/tools/cooja/apps/arm/lib/jfreechart-1.0.1.jar differ diff --git a/tools/cooja/config/external_tools_linux.config b/tools/cooja/config/external_tools_linux.config index d0a65c076..2d8fc189f 100644 --- a/tools/cooja/config/external_tools_linux.config +++ b/tools/cooja/config/external_tools_linux.config @@ -20,3 +20,4 @@ COMPILER_ARGS = CONTIKI_STANDARD_PROCESSES = sensors_process;etimer_process;tcpip_process;uip_fw_process;cfs_cooja_process CONTIKI_MAIN_TEMPLATE_FILENAME = code_main_template MANTIS_MAIN_TEMPLATE_FILENAME = mantis_template.c +DEFAULT_USERPLATFORMS = ../apps/arm diff --git a/tools/cooja/config/external_tools_win32.config b/tools/cooja/config/external_tools_win32.config index 33a0ab045..b7fb0796f 100644 --- a/tools/cooja/config/external_tools_win32.config +++ b/tools/cooja/config/external_tools_win32.config @@ -20,3 +20,4 @@ COMPILER_ARGS = -mno-cygwin -I'C:/Program Files/Java/jdk1.5.0_06/include' -I'C:/ CONTIKI_STANDARD_PROCESSES = sensors_process;etimer_process;tcpip_process;uip_fw_process;cfs_cooja_process CONTIKI_MAIN_TEMPLATE_FILENAME = code_main_template MANTIS_MAIN_TEMPLATE_FILENAME = mantis_template.c +DEFAULT_USERPLATFORMS = ../apps/arm