001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.survival.cox.stats;
022
023/**
024 *
025 * @author Scooter Willis <willishf at gmail dot com>
026 */
027public class ChiSq {
028
029        /**
030         *
031         * @param z
032         * @return
033         */
034        public static double norm(double z) {
035                return ChiSq.chiSq(z * z, 1);
036        }
037
038        /**
039         *
040         * @param x
041         * @param n
042         * @return
043         */
044        public static double chiSq(double x, int n) {
045                double p = Math.exp(-0.5 * x);
046                if ((n % 2) == 1) {
047                        p = p * Math.sqrt(2 * x / Math.PI);
048                }
049                double k = n;
050                while (k >= 2) {
051                        p = p * x / k;
052                        k = k - 2;
053                }
054                double t = p;
055                double a = n;
056                while (t > 0.000001 * p) {
057                        a = a + 2;
058                        t = t * x / a;
059                        p = p + t;
060                }
061                return 1 - p;
062        }
063
064
065
066        /**
067         * @param args the command line arguments
068         */
069        public static void main(String[] args) {
070                // TODO code application logic here
071        }
072}