// Pi method for Dichotomous items public static PiList Pi_Calc(double th, CATItems it, string model, double D) { PiList objPi = null; if (String.IsNullOrEmpty(model)) { #region "Functional Logic" try { double[] e = new double[it.NumOfItems]; objPi = new PiList(it.NumOfItems); for (int i = 0; i < it.NumOfItems; i++) { e[i] = Math.Exp(D * it.a[i] * (th - it.b[i])); // round test double final_pi = 0; double temp_pi = it.c[i] + (it.d[i] - it.c[i]) * e[i] / (1 + e[i]); final_pi = (temp_pi == 0) ? 1e-10 : temp_pi; final_pi = (temp_pi == 1) ? 1 - 1e-10 : temp_pi; double temp_dPi = D * it.a[i] * e[i] * (it.d[i] - it.c[i]) / Math.Pow(1 + e[i], 2); // round test double temp_d2Pi = Math.Pow(D, 2) * Math.Pow(it.a[i], 2) * e[i] * (1 - e[i]) * (it.d[i] - it.c[i]) / Math.Pow(1 + e[i], 3); // round test double temp_d3Pi = Math.Pow(D, 3) * Math.Pow(it.a[i], 3) * e[i] * (it.d[i] - it.c[i]) * (Math.Pow(e[i], 2) - 4 * e[i] + 1) / Math.Pow(1 + e[i], 4); // round test objPi.AddPiList(final_pi, temp_dPi, temp_d2Pi, temp_d3Pi, i); } } catch (Exception ex) { if (ex != null) { if (objPi == null) { objPi = new PiList(ex.Message); } } return(objPi); } #endregion } else { objPi = new PiList(); objPi.Errors.SetValue("Model is not empty!!", 0); } return(objPi); }
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); }
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); }
public void testPi_D() { bool OSProc = System.Environment.Is64BitProcess; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Dichotomous Items DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = 100)").AsDataFrame(); engineObj.SetSymbol("DichoItems", DichoItems); // Adapting with the existing "CAT-Items" type (Wrapper) CATItems itemBank = new CATItems(DichoItems[0].Select(y => (double)y).ToArray(), DichoItems[1].Select(y => (double)y).ToArray(), DichoItems[2].Select(y => (double)y).ToArray(), DichoItems[3].Select(y => (double)y).ToArray()); #region "Test block for Ability Values (th)" PiList objPI = null; int decimalPoint = 4; double[] th_Values = CatRcs.Utils.CommonHelper.Sequence(-6, 6, by: 1); Console.WriteLine("******* TEST for Ability value Theta ********"); for (int k = 0; k < th_Values.Length; k++) { // Sequence Generation for Theta (Ability) values NumericVector th_val = engineObj.CreateNumericVector(new double[] { th_Values[k] }); engineObj.SetSymbol("th_val", th_val); // Calling the "Pi" from R GenericVector result_Pi = engineObj.Evaluate("result_Pi <- Pi(th = th_val, DichoItems, D = 1)").AsList(); // Getting the 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[k]); objPI = CatRLib.Pi_D(th_Values[k], itemBank, "", 1); #region "Pi" int z = 0; resultFlag = true; Console.WriteLine("****** Pi ******"); for (int i = 0; i < Pi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(Pi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.Pi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag, "Passed"); Console.WriteLine("Values of Pi are Matched!"); #endregion #region "dPi" z = 0; resultFlag = true; Console.WriteLine("****** dPi ******"); for (int i = 0; i < dPi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(dPi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.dPi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of dPi are Matched!"); #endregion #region "d2Pi" z = 0; resultFlag = true; Console.WriteLine("****** d2Pi ******"); for (int i = 0; i < d2Pi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(d2Pi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d2Pi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of d2Pi are Matched!"); #endregion #region "d3Pi" z = 0; resultFlag = true; Console.WriteLine("****** d3Pi ******"); for (int i = 0; i < d3Pi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(d3Pi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d3Pi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of d3Pi are Matched!"); #endregion } Assert.IsTrue(resultFlag); #endregion #region "Test block for Metric values" objPI = null; double[] D_Values = CatRcs.Utils.CommonHelper.Sequence(0.5, 1, by: 0.1); Console.WriteLine("******* TEST for Metric Constant ********"); for (int k = 0; k < D_Values.Length; k++) { // Sequence Generation for Theta (Ability) values NumericVector D_val = engineObj.CreateNumericVector(new double[] { D_Values[k] }); engineObj.SetSymbol("D_val", D_val); // Calling the "Pi" from R GenericVector result_Pi = engineObj.Evaluate("result_Pi <- Pi(th = 0, DichoItems, D = D_val)").AsList(); // Getting the 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 Metric constant: " + D_Values[k]); objPI = CatRLib.Pi_D(0, itemBank, "", D_Values[k]); #region "Pi" int z = 0; resultFlag = true; Console.WriteLine("****** Pi ******"); for (int i = 0; i < Pi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(Pi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.Pi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of Pi are matched!"); #endregion #region "dPi" z = 0; resultFlag = true; Console.WriteLine("****** dPi ******"); for (int i = 0; i < dPi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(dPi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.dPi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of dPi are matched!"); #endregion #region "d2Pi" z = 0; resultFlag = true; Console.WriteLine("****** d2Pi ******"); for (int i = 0; i < d2Pi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(d2Pi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d2Pi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of d2Pi are Matched!"); #endregion #region "d3Pi" z = 0; resultFlag = true; Console.WriteLine("****** d3Pi ******"); for (int i = 0; i < d3Pi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(d3Pi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d3Pi[i]), decimalPoint)) { //Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of d3Pi are Matched!"); #endregion } #endregion }
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); }
// Function "Pi" for Dichotmous items public static PiList Pi_D(double th, CATItems it, string model, double D) { PiList objPi = CatRcs.Pi.Pi_Calc(th, it, model, D); return(objPi); }