Final Project (Monte Carlo Simulator)
The Monte Carlo Package
is a Python package with three classes: Die
, Game
, and Analyzer
. Class descriptions and demonstration code are included below to demo how to install and import the package and how to use the three classes.
A die class with N sides/faces and W weights that can be rolled to select a side/face.
- None
Initializer that takes an array or list of faces
of any length, initializes weights to 1.
faces
: NumPy array or a list
A method that changes a side/face weight and checks whether side/face and weight are valid.
face
: str or intnew_weight
: float
ValueError
ifnew_weight
is not a float or convertible to a float. This error will also appear if theface
is not included in the die.
A method that rolls the die one or more times but defaults to 1.
rolls
: int
rolled_outcome
: a list of the rolled outcomes.
A method that shows the dataframe of faces and weights.
- None
- a Pandas df of faces and weights of the die.
A game class that consists of rolling of one or more dice of the same kind one or more times.
- None
Initializer that takes a single parameter — a list of already instantiated similar Die objects.
die_list
: list
A method that takes a parameter to specify how many times the dice should be rolled.
rolls
: int
A method that shows the user the results of the most recent play. Takes a parameter to return the dataframe in narrow or wide form. This parameter defaults to wide form. This parameter raises an exception if the user passes an invalid option. The narrow form of the dataframe will have a two-column index with:
- the roll number
- the die number
- a column for the face rolled
The wide form of the dataframe will be a single-column index with:
- the roll number
- each die number as a column
form
: string
ValueError
ifform
is not narrow or wide.
A Pandas df with the most recent result from play
, including:
- the die number
- the roll number
- the face rolled for each respective roll
An analyzer class that takes the results of a single game and computes various descriptive statistical properties about it. These properties and results are available as attributes of an Analyzer object.
- None
Initializer that takes a game object as its input parameter. At initialization time, it also infers the data type of the die faces.
game
: Game object.
A method that computes how many times a given face is rolled in each event. Stores the results as a dataframe in a public attribute. The dataframe has an index of the roll number and face values as columns (i.e., in wide form).
- None
A Pandas df with the counts of each face value per roll. Includes indexes for the roll number and columns showing die face values.
A method that computes how many times the game resulted in all faces being identical and returns an integer for the number of times to the user. Stores the results as a dataframe of jackpot results in a public attribute.
- None
A Pandas df with the rows for when a jackpot occurred, including:
- the roll number
- the die number
- the respective face rolled
A method that computes the distinct combinations of faces rolled and their counts. Combinations are sorted and saved as a multicolumned index. Stores the results as a dataframe in a public attribute.
- None
A Pandas df with combinations where the face values are multi-indexes and columns show the combination count.
!pip install .
from montecarlo import Die, Game, Analyzer
- Create the die object called
myDie
:
myDie = Die(['face1', 'face2', 'face3'])
- Change the weight of
'face1'
:
myDie.change_weight('face1', 3)
- Show the faces and weights of
myDie
:
myDie.show_die()
- Roll
myDie
five times:
myDie.rolls(5)
- Create the game object
myGame
:
myGame = Game([Die([1,2,3]), Die([1,2,3]), Die([1,2,3])])
- Play the game using
myGame
, input value in therolls
parameter to specify number of times each die is to be rolled:
myGame.play(3)
- Show the results of
play
, input either 'wide' or 'narrow' for theform
parameter to specify the format of the df of results to be shown (defaults to wide).
myGame.show_play()
- Create the analyzer object
myAnalyzer
usingmyGame
:
myAnalyzer = Analyzer(myGame)
- Return a df with counts for the occurrence of each face value per roll:
myAnalyzer.face_count()
- Find out how many times a jackpot occurred:
myAnalyzer.jackpot()
- Find out the combinations of faces rolled and their counts:
myAnalyzer.combo()
- final_project
- montecarlo.egg-info
- PKG-INFO
- SOURCES.txt
- init
- dependency_links.txt
- requires.txt
- top_level.txt
- montecarlo
- init.py
- montecarlo.py
- pycache
- init.cpython-39.pyc
- montecarlo.cpython-39.pyc
- montecarlo_demo.ipynb
- montecarlo_tests.py
- setup.py
- test_results.txt
- montecarlo.egg-info
- LICENSE
- README.md