Class: Eventsims::Calculate
- Inherits:
-
Object
- Object
- Eventsims::Calculate
- Defined in:
- lib/eventsims/discrete.rb
Instance Method Summary collapse
-
#discreteemp ⇒ Object
Generates a discreteEmp for the given outcome.
- #estmean ⇒ Object
-
#eststddev ⇒ Object
Calculates the estimated variance of the given lists.
-
#estvar ⇒ Object
Calculates the estimated standard deviation of the given lists.
-
#expectval ⇒ Object
Calculates the expectation value given its outcome and cummulative probability.
-
#initialize(*args) ⇒ Calculate
constructor
A new instance of Calculate.
-
#prob ⇒ Object
Calculates the probability of an outcome given its cummulative probability.
- #twoargs ⇒ Object
Constructor Details
#initialize(*args) ⇒ Calculate
Returns a new instance of Calculate.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/eventsims/discrete.rb', line 53 def initialize(*args) ''' Initialising the instances ''' #Checking for valid arguments and value assignment @args = args if args.length == 3 @steps = args[2] elsif args.length == 2 @steps = 1 else raise "Invalid arguments: must be 2 or 3 --> Outcome , Cummulative probability, optional: steps" end @outcome, @cum_prob, @probability = args[0], args[1], [] # Checks in case user hasn't inputted the right information #Error checks for invalid inputs @last_cum = @cum_prob.at(-1) if @outcome.size != @cum_prob.size raise "'prob' arguments must be of same length" elsif @last_cum != 1 raise "last value of 2nd argument must be 1" end args[1].each{|i| raise "cummulative probability must be between 0 and 1" if 0 > i or i > 1 } end |
Instance Method Details
#discreteemp ⇒ Object
Generates a discreteEmp for the given outcome
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/eventsims/discrete.rb', line 100 def discreteemp() '''returns a random number from the outcome list''' #--- generating a random number based on discreteemp emplist = [] def twoargs() count, number = 0, rand() while count < (@cum_prob).size if @cum_prob[count] < number and number <= @cum_prob[count+1] return Eventsims.trimval(@outcome[count+1]) elsif 0 <= number and number <= @cum_prob[0] return Eventsims.trimval(@outcome[0]) end count+=1 end end if @args.length == 2 return Eventsims.trimval(twoargs()) elsif @args.length == 3 @amount, increment = @args[2], 0 if @amount.is_a? (String ) raise "Only integers allowed as third argument" end if @amount == 1 return Eventsims.trimval(twoargs()) else #try: while increment < @amount generated = twoargs() if generated.is_a?(Float) generated = Eventsims.trimval(generated) end emplist << (generated) increment +=1 end return emplist end end end |
#estmean ⇒ Object
191 192 193 |
# File 'lib/eventsims/discrete.rb', line 191 def estmean() return expectval end |
#eststddev ⇒ Object
Calculates the estimated variance of the given lists
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/eventsims/discrete.rb', line 171 def eststddev() '''returns estimated variance of the outcome''' #arguments are: [outcomes], [cummulative probabilities], optional: float(steps)] mean = expectval / @steps increment, occurtimes = 0, 0 while increment < @cum_prob.size occurtimes += @probability[increment] * (@outcome[increment] - mean)**2 increment +=1 end if @args.size == 2 return Eventsims.trimval((occurtimes)**0.5) elsif @args.size == 3 return Eventsims.trimval(occurtimes**0.5 * @steps**0.5) end end |
#estvar ⇒ Object
Calculates the estimated standard deviation of the given lists
196 197 198 199 200 201 |
# File 'lib/eventsims/discrete.rb', line 196 def estvar() ''' Returns the estimated standard deviation of the outcome''' #arguments are: [outcomes], [cummulative probabilities], optional: float(steps)] variance = eststddev**2 return Eventsims.trimval(variance) end |
#expectval ⇒ Object
Calculates the expectation value given its outcome and cummulative probability
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/eventsims/discrete.rb', line 148 def expectval() ''' returns the expectation value of the outcomes''' expectation, increment, probability = 0,0, prob() while increment < @cum_prob.size expectation += probability[increment] * @outcome[increment] increment += 1 end if @args.size == 2 return Eventsims.trimval(expectation) elsif @args.size == 3 expectation *= @steps return Eventsims.trimval(expectation) else raise "arguments must be two or three" end end |
#prob ⇒ Object
Calculates the probability of an outcome given its cummulative probability
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/eventsims/discrete.rb', line 84 def prob() ''' Returns a probability given its cummulative probability ''' # Starting variables y = 1; @probability.push(@cum_prob[0]) while y < @cum_prob.size @probability << (@cum_prob[y] - @cum_prob[y-1]).round(4) y+=1 end return Eventsims.trimval(@probability) end |
#twoargs ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/eventsims/discrete.rb', line 106 def twoargs() count, number = 0, rand() while count < (@cum_prob).size if @cum_prob[count] < number and number <= @cum_prob[count+1] return Eventsims.trimval(@outcome[count+1]) elsif 0 <= number and number <= @cum_prob[0] return Eventsims.trimval(@outcome[0]) end count+=1 end end |