Random Walks and GBM (Concept) TODO
Concept
A random walk is a stochastic process built by repeatedly adding independent increments; in a symmetric simple random walk, variance grows proportionally with time, so the typical distance traveled scales with the square root of time. Taking this to a continuous-time limit gives Brownian motion, where increments are independent and normally distributed. Geometric Brownian motion (GBM) is a model where the logarithm of the value follows Brownian motion; because this keeps the value from going negative and gives returns a lognormal distribution, it's widely used to model asset prices. Real markets, though, show fat tails and volatility clustering, so GBM is ultimately only a first-order approximation.
Being able to estimate the swing of an accumulating stochastic process — price, balance, queue length — using square-root-of-time scaling is what lets you set risk limits or timeouts on solid ground rather than guesswork.
Code & Formula
# Day 38 — 랜덤워크/GBM(개념)
# 대칭 단순 랜덤워크를 시뮬레이션하고, 로그값이 랜덤워크를 따르는 기하 브라운 운동(GBM) 근사 경로도 만든다.
import random
import math
random.seed(1)
# 1) 대칭 단순 랜덤워크: 매 스텝 +1 또는 -1
n_steps = 20
walk = [0]
for _ in range(n_steps):
step = random.choice([-1, 1])
walk.append(walk[-1] + step)
print(f"단순 랜덤워크 경로 ({n_steps}스텝):")
print(walk)
print(f"최종 위치 = {walk[-1]}, 이론적 표준편차(sqrt(n)) = {math.sqrt(n_steps):.3f}\n")
# 2) 이산시간 GBM 근사: S_t = S_0 * exp(sum of small normal increments)
# dlogS = (mu - 0.5*sigma^2)*dt + sigma*sqrt(dt)*Z, Z ~ N(0,1)
S0 = 100.0
mu = 0.05 # 연간 기대수익률
sigma = 0.2 # 연간 변동성
n_gbm_steps = 10
dt = 1 / 252 # 하루 단위
prices = [S0]
for _ in range(n_gbm_steps):
z = random.gauss(0, 1)
drift = (mu - 0.5 * sigma ** 2) * dt
diffusion = sigma * math.sqrt(dt) * z
next_price = prices[-1] * math.exp(drift + diffusion)
prices.append(next_price)
print(f"GBM 근사 가격 경로 ({n_gbm_steps}일):")
for i, p in enumerate(prices):
print(f" day {i}: {p:.4f}")
Exercise
Simulate a few thousand paths each of a simple random walk and a GBM process, then plot the variance over time and the distribution of final values to visually confirm the normal and lognormal shapes.
Practical Connection
A prediction-market price behaves close to a martingale that updates whenever new information arrives, but because it's confined between 0 and 1, GBM can't be applied directly — understanding that difference is necessary to quantitatively estimate Verex's price swings or its collateral requirements.
If you study this on a given day, add a note link and a ✅ to this line in the source curriculum (docs/knowledge/math-50-curriculum.md) and this spot will lead straight to the note body. You can also write directly on this page — but regenerating overwrites it, so it's safer to keep anything you want to save as markdown under docs/algorithms/.