| 80 | |
| 81 | |
| 82 | public static ArrayList<GeneralIndicator> encode(final BStat stat, final String variable, final VariableEncodings oldAdapter, final double[] oldX) { |
| 83 | final ArrayList<GeneralIndicator> res = new ArrayList<GeneralIndicator>(); |
| 84 | final double smooth = 0.5; |
| 85 | final double sumAll = stat.sumTotal + smooth; |
| 86 | final Set<String> levels = oldAdapter.def().catLevels.get(variable).keySet(); |
| 87 | final int nlevels = levels.size(); |
| 88 | for(final Map.Entry<String,Integer> me: oldAdapter.outcomeCategories.entrySet()) { |
| 89 | final String outcome = me.getKey(); |
| 90 | final int category = me.getValue(); |
| 91 | final double sumOutcome = stat.totalByCategory[category] + smooth; |
| 92 | final GeneralIndicator logBayesI = new GeneralIndicator(variable,"logbayes_" + outcome,nlevels,-1); |
| 93 | final GeneralIndicator runI= new GeneralIndicator(variable,"runTerm_" + outcome,nlevels,-1); |
| 94 | final GeneralIndicator logRunI = new GeneralIndicator(variable,"logRunTerm_" + outcome,nlevels,-1); |
| 95 | final GeneralIndicator runFI= new GeneralIndicator(variable,"runTermF_" + outcome,nlevels,-1); |
| 96 | final GeneralIndicator logRunFI = new GeneralIndicator(variable,"logRunTermF_" + outcome,nlevels,-1); |
| 97 | final GeneralIndicator balanceI = new GeneralIndicator(variable,"balance_" + outcome,nlevels,-1); |
| 98 | final GeneralIndicator balanceLR = new GeneralIndicator(variable,"balanceLR_" + outcome,nlevels,-1); |
| 99 | final GeneralIndicator superBalanceI = new GeneralIndicator(variable,"superBalance_" + outcome,nlevels,-1); |
| 100 | final GeneralIndicator superBalanceLR = new GeneralIndicator(variable,"superBalanceLR_" + outcome,nlevels,-1); |
| 101 | final GeneralIndicator effectI; |
| 102 | if(oldX!=null) { |
| 103 | effectI = new GeneralIndicator(variable,"effectTerm_" + outcome,nlevels,category); |
| 104 | } else { |
| 105 | effectI = null; |
| 106 | } |
| 107 | for(final String level: levels) { |
| 108 | final BLevelRow blevelRow = stat.levelStats.get(level); |
| 109 | if(blevelRow!=null) { |
| 110 | final double sumLevel = blevelRow.totalForLevel + smooth; |
| 111 | { |
| 112 | final double sumLevelOutcome = blevelRow.totalByCorrectCategory[category] + smooth; |
| 113 | final double bayesTerm = (sumAll*sumLevelOutcome)/(sumOutcome*sumLevel); // initial Bayesian utility |
| 114 | logBayesI.levelEncodings.put(level,Math.log(bayesTerm)); |
| 115 | } |
| 116 | { |
| 117 | final double runTerm = (blevelRow.sumRunCorrectCategory[category]+smooth)/(blevelRow.totalByCorrectCategory[category]+smooth); |
| 118 | runI.levelEncodings.put(level,runTerm); |
| 119 | logRunI.levelEncodings.put(level,Math.log(runTerm)); |
| 120 | final double runTermF = (blevelRow.sumRunFixedCategory[category]+smooth)/(blevelRow.totalForLevel+smooth); |
| 121 | runFI.levelEncodings.put(level,runTermF); |
| 122 | logRunFI.levelEncodings.put(level,Math.log(runTermF)); |
| 123 | } |
| 124 | { |
| 125 | final double balanceTerm = (blevelRow.totalByCorrectCategory[category] - blevelRow.sumPFixedCategory[category])/sumLevel; |
| 126 | balanceI.levelEncodings.put(level,balanceTerm); |
| 127 | final double balanceRTerm = (blevelRow.totalByCorrectCategory[category]+smooth)/(blevelRow.sumPFixedCategory[category]+smooth); |
| 128 | balanceLR.levelEncodings.put(level,Math.log(balanceRTerm)); |
| 129 | } |
| 130 | { |
| 131 | final double superBalanceTerm = (blevelRow.totalByCorrectCategory[category] - blevelRow.sumPCorrectCategory[category])/sumLevel; |
| 132 | superBalanceI.levelEncodings.put(level,superBalanceTerm); |
| 133 | final double superBalanceRTerm = (blevelRow.totalByCorrectCategory[category]+smooth)/(blevelRow.sumPCorrectCategory[category]+smooth); |
| 134 | superBalanceLR.levelEncodings.put(level,Math.log(superBalanceRTerm)); |
| 135 | } |
| 136 | } |
| 137 | if(oldX!=null) { |
| 138 | final int base = category*oldAdapter.vdim; |
| 139 | final double cumulativeEffect = stat.oldAdaption.effect(base,oldX,level); // cumulative wisdom to date |