Skip to content

Project 1

Concurrent Monte Carlo simulation in Go

I am asking you to build a concurrent Monte Carlo simulation engine in Go. Monte Carlo methods use random sampling to estimate probabilities, expectations, and risk measures. They are widely used in data science and they are applied in finance and engineering.

Your engine will launch multiple goroutines, each running independent simulation trials. Results will be sent through channels, aggregated, and used to construct estimates of expected outcomes and confidence intervals. A critical question is how does concurrency - tested in Go here - enable faster solutions and/or more accurate solutions at a fixed time budget.

What you'll learn

  • Apply goroutines and channels to parallelize simulations.
  • Understand synchronization and aggregation of concurrent results.
  • Implement statistical concepts such as mean, variance, and confidence intervals.
  • Measure performance of concurrent programs.

Why Monte Carlo?

Monte Carlo (MC) simulation is often used for risk analysis. Example applications include:

  • estimating the expected return of a financial portfolio
  • modeling failure probabilities of engineering systems
  • evaluating the outcome of stochastic processes (e.g., random walks, epidemics)

Basic Monte Carlo workflow

  1. run many independent trials of a random experiment
  2. record the outcome for each trial
  3. aggregate results (average, variance, quantiles)
  4. calculate statistics to construct, for instance, 95% confidence intervals around the estimate

Leveraging concurrency

Because trials are independent, Monte Carlo simulations are embarrassingly parallel. This is ideal for implementing and testing concurrency like provided in Go.


General Instructions

All submitted work must be stored in your team's GitLab repository. Below it will be explained how to do that. For the repository it is important that:

  1. Create a new repository on GitLab at code.vt.edu:

    • one project = one GitLab repository → each project gets its own GitLab repository
    • all team members of your project must be members of the repository
    • your repository must be private
    • your repository must be named exactly as specified: do not change the name or the upper-case/lower-case letters
      cmda3634-pr1
      
    • create commits frequently, at least one commit per task is recommended
    • push your work frequently (this also is your backup if your laptop breaks)
    • your repository must be up-to-date before the deadline
    • you must double-check your repository in the web browser to make sure all your work is uploaded to the code.vt.edu server
  2. Add the instructor and TA to your repository as a member with Developer access. In the web browser with your new repository open:

    • in the left sidebar, hover the mouse cursor over "Manage"
    • in the menu that pops up, click "Members"; a new page loads
    • on the new page, click button "Invite members"
      • enter the jrudi
      • select maximum role: "Developer"
      • click the button "Invite"
    • repeat to invite a new member:
      • enter the jraind
      • select maximum role: "Developer"
      • click the button "Invite"

⚠️ You must run your code yourself! Not following this is an Honor Code violation.

⚠️ You must create complete log files of all AI conversations! Not following this is an Honor Code violation.


Project Description

Core functionality

  1. Experiment Function:

    • Implement a function that simulates one trial of a stochastic process.
    • Use the description in Portfolio to Simulate for the implementation.
    • The function should return a single numerical result (e.g., profit/loss).
  2. Concurrent Execution:

    • Use goroutines to run many trials in parallel.
    • Distribute work across goroutines (e.g., each runs N trials).
    • Results should be sent back to a central aggregator via channels.
  3. Aggregation: Collect results in the main goroutine. Compute:

    • Mean
    • Standard deviation
    • 95% confidence interval for the mean
  4. User Interface (CLI): The program should take command-line arguments:

    • -trials (total number of simulations)
    • -workers (number of goroutines to spawn)
    • -seed (random seed, optional)
  5. Output: Print results clearly:

    Monte Carlo Simulation Results
    Trials: 100000
    Workers: 8
    Seed: 12345
    Time to run: 1234.5 seconds
    Mean outcome: 12.34
    Std dev: 5.67
    95% CI: [11.90, 12.78]
    

    Note that Seed is only output when it's set.

Portfolio to Simulate

Let's define a benchmark experiment so everybody's results can be compared.

  • Suppose an investor puts $100 into a risky asset.
  • Each day, the return is modeled as N(μ = 0.001, σ = 0.02) (normal distribution with mean μ and standard deviation σ).
  • After 252 trading days (1 year), compute the final portfolio value.
  • One simulation trial = one year of returns.

The Monte Carlo estimate: expected final portfolio value + 95% CI.

Extra (stretch goals, if you have time)

  • Visualization: Generate a histogram of final portfolio values (use gonum/plot).
  • Streaming output: Report progress (e.g., every 10k trials) without blocking due to communication.

Outcome

  1. Go source code implementing the engine.
  2. Example source code for the benchmark problem.
  3. Documentation: README.md (and possibly additional files in a docs/ folder) describing

    • how to run the program
    • example output
    • any extensions attempted
    • if applicable, clear documentation of AI use
    • benefits and limitations of AI use

Demonstrate (in your documentation and project presentation)

  • Correctness: Simulation produces reasonable estimates and CIs. As the number of trials N increases the results fluctuate less. → Why do you trust your code?
  • Performance/scalability: Demonstrate strong and weak scalability. What is the max number of goroutines that makes sense for the hardware? → The benefits of concurrency/parallelism.
  • Code quality: Clear structure, proper error handling.
  • Clarity: Documentation and output formatting.