19

Click here to load reader

Monte Carlo C++

Embed Size (px)

DESCRIPTION

Slides from my MC talk at the C++ User Group, St.Petersburg, Russia, June 2014

Citation preview

Page 1: Monte Carlo C++

Monte Carlo Methodsa case study

Dmitri Nesteruk

[email protected]

Page 2: Monte Carlo C++

Hi!

• My name is Dmitri

• Quant finance

• Developer Evangelist @ JetBrains (2010-…)

• Visiting Researcher @ University of Southampton (2002-…)

• MVP Visual C# (2009-…)

Page 3: Monte Carlo C++

Calculation of 𝜋𝜋

• Generate lots of points in square

• Area of circle𝜋𝜋𝑟𝑟2

• Area of square4𝑟𝑟2

• ∴ 𝜋𝜋 = 4 𝑆𝑆𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑆𝑆𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑐𝑐𝑐𝑐

• Area ~ number of points

r

Page 4: Monte Carlo C++
Page 5: Monte Carlo C++

�𝑒𝑒𝑥𝑥2

Page 6: Monte Carlo C++

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

2

4

6

8

10

12

14

16

18

Page 7: Monte Carlo C++

Poker (is largely bots )

Page 8: Monte Carlo C++

AH 7S

? ?

AS KH TC

try every single combination6C

6D win

win

loss

loss

loss

AH 7S40% to win

Page 9: Monte Carlo C++

Challenging (&geeky) games for heavy MC

Page 10: Monte Carlo C++

HearthStone

• ~400 different cards

• Player pick a deck of 30

• Each card has stationary and transient behaviors• May affect 0, 1 or several other cards

Page 11: Monte Carlo C++

Rules

• Stored in an XML file

• Parsed via boost::regex

• Result in creation of std::function objects

Page 12: Monte Carlo C++

Everything affects everything

• This card gives bonus to other card

• When card enters play, …

• When card leaves play, …

• This interaction method does not map well to OOP

Page 13: Monte Carlo C++

Properties

• Properties are built-in in C#, not available in C++ or Java

• __declspec(property) compiler extension• Supported on at least Intel and MSVC compilers

• A person structure keeps the person’s age• Age (read/write)

• Only people older than 16 can vote• CanVote (readonly)• This is a derivative property (at runtime you need an std::function)

Page 14: Monte Carlo C++

Change notifications

• We want to be informed when properties change

• Use boost::signals

• Let’s borrow .NET ideology!

Page 15: Monte Carlo C++

Event Bus

• Global communication pipeline

• Entity signs up when enters game, disconnects on exit

• Also uses signal/slot paradigm

Page 16: Monte Carlo C++

Realtime Analysis

• The game uses Unity

• Decompiled .NET libs

• Reverse-engineer Protocol Buffer definitions

• …

• PROFIT!!!

Page 17: Monte Carlo C++

Packet Capture

• Winpcap

• Capture game state

• Reanalyze at each significant change

Page 18: Monte Carlo C++

Architecture

• Intel C++

• >10k Xeon cores

• 24x Intel Xeon Phi

Page 19: Monte Carlo C++

That’s it!

• Monte-Carlo Methods in Financial Engineering, Paul Glasserman, Springer

• Boosthttp://boost.org

• Course on Boost at http://pluralsight.com

• Send feedback to:@[email protected]