Example #1
0
        public static void FitOrigin(double[] x, double[] y, double[] sig,
                                     out double b, out double sigb, out double chi2, out double q)
        {
            int ndata = x.Length;

            if (x.Length != y.Length)
            {
                throw new Exception("x.Length != y.Length: " + x.Length + " " + y.Length);
            }
            if (sig != null && x.Length != sig.Length)
            {
                throw new Exception("x.Length != sig.Length: " + x.Length + " " + sig.Length);
            }
            double t;
            double st2 = 0.0;

            b = 0.0;
            if (sig != null)
            {
                for (int i = 0; i < ndata; i++)
                {
                    t    = (x[i]) / sig[i];
                    st2 += t * t;
                    b   += t * y[i] / sig[i];
                }
            }
            else
            {
                for (int i = 0; i < ndata; i++)
                {
                    t    = x[i];
                    st2 += t * t;
                    b   += t * y[i];
                }
            }
            b   /= st2;
            sigb = Math.Sqrt(1.0 / st2);
            chi2 = 0.0;
            if (sig == null)
            {
                for (int i = 0; i < ndata; i++)
                {
                    double tmp = y[i] - b * x[i];
                    chi2 += tmp * tmp;
                }
                q = 1.0;
                double sigdat = Math.Sqrt((chi2) / (ndata - 2));
                sigb *= sigdat;
            }
            else
            {
                for (int i = 0; i < ndata; i++)
                {
                    double tmp = (y[i] - b * x[i]) / sig[i];
                    chi2 += tmp * tmp;
                }
                q = NumericalRecipes.Gammq(0.5 * (ndata - 1), 0.5 * (chi2));
            }
        }
Example #2
0
 public static double Errfunc(double z)
 {
     try {
         return((NumericalRecipes.Erffc(z / Math.Sqrt(2.0))) / 2.0);
     } catch (Exception) {
         return(1);
     }
 }
Example #3
0
 public static double[] DiagonalizeSymmMatrix(double[,] m, out double[,] evec)
 {
     double[] d;
     double[] e;
     evec = (double[, ])m.Clone();
     NumericalRecipes.Tred2(evec, out d, out e);
     NumericalRecipes.Tqli(d, e, evec);
     return(d);
 }
Example #4
0
        public static double LnMultinomial(int a, int[] bs)
        {
            double result = NumericalRecipes.Gammln(a + 1);

            for (int i = 0; i < bs.Length; i++)
            {
                result -= NumericalRecipes.Gammln(bs[i] + 1);
            }
            return(result);
        }
Example #5
0
 public static void LinFit2(double[] x, double[] y, double[] sig, double[] a,
                            out double chisq, LfitFunc funcs)
 {
     double[,] covar;
     if (sig == null)
     {
         sig = new double[x.Length];
         for (int i = 0; i < sig.Length; i++)
         {
             sig[i] = 1E-2;
         }
     }
     NumericalRecipes.Lfit(x, y, sig, a, out covar, out chisq, funcs);
 }
Example #6
0
        public static void FitNonlin(double[] x, double[] y, double[] sig, double[] a,
                                     out double chisq, MrqminFunc func)
        {
            int ndata = x.Length;

            if (sig == null)
            {
                sig = new double[ndata];
                for (int i = 0; i < sig.Length; i++)
                {
                    sig[i] = 1;
                }
            }
            int ma = a.Length;

            double[,] covar = new double[ma, ma];
            double[,] alpha = new double[ma, ma];
            int[] ia = new int[ma];
            for (int i = 0; i < ma; i++)
            {
                ia[i] = 1;
            }
            double alamda = -1;
            double ochisq = 0;

            double[,] oneda = null;
            int mfit = 0;

            double[] atry = null;
            double[] beta = null;
            double[] da   = null;
            NumericalRecipes.Mrqmin(x, y, sig, ndata, a, ia, ma, covar, alpha, out chisq, func, ref alamda, ref ochisq,
                                    ref oneda, ref mfit, ref atry, ref beta, ref da);
            int count1 = 0;

            while (alamda > 1e-20 && alamda < 1e20 && count1 < 100)
            {
                NumericalRecipes.Mrqmin(x, y, sig, ndata, a, ia, ma, covar, alpha, out chisq, func, ref alamda, ref ochisq,
                                        ref oneda, ref mfit, ref atry, ref beta, ref da);
                count1++;
            }
            alamda = 0;
            NumericalRecipes.Mrqmin(x, y, sig, ndata, a, ia, ma, covar, alpha, out chisq, func, ref alamda, ref ochisq,
                                    ref oneda, ref mfit, ref atry, ref beta, ref da);
        }