Discrete

The Discrete module has a Calculate class, trimval and trimlist method

Calculate

Calculate has six methods for calculating/generating probability, estimated variance, estimated mean, estimated standard deviation, expectation value and discreteEmp.

Discrete’s “Calculate class” takes two arguments: outcome and cummulative probability or a third optional argument which is amount of times to be generated (steps).

Methods

Calculate methods are:

  • prob() —-> To calculate the probability based on the given outcome list(second argument of the Calculate instance).
  • discreteemp() —-> To generate a random outcome depending on its probability of occurrence.
  • expectval() —-> To generate an expectation value i.e. the mean of the outcome depending on its probability
  • estmean() —-> Same as expectval() because they always give the same output.
  • estvar() —-> To calculate the estimated variance of the list data
  • eststddev —-> To calculate the estimated standard deviation of the list data

Importing the Calculate class

You can import the Calculate class in three ways:

import eventsim.discrete 
from eventsim.discrete import Calculate
from eventsim.discrete import *

and you can then respectively use it like this:

sample = eventsim.discrete.Calculate(a, b, 10)
sample = Calculate(a, b)

Example 1

Two arguments Usage: Calculate(outcome list, cummulative probability list)

from eventsim.discrete import Calculate

a = [-1,0,3,4]
b = [0.1, 0.4, 0.7, 1]

sample = Calculate(a, b)

print("Probability:",sample.prob())
print("DiscreteEmp:",sample.discreteemp())
print("Expectation value:",sample.expectval())
print("Estimated Mean:",sample.estmean())
print("Estimated Variance:",sample.estvar())
print("Estimated Standard deviation:",sample.eststddev())

Result

Probability: [0.1, 0.3, 0.3, 0.3]
DiscreteEmp: 3
Expectation value: 2.0
Estimated Mean: 2.0
Estimated Variance: 3.6
Estimated Standard deviation: 1.8974

Example 2

three arguments

Usage: Calculate(outcome list, cummulative probability list, “Optional: amount/steps”)

import eventsim.discrete 

a = [-1,0,3,4]
b = [0.1, 0.4, 0.7, 1]

sample = eventsim.discrete.Calculate(a, b, 10)

print("Probability:",sample.prob())
print("DiscreteEmp:",sample.discreteemp())
print("Expectation value:",sample.expectval())
print("Estimated Mean:",sample.estmean())
print("Estimated Variance:",sample.estvar())
print("Estimated Standard deviation:",sample.eststddev())

Result

Probability: [0.1, 0.3, 0.3, 0.3]
DiscreteEmp: [3, 0, 4, 4, 4, 4, 3, 0, 0, 0]
Expectation value: 20.0
Estimated Mean: 20.0
Estimated Variance: 36.0
Estimated Standard deviation: 6.0

Note

argument one of the Calculate class should be the outcome list while argument two should be the cummulative probability list. Argument three (steps) is optional and only needed if any of the data is to be calculated after a certain number of times or to generate a list of discreteEmp values.

If no optional argument is given (same as if an optional argument is set to 1), discreteemp will generate only one outcome but if more the optional argument is more than one e.g. 5, then discreteemp will generate a list containing several generated outcomes (5 items in a list in this case).

trimval and trimlist

trimval is a discrete module method that takes in one argument, (numbers or lists and strips it of leading zeros and round up to 4 decimal places. If more than one argument is given, none of the other arguments after the first would remain unchanged but no error would be raised.

trimlist on the other takes as many arguments as possibe and does the same thing trimval does but it’s very useful if there is a nested list in the list of arguments. It is also very useful than trimval because it alter’s all the arguments even to the first nested list of any of the arguments.

Note

It originally wasn’t meant to be included in the module but because I used it a lot, I decided to include it in case.

They both help to display lists and numbers in a better and easier way to read rather than have values with many leading decimal numbers in a list keeping it concise.

Importing the methods

They can be imported similar to the Calculate class. in any of the three ways.

import eventsim.discrete 
from eventsim.discrete import trimval
from eventsim.discrete import trimlist
from eventsim.discrete import *

and you can then respectively use it like this:

sample = [12.00647886, 6543.23456, 9]

print(eventsim.discrete.trimval(sample))
print(trimlist(sample))

Example 1 (Using trimval)

from eventsim.discrete import *

sample = [12.00647886, 6543.23456, 9]
print(trimval(sample))

sample = [12.00647886, 6543.23456, 9], 12.87532
print(trimval(sample))

Result

[12.0065, 6543.2346, 9]
([12.00647886, 6543.23456, 9], 12.87532)

Note

The last highlighted result was not completely changed because trimval only works on the first argument. Use trimlist if this is the case.

Example 2 (Using trimlist)

from eventsim.discrete import *

sample = [1, 12.063506, ["cat", 56.000001, 2, [7, 9.046376] ] ]
print(trimlist(sample))

sample = [1, 12.063506, ["cat", 56.000001, 2, 9.03479] ]
print(trimlist(sample))

Result

[1, 12.064, ['cat', 56.0, 2, [7, 9.046376]]]
[1, 12.064, ['cat', 56.0, 2, 9.035]]

Note

trimlist can only trim values up to and including one nested list and does not affect lists nested two levels within each other