Skip to main content

A simple Epsilon-Greedy multi-armed bandit implementation

Project description

Epsilon-Greedy Multi-Armed Bandit

A simple Python implementation of the Epsilon-Greedy algorithm for the multi-armed bandit problem. This package provides classes for creating bandit environments and an agent that learns to select the best action. It also includes a command-line interface for running simulations.

Features

  • EpsilonGreedyAgent: A configurable agent with parameters for exploration rate (epsilon), epsilon decay, and optimistic initial values.
  • MultiArmedBandit Environment: Supports arms with different reward distributions:
    • Bernoulli: For modeling binary outcomes (e.g., click/no-click).
    • Normal (Gaussian): For modeling continuous outcomes.
  • Simulation Runner: A function to run experiments and collect results like average reward and optimal action rate.
  • Command-Line Interface: A convenient CLI for running simulations without writing Python code.

Project Structure

.
├── EpsilonGreedy/
│   ├── __init__.py
│   └── main.py
└── README.md
  • EpsilonGreedy/main.py: Contains all the core logic for the Arm, MultiArmedBandit, and EpsilonGreedyAgent classes, as well as the CLI entry point.
  • EpsilonGreedy/__init__.py: Makes the directory a Python package and exposes the main classes for easy import.

Usage

You can use this project either as a Python library or as a command-line tool.

As a Library

Import the necessary classes from the EpsilonGreedy package to build and run your own bandit simulations.

from EpsilonGreedy import Arm, MultiArmedBandit, EpsilonGreedyAgent, run_bandit

# 1. Define the arms for the bandit problem
arms = [
    Arm(name="arm_1", distribution="bernoulli", p=0.1),
    Arm(name="arm_2", distribution="bernoulli", p=0.5),
    Arm(name="arm_3", distribution="bernoulli", p=0.9),
]

# 2. Create the bandit environment
bandit_env = MultiArmedBandit(arms=arms, seed=42)

# 3. Create the Epsilon-Greedy agent
agent = EpsilonGreedyAgent(
    n_actions=bandit_env.k,
    epsilon=0.1,
    seed=123
)

# 4. Run the simulation
results = run_bandit(env=bandit_env, agent=agent, steps=1000)

# 5. Print the results
print(f"Average reward: {results['avg_reward']:.4f}")
print(f"Optimal action selection rate: {results['optimal_action_rate']:.4f}")
print(f"Final Q-value estimates: {results['final_estimates']}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

epsilongreedy-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

epsilongreedy-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file epsilongreedy-0.1.1.tar.gz.

File metadata

  • Download URL: epsilongreedy-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for epsilongreedy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3993e49cd1b2a32d8c34179cff1b5fdab4091dbd87e2ab73e543c691936f5713
MD5 09f64ab09f0e863cb0b04135adf23bc4
BLAKE2b-256 0bf1fb877ec800aa64cbf271a2eff21db54882eba6afa257a1549e7dac9209cd

See more details on using hashes here.

File details

Details for the file epsilongreedy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: epsilongreedy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for epsilongreedy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d2bf216553179b8fc3313fc9c5314d4ebd86dc2ea8694d24041c05bf511bf5eb
MD5 fe1fc780734093bc30b2db043d9e8747
BLAKE2b-256 daf227ff9b68e6264b535ac0bf8161881921673b2fdbc732dc4313e1e3af5e93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page