Workspace IndexMath › Day 38

Random Walks and GBM (Concept) TODO

Math · Day 38 / 52 · November — Probability, Statistics & Financial Math (Day 35-43)

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/.


한국어

랜덤워크/GBM(개념) TODO

Math · Day 38 / 52 · 11월 — 확률·통계·금융수학 (Day 35–43)

개념

랜덤워크는 독립적인 증분을 계속 더해가는 확률과정으로, 대칭 단순 랜덤워크에서는 분산이 시간에 비례해 커지므로 전형적인 이동 거리는 시간의 제곱근 규모다. 이를 연속시간으로 극한을 취한 것이 브라운 운동이며, 증분이 서로 독립이고 정규분포를 따른다. 기하 브라운 운동은 값의 로그가 브라운 운동을 따르도록 만든 모형으로, 값이 음수가 되지 않고 수익률이 로그정규분포를 갖기 때문에 자산가격 모형으로 널리 쓰인다. 다만 실제 시장은 두꺼운 꼬리와 변동성 군집을 보여 GBM은 어디까지나 1차 근사다.

가격·잔고·큐 길이처럼 누적되는 확률 과정의 변동 폭을 제곱근 스케일로 어림할 수 있어야 리스크 한도나 타임아웃을 근거 있게 정할 수 있다.

코드 · 수식

# 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}")

연습

단순 랜덤워크와 GBM 경로를 각각 수천 개 시뮬레이션해 시점별 분산과 최종값 분포를 그려 정규·로그정규 형태를 눈으로 확인하라.

실무 · Verex 연결

예측시장 가격은 새 정보가 들어올 때마다 갱신되는 마팅게일에 가까운 과정이지만 0과 1 사이에 갇혀 있어 GBM을 그대로 쓸 수는 없고, 이 차이를 알아야 Verex의 가격 변동 폭이나 담보 요구 수준을 정량적으로 추정할 수 있다.

공부한 날 원본 커리큘럼(docs/knowledge/math-50-curriculum.md)의 이 줄에 노트 링크와 ✅ 를 붙이면, 이 자리는 노트 본문으로 바로 이어집니다. 노트 없이 이 페이지에 바로 적어도 됩니다 — 다만 다시 생성하면 덮어쓰이므로, 남길 글은 docs/algorithms/ 의 마크다운으로 쓰는 편이 안전합니다.

← 37. 집중부등식(Chebyshev·Hoeffding, 12월 다리)39. 랜덤워크·열확산 방정식 →