예제 #1
0
        /// <summary>
        /// Kobatake, Wang, Tanaka JN 1998: The statistical significance of the responses was determined by comparing the mean firing rates within the window for 10 individual responses, with 10 spontaneous firing rates immediately preceding each stimulus presentation, using the Kolmogorov-Smirnov (K-S) test.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="Alpha">The confidence level: 0.05 or 0.001</param>
        /// <param name="Tail">Two taild?</param>
        /// <returns></returns>
        public static double KStest2(double[] x, double[] y, double Alpha, StatisticalTestTail Tail)
        {
            double pValue = 0;
            ulong j1=1,j2=1;
            double d1,d2,dt,en1,en2,en,fn1=0.0,fn2=0.0;
            Array.Sort(x);
            Array.Sort(y);
            en1=x.GetLength(0);
            en2=y.GetLength(0);
            double d=0.0d;
            while (j1 < en1 && j2 < en2) //If we are not done...
            {
                if ((d1=x[j1]) <= (d2=y[j2]))
                    fn1=j1++/en1; //Next step is in data1.
                if (d2 <= d1)
                    fn2=j2++/en2; //Next step is in data2.
                if ((dt=Math.Abs(fn2-fn1)) > d)
                    d=dt;
            }
            en=Math.Sqrt(en1*en2/(en1+en2));
            pValue = probks((en+0.12+0.11/en)*(d)); //Compute significance.

            return pValue;
        }
예제 #2
0
        public static double tTestPaired(double[] p1, double[] p2, Single Alpha, StatisticalTestTail Tail)
        {
            double pValue=0;
            if ((Alpha>1)||(Alpha<0)) throw new Exception("Wronge Alpha for tTest: " + Alpha.ToString());
            if(p1.Length!=p2.Length) throw new Exception("In a paired tTest two samples must have equal length!");
            for (int i = 0; i < p1.GetLength(0); i++)
            {
                p1[i] = p1[i] - p2[i];
            }
            int m = 0;
            int samplesize  = p1.Length;
            int df = samplesize - 1;
            double xmean = Mean(p1);
            double sdpop = Math.Sqrt(Variance(p1));
            //Ali added this to avoid division by zero
            if (sdpop == 0) sdpop = Double.MinValue;

            double ser = sdpop / Math.Sqrt(samplesize);
            double tval = (xmean - m) / ser;
            switch (Tail)
            {
                case StatisticalTestTail.Both:
                    pValue = 2 * TCDF(-Math.Abs(tval), df);
                    //if nargout > 2
                    //    crit = tinv((1 - alpha / 2), samplesize - 1) .* ser;
                    //    ci = [(xmean - crit) (xmean + crit)];
                    //end
                    break;
                case StatisticalTestTail.Right:
                    pValue = TCDF(-tval, df);
                    //if nargout > 2
                    //    crit = tinv(1 - alpha, samplesize - 1) .* ser;
                    //    ci = [(xmean - crit), Inf];
                    //end
                    break;
                case StatisticalTestTail.Left:
                    pValue = TCDF(tval, df);
                    //if nargout > 2
                    //    crit = tinv(1 - alpha, samplesize - 1) .* ser;
                    //    ci = [-Inf, (xmean + crit)];
                    //end
                    break;
            }

            //if (((xmean-m) == 0) && (ser == 0)) { pValue = 10; return pValue; } // ALI: if there is no difference between means of two samples or sd is zero these are not different or at least test is impossible (2006-02-09 ASHOORA!)

            //// Determine if the actual significance exceeds the desired significance
            //if p <= alpha
            //    h = 1;
            //elseif p > alpha
            //    h = 0;
            //else // isnan(p) must be true
            //    h = NaN;
            //end
            return pValue;
        }
예제 #3
0
        public static double tTest2Samples(int[] p1, int[] p2, Single Alpha, StatisticalTestTail Tail, bool EqualVarianceAssumed)
        {
            double pValue = 0;
            if ((Alpha>1)||(Alpha<0)) throw new Exception("Wronge Alpha for tTest: " + Alpha.ToString());

            double DegreeofFreedom = 0;
            double sPooled = 0;
            double p1Variance = 0;
            double p2Variance = 0;
            double se = 0;
            double ratio = 0;
            double difference = 0;
            difference = Mean(p1) - Mean(p2);
            p1Variance = Variance(p1);
            p2Variance = Variance(p2);

            // Ali: I added this to prevent devide by zero error in zero vaiances
            if (p1Variance == 0) p1Variance = 0.000001f;
            if (p2Variance == 0) p2Variance = 0.000001f;
            // Ali

            if (EqualVarianceAssumed)
            {
                DegreeofFreedom = p1.Length + p2.Length - 2;
                sPooled = Math.Sqrt(((p1.Length-1) * p1Variance + (p2.Length-1) * p2Variance) / DegreeofFreedom);
                se = sPooled * Math.Sqrt(1.0f/p1.Length + 1.0f/p2.Length);
                ratio = difference / se;

            }
            else // Not EqualVarianceAssumed
            {
                double P1VBar = p1Variance / p1.Length;
                double P2VBar = p2Variance / p2.Length;
                DegreeofFreedom = Math.Pow((P1VBar + P2VBar),2) / (Math.Pow(P1VBar,2) / (p1.Length-1) + Math.Pow(P2VBar,2) / (p2.Length-1));
                se = Math.Sqrt(P1VBar + P2VBar);
                ratio = difference / se;
            }

            // Compute the correct p-value for the test, and confidence intervals if requested.
            switch(Tail)
            {
                case StatisticalTestTail.Both :
                    pValue = 2 * TCDF(-Math.Abs(ratio),DegreeofFreedom);
                    //spread = tinv(1 - alpha ./ 2, DegreeofFreedom) .* se;
                    //ci = [(difference - spread) (difference + spread)];
                    break;
                case StatisticalTestTail.Left :
                    pValue = TCDF(ratio,DegreeofFreedom);
                    //    if nargout > 2
                    //        spread = tinv(1 - alpha, dfe) .* se;
                    //        ci = [-Inf, (difference + spread)];
                    break;
                case StatisticalTestTail.Right:
                    pValue = TCDF(-ratio,DegreeofFreedom);
                    //    if nargout > 2
                    //        spread = tinv(1 - alpha, dfe) .* se;
                    //        ci = [(difference - spread), Inf];
                    break;
            }

            // Determine if the actual significance exceeds the desired significance
            //if (p <= alpha)
            //    h = 1;
            //else p > alpha
            //    h = 0;
            //else // isnan(p) must be true
            //    h = NaN;
            //end

            return pValue;
        }
예제 #4
0
 public static double tTestPaired(int[] p1, int[] p2, Single Alpha, StatisticalTestTail Tail)
 {
     double[] p11 = new double[p1.GetLength(0)];
     double[] p22 = new double[p2.GetLength(0)];
     for (int i = 0; i < p1.GetLength(0); i++)
     {
         p11[i] = (double)p1[i];
         p22[i] = (double)p2[i];
     }
     return tTestPaired(p11, p22, Alpha, Tail);
 }
예제 #5
0
        public static double tTest1Sample(double[] p, double m, Single Alpha, StatisticalTestTail Tail)
        {
            if ((Alpha > 1) || (Alpha < 0)) throw new Exception("Wronge Alpha for tTest: " + Alpha.ToString());
            double pValue = 0;

            double xmean = Mean(p);
            double STDp = Math.Sqrt(Variance(p));
            double ser = STDp / Math.Sqrt(p.Length);
            double tval = (xmean - m) / ser;

            // Compute the correct p-value for the test, and confidence intervals if requested.
            switch(Tail)
            {
                case StatisticalTestTail.Both:
                pValue = 2 * TCDF(-Math.Abs(tval), p.Length - 1);
                //if nargout > 2
                //    crit = tinv((1 - alpha / 2), samplesize - 1) .* ser;
                //    ci = [(xmean - crit) (xmean + crit)];
                    break;
                case StatisticalTestTail.Left:
                    pValue = TCDF(-tval, p.Length - 1);
                //if nargout > 2
                //    crit = tinv(1 - alpha, samplesize - 1) .* ser;
                //    ci = [(xmean - crit), Inf];
                    break;
                case StatisticalTestTail.Right:
                    pValue = TCDF(tval, p.Length - 1);
                //if nargout > 2
                //    crit = tinv(1 - alpha, samplesize - 1) .* ser;
                //    ci = [-Inf, (xmean + crit)];
                    break;
            }
            //// Determine if the actual significance exceeds the desired significance
            //if p <= alpha
            //    h = 1;
            //elseif p > alpha
            //    h = 0;
            //else // isnan(p) must be true
            //    h = NaN;
            //end

            return pValue;
        }