Esempio n. 1
0
        public static OIiList OIi_Calc(double th, CATItems it, int[] x, string model, double D)
        {
            OIiList objOIi = null;

            double[] OIi = null;
            try
            {
                if (String.IsNullOrEmpty(model))
                {
                    #region "Calculation for Dichotmous Items"

                    PiList pr = Pi.Pi_Calc(th, it, model, D);

                    if (pr == null)
                    {
                        objOIi           = new OIiList();
                        objOIi.Exception = "No Pi values found!";
                        return(objOIi);
                    }

                    double[] P = pr.Pi; double[] dP = pr.dPi; double[] d2P = pr.d2Pi;

                    OIi = new double[P.Length];

                    objOIi = new OIiList(P.Length);

                    double[] Q = new double[P.Length];

                    for (int i = 0; i < P.Length; i++)
                    {
                        Q[i] = 1 - P[i];
                    }

                    for (int j = 0; j < OIi.Length; j++)
                    {
                        OIi[j] = (P[j] * Q[j] * Math.Pow(dP[j], 2) - (x[0] - P[j]) * (P[j] * Q[j] * d2P[j] + Math.Pow(dP[j], 2) * (P[j] - Q[j]))) / (Math.Pow(P[j], 2) * Math.Pow(Q[j], 2));
                    }

                    #endregion
                }
                else
                {
                    #region "Calculation for Polytomous Items"

                    PiListPoly pr = Pi.Pi_Poly_Calc(th, it, model, D);

                    if (pr == null)
                    {
                        objOIi           = new OIiList();
                        objOIi.Exception = "No Pi values found!";
                        return(objOIi);
                    }

                    double[,] P = pr.Pi; double[,] dP = pr.dPi; double[,] d2P = pr.d2Pi;

                    OIi = new double[it.NumOfItems];

                    objOIi = new OIiList(it.NumOfItems);

                    for (int i = 0; i < x.Length; i++)
                    {
                        double tempdP  = dP[i, x[i]];
                        double tempP   = P[i, x[i]];
                        double tempd2P = d2P[i, x[i]];

                        OIi[i] = (Math.Pow(dP[i, x[i]], 2) / Math.Pow(P[i, x[i]], 2)) - (d2P[i, x[i]] / P[i, x[i]]);
                    }

                    #endregion
                }

                objOIi.Add(OIi);
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    if (objOIi == null)
                    {
                        objOIi = new OIiList();
                    }
                    objOIi.Exception = "No Pi value found!";
                }
                return(objOIi);
            }

            return(objOIi);
        }
Esempio n. 2
0
        public static double ThetaEst_Calc(CATItems it, int[] x, string model = null, double[] priorPar = null, double[] range = null, int[] parInt = null, double D = 1, string method = "BM", string priorDist = "norm")
        {
            double result = 0; double[] RANGE = null;

            #region "Parameter Validation"

            if (priorPar == null || priorPar.Length < 2)
            {
                priorPar    = new double[2];
                priorPar[0] = 0;
                priorPar[1] = 1;
            }

            if (range == null || range.Length < 2)
            {
                range    = new double[2];
                range[0] = -4;
                range[1] = 4;
            }

            if (parInt == null || parInt.Length < 3)
            {
                parInt    = new int[3];
                parInt[0] = -4;
                parInt[1] = 4;
                parInt[2] = 33;
            }

            #endregion

            if (method == ModelNames.EstimaatorMethods.EAP.EnumToString())
            {
                result = EapEST.EapEST_Calc(it, x, model, priorPar, D, priorDist, parInt[0], parInt[1], parInt[2]);
            }
            else
            {
                if (String.IsNullOrEmpty(model))   // Dichotomous Items
                {
                    #region "Function 'r0' "
                    Func <double, string, double> r0 = (th, met) =>
                    {
                        double res = 0;

                        if (met == ModelNames.EstimaatorMethods.BM.EnumToString())
                        {
                            switch (priorDist)
                            {
                            case "norm":
                                res = (priorPar[0] - th) / Math.Pow(priorPar[1], 2);
                                break;

                            case "unif":
                                res = 0;
                                break;

                            case "Jeffreys":
                                IiList objIi = Ii.Ii_Calc(th, it, model, D);
                                res = CatRcs.Utils.RowColumn.Sum(objIi.dIi) / (2 * CatRcs.Utils.RowColumn.Sum(objIi.Ii));
                                break;
                            }
                        }
                        else
                        {
                            ModelNames.EstimaatorMethods md = ModelNames.StringToEnumMethods(met);

                            switch (md)
                            {
                            case ModelNames.EstimaatorMethods.ML:
                                res = 0;
                                break;

                            case ModelNames.EstimaatorMethods.WL:
                                res = CatRcs.Utils.RowColumn.Sum(Ji.Ji_Calc(th, it, model, D).Ji) / (2 * CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(th, it, model, D).Ii));
                                break;
                            }
                        }

                        return(res);
                    };
                    #endregion

                    #region "Function 'r' "
                    Func <double, double> r = (th) =>
                    {
                        double   res = 0;
                        double[] Q   = new double[it.NumOfItems], x_p = new double[it.NumOfItems], p_q = new double[it.NumOfItems], temp = new double[it.NumOfItems];

                        double[] pi  = Pi.Pi_Calc(th, it, model, D).Pi;
                        double[] dpi = Pi.Pi_Calc(th, it, model, D).dPi;

                        for (int ind_p = 0; ind_p < pi.Length; ind_p++)
                        {
                            Q[ind_p]    = 1 - pi[ind_p];
                            x_p[ind_p]  = x[ind_p] - pi[ind_p];
                            p_q[ind_p]  = pi[ind_p] * Q[ind_p];
                            temp[ind_p] = dpi[ind_p] * (x_p[ind_p] / p_q[ind_p]);
                        }

                        res = temp.Sum();

                        return(res);
                    };
                    #endregion

                    #region "Function 'f' "
                    Func <double, double> f = (th) =>
                    {
                        double res = 0;
                        return(res);
                    };


                    if (method == ModelNames.EstimaatorMethods.BM.EnumToString() && priorDist == "unif")
                    {
                        f = (th) =>
                        {
                            double res = 0;

                            string methodName = ModelNames.EstimaatorMethods.ML.EnumToString();

                            res = r0(th, methodName) + r(th);

                            return(res);
                        };
                    }
                    else
                    {
                        f = (th) =>
                        {
                            double res = 0;

                            res = r0(th, method) + r(th);

                            return(res);
                        };
                    }

                    #endregion

                    if (method == ModelNames.EstimaatorMethods.BM.EnumToString() && priorDist == "unif")
                    {
                        RANGE = priorPar;
                    }
                    else
                    {
                        RANGE = range;
                    }

                    if (f(RANGE[0]) < 0 && f(RANGE[1]) < 0)
                    {
                        result = RANGE[0];
                    }
                    else
                    {
                        if (f(RANGE[0]) > 0 && f(RANGE[1]) > 0)
                        {
                            result = RANGE[1];
                        }
                        else
                        {
                            result = CatRcs.Utils.Maths.UniRoot(f, RANGE).root;
                        }
                    }
                }
                else   // Polytomous Items
                {
                    int met = 0, pd = 0;
                    ModelNames.EstimaatorMethods md = ModelNames.StringToEnumMethods(method);

                    #region "Conditional Block"

                    switch (md)
                    {
                    case ModelNames.EstimaatorMethods.ML:
                        met = 1;
                        break;

                    case ModelNames.EstimaatorMethods.BM:
                        met = 2;
                        break;

                    case ModelNames.EstimaatorMethods.WL:
                        met = 3;
                        break;

                    case ModelNames.EstimaatorMethods.EAP:
                        met = 4;
                        break;
                    }

                    switch (priorDist)
                    {
                    case "norm":
                        pd = 1;
                        break;

                    case "unif":
                        pd = 2;
                        break;

                    case "Jeffreys":
                        pd = 3;
                        break;
                    }

                    if (met == 2 && pd == 2)
                    {
                        RANGE    = new double[priorPar.Length];
                        RANGE[0] = Math.Max(priorPar[0], range[0]);
                        RANGE[1] = Math.Min(priorPar[1], range[1]);
                    }
                    else
                    {
                        RANGE = range;
                    }

                    #endregion

                    #region "Function 'dl' "
                    Func <double, double> dl = (th) =>
                    {
                        double res = 0;

                        PiListPoly p = Pi.Pi_Poly_Calc(th, it, model, D);
                        double[,] pr  = p.Pi;
                        double[,] dpr = p.dPi;

                        for (int i = 0; i < x.Length; i++)
                        {
                            res = res + dpr[i, x[i]] / pr[i, x[i]];
                        }

                        return(res);
                    };
                    #endregion

                    #region "Function 'f0' "
                    Func <double, double> f0 = (th) =>
                    {
                        double res = 0;

                        if (met == 2)
                        {
                            switch (pd.ToString())
                            {
                            case "1":
                                res = (priorPar[0] - th) / Math.Pow(priorPar[1], 2);
                                break;

                            case "2":
                                res = 0;
                                break;

                            case "3":
                                IiList objIi = CatRcs.Ii.Ii_Calc(th, it, model, D);
                                res = CatRcs.Utils.RowColumn.Sum(objIi.dIi) / (2 * CatRcs.Utils.RowColumn.Sum(objIi.Ii));
                                break;
                            }
                        }
                        else
                        {
                            switch (met.ToString())
                            {
                            case "1":
                                res = 0;
                                break;

                            case "2":
                                res = 0;
                                break;

                            case "3":
                                res = CatRcs.Utils.RowColumn.Sum(CatRcs.Ji.Ji_Calc(th, it, model, D).Ji) / (2 * CatRcs.Utils.RowColumn.Sum(CatRcs.Ii.Ii_Calc(th, it, model, D).Ii));
                                break;
                            }
                        }

                        return(res);
                    };
                    #endregion

                    #region "Function 'optF' "
                    Func <double, double> optF = (th) =>
                    {
                        double res = 0;
                        res = dl(th) + f0(th);
                        return(res);
                    };
                    #endregion

                    if (optF(RANGE[0]) < 0 && optF(RANGE[1]) < 0)
                    {
                        result = RANGE[0];
                    }
                    else
                    {
                        if (optF(RANGE[0]) > 0 && optF(RANGE[1]) > 0)
                        {
                            result = RANGE[1];
                        }
                        else
                        {
                            result = CatRcs.Utils.Maths.UniRoot(optF, RANGE).root;
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        public static IiList Ii_Calc(double th, CATItems it, string model, double D)
        {
            IiList objIi = null;

            double[] Ii = null; double[] dIi = null; double[] d2Ii = null;

            try
            {
                if (String.IsNullOrEmpty(model))
                {
                    #region "Calculation for Dichotmous Items"

                    double[] P = null; double[] dP = null; double[] d2P = null; double[] d3P = null;

                    PiList pr = Pi.Pi_Calc(th, it, model, D); // Check return value of Pi

                    if (pr == null)
                    {
                        objIi           = new IiList();
                        objIi.Exception = "No Pi values found!";
                        return(objIi);
                    }

                    P   = pr.Pi;
                    dP  = pr.dPi;
                    d2P = pr.d2Pi;
                    d3P = pr.d3Pi;

                    Ii   = new double[dP.Length];
                    dIi  = new double[d2P.Length];
                    d2Ii = new double[d3P.Length];

                    objIi = new IiList(P.Length);

                    double[] Q = new double[P.Length];

                    for (int i = 0; i < P.Length; i++)
                    {
                        Q[i] = 1 - P[i];
                    }

                    for (int j = 0; j < Ii.Length; j++)
                    {
                        Ii[j] = Math.Pow(dP[j], 2) / (P[j] * Q[j]);
                    }

                    for (int k = 0; k < dIi.Length; k++)
                    {
                        dIi[k] = dP[k] * (2 * P[k] * Q[k] * d2P[k] - Math.Pow(dP[k], 2) * (Q[k] - P[k])) / (Math.Pow(P[k], 2) * Math.Pow(Q[k], 2));
                    }

                    for (int l = 0; l < d2Ii.Length; l++)
                    {
                        d2Ii[l] = (2 * P[l] * Q[l] * (Math.Pow(d2P[l], 2) + dP[l] * d3P[l]) - 2 * Math.Pow(dP[l], 2) * d2P[l] * (Q[l] - P[l])) / (Math.Pow(P[l], 2) * Math.Pow(Q[l], 2)) -
                                  (3 * Math.Pow(P[l], 2) * Q[l] * Math.Pow(dP[l], 2) * d2P[l] - P[l] * Math.Pow(dP[l], 4) * (2 * Q[l] - P[l])) / (Math.Pow(P[l], 4) * Math.Pow(Q[l], 2)) +
                                  (3 * P[l] * Math.Pow(Q[l], 2) * Math.Pow(dP[l], 2) * d2P[l] - Q[l] * Math.Pow(dP[l], 4) * (Q[l] - 2 * P[l])) / (Math.Pow(P[l], 2) * Math.Pow(Q[l], 4));
                    }

                    #endregion
                }
                else
                {
                    #region "Calculation for Polytomous Items"

                    double[,] P = null; double[,] dP = null; double[,] d2P = null; double[,] d3P = null;

                    PiListPoly pr = Pi.Pi_Poly_Calc(th, it, model, D);

                    if (pr == null)
                    {
                        objIi           = new IiList();
                        objIi.Exception = "No Pi values found!";
                        return(objIi);
                    }

                    P   = pr.Pi;
                    dP  = pr.dPi;
                    d2P = pr.d2Pi;
                    d3P = pr.d3Pi;

                    int rowLength    = pr.Pi.GetLength(0);
                    int columnLength = pr.Pi.GetLength(1);

                    objIi = new IiList(rowLength);

                    double[,] pr0 = new double[rowLength, columnLength];
                    double[,] pr1 = new double[rowLength, columnLength];
                    double[,] pr2 = new double[rowLength, columnLength];

                    Ii   = new double[rowLength];
                    dIi  = new double[rowLength];
                    d2Ii = new double[rowLength];

                    for (int i = 0; i < rowLength; i++)
                    {
                        for (int j = 0; j < columnLength; j++)
                        {
                            pr0[i, j] = Math.Pow(dP[i, j], 2) / P[i, j];

                            pr1[i, j] = 2 * dP[i, j] * (d2P[i, j] / P[i, j]) - (Math.Pow(dP[i, j], 3) / Math.Pow(P[i, j], 2));

                            pr2[i, j] = (2 * Math.Pow(d2P[i, j], 2) + 2 * dP[i, j] * d3P[i, j]) / P[i, j] - 2 * Math.Pow(dP[i, j], 2) * (d2P[i, j] / -3) * dP[i, j] * (d2P[i, j] / Math.Pow(P[i, j], 2)) + 2 * (Math.Pow(dP[i, j], 4) / Math.Pow(P[i, j], 3));
                        }
                    }

                    Ii   = RowColumn.rowSums(pr0);
                    dIi  = RowColumn.rowSums(pr1);
                    d2Ii = RowColumn.rowSums(pr2);

                    #endregion
                }

                objIi.Add(Ii, dIi, d2Ii);
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    if (objIi == null)
                    {
                        objIi = new IiList();
                    }
                    objIi.Exception = ex.Message;
                }
                return(objIi);
            }

            return(objIi);
        }
Esempio n. 4
0
        public void testPi_P_New(ModelNames.Models paramModel)
        {
            REngine.SetEnvironmentVariables();

            REngine engineObj = REngine.GetInstance();

            // Loading a library from R
            engineObj.Evaluate("library(catR)");

            // Polytomous Items
            CharacterVector modelName = engineObj.CreateCharacterVector(new string[] { paramModel.EnumToString() });

            engineObj.SetSymbol("modelName", modelName);
            DataFrame PolyItems = engineObj.Evaluate("PolyItems <- genPolyMatrix(100, 5, model = modelName, same.nrCat = FALSE)").AsDataFrame();

            engineObj.SetSymbol("PolyItems", PolyItems);

            // Adapting with the existing "CAT-Items" type (Wrapper)

            Console.WriteLine("*******************************************");
            Console.WriteLine("Polytomous Items, Model : " + paramModel.EnumToString());
            Console.WriteLine("*******************************************");

            CATItems itemBank = new CATItems(NumOfItems: PolyItems[0].Length, model: paramModel.EnumToString(), nrCat: 5);

            for (int i = 0; i < itemBank.colSize; i++)
            {
                itemBank.all_items_poly[i] = PolyItems[i].Select(y => (double)y).ToArray();
            }

            PiListPoly objPI        = null;
            int        decimalPoint = 4;

            double[]      th_Values = CatRcs.Utils.CommonHelper.Sequence(-4, 4, by: 1);
            NumericVector th_val    = engineObj.CreateNumericVector(new double[] { th_Values[0] });

            engineObj.SetSymbol("th_val", th_val);

            // Calling the "Pi" from R
            GenericVector result_Pi = engineObj.Evaluate("result_Pi <- Pi(th = th_val, PolyItems, model = modelName, D = 1)").AsList();

            // Getting the "Pi" function result
            NumericVector Pi   = result_Pi[0].AsNumeric();
            NumericVector dPi  = result_Pi[1].AsNumeric();
            NumericVector d2Pi = result_Pi[2].AsNumeric();
            NumericVector d3Pi = result_Pi[3].AsNumeric();

            Console.WriteLine("Value of Theta: " + th_Values[0]);

            // Calling "Pi" function
            objPI = CatRLib.Pi_P(th_Values[0], itemBank, paramModel.EnumToString(), 1);

            #region "Pi"

            int z = 0, pi_index = 0;
            resultFlag = true;
            Console.WriteLine("****** Pi ******");

            int len  = objPI.Pi.GetLength(0);
            int len2 = objPI.Pi.GetLength(1);

            for (int i = 0; i < objPI.Pi.GetLength(1); i++)     // column
            {
                z = i + 1;                                      // item number

                for (int j = 0; j < objPI.Pi.GetLength(0); j++) // row
                {
                    double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(Pi[pi_index]) ? Pi[pi_index] : 0;

                    if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.Pi[j, i]), decimalPoint))
                    {
                        Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }

                    pi_index++;
                }
            }

            Assert.IsTrue(resultFlag);
            Console.WriteLine("Values of Pi are Matched!");

            #endregion

            #region "dPi"

            z          = 0; pi_index = 0;
            resultFlag = true;
            Console.WriteLine("****** dPi ******");

            for (int i = 0; i < objPI.dPi.GetLength(1); i++)     // column
            {
                z = i + 1;                                       // item number

                for (int j = 0; j < objPI.dPi.GetLength(0); j++) // row
                {
                    double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(dPi[pi_index]) ? dPi[pi_index] : 0;

                    if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.dPi[j, i]), decimalPoint))
                    {
                        Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }

                    pi_index++;
                }
            }

            Assert.IsTrue(resultFlag);
            Console.WriteLine("Values of dPi are Matched!");

            #endregion

            #region "d2Pi"

            z          = 0; pi_index = 0;
            resultFlag = true;
            Console.WriteLine("****** d2Pi ******");

            for (int i = 0; i < objPI.d2Pi.GetLength(1); i++)     // column
            {
                z = i + 1;                                        // item number

                for (int j = 0; j < objPI.d2Pi.GetLength(0); j++) // row
                {
                    double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(d2Pi[pi_index]) ? d2Pi[pi_index] : 0;

                    if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d2Pi[j, i]), decimalPoint))
                    {
                        Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }

                    pi_index++;
                }
            }

            Assert.IsTrue(resultFlag);
            Console.WriteLine("Values of d2Pi are Matched!");

            #endregion

            #region "d3Pi"

            z          = 0; pi_index = 0;
            resultFlag = true;
            Console.WriteLine("****** d3Pi ******");

            for (int i = 0; i < objPI.d3Pi.GetLength(1); i++)     // column
            {
                z = i + 1;                                        // item number

                for (int j = 0; j < objPI.d3Pi.GetLength(0); j++) // row
                {
                    double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(d3Pi[pi_index]) ? d3Pi[pi_index] : 0;

                    if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d3Pi[j, i]), decimalPoint))
                    {
                        Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }

                    pi_index++;
                }
            }

            Assert.IsTrue(resultFlag);
            Console.WriteLine("Values of d3Pi are Matched!");

            #endregion
        }
Esempio n. 5
0
        // Pi method for Polytomous Items
        public static PiListPoly Pi_Poly_Calc(double th, CATItems it, string model, double D)
        {
            PiListPoly objPi = null;

            double[,] prov = null, prov1 = null, prov2 = null, prov3 = null;

            try
            {
                if (!String.IsNullOrEmpty(model))
                {
                    ModelNames.Models model_Name = ModelNames.StringToEnum(model);

                    if (model_Name == ModelNames.Models.GRM || model_Name == ModelNames.Models.MGRM)
                    {
                        #region "Variable Declarations"

                        if (model_Name == ModelNames.Models.GRM)
                        {
                            prov  = new double[it.NumOfItems, it.colSize];
                            prov1 = new double[it.NumOfItems, it.colSize];
                            prov2 = new double[it.NumOfItems, it.colSize];
                            prov3 = new double[it.NumOfItems, it.colSize];

                            objPi = new PiListPoly(it.NumOfItems, it.colSize);
                        }

                        if (model_Name == ModelNames.Models.MGRM)
                        {
                            prov  = new double[it.NumOfItems, it.colSize - 1];
                            prov1 = new double[it.NumOfItems, it.colSize - 1];
                            prov2 = new double[it.NumOfItems, it.colSize - 1];
                            prov3 = new double[it.NumOfItems, it.colSize - 1];

                            objPi = new PiListPoly(it.NumOfItems, it.colSize - 1);
                        }

                        #endregion

                        #region "Functional Logic"

                        for (int i = 0; i < it.NumOfItems; i++)  // Traversing through items
                        {
                            // "alphaj", 1st column
                            double aj = it.all_items_poly[0][i];

                            // Calculation of "betaj" vector values
                            double[] bj = null;

                            // "GRM"
                            if (model_Name == ModelNames.Models.GRM)
                            {
                                bj = new double[it.colSize - 1];  // Values starting from 2nd column
                                int b_Index = 1;

                                for (int k = 0; k < bj.Length; k++)
                                {
                                    bj[k] = it.all_items_poly[b_Index][i];;

                                    b_Index++;
                                }
                            }
                            else  // "MGRM"
                            {
                                // "betaj", 2nd column
                                double bj0 = it.all_items_poly[1][i];

                                bj = new double[it.colSize - 2];  // Values starting from 3rd column
                                int c_Index = 2;

                                for (int k = 0; k < bj.Length; k++)
                                {
                                    bj[k] = bj0 - it.all_items_poly[c_Index][i];

                                    c_Index++;
                                }
                            }

                            // Final Array must be NaN value free
                            bj = CheckNaValues.GetArrayWithoutNaN(bj);

                            double[] ej    = new double[bj.Length];
                            double[] Pjs   = new double[bj.Length + 2];
                            double[] dPjs  = new double[Pjs.Length];
                            double[] d2Pjs = new double[Pjs.Length];
                            double[] d3Pjs = new double[Pjs.Length];

                            Pjs[0] = 1.000;              // 1st Column
                            Pjs[Pjs.Length - 1] = 0.000; // last column

                            for (int m = 0; m < bj.Length; m++)
                            {
                                // Calculation of "ej"
                                ej[m] = Math.Exp(D * aj * (th - bj[m]));

                                // Calculation of "Pjs"
                                Pjs[m + 1] = ej[m] / (1 + ej[m]);
                            }

                            // Calculation of "dPj", "d2Pj", "d3Pj"
                            for (int j = 0; j < Pjs.Length; j++)
                            {
                                dPjs[j]  = D * aj * Pjs[j] * (1 - Pjs[j]);
                                d2Pjs[j] = D * aj * (dPjs[j] - 2 * Pjs[j] * dPjs[j]);
                                d3Pjs[j] = D * aj * (d2Pjs[j] - 2 * Math.Pow(dPjs[j], 2) - 2 * Pjs[j] * d2Pjs[j]);
                            }

                            for (int index = 0; index < Pjs.Length - 1; index++)
                            {
                                prov[i, index]  = Pjs[index] - Pjs[index + 1];
                                prov1[i, index] = dPjs[index] - dPjs[index + 1];
                                prov2[i, index] = d2Pjs[index] - d2Pjs[index + 1];
                                prov3[i, index] = d3Pjs[index] - d3Pjs[index + 1];
                            }
                        }

                        #endregion
                    }
                    else if (model_Name == ModelNames.Models.PCM || model_Name == ModelNames.Models.GPCM || model_Name == ModelNames.Models.RSM || model_Name == ModelNames.Models.NRM)
                    {
                        double[] dj = null; double[] v = null;  // Common column for the following models

                        if (model_Name == ModelNames.Models.PCM)
                        {
                            #region "Variable Declarations"

                            prov  = new double[it.NumOfItems, it.colSize + 1];
                            prov1 = new double[it.NumOfItems, it.colSize + 1];
                            prov2 = new double[it.NumOfItems, it.colSize + 1];
                            prov3 = new double[it.NumOfItems, it.colSize + 1];

                            objPi = new PiListPoly(it.NumOfItems, it.colSize + 1);

                            #endregion

                            #region "Functional Logic"

                            for (int i = 0; i < it.NumOfItems; i++)
                            {
                                dj = new double[it.colSize + 1];
                                v  = new double[it.colSize + 1];

                                dj[0] = 0.00; v[0] = 0.00;

                                for (int k = 1; k < dj.Length; k++)
                                {
                                    dj[k] = dj[k - 1] + D * (th - it.all_items_poly[k - 1][i]);
                                    v[k]  = k;
                                }

                                double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v);
                                dj = temp_dj_v[0];
                                v  = temp_dj_v[1];

                                calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i);
                            }

                            #endregion
                        }

                        if (model_Name == ModelNames.Models.GPCM)
                        {
                            #region "Variable Declarations"

                            prov  = new double[it.NumOfItems, it.colSize];
                            prov1 = new double[it.NumOfItems, it.colSize];
                            prov2 = new double[it.NumOfItems, it.colSize];
                            prov3 = new double[it.NumOfItems, it.colSize];

                            objPi = new PiListPoly(it.NumOfItems, it.colSize);

                            #endregion

                            #region "Functional Logic"

                            for (int i = 0; i < it.NumOfItems; i++)
                            {
                                // "alphaj", 1st column
                                double aj = it.all_items_poly[0][i];

                                dj    = new double[it.colSize];
                                v     = new double[it.colSize];
                                dj[0] = 0.00; v[0] = 0.00;

                                for (int k = 1; k < dj.Length; k++)
                                {
                                    dj[k] = dj[k - 1] + aj * D * (th - it.all_items_poly[k][i]);
                                    v[k]  = aj * k;
                                }

                                double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v);
                                dj = temp_dj_v[0];
                                v  = temp_dj_v[1];

                                calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i);
                            }

                            #endregion
                        }

                        if (model_Name == ModelNames.Models.RSM)
                        {
                            #region "Variable Declarations"

                            prov  = new double[it.NumOfItems, it.colSize];
                            prov1 = new double[it.NumOfItems, it.colSize];
                            prov2 = new double[it.NumOfItems, it.colSize];
                            prov3 = new double[it.NumOfItems, it.colSize];

                            objPi = new PiListPoly(it.NumOfItems, it.colSize);

                            #endregion

                            #region "Functional Logic"

                            for (int i = 0; i < it.NumOfItems; i++)
                            {
                                // "lambdaj", 1st column
                                double lambdaj = it.all_items_poly[0][i];

                                dj    = new double[it.colSize];
                                v     = new double[it.colSize];
                                dj[0] = 0.00; v[0] = 0.00;

                                for (int k = 1; k < dj.Length; k++)
                                {
                                    dj[k] = dj[k - 1] + D * (th - (lambdaj + it.all_items_poly[k][i]));
                                    v[k]  = k;
                                }

                                double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v);
                                dj = temp_dj_v[0];
                                v  = temp_dj_v[1];

                                calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i);
                            }

                            #endregion
                        }

                        if (model_Name == ModelNames.Models.NRM)
                        {
                            #region "Variable Declarations"

                            int nc = (it.colSize / 2) + 1;

                            prov  = new double[it.NumOfItems, nc];
                            prov1 = new double[it.NumOfItems, nc];
                            prov2 = new double[it.NumOfItems, nc];
                            prov3 = new double[it.NumOfItems, nc];

                            objPi = new PiListPoly(it.NumOfItems, nc);

                            #endregion

                            #region "Functional Logic"

                            for (int i = 0; i < it.NumOfItems; i++)  // Row Iteration
                            {
                                dj    = new double[nc];
                                v     = new double[nc];
                                dj[0] = 0.00; v[0] = 0.00;

                                for (int k = 1; k < dj.Length; k++)
                                {
                                    dj[k] = it.all_items_poly[2 * (k - 2) + 2][i] * th + it.all_items_poly[2 * (k - 2) + 3][i];
                                    v[k]  = it.all_items_poly[2 * (k - 2) + 2][i];
                                }

                                double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v);
                                dj = temp_dj_v[0];
                                v  = temp_dj_v[1];

                                calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i);
                            }

                            #endregion
                        }
                    }
                    else
                    {
                        objPi.Exception = "Polytomous items model not matched!";
                    }
                }
                else
                {
                    objPi.Exception = "Polytomous model not provided!";
                }

                objPi.Add(prov, prov1, prov2, prov3);

                //objPi.catDictionaryList = new Dictionary<string, Dictionary<string, catList>>();

                //GetCATList(objPi.Pi, objPi.catDictionaryList, "Pi");
                //GetCATList(objPi.dPi, objPi.catDictionaryList, "dPi");
                //GetCATList(objPi.d2Pi, objPi.catDictionaryList, "d2Pi");
                //GetCATList(objPi.d3Pi, objPi.catDictionaryList, "d3Pi");
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    if (objPi == null)
                    {
                        objPi = new PiListPoly();
                    }
                    objPi.Exception = ex.Message;
                }

                return(objPi);
            }

            return(objPi);
        }
Esempio n. 6
0
        public static JiList Ji_Calc(double th, CATItems it, string model, double D)
        {
            JiList objJi = null;

            double[] Ji = null; double[] dJi = null;

            try
            {
                if (String.IsNullOrEmpty(model))
                {
                    #region "Calculation for Dichotmous Items"

                    PiList pr = Pi.Pi_Calc(th, it, model, D);

                    if (pr == null)
                    {
                        objJi           = new JiList();
                        objJi.Exception = "No Pi values found!";
                        return(objJi);
                    }

                    double[] P = pr.Pi; double[] dP = pr.dPi; double[] d2P = pr.d2Pi; double[] d3P = pr.d3Pi;

                    Ji  = new double[dP.Length];
                    dJi = new double[d2P.Length];

                    objJi = new JiList(P.Length);

                    double[] Q = new double[P.Length];

                    for (int i = 0; i < P.Length; i++)
                    {
                        Q[i] = 1 - P[i];
                    }

                    for (int j = 0; j < Ji.Length; j++)
                    {
                        Ji[j] = dP[j] * d2P[j] / (P[j] * Q[j]);
                    }

                    for (int k = 0; k < dJi.Length; k++)
                    {
                        dJi[k] = (P[k] * Q[k] * (Math.Pow(d2P[k], 2) + dP[k] * d3P[k]) - Math.Pow(dP[k], 2) * d2P[k] * (Q[k] - P[k])) / (Math.Pow(P[k], 2) * Math.Pow(Q[k], 2));
                    }

                    #endregion
                }
                else
                {
                    #region "Calculation for Polytomous Items"

                    PiListPoly pr = Pi.Pi_Poly_Calc(th, it, model, D);

                    if (pr == null)
                    {
                        objJi           = new JiList();
                        objJi.Exception = "No Pi values found!";
                        return(objJi);
                    }

                    double[,] P = pr.Pi; double[,] dP = pr.dPi; double[,] d2P = pr.d2Pi; double[,] d3P = pr.d3Pi;

                    int rowLength    = pr.Pi.GetLength(0);
                    int columnLength = pr.Pi.GetLength(1);

                    objJi = new JiList(rowLength);

                    double[,] prov  = new double[rowLength, columnLength];
                    double[,] prov1 = new double[rowLength, columnLength];

                    Ji  = new double[rowLength];
                    dJi = new double[rowLength];

                    for (int i = 0; i < rowLength; i++)
                    {
                        for (int j = 0; j < columnLength; j++)
                        {
                            prov[i, j] = dP[i, j] * d2P[i, j] / P[i, j];

                            prov1[i, j] = (P[i, j] * Math.Pow(d2P[i, j], 2) + P[i, j] * dP[i, j] * d3P[i, j] - Math.Pow(dP[i, j], 2) * d2P[i, j]) / Math.Pow(P[i, j], 2);
                        }
                    }

                    Ji  = RowColumn.rowSums(prov);
                    dJi = RowColumn.rowSums(prov1);

                    #endregion
                }

                objJi.Add(Ji, dJi);
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    if (objJi == null)
                    {
                        objJi = new JiList();
                    }
                    objJi.Exception = ex.Message;
                }
                return(objJi);
            }

            return(objJi);
        }
Esempio n. 7
0
        // Function "Pi" for Polytomous items
        public static PiListPoly Pi_P(double th, CATItems it, string model, double D)
        {
            PiListPoly objPi = CatRcs.Pi.Pi_Poly_Calc(th, it, model, D);

            return(objPi);
        }