1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.probfunction.math.impl; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collections; |
8 | import java.util.HashMap; |
9 | import java.util.HashSet; |
10 | import java.util.Iterator; |
11 | import java.util.List; |
12 | |
13 | import de.uka.ipd.sdq.probfunction.math.IBoxedPDF; |
14 | import de.uka.ipd.sdq.probfunction.math.IContinuousSample; |
15 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
16 | import de.uka.ipd.sdq.probfunction.math.IRandomGenerator; |
17 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
18 | import de.uka.ipd.sdq.probfunction.math.IUnit; |
19 | import de.uka.ipd.sdq.probfunction.math.exception.DomainNotNumbersException; |
20 | import de.uka.ipd.sdq.probfunction.math.exception.DoubleSampleException; |
21 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInFrequencyDomainException; |
22 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
23 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionsInDifferenDomainsException; |
24 | import de.uka.ipd.sdq.probfunction.math.exception.IncompatibleUnitsException; |
25 | import de.uka.ipd.sdq.probfunction.math.exception.InvalidSampleValueException; |
26 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilitySumNotOneException; |
27 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNameNotSetException; |
28 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNotSetException; |
29 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
30 | import de.uka.ipd.sdq.probfunction.math.exception.UnorderedDomainException; |
31 | import de.uka.ipd.sdq.probfunction.math.util.Line; |
32 | import de.uka.ipd.sdq.probfunction.math.util.MathTools; |
33 | |
34 | /** |
35 | * @author Ihssane |
36 | * |
37 | */ |
38 | public class BoxedPDFImpl extends ProbabilityDensityFunctionImpl |
39 | implements |
40 | IBoxedPDF { |
41 | |
42 | private List<IContinuousSample> samples; |
43 | |
44 | protected BoxedPDFImpl(IUnit unit, IRandomGenerator generator) { |
45 | super(unit, false); |
46 | this.randomGenerator = generator; |
47 | samples = new ArrayList<IContinuousSample>(); |
48 | } |
49 | |
50 | public IProbabilityDensityFunction add(IProbabilityDensityFunction pdf) |
51 | throws FunctionsInDifferenDomainsException, |
52 | UnknownPDFTypeException, IncompatibleUnitsException { |
53 | ISamplePDF sPDF = pfFactory.transformToSamplePDF(this); |
54 | return sPDF.add(pdf); |
55 | } |
56 | |
57 | public IProbabilityDensityFunction mult(IProbabilityDensityFunction pdf) |
58 | throws FunctionsInDifferenDomainsException, |
59 | UnknownPDFTypeException, IncompatibleUnitsException { |
60 | ISamplePDF sPDF = pfFactory.transformToSamplePDF(this); |
61 | return sPDF.mult(pdf); |
62 | } |
63 | |
64 | public IProbabilityDensityFunction scale(double scalar) { |
65 | List<IContinuousSample> list = new ArrayList<IContinuousSample>(); |
66 | for (IContinuousSample s : this.samples) |
67 | list.add(pfFactory.createContinuousSample(s.getValue(), s |
68 | .getProbability() |
69 | * scalar)); |
70 | |
71 | IBoxedPDF result = null; |
72 | try { |
73 | result = pfFactory.createBoxedPDF(list, this.getUnit()); |
74 | } catch (DoubleSampleException e) { |
75 | e.printStackTrace(); |
76 | throw new RuntimeException(e); // should never happen |
77 | } |
78 | return result; |
79 | } |
80 | |
81 | public List<IContinuousSample> getSamples() { |
82 | return Collections.unmodifiableList(samples); |
83 | } |
84 | |
85 | public List<Double> getValues() { |
86 | List<Double> values = new ArrayList<Double>(); |
87 | for (IContinuousSample cs : samples) |
88 | values.add(cs.getValue()); |
89 | return values; |
90 | } |
91 | |
92 | public List<Double> getProbabilities() { |
93 | List<Double> probs = new ArrayList<Double>(); |
94 | for (IContinuousSample cs : samples) |
95 | probs.add(cs.getProbability()); |
96 | return probs; |
97 | } |
98 | |
99 | public void setSamples(List<IContinuousSample> samples) |
100 | throws DoubleSampleException { |
101 | if (containsDuplicateSamples(samples)) |
102 | throw new DoubleSampleException("found duplicate sample values (not probabilities)"); |
103 | |
104 | Collections.sort(samples, MathTools.getContinuousSampleComparator()); |
105 | this.samples = samples; |
106 | initDrawSampleDataStructures(); |
107 | } |
108 | |
109 | public IProbabilityDensityFunction div(IProbabilityDensityFunction pdf) |
110 | throws FunctionsInDifferenDomainsException, |
111 | UnknownPDFTypeException, IncompatibleUnitsException { |
112 | ISamplePDF sPDF = pfFactory.transformToSamplePDF(this); |
113 | return sPDF.div(pdf); |
114 | } |
115 | |
116 | private void initDrawSampleDataStructures() { |
117 | initPartedIntervals(); |
118 | initPartedLines(); |
119 | } |
120 | |
121 | private List<Double> partedIntervals = null; |
122 | private void initPartedIntervals() { |
123 | // StB: getValues() ---> getProbabilities gefixt |
124 | partedIntervals = MathTools |
125 | .computeCumulativeProbabilities(getProbabilities()); |
126 | } |
127 | |
128 | private HashMap<Double, Line> lines = null; |
129 | private void initPartedLines() { |
130 | lines = MathTools.computeLines(samples, partedIntervals); |
131 | } |
132 | |
133 | public double drawSample() { |
134 | double random = randomGenerator.random(); |
135 | for (Double currentInterval : partedIntervals) |
136 | if (random < currentInterval) { |
137 | return lines.get(currentInterval).getX(random); |
138 | } |
139 | throw new RuntimeException( |
140 | "No interval found for probability. This should never happen!"); |
141 | } |
142 | |
143 | public IProbabilityDensityFunction getFourierTransform() |
144 | throws FunctionNotInTimeDomainException { |
145 | if (!isInTimeDomain()) |
146 | throw new FunctionNotInTimeDomainException(); |
147 | |
148 | ISamplePDF sPDF = null; |
149 | try { |
150 | sPDF = pfFactory.transformToSamplePDF(this); |
151 | } catch (UnknownPDFTypeException e) { |
152 | // should never happen... |
153 | e.printStackTrace(); |
154 | throw new RuntimeException(e); |
155 | } |
156 | return sPDF.getFourierTransform(); |
157 | } |
158 | |
159 | public IProbabilityDensityFunction getInverseFourierTransform() |
160 | throws FunctionNotInFrequencyDomainException { |
161 | if (isInTimeDomain()) |
162 | throw new FunctionNotInFrequencyDomainException(); |
163 | |
164 | ISamplePDF sPDF = null; |
165 | try { |
166 | sPDF = pfFactory.transformToSamplePDF(this); |
167 | } catch (UnknownPDFTypeException e) { |
168 | // should never happen... |
169 | e.printStackTrace(); |
170 | throw new RuntimeException(e); |
171 | } |
172 | return sPDF.getInverseFourierTransform(); |
173 | } |
174 | |
175 | public double getLowerDomainBorder() { |
176 | return 0; |
177 | } |
178 | |
179 | public IProbabilityDensityFunction sub(IProbabilityDensityFunction pdf) |
180 | throws FunctionsInDifferenDomainsException, |
181 | UnknownPDFTypeException, IncompatibleUnitsException { |
182 | ISamplePDF sPDF = pfFactory.transformToSamplePDF(this); |
183 | return sPDF.sub(pdf); |
184 | } |
185 | |
186 | /** |
187 | * Get the mean value of the BoxedPDF |
188 | * @param list |
189 | * @return the mean value |
190 | */ |
191 | public double getArithmeticMeanValue() throws DomainNotNumbersException { |
192 | List<IContinuousSample> list = this.getSamples(); |
193 | double mean = 0; |
194 | double previousValue = 0; |
195 | |
196 | for (IContinuousSample continuousSample : list) { |
197 | double number = (continuousSample.getValue() + previousValue)/2; |
198 | mean += number * continuousSample.getProbability(); |
199 | previousValue = continuousSample.getValue(); |
200 | } |
201 | |
202 | return mean; |
203 | } |
204 | |
205 | public Object getMedian() throws UnorderedDomainException { |
206 | if (!hasOrderedDomain()) |
207 | throw new UnorderedDomainException(); |
208 | |
209 | if (samples.size() % 2 != 0) { |
210 | int i = (int) Math.floor(samples.size() / 2.0); |
211 | return samples.get(i).getValue(); |
212 | } else { |
213 | int i1 = (int) Math.round(samples.size() / 2.0); |
214 | return (samples.get(i1).getValue() + samples.get(i1 - 1).getValue()) / 2; |
215 | } |
216 | } |
217 | |
218 | public Object getPercentile(int p) throws IndexOutOfBoundsException, |
219 | UnorderedDomainException { |
220 | if (p < 0 || p > 100) |
221 | throw new IndexOutOfBoundsException(); |
222 | |
223 | int rank = (int) Math.round((p * (samples.size() - 1.0)) / 100.0); |
224 | return samples.get(rank).getProbability(); |
225 | } |
226 | |
227 | @Override |
228 | public boolean isInFrequencyDomain() { |
229 | return false; |
230 | } |
231 | |
232 | @Override |
233 | public boolean isInTimeDomain() { |
234 | return true; |
235 | } |
236 | |
237 | public double getProbabilitySum() { |
238 | double sum = 0; |
239 | for (IContinuousSample sample : samples) { |
240 | sum += sample.getProbability(); |
241 | } |
242 | return sum; |
243 | } |
244 | |
245 | private boolean containsDuplicateSamples(List<IContinuousSample> samples) { |
246 | HashSet<Double> set = new HashSet<Double>(); |
247 | for (IContinuousSample s : samples) |
248 | set.add(s.getValue()); |
249 | |
250 | return set.size() != samples.size(); |
251 | } |
252 | |
253 | public void checkConstrains() throws InvalidSampleValueException, |
254 | UnitNameNotSetException, UnitNotSetException, |
255 | ProbabilitySumNotOneException { |
256 | if (!MathTools.equalsDouble(getProbabilitySum(), 1.0)) |
257 | throw new ProbabilitySumNotOneException(); |
258 | |
259 | // TODO: Refactor to new UNIT framework |
260 | // if (getUnit() == null) |
261 | // throw new UnitNotSetException(); |
262 | // if (getUnit().getUnitName() == null) |
263 | // throw new UnitNameNotSetException(); |
264 | |
265 | double value = 0; |
266 | for (IContinuousSample s : samples) { |
267 | if (s == null || s.getValue() < 0.0 || s.getProbability() < 0.0 |
268 | || s.getProbability() > 1.0) |
269 | throw new InvalidSampleValueException(); |
270 | //Samples must be ordered by their value. |
271 | if (s.getValue() < value){ |
272 | throw new InvalidSampleValueException(); |
273 | } |
274 | value = s.getValue(); |
275 | } |
276 | } |
277 | public IProbabilityDensityFunction getCumulativeFunction() { |
278 | List<Double> cumulativeProbabilities = MathTools |
279 | .computeCumulativeProbabilities(getProbabilities()); |
280 | List<IContinuousSample> cdfSamples = new ArrayList<IContinuousSample>(); |
281 | |
282 | for (int i = 0; i < cumulativeProbabilities.size(); i++) { |
283 | double value = samples.get(i).getValue(); |
284 | double cumulativeProb = cumulativeProbabilities.get(i); |
285 | IContinuousSample sample = pfFactory.createContinuousSample(value, |
286 | cumulativeProb); |
287 | cdfSamples.add(sample); |
288 | } |
289 | |
290 | IBoxedPDF bpdf = null; |
291 | try { |
292 | bpdf = pfFactory.createBoxedPDF(cdfSamples, this.getUnit()); |
293 | } catch (DoubleSampleException e) { |
294 | // should never happen |
295 | e.printStackTrace(); |
296 | throw new RuntimeException(e); |
297 | } |
298 | return bpdf; |
299 | } |
300 | |
301 | /** |
302 | * |
303 | */ |
304 | @Override |
305 | public boolean equals(Object obj) { |
306 | if (obj instanceof IBoxedPDF) { |
307 | IBoxedPDF pdf = (IBoxedPDF) obj; |
308 | |
309 | if (pdf.getSamples().size() != samples.size()) |
310 | return false; |
311 | |
312 | Iterator<IContinuousSample> iter = pdf.getSamples().iterator(); |
313 | for (IContinuousSample s : samples) |
314 | if (!s.equals(iter.next())) |
315 | return false; |
316 | return true; |
317 | } |
318 | return false; |
319 | } |
320 | |
321 | public double probabilisticEquals(IProbabilityDensityFunction pdf) { |
322 | // TODO Auto-generated method stub |
323 | return 0; |
324 | } |
325 | |
326 | public double greaterThan(IProbabilityDensityFunction pdf) { |
327 | // TODO Auto-generated method stub |
328 | return 0; |
329 | } |
330 | |
331 | public double lessThan(IProbabilityDensityFunction pdf) { |
332 | // TODO Auto-generated method stub |
333 | return 0; |
334 | } |
335 | |
336 | /** |
337 | * {@inheritDoc} |
338 | * |
339 | * Scalar must not be 0. If it is 0, a RuntimeException is thrown. |
340 | * @param scalar must not be 0 |
341 | */ |
342 | public IProbabilityDensityFunction stretchDomain(double scalar) { |
343 | |
344 | List<IContinuousSample> newSamples = new ArrayList<IContinuousSample>(); |
345 | if (scalar != 0){ |
346 | for (IContinuousSample oldSample : samples) { |
347 | newSamples.add(pfFactory.createContinuousSample(oldSample |
348 | .getValue() |
349 | * scalar, oldSample.getProbability())); |
350 | } |
351 | } else { |
352 | //TODO: Is there a better way to handle a factor 0 for stretching the domain? Maybe creating a static 0-PDF? |
353 | //TODO: Introduce proper error handling in whole probfunction package. |
354 | throw new RuntimeException("Error: Stretching the domain of PDF "+this.toString()+" with factor 0 is undefined. Please change your models so that no PDf is multiplied by 0."); |
355 | } |
356 | |
357 | IBoxedPDF result = null; |
358 | try { |
359 | result = pfFactory.createBoxedPDF(newSamples, this.getUnit()); |
360 | } catch (DoubleSampleException e) { |
361 | e.printStackTrace(); |
362 | throw new RuntimeException(e); |
363 | } |
364 | return result; |
365 | } |
366 | |
367 | /** |
368 | * {@inheritDoc} |
369 | * |
370 | * Returns itself if scalar == 0. |
371 | */ |
372 | public IProbabilityDensityFunction shiftDomain(double scalar) |
373 | throws DomainNotNumbersException { |
374 | // Achtung: does not work with negative scalars! |
375 | |
376 | if (scalar == 0){ |
377 | return this; |
378 | } |
379 | |
380 | List<IContinuousSample> newSamples = new ArrayList<IContinuousSample>(); |
381 | if (samples.get(0).getProbability() != 0.0){ |
382 | newSamples.add(pfFactory.createContinuousSample(scalar, 0.0)); |
383 | } |
384 | |
385 | for (IContinuousSample oldSample: samples){ |
386 | newSamples.add(pfFactory.createContinuousSample(oldSample.getValue()+scalar, oldSample.getProbability())); |
387 | } |
388 | |
389 | IBoxedPDF result = null; |
390 | try { |
391 | result = pfFactory.createBoxedPDF(newSamples, this.getUnit()); |
392 | } catch (DoubleSampleException e) { |
393 | e.printStackTrace(); |
394 | throw new RuntimeException(e); // should never happen |
395 | } |
396 | return result; |
397 | } |
398 | |
399 | @Override |
400 | public String toString() { |
401 | String result = ""; |
402 | result += "samples: "; |
403 | boolean isFirst = true; |
404 | for (IContinuousSample ics : samples){ |
405 | if (isFirst) { |
406 | isFirst = false; |
407 | } else { |
408 | result += ", "; |
409 | } |
410 | result += "(" + ics.getValue() + ", " + ics.getProbability() + ")"; |
411 | } |
412 | |
413 | return result; |
414 | } |
415 | |
416 | } |