private Map<T, Double> normalize() { if (isNormalised) return normalised; Map<T, Double> temp = createMapDataStructure(); double sum = 0; for (Entry<T, Double> entry : map.entrySet()) { Double p = entry.getValue(); if (p != null) { sum += p; } } for (Entry<T, Double> entry : temp.entrySet()) { T t = entry.getKey(); Double p = entry.getValue(); if (p != null) { temp.put(t, p / sum); } } normalised = temp; isNormalised = true; return temp; }
public String toString() { return getAll().toString(); }
/** * Returns the probabilities associated with all labels * * @return the HashMap of labels and their probabilities */ public Map<T, Double> getAll() { return normalize(); }
/** * Returns the log probability associated with a label * * @param t the label whose log probability needs to be returned * @return the log probability associated with the label */ public Double getLog(T t) { return Math.log(get(t)); }
/** * Assigns a log probability to a label, discarding any previously assigned probability. * * @param t the label to which the log probability is being assigned * @param probability the log probability to assign */ public void setLog(T t, double probability) { set(t, Math.exp(probability)); }
/** * Returns the probability associated with a label * * @param t the label whose probability needs to be returned * @return the probability associated with the label */ public Double get(T t) { Double d = normalize().get(t); if (d == null) return 0.0; return d; }
/** * Returns the log probability associated with a label * * @param t the label whose log probability needs to be returned * @return the log probability associated with the label */ public Double getLog(T t) { return Math.log(get(t)); }
/** * Assigns a log probability to a label, discarding any previously assigned probability. * * @param t the label to which the log probability is being assigned * @param probability the log probability to assign */ public void setLog(T t, double probability) { set(t, Math.exp(probability)); }
/** * Returns the probabilities associated with all labels * * @return the HashMap of labels and their probabilities */ public Map<T, Double> getAll() { return normalize(); }
/** * Returns the log probability associated with a label * * @param t the label whose log probability needs to be returned * @return the log probability associated with the label */ public Double getLog(T t) { return Math.log(get(t)); }
/** * Assigns a log probability to a label, discarding any previously assigned probability. * * @param t the label to which the log probability is being assigned * @param probability the log probability to assign */ public void setLog(T t, double probability) { set(t, Math.exp(probability)); }
public String toString() { return getAll().toString(); }
private Map<T, Double> normalize() { if (isNormalised) return normalised; Map<T, Double> temp = createMapDataStructure(); double sum = 0; for (Entry<T, Double> entry : map.entrySet()) { Double p = entry.getValue(); if (p != null) { sum += p; } } for (Entry<T, Double> entry : temp.entrySet()) { T t = entry.getKey(); Double p = entry.getValue(); if (p != null) { temp.put(t, p / sum); } } normalised = temp; isNormalised = true; return temp; }
/** * Returns the probabilities associated with all labels * * @return the HashMap of labels and their probabilities */ public Map<T, Double> getAll() { return normalize(); }