List of usage examples for org.apache.commons.math3.stat.descriptive.moment Mean increment
@Override public void increment(final double d)
Note that when #Mean(FirstMoment) is used to create a Mean, this method does nothing.
From source file:com.itemanalysis.psychometrics.irt.model.Irm3PL.java
/** * Mean/mean linking coefficients are computed from the mean item difficulty and mean item discrimination. * The summary statistics are computed in a storeless manner. This method allows for the incremental * update to item difficulty summary statistics by combining them with other summary statistics. * * @param meanDiscrimination item discrimination mean. * @param meanDifficulty item difficulty mean. *///from w w w . j ava 2 s .co m public void incrementMeanMean(Mean meanDiscrimination, Mean meanDifficulty) { meanDiscrimination.increment(discrimination); meanDifficulty.increment(difficulty); }
From source file:com.itemanalysis.psychometrics.rasch.JMLE.java
/** * Computes PROX starting values fro items, thresholds, and persons. *//*from w ww . j ava 2s .c o m*/ public void prox() { Mean m = new Mean(); RatingScaleItem rsi = null; for (VariableName v : items.keySet()) { rsi = items.get(v); rsi.prox(MPRIS(v), adjustedRIS(v)); m.increment(rsi.getDifficulty()); } for (VariableName v : items.keySet()) { rsi = items.get(v); rsi.recenter(m.getResult()); rsi.recenterProposalDifficulty(m.getResult()); } RatingScaleThresholds rst = null; for (String s : thresholds.keySet()) { rst = thresholds.get(s); if (!rst.extremeThreshold()) { rst.categoryProx(Spj(rst.getGroupId())); rst.recenterProposalThresholds(); rst.recenterThresholds(); } } for (int i = 0; i < nPeople; i++) { theta[i] = prox(data[i]); } }
From source file:com.itemanalysis.psychometrics.irt.estimation.JointMaximumLikelihoodEstimation.java
/** * Update of all persons is handled here. * * @param maxIter maximum number of iteration in the person update. * @param converge convergence criterion for the person update. The criterion is the maximum change in logits. * @param adjustment extreme score adjustment. * @param centerItems establish identification by centering item about the item difficulty mean (the approach * typically used in Rasch measurement). If false establish identification by centering * persons around the mean ability. Centering only done for nonextreme persons and items. * @return maximum observed value change in logits from updating all examinees. *///from w w w . j a v a 2 s. c om private double updateAllPersons(int maxIter, double converge, double adjustment, boolean centerItems) { double maxDelta = 0.0; double tempTheta = 0.0; Mean personMean = new Mean(); for (int i = 0; i < nPeople; i++) { tempTheta = theta[i]; theta[i] = updatePerson(i, maxIter, converge, adjustment); if (extremePerson[i] == 0) { personMean.increment(theta[i]); maxDelta = Math.max(Math.abs(theta[i] - tempTheta), maxDelta); } } if (!centerItems) { double m = personMean.getResult(); for (int i = 0; i < nPeople; i++) { if (extremePerson[i] == 0) theta[i] -= m; } } return maxDelta; }
From source file:com.itemanalysis.psychometrics.rasch.JMLE.java
public void linearTransformation(DefaultLinearTransformation lt, int precision) { Mean pMean = new Mean(); StandardDeviation pSd = new StandardDeviation(); //set transformation and rescale persons double newScale = lt.getScale(); double newMean = lt.getIntercept(); double oldPersonMean = pMean.evaluate(theta); double oldPersonSd = pSd.evaluate(theta); lt.setScaleAndIntercept(oldPersonMean, newMean, oldPersonSd, newScale); for (int i = 0; i < theta.length; i++) { theta[i] = lt.transform(theta[i]); }// ww w . j a v a 2 s. c o m //set transformation and rescale items Mean iMean = new Mean(); StandardDeviation iSd = new StandardDeviation(); double tempDifficulty = 0.0; for (VariableName v : items.keySet()) { tempDifficulty = items.get(v).getDifficulty(); iMean.increment(tempDifficulty); iSd.increment(tempDifficulty); } lt.setScaleAndIntercept(iMean.getResult(), newMean, iSd.getResult(), newScale); for (VariableName v : items.keySet()) { items.get(v).linearTransformation(lt, precision); } //set transformation and rescale thresholds RatingScaleThresholds tempThresholds = null; for (String s : thresholds.keySet()) { tempThresholds = thresholds.get(s); lt.setScaleAndIntercept(tempThresholds.getThresholdMean(), newMean, tempThresholds.getThresholdStandardDeviation(), newScale); thresholds.get(s).linearTransformation(lt, precision); } }
From source file:com.itemanalysis.psychometrics.irt.estimation.JointMaximumLikelihoodEstimation.java
/** * Computes PROX difficulty estimates for item difficulty. These are used as starting values in JMLE. *//*ww w . j a v a 2s . c om*/ public void itemProx() { for (int j = 0; j < nItems; j++) { if (droppedStatus[j] == 0 && !irm[j].isFixed()) { double maxItemScore = itemSummary[j].maxSip(); double adjustedScore = itemSummary[j].Sip(); double p = adjustedScore / maxItemScore; double q = 1.0 - p; double prox = Math.log(q / p); irm[j].setDifficulty(prox); irm[j].setProposalDifficulty(prox); int ncat = irm[j].getNcat(); //threshold prox values if (ncat > 2) { double previous = 0.0; double current = 0.0; double[] threshold = new double[ncat - 1]; RaschRatingScaleGroup group = rsg.get(irm[j].getGroupId()); Mean tMean = new Mean(); for (int k = 0; k < ncat; k++) { current = group.SpjAt(k); if (k > 0) { threshold[k - 1] = Math.log(previous / current); tMean.increment(threshold[k - 1]); } previous = current; } for (int k = 0; k < ncat - 1; k++) { threshold[k] -= tMean.getResult(); } irm[j].setThresholdParameters(threshold); irm[j].setProposalThresholds(threshold); } } } }
From source file:com.itemanalysis.psychometrics.irt.estimation.JointMaximumLikelihoodEstimation.java
/** * This method manages the item updates during the JMLE iterations. * * @param delta current value of the maximum observed change in logits * @param centerItems establish identification by centering item about the item difficulty mean (the approach * typically used in Rasch measurement). If false establish identification by centering * persons around the mean ability. Centering only done for nonextreme persons and items. * @return maximum change in logits observed during the item updates *//* www. ja va 2 s.co m*/ private double updateAllItems(double delta, boolean centerItems) { double maxDelta = 0.0; double difficulty = 0.0; double tempDifficulty = 0.0; Mean mean = new Mean(); boolean hasFixed = false; //update each non extreme item for (int j = 0; j < nItems; j++) { if (droppedStatus[j] == 0) { tempDifficulty = updateDifficulty(irm[j], itemSummary[j], delta); if (extremeItem[j] == 0) mean.increment(tempDifficulty); } if (irm[j].isFixed()) hasFixed = true; } //Center non extreme items about the mean item difficulty. //Accept all proposal values. for (int j = 0; j < nItems; j++) { if (hasFixed) { //with fixed values, there is no need to constrain item difficulty to be zero. maxDelta = Math.max(maxDelta, Math.abs(irm[j].getProposalDifficulty() - irm[j].getDifficulty())); } else { //without fixed item parameter, constrain item difficulty to be zero. if (droppedStatus[j] == 0 && extremeItem[j] == 0) { difficulty = irm[j].getDifficulty(); tempDifficulty = irm[j].getProposalDifficulty(); if (centerItems) tempDifficulty -= mean.getResult();//center irm[j].setProposalDifficulty(tempDifficulty); maxDelta = Math.max(maxDelta, Math.abs(tempDifficulty - difficulty)); } } } return maxDelta; }
From source file:net.myrrix.common.LoadRunner.java
public void runLoad() throws ExecutionException, InterruptedException { final Mean recommendedBecause = new Mean(); final Mean setPreference = new Mean(); final Mean removePreference = new Mean(); final Mean setTag = new Mean(); final Mean ingest = new Mean(); final Mean refresh = new Mean(); final Mean estimatePreference = new Mean(); final Mean mostSimilarItems = new Mean(); final Mean similarityToItem = new Mean(); final Mean mostPopularItems = new Mean(); final Mean recommendToMany = new Mean(); final Mean recommend = new Mean(); Processor<Integer> processor = new Processor<Integer>() { private final RandomGenerator random = RandomManager.getRandom(); @Override/*from www . j ava 2 s .c o m*/ public void process(Integer step, long count) { double r; long userID; long itemID; long itemID2; float value; synchronized (random) { r = random.nextDouble(); userID = uniqueUserIDs[random.nextInt(uniqueUserIDs.length)]; itemID = uniqueItemIDs[random.nextInt(uniqueItemIDs.length)]; itemID2 = uniqueItemIDs[random.nextInt(uniqueItemIDs.length)]; value = random.nextInt(10); } long stepStart = System.currentTimeMillis(); try { if (r < 0.05) { client.recommendedBecause(userID, itemID, 10); recommendedBecause.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.07) { client.setPreference(userID, itemID); setPreference.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.08) { client.setPreference(userID, itemID, value); setPreference.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.09) { client.setUserTag(userID, Long.toString(itemID)); setTag.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.10) { client.setItemTag(Long.toString(userID), itemID); setTag.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.11) { client.removePreference(userID, itemID); removePreference.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.12) { StringReader reader = new StringReader(userID + "," + itemID + ',' + value + '\n'); client.ingest(reader); ingest.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.13) { client.refresh(); refresh.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.14) { client.similarityToItem(itemID, itemID2); similarityToItem.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.15) { client.mostPopularItems(10); mostPopularItems.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.19) { client.estimatePreference(userID, itemID); estimatePreference.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.20) { client.estimateForAnonymous(itemID, new long[] { itemID2 }); estimatePreference.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.25) { client.mostSimilarItems(new long[] { itemID }, 10); mostSimilarItems.increment(System.currentTimeMillis() - stepStart); } else if (r < 0.30) { client.recommendToMany(new long[] { userID, userID }, 10, true, null); recommendToMany.increment(System.currentTimeMillis() - stepStart); } else { client.recommend(userID, 10); recommend.increment(System.currentTimeMillis() - stepStart); } } catch (TasteException te) { log.warn("Error during request", te); } if (count % 1000 == 0) { log.info("Finished {} load steps", count); } } }; log.info("Starting load test..."); long start = System.currentTimeMillis(); new Paralleler<Integer>(new CountingIterator(steps), processor, "Load").runInParallel(); long end = System.currentTimeMillis(); log.info("Finished {} steps in {}ms", steps, end - start); log.info("recommendedBecause: {}", recommendedBecause.getResult()); log.info("setPreference: {}", setPreference.getResult()); log.info("removePreference: {}", removePreference.getResult()); log.info("setTag: {}", setTag.getResult()); log.info("ingest: {}", ingest.getResult()); log.info("refresh: {}", refresh.getResult()); log.info("estimatePreference: {}", estimatePreference.getResult()); log.info("mostSimilarItems: {}", mostSimilarItems.getResult()); log.info("similarityToItem: {}", similarityToItem.getResult()); log.info("mostPopularItems: {}", mostPopularItems.getResult()); log.info("recommendToMany: {}", recommendToMany.getResult()); log.info("recommend: {}", recommend.getResult()); }
From source file:com.itemanalysis.psychometrics.rasch.JMLE.java
/** * Run the primary JML estimation routines. * * To estimate thetas using known item parameters, the known parameters must be established and updateItems * should be set to false./*from w w w . j av a 2 s .c om*/ * * Extreme persons and items are updated at the same time as nonextreme items and persons. However, the extreme * items and persons are not counted toward the convergence criterion and they are not used to estimate * parameters for the nonextreme items and persons. * * * @param globalMaxIter * @param globalConvergence maximum change in logits (LCONV in WINSTEPS documentation) * @param updateItems set to true if items are to be updated. Note individual items can be fixed too. * @throws SQLException */ public void update(int globalMaxIter, double globalConvergence, boolean updateItems, boolean updatePersons) { double DELTA = globalConvergence + 1.0; //LCONV maxDelta = new Max(); Mean itemMean = new Mean(); int iter = 0; RatingScaleItem rsi = null; RatingScaleThresholds rst = null; double newDifficulty = 0.0; while (DELTA >= globalConvergence && iter < globalMaxIter) { if (updateItems) { itemMean.clear(); //update items that are not fixed for (VariableName v : items.keySet()) { rsi = items.get(v); if (rsi.fixedParameter()) { itemMean.increment(rsi.getDifficulty()); } else { if (!rsi.extremeItem() && !rsi.droppedItem()) { newDifficulty = updateDifficulty(items.get(v), validRIS(rsi.getColumn()), vMPRIS(rsi.getColumn()), 0.0, DELTA); itemMean.increment(newDifficulty); } } } //update thresholds for (String s : thresholds.keySet()) { rst = thresholds.get(s); if (!rst.extremeThreshold() && !rst.fixedParameter()) updateThresholds(rst); } //accept new thresholds and increment delta. Only increments delta for non extreme categories for (String s : thresholds.keySet()) { maxDelta.increment(thresholds.get(s).acceptProposalThresholds()); } //Recenter proposal difficulties, accept proposal difficulties, and increment delta //Extreme items are not recentered, and their change in rho not counted in delta. for (VariableName v : items.keySet()) { rsi = items.get(v); if (!rsi.extremeItem() && !rsi.droppedItem()) { rsi.recenterProposalDifficulty(itemMean.getResult()); maxDelta.increment(rsi.acceptProposalDifficulty()); } /** * Set new threshold rho in RatingScaleItem object */ if (rsi.getNumberOfCategories() > 2) { rsi.setThresholds(thresholds.get(rsi.getGroupId()).getThresholds()); } } } //update persons //Change in person parameter is not counted toward delta. double tDelta = 0.0; if (updatePersons) { for (int i = 0; i < nPeople; i++) { if (!extremePersons[i]) { tDelta = updatePersons(i, validRawScore(data[i]), vMPRS(data[i]), DELTA); maxDelta.increment(tDelta); } } } DELTA = maxDelta.getResult(); maxDelta.clear(); iterationDelta.add(DELTA); //compute residuals for all nonextreme items completed by nonextreme examinees //the residual to compute are the expected score (TCC, iTCC) and observed score (RS, RIS) iter++; } //end JMLE loop }
From source file:com.itemanalysis.psychometrics.irt.estimation.JointMaximumLikelihoodEstimation.java
/** * Thresholds for a single rating scale group are updated in this method. Updates only involve nonextreme * examinees that respond to the item./*from w w w .ja v a2s . c o m*/ * * @param raschRatingScaleGroup group for which thresholds are updated. * @return maximum change in logits for this update. */ private double updateThresholds(RaschRatingScaleGroup raschRatingScaleGroup) { double thresh = 0.0; int[] pos = raschRatingScaleGroup.getPositions(); int nCat = raschRatingScaleGroup.getNumberOfCategories(); double[] catKSum = new double[nCat]; double[] thresholds = null; double[] proposalThresholds = new double[nCat - 1]; Mean tMean = new Mean(); double maxDelta = 0.0; thresholds = raschRatingScaleGroup.getThresholds(); for (int i = 0; i < nPeople; i++) { if (extremePerson[i] == 0) { thresholds = raschRatingScaleGroup.getThresholds(); for (int k = 0; k < nCat; k++) { catKSum[k] += raschRatingScaleGroup.probabilitySumAt(theta[i], k); } } } int prevCat = 0; int nextCat = 0; for (int k = 0; k < nCat - 1; k++) { nextCat++; thresh = thresholds[k]; proposalThresholds[k] = thresh - Math.log(raschRatingScaleGroup.TpjAt(nextCat) / raschRatingScaleGroup.TpjAt(prevCat)) + Math.log(catKSum[nextCat] / catKSum[prevCat]); //do not change threshold by more than one logit - from WINSTEPS documentation proposalThresholds[k] = Math.max(Math.min(thresh + 1.0, proposalThresholds[k]), thresh - 1.0); tMean.increment(proposalThresholds[k]); prevCat = nextCat; } //recenter thresholds around the mean threshold double m = tMean.getResult(); for (int k = 0; k < nCat - 1; k++) { proposalThresholds[k] = proposalThresholds[k] - m; maxDelta = Math.max(Math.abs(proposalThresholds[k] - thresholds[k]), maxDelta); } raschRatingScaleGroup.setProposalThresholds(proposalThresholds); return maxDelta; }
From source file:com.itemanalysis.psychometrics.irt.estimation.StartingValues.java
/** * Computes normal approximation estimates (PROX) of item difficulty and person ability * in a way that allows for missing data (Linacre, 1994). It is an iterative procedure. * * Linacre, J. M., (1994). PROX with missing data, or known item or person measures. * Rasch Measurement Transactions, 8:3, 378, http://www.rasch.org/rmt/rmt83g.htm. * * @param converge convergence criterion as the maximum change in person logits. * @param maxIter maximum number of iterations. About 10 iterations works well. *///from ww w . ja va 2 s . c om private void prox(double converge, int maxIter) { double delta = 1.0 + converge; int iter = 0; double pProx = 0; double pScore = 0; double maxTestScore = 0; double maxChange = 0; double logit = 0; Mean personGrandMean = new Mean(); StandardDeviation personGrandSd = new StandardDeviation(); double iProx = 0.0; double iMean = 0; theta = new double[nResponseVectors]; Mean[] mPerson = new Mean[nItems];//Item difficulty mean for those examinees completing item j StandardDeviation[] sdPerson = new StandardDeviation[nItems];//Item difficulty standard deviation for those examinees completing item j double[] Si = null; double[] Ni = null; Mean[] mItem = new Mean[nResponseVectors]; StandardDeviation[] sdItem = new StandardDeviation[nResponseVectors]; while (delta > converge && iter < maxIter) { Si = new double[nItems]; Ni = new double[nItems]; //Compute descriptive statistics for persons and items double resp = 0; double freq = 0; for (int l = 0; l < nResponseVectors; l++) { freq = responseVector[l].getFrequency(); for (int j = 0; j < nItems; j++) { //initialize arrays if (l == 0) { mPerson[j] = new Mean(); sdPerson[j] = new StandardDeviation(); } if (j == 0) { mItem[l] = new Mean(); sdItem[l] = new StandardDeviation(); } if (irm[j].getType() == IrmType.L3 || irm[j].getType() == IrmType.L4) { resp = responseVector[l].getResponseAt(j); //increment item and person summary statistics if (resp != -1) { //incorporate weights - crude workaround for (int w = 0; w < freq; w++) { mItem[l].increment(irm[j].getDifficulty()); sdItem[l].increment(irm[j].getDifficulty()); mPerson[j].increment(theta[l]); sdPerson[j].increment(theta[l]); Si[j] += resp; Ni[j]++; } } } } //end item loop } //end summary loop //Compute item PROX for binary items only iMean = 0; double pSd = 1e-8; double ni = 0; for (int j = 0; j < nItems; j++) { if (irm[j].getType() == IrmType.L3 || irm[j].getType() == IrmType.L4) { pSd = sdPerson[j].getResult(); //adjust extreme item scores if (Si[j] == 0) Si[j] += 0.3; if (Si[j] == Ni[j]) Si[j] -= 0.3; logit = Math.log(Si[j] / (Ni[j] - Si[j])); iProx = mPerson[j].getResult() - Math.sqrt(1.0 + pSd / 2.9) * logit; irm[j].setDifficulty(iProx); iMean += iProx; ni++; } } iMean /= ni; //center difficulties about the mean item difficulty for (int j = 0; j < nItems; j++) { if (irm[j].getType() == IrmType.L3 || irm[j].getType() == IrmType.L4) { iProx = irm[j].getDifficulty(); irm[j].setDifficulty(iProx - iMean); } } //Compute person PROX maxChange = 0; personGrandMean.clear(); personGrandSd.clear(); Pair<Double, Double> personScores = null; for (int l = 0; l < nResponseVectors; l++) { personScores = computePersonScores(responseVector[l]); pScore = personScores.getFirst(); maxTestScore = personScores.getSecond(); //adjust extreme person scores if (pScore == 0) pScore += 0.3; if (pScore == maxTestScore) pScore -= 0.3; logit = Math.log(pScore / (maxTestScore - pScore)); pProx = mItem[l].getResult() + Math.sqrt(1.0 + sdItem[l].getResult() / 2.9) * logit; maxChange = Math.max(maxChange, Math.abs(theta[l] - pProx)); theta[l] = pProx; personGrandMean.increment(pProx); personGrandSd.increment(pProx); } delta = maxChange; iter++; fireEMStatusEvent(iter, delta, Double.NaN); } //end while //Linearly transform theta estimate to have a mean of 0 and a standard deviation of 1. //Apply the same transformation to item difficulty values. double A = 1.0 / personGrandSd.getResult(); double B = -A * personGrandMean.getResult(); for (int l = 0; l < nResponseVectors; l++) { theta[l] = theta[l] * A + B; } double a = 1; double b = 0; for (int j = 0; j < nItems; j++) { if (irm[j].getType() == IrmType.L3 || irm[j].getType() == IrmType.L3) { b = irm[j].getDifficulty(); irm[j].setDifficulty(b * A + B); //Adjust discrimination parameter for scaling constant. //PROX assumes a logit scale. This conversion is to convert to the normal metric. a = irm[j].getDiscrimination(); irm[j].setDiscrimination(a / irm[j].getScalingConstant()); } } //For debugging // System.out.println("ITER: " + iter); // for(int j=0;j<nItems;j++){ // System.out.println("PROX: " + irm[j].toString()); // } }