Modified cumprod using bsxfun?

조회 수: 9 (최근 30일)
Ernest
Ernest 2013년 3월 10일
I am trying to generate Monte Carlo simulations whereby my sample paths are cumulative products. However, I want each iteration step to be allowed to be incremented/decremented only by multiples of a specified minimum value.
The simplest example I can think of to illustrate this is to simulate roulette betting from the casino's perspective. This is the fastest vectorized example I can think of if there was no restriction on the minimum increments:
% Inputs
initialCapital = 1e6;
betFraction = 0.0015;
numBets = 1000;
numSamplePaths = 1000;
winProbability = 37/38;
winAmount = 1;
loseAmount = 30;
% Simulation
spins = sign(rand(numBets, numSamplePaths) - 1 + winProbability);
profitFactor = spins;
profitFactor(spins>=0) = 1 + winAmount*betFraction*spins(spins>=0);
profitFactor(spins<0) = 1 + loseAmount*betFraction*spins(spins<0);
accumulatedCapital = cumprod(profitFactor) * initialCapital;
However, in a realistic situation, the bet sizes must be multiples of a fixed value, let's say it's $1 - so all of my elements in the accumulatedCapital array should be integers. Note that this is different from saying that:
accumulatedCapital = floor(cumprod(profitFactor) * initialCapital);
Of course, the easiest way is simply to use a loop twice over:
if spin(n) >= 0
accumulatedCapital(n) = ...
accumulatedCapital(n-1) + ...
winAmount*floor(betFraction(n)*accumulatedCapital(n-1))
elseif spin(n) < 0
accumulatedCapital(n) = ...
accumulatedCapital(n-1) - ...
loseAmount*floor(betFraction(n)*accumulatedCapital(n-1))
but this is much slower. Any of you can suggest some vectorized wizardry to solve this? Thanks!

답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by