Skip to main content

baccarat

Monte Carlo simulation framework in Python, with a focus on ease of use and flexibility.

Motivation

I wanted to create a framework that would allow users to easily create Monte Carlo simulations without having to worry about boilerplate or optimization. The goal of baccarat is to provide a simple and intuitive interface for building simulations

Getting Started

Installation

To get started with baccarat, you can install it using pip (or uv or poetry or your package manager of choice):

pip install baccarat

Usage

A simple example using baccarat to approximate pi:

from baccarat import Simulator, UniformParam, StaticParam

class PiSimulator(Simulator):
    radius = 1
    # These parameters are descriptors, used to generate random values when accessed
    x = UniformParam(-radius, radius)
    y = UniformParam(-radius, radius)
    # Strictly speaking, this could be a constant, but we use a Param for consistency
    r = StaticParam(radius)  
    
    def simulation(self):
        # Use assignments since attribute access will generate the random value
        x, y, r = self.x, self.y, self.r
        # Check to see if the random point is inside the circle
        return x**2 + y**2 <= r**2
    
    def compile_results(self):
        return 4 * len([res for res in self.results if res]) / len(self.results)

approx_pi = PiSimulator(1_000_000).run()

Notes:

  1. All user simulations start with a class that inherits from Simulator and implements the simulation method.
    1. The simulation method defines the vectorized logic for the simulation. It should return a NumPy array of results.
    2. Optionally, the compile_results method can be implemented to process the results array once the simulation is complete.
  2. Parameters are defined as class attributes.
    1. Parameters can be either static or dynamic. Static parameters are fixed values, while dynamic parameters are generated randomly according to a distribution.
    2. Distributions are specified using the concrete implementations of the Param class, such as UniformParam and GaussianParam.
    3. When accessed, parameters return NumPy arrays containing the random values, enabling vectorized operations.
    4. Custom distributions can be created by subclassing the Param class and implementing the generate method to return a NumPy array.
  3. The number of samples used in the simulation are specified when creating an instance of the Simulator subclass.

Examples

For more examples, see the examples folder.

About the name

Baccarat is a popular card game in casinos around the world. I first learned about baccarat when reading Ian Fleming's Casino Royale, the first novel in the James Bond series.

Release files for baccarat_sim 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for baccarat_sim 0.1.0
File Size Uploaded
baccarat_sim-0.1.0.tar.gz 36.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for baccarat_sim 0.1.0
File Interpreter ABI Platform
baccarat_sim-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 53.3 kB

Release files / baccarat_sim-0.1.0.tar.gz

Download URL baccarat_sim-0.1.0.tar.gz
Size 36.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2525d2716d458fd9f097f5353cc072fdb1eeb1363751fb01fcb31f66e460f9eb
BLAKE2b-256 checksum
How to use checksums
e27c136e0cb600850f71fe3c0880237ed97723570bd38783f1b202bc1f71e59c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.2

Release files / baccarat_sim-0.1.0-py3-none-any.whl

Download URL baccarat_sim-0.1.0-py3-none-any.whl
Size 17.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6824f8a782dc196aacb0a41f29d601a524b32f450a0422783b0b5dd9c06d9fee
BLAKE2b-256 checksum
How to use checksums
fd8e6ac52d2b79de310f71c5e248e2e3d32763fa627458abc80e9612e7687b31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page