| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: dpdf.h ( POWDER Support Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | * Stores and manipulates a discrete probability function. |
| 14 | * Tracks the probability of a function returning certain numbers. |
| 15 | * Allows you to compose dpdfs in obvious fashions. |
| 16 | */ |
| 17 | |
| 18 | #ifndef __dpdf__ |
| 19 | #define __dpdf__ |
| 20 | |
| 21 | #include <iostream> |
| 22 | using namespace std; |
| 23 | |
| 24 | class DPDF |
| 25 | { |
| 26 | public: |
| 27 | // Constructors... |
| 28 | DPDF(); |
| 29 | DPDF(int constant); // Always returns given constant. |
| 30 | // NOT explicit as we want to be able |
| 31 | // to cast up! |
| 32 | DPDF(int min, int max); // Uniform distribution in [min..max] |
| 33 | |
| 34 | DPDF(const DPDF &dpdf); // Copy constructor |
| 35 | |
| 36 | virtual ~DPDF(); |
| 37 | |
| 38 | void save(ostream &os) const; |
| 39 | void load(istream &is); |
| 40 | |
| 41 | DPDF &operator=(const DPDF &dpdf); // Assignment operator. |
| 42 | |
| 43 | double probability(int num) const; |
| 44 | |
| 45 | double expectedValue() const; |
| 46 | |
| 47 | // Uses the random number generator to pick a random integer inside |
| 48 | // our DPDF using our DPDF. |
| 49 | int evaluate() const; |
| 50 | |
| 51 | // Returns the probability of this function returning something |
| 52 | // strictly greater than the given number. |
| 53 | double probabilityGreaterThan(int num) const; |
| 54 | |
| 55 | // Clips out any values less than or equal to the number. |
| 56 | void applyGivenGreaterThan(int num); |
| 57 | |
| 58 | // Creates the DPDF of the sum of two DPDFs. |
| 59 | DPDF &operator+=(const DPDF &dpdf); |
| 60 | DPDF &operator-=(const DPDF &dpdf); |
| 61 | |
| 62 | // The maximum operator applied to DPDFs |
| 63 | // Rewrites this. |
| 64 | // We force a copy constructor so this can be a parameter. |
| 65 | void max(DPDF a, DPDF b); |
| 66 | |
| 67 | // Ditto, but min. |
| 68 | void min(DPDF a, DPDF b); |
| 69 | |
| 70 | // Adds a constant value to the DPDF - this is somewhat of a no-op |
| 71 | // as it just shifts the DPDF. |
| 72 | // Note that this is equivalent to upcasting the constant to a |
| 73 | // DPDF and then adding, thus meaning we aren't making an ambiguity |
| 74 | // with casting operations |
| 75 | // It is just this is more effecient, and more importantly, the |
| 76 | // DPDF += uses this as an atomic operation. |
| 77 | DPDF &operator+=(int constant); |
| 78 | |
| 79 | // Multiplies this DPDF by the given PROBABILITY. This multiplies |
| 80 | // all entries by PROBABILITY and adds to the 0 entry 1-PROBABIILITY |
| 81 | DPDF &operator*=(double prob); |
| 82 | |
| 83 | // Negates the range of the probability [myMin..myMax] -> [-myMax..-myMin] |
| 84 | // and a similar reversal of the actual values. |
| 85 | void negate(); |
| 86 | |
| 87 | void getQuartile(int &min, int &q1, int &q2, int &q3, int &max) const; |
| 88 | |
| 89 | // Returns the smallest i such that prob(<=i) > percent |
| 90 | int getPercentile(double percent) const; |
| 91 | |
| 92 | |
| 93 | protected: |
| 94 | // Sums our probabilities. This is supposed to be one. |
| 95 | double totalprob() const; |
| 96 | |
| 97 | // Verifies our total is 1.0 |
| 98 | bool isValid(double tol=1e-4f) const; |
| 99 | |
| 100 | // Expands this to encompass the given range, adding zero probability |
| 101 | // as necessary |
| 102 | void expandRange(int min, int max); |
| 103 | |
| 104 | // Expands the range to include the given number. |
| 105 | void expandRange(int min); |
| 106 | |
| 107 | // Expands to include range of other dpdf. |
| 108 | void expandRange(const DPDF &dpdf); |
| 109 | |
| 110 | // The following math operations are not DPDF operations! They are, |
| 111 | // however, useful in building them if you don't care about |
| 112 | // efficiency. |
| 113 | // (DPDF operations have the property that the resulting DPDF |
| 114 | // always sums to 1) |
| 115 | |
| 116 | // Adds each probability into this dpdf in a component wise manner. |
| 117 | // Expands this as necessary. |
| 118 | void addComponentWise(const DPDF &dpdf); |
| 119 | |
| 120 | // Multiplies all entries by the given scale. |
| 121 | void scaleComponentWise(double scale); |
| 122 | |
| 123 | // Zeros the system |
| 124 | void zero(); |
| 125 | |
| 126 | private: |
| 127 | int myMin, myMax; // Min & max possible values. |
| 128 | double *myProb; // Probability for each value. |
| 129 | }; |
| 130 | |
| 131 | #endif |
| 132 |