Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks for all your suggestions. I implemented my solution by creating new nested classes for the values of <code>P</code> and <code>F</code> already calculated, then used a <code>HashMap</code> to store the results. The <code>HashMap</code> is then queried for the result before computation takes place; if it is present it just returns the result, if it is not it computes the result and adds it to the <code>HashMap</code>.</p> <p>The final product looks a bit like this:</p> <pre><code>public class ProbabilityCalculator { private NavigableSet&lt;DataPoint&gt; points; private ProbabilityCalculator(NavigableSet&lt;DataPoint&gt; points) { this.points = points; } private static class P { public final DataPoint left; public final Event leftEvent; public final DataPoint right; public final Event rightEvent; public P(DataPoint left, Event leftEvent, DataPoint right, Event rightEvent) { this.left = left; this.leftEvent = leftEvent; this.right = right; this.rightEvent = rightEvent; } public boolean equals(Object o) { if(!(o instanceof P)) return false; P p = (P) o; if(!(this.leftEvent == null ? p.leftEvent == null : this.leftEvent.equals(p.leftEvent))) return false; if(!(this.rightEvent == null ? p.rightEvent == null : this.rightEvent.equals(p.rightEvent))) return false; return this.left.equals(p.left) &amp;&amp; this.right.equals(p.right); } public int hashCode() { int result = 93; result = 31 * result + this.left.hashCode(); result = 31 * result + this.right.hashCode(); result = this.leftEvent != null ? 31 * result + this.leftEvent.hashCode() : 31 * result; result = this.rightEvent != null ? 31 * result + this.rightEvent.hashCode() : 31 * result; return result; } } private Map&lt;P, Double&gt; usedPs = new HashMap&lt;P, Double&gt;(); private static class F { public final DataPoint left; public final Event leftEvent; public final NavigableSet&lt;DataPoint&gt; dataPointsToLeft; public F(DataPoint dataPoint, Event dataPointEvent, NavigableSet&lt;DataPoint&gt; dataPointsToLeft) { this.dataPoint = dataPoint; this.dataPointEvent = dataPointEvent; this.dataPointsToLeft = dataPointsToLeft; } public boolean equals(Object o) { if(!(o instanceof F)) return false; F f = (F) o; return this.dataPoint.equals(f.dataPoint) &amp;&amp; this.dataPointEvent.equals(f.dataPointEvent) &amp;&amp; this.dataPointsToLeft.equals(f.dataPointsToLeft); } public int hashCode() { int result = 7; result = 31 * result + this.dataPoint.hashCode(); result = 31 * result + this.dataPointEvent.hashCode(); result = 31 * result + this.dataPointsToLeft.hashCode(); return result; } } private Map&lt;F, Double&gt; usedFs = new HashMap&lt;F, Double&gt;(); private Double p(DataPoint right, Event rightEvent, DataPoint left, Event leftEvent) { P newP = new P(right, rightEvent, left, leftEvent); if(this.usedPs.containsKey(newP)) return usedPs.get(newP); // do some stuff usedPs.put(newP, result); return result; } private Double f(DataPoint right, Event rightEvent) { NavigableSet&lt;DataPoint&gt; dataPointsToLeft = dataPoints.headSet(right, false); F newF = new F(right, rightEvent, dataPointsToLeft); if(usedFs.containsKey(newF)) return usedFs.get(newF); DataPoint left = points.lower(right); Double result = 0.0; if(left.isLefthandNode()) { result = 0.25 * p(right, rightEvent, left, null); } else if(left.isQ()) { result = p(right, rightEvent, left, left.getQEvent()) * f(left, left.getQEvent(), points); } else { // if M_k for(Event leftEvent : left.getEvents()) result += p(right, rightEvent, left, leftEvent) * f(left, leftEvent, points); } usedFs.put(newF, result) return result; } public Double S() { return f(points.last(), points.last().getRightNodeEvent(), points) } public static probabilityOfQ(DataPoint q, NavigableSet&lt;DataPoint&gt; points) { ProbabilityCalculator pc = new ProbabilityCalculator(points); Double S1 = S(); points.add(q); Double S2 = S(); return S2/S1; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload