public static double MWI_Calc(CATItems itemBank, int item, int[] x, CATItems it_given, string model, int type = 1 /* MLWI */, double[] priorPar = null, double D = 1, string priorDist = "norm", int lower = -4, int upper = 4, int nqp = 33) { double res = 0; if (priorPar == null || priorPar.Length < 2) { priorPar = new double[2]; priorPar[0] = 0; priorPar[1] = 1; } if (type != (int)ModelNames.MWI_Type.MLWI && type != (int)ModelNames.MWI_Type.MPWI) { return(res); } else { double[] lik = null; double[] info = null; double[] crit_value = null; double[] X = CatRcs.Utils.CommonHelper.Sequence(lower, upper, nqp); if (String.IsNullOrEmpty(model)) // Dichotomous Items { #region "Function 'L' " Func <double, int[], CATItems, double> L = (th, x_in, items) => { double result = 0; var temp_1 = Pi.Pi_Calc(th, it_given, model, D).Pi.Select((p, i) => Math.Pow(p, x_in[i])).ToArray(); var temp_2 = Pi.Pi_Calc(th, it_given, model, D).Pi.Select((p, i) => Math.Pow(1 - p, 1 - x_in[i])).ToArray(); if (temp_1.Length == temp_2.Length) { var temp_3 = temp_1.Select((p, i) => p * temp_2[i]).ToArray(); result = temp_3.Aggregate((acc, val) => acc * val); } return(result); }; #endregion lik = X.Select((a, i) => L(a, x, it_given)).ToArray(); // "sapply" function shortcut using LINQ info = X.Select(b => Ii.Ii_Calc(b, itemBank, model, D).Ii[item - 1]).ToArray(); crit_value = lik.Select((c, j) => c * info[j]).ToArray(); } else // Polytomous Items { #region "Function 'LL' " Func <double, CATItems, int[], double> LL = (th, items, x_in) => { double result = 0; if (it_given.colSize == 0) { result = 1; } else { result = 1; double[,] prob = Pi.Pi_Poly_Calc(th, it_given, model, D).Pi; for (int i = 0; i < x_in.Length; i++) { res = res * prob[i, x[i]]; } } return(result); }; #endregion lik = X.Select((a, i) => LL(a, it_given, x)).ToArray(); CATItems temp_ItemBank = itemBank.FindItem(item); info = X.Select(b => CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(b, temp_ItemBank, model, D).Ii)).ToArray(); crit_value = lik.Select((c, j) => c * info[j]).ToArray(); } if (lik.Length > 0 && info.Length > 0) { if (type == (int)ModelNames.MWI_Type.MPWI) { double[] pd = new double[X.Length]; for (int i = 0; i < X.Length; i++) { switch (priorDist) { case "norm": pd[i] = CatRcs.Utils.CommonHelper.Dnorm(X[i], priorPar[0], priorPar[1]); break; case "unif": pd[i] = CatRcs.Utils.CommonHelper.Dunif(X[i], priorPar[0], priorPar[1]); break; } } crit_value = crit_value.Select((d, k) => d * pd[k]).ToArray(); } res = CatRcs.Integrate_catR.Integrate_CatR_Calc(X, crit_value); } } return(res); }
public static double EapEST_Calc(CATItems it, int[] x, string model, double[] priorPar = null, double D = 1, string priorDist = "norm", double lower = -4, double upper = 4, double nqp = 33) { double res = 0; double[] X = CatRcs.Utils.CommonHelper.Sequence(lower, upper, nqp); double[] Y1 = null; double[] Y2 = null; if (priorPar == null || priorPar.Length < 2) { priorPar = new double[2]; priorPar[0] = 0; priorPar[1] = 1; } if (String.IsNullOrEmpty(model)) // Dichotomous Items { #region "Function 'L' " Func <double, CATItems, int[], double> L = (th, items, x_in) => { double result = 0; double[] pi = Pi.Pi_Calc(th, it, model, D).Pi; double[] x_p = new double[pi.Length]; double[] x_q = new double[pi.Length]; double[] p_q = new double[pi.Length]; for (int ind_p = 0; ind_p < pi.Length; ind_p++) { x_p[ind_p] = Math.Pow(pi[ind_p], x_in[ind_p]);; x_q[ind_p] = Math.Pow(1 - pi[ind_p], 1 - x_in[ind_p]); p_q[ind_p] = x_p[ind_p] * x_q[ind_p]; } result = p_q.Aggregate((acc, val) => acc * val); return(result); }; #endregion #region "Function 'g' " Func <double[], double[]> g = (s) => { double[] resList = new double[s.Length]; for (int i = 0; i < s.Length; i++) { switch (priorDist) { case "norm": resList[i] = s[i] * CatRcs.Utils.CommonHelper.Dnorm(s[i], priorPar[0], priorPar[1]) * L(s[i], it, x); break; case "unif": resList[i] = s[i] * CatRcs.Utils.CommonHelper.Dunif(s[i], priorPar[0], priorPar[1]) * L(s[i], it, x); break; case "Jeffreys": resList[i] = s[i] * Math.Sqrt(CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(s[i], it, model, D).Ii)) * L(s[i], it, x); break; } } return(resList); }; #endregion #region "Function 'h' " Func <double[], double[]> h = (s) => { double[] resList = new double[s.Length]; for (int i = 0; i < s.Length; i++) { switch (priorDist) { case "norm": resList[i] = CatRcs.Utils.CommonHelper.Dnorm(s[i], priorPar[0], priorPar[1]) * L(s[i], it, x); break; case "unif": resList[i] = CatRcs.Utils.CommonHelper.Dunif(s[i], priorPar[0], priorPar[1]) * L(s[i], it, x); break; case "Jeffreys": resList[i] = Math.Sqrt(CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(s[i], it, model, D).Ii)) * L(s[i], it, x); break; } } return(resList); }; #endregion Y1 = g(X); Y2 = h(X); } else // Polytomous Items { #region "Function 'LL' " Func <double, CATItems, int[], double> LL = (th, items, x_in) => { double result = 1; double[,] prob = Pi.Pi_Poly_Calc(th, it, model, D).Pi; for (int i = 0; i < x_in.Length; i++) { result = result * prob[i, x_in[i]]; } return(result); }; #endregion #region "Function 'gg' " Func <double[], double[]> gg = (s) => { double[] resList = new double[s.Length]; for (int i = 0; i < s.Length; i++) { switch (priorDist) { case "norm": resList[i] = s[i] * CatRcs.Utils.CommonHelper.Dnorm(s[i], priorPar[0], priorPar[1]) * LL(s[i], it, x); break; case "unif": resList[i] = s[i] * CatRcs.Utils.CommonHelper.Dunif(s[i], priorPar[0], priorPar[1]) * LL(s[i], it, x); break; case "Jeffreys": resList[i] = s[i] * Math.Sqrt(CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(s[i], it, model, D).Ii)) * LL(s[i], it, x); break; } } return(resList); }; #endregion #region "Function 'hh' " Func <double[], double[]> hh = (s) => { double[] resList = new double[s.Length]; for (int i = 0; i < s.Length; i++) { switch (priorDist) { case "norm": resList[i] = CatRcs.Utils.CommonHelper.Dnorm(s[i], priorPar[0], priorPar[1]) * LL(s[i], it, x); break; case "unif": resList[i] = CatRcs.Utils.CommonHelper.Dunif(s[i], priorPar[0], priorPar[1]) * LL(s[i], it, x); break; case "Jeffreys": resList[i] = Math.Sqrt(CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(s[i], it, model, D).Ii)) * LL(s[i], it, x); break; } } return(resList); }; #endregion Y1 = gg(X); Y2 = hh(X); } if ((Y1 != null) && (Y2 != null)) { res = CatRcs.Integrate_catR.Integrate_CatR_Calc(X, Y1) / CatRcs.Integrate_catR.Integrate_CatR_Calc(X, Y2); } return(res); }
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); }