Workspace IndexMath › Day 28

Partial Derivatives and the Gradient TODO

Math · Day 28 / 52 · October — Calculus & Optimization (Day 27-34)

Concept

A partial derivative is the derivative of a multivariable function taken with respect to one variable while holding the others fixed. The gradient collects these partial derivatives into a vector that points in the direction of steepest increase at that point, with its magnitude giving the rate of increase in that direction. At a differentiable point, the gradient is perpendicular to the level set passing through that point. The basic optimization move is gradient descent — taking small steps opposite the gradient — and at an unconstrained local optimum of a smooth function, the gradient is zero. If the function is convex, a point where the gradient vanishes is the global minimum.

Parameter calibration, cost minimization, and model training are all gradient-based, so if you can't read sensitivity off the equations you can't diagnose why something is diverging or why convergence has stalled.

Code & Formula

# Day 28 — 편미분/그래디언트
# 다변수 함수의 편미분을 수치로 구해 그래디언트 벡터를 만들고, 최급상승 방향임을 확인한다.

import numpy as np


def f(v):
    x, y = v
    return x ** 2 + 3 * y ** 2 - 2 * x * y


def gradient(f, v, h=1e-6):
    grad = np.zeros_like(v)
    for i in range(len(v)):
        v_plus = v.copy()
        v_minus = v.copy()
        v_plus[i] += h
        v_minus[i] -= h
        grad[i] = (f(v_plus) - f(v_minus)) / (2 * h)
    return grad


v0 = np.array([1.0, 2.0])
grad = gradient(f, v0)
print(f"f({v0}) = {f(v0):.4f}")
print(f"gradient = {grad}")

# 그래디언트 방향으로 조금 이동하면 함수값이 증가, 반대 방향이면 감소해야 한다.
step = 0.01
unit = grad / np.linalg.norm(grad)
f_plus = f(v0 + step * unit)
f_minus = f(v0 - step * unit)
print(f"\ngradient 방향으로 이동: f = {f_plus:.6f} (증가해야 함)")
print(f"반대 방향으로 이동:   f = {f_minus:.6f} (감소해야 함)")
print(f"원래 값:              f = {f(v0):.6f}")

Exercise

Take the partial derivatives of the LMSR cost function C(q) = b·ln(Σ exp(q_i/b)) by hand and confirm that these values are exactly the prices of each outcome, and that they sum to 1.

Practical Connection

Verex's LMSR price is literally the gradient of the cost function, so this calculation isn't abstract — it directly explains price, slippage, and sensitivity to the liquidity parameter b.

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


한국어

편미분/그래디언트 TODO

Math · Day 28 / 52 · 10월 — 미적분·최적화 (Day 27–34)

개념

편미분은 다변수 함수에서 한 변수만 변화시키고 나머지를 고정한 채 구한 미분이다. 그래디언트는 편미분들을 모은 벡터로, 그 점에서 함수가 가장 가파르게 증가하는 방향을 가리키고 크기는 그 방향의 증가율이다. 미분 가능한 점에서 그래디언트는 그 점을 지나는 등위면에 수직이다. 최적화의 기본은 그래디언트 반대 방향으로 조금씩 이동하는 경사하강이며, 제약 없는 매끄러운 함수의 국소 최적점에서는 그래디언트가 0이 된다. 함수가 볼록하면 그래디언트가 0인 점이 곧 전역 최소점이다.

파라미터 캘리브레이션·비용 최소화·모델 학습이 전부 그래디언트 기반이라, 수식에서 민감도를 읽지 못하면 왜 발산하거나 수렴이 멈추는지 진단할 수 없다.

코드 · 수식

# Day 28 — 편미분/그래디언트
# 다변수 함수의 편미분을 수치로 구해 그래디언트 벡터를 만들고, 최급상승 방향임을 확인한다.

import numpy as np


def f(v):
    x, y = v
    return x ** 2 + 3 * y ** 2 - 2 * x * y


def gradient(f, v, h=1e-6):
    grad = np.zeros_like(v)
    for i in range(len(v)):
        v_plus = v.copy()
        v_minus = v.copy()
        v_plus[i] += h
        v_minus[i] -= h
        grad[i] = (f(v_plus) - f(v_minus)) / (2 * h)
    return grad


v0 = np.array([1.0, 2.0])
grad = gradient(f, v0)
print(f"f({v0}) = {f(v0):.4f}")
print(f"gradient = {grad}")

# 그래디언트 방향으로 조금 이동하면 함수값이 증가, 반대 방향이면 감소해야 한다.
step = 0.01
unit = grad / np.linalg.norm(grad)
f_plus = f(v0 + step * unit)
f_minus = f(v0 - step * unit)
print(f"\ngradient 방향으로 이동: f = {f_plus:.6f} (증가해야 함)")
print(f"반대 방향으로 이동:   f = {f_minus:.6f} (감소해야 함)")
print(f"원래 값:              f = {f(v0):.6f}")

연습

LMSR 비용함수 C(q) = b·ln(Σ exp(q_i/b))의 편미분을 손으로 구해 그 값이 각 결과의 가격이 되고 합이 1이 됨을 확인하라.

실무 · Verex 연결

Verex의 LMSR 가격이 정확히 비용함수의 그래디언트이므로 이 계산은 추상 개념이 아니라 가격, 슬리피지, 유동성 파라미터 b에 대한 민감도를 그대로 설명해준다.

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

← 27. 미분·기울기·연쇄법칙29. Gradient Descent / Convex 직관 →