Workspace IndexMath › Day 34

Convex Sets and Testing for Convex Functions TODO

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

Concept

A set C is convex if the line segment joining any two points of C lies entirely within C, and a function f is convex if its domain is convex and f(θx + (1−θ)y) ≤ θf(x) + (1−θ)f(y) holds for any two points and any θ between 0 and 1. An equivalent, geometric characterization is that f's epigraph is a convex set. If f is differentiable, there's a first-order test — the tangent plane at any point is a global lower bound on the function — and if f is twice differentiable, there's a second-order test: the Hessian is positive semidefinite (PSD). Convexity is preserved under several operations, notably non-negative weighted sums, composition with affine maps, pointwise supremum, and composition with a convex, non-decreasing function. In convex problems, a local minimum is automatically the global minimum, and the conditions under which strong duality holds are well understood — so whether a problem can be made convex is often the key fork in the road for optimization work.

Whether a problem is convex determines whether you can use a solver that guarantees a global optimum or have to fall back on an initialization-dependent heuristic, so you need to be able to tell at the modeling stage.

Code & Formula

# Day 34 — 볼록집합/볼록함수 판별
# 정의(선분 부등식)와 2차 조건(Hessian이 준정부호)으로 볼록함수 여부를 판별한다.

import numpy as np


def is_convex_by_definition(f, x, y, n_thetas=11):
    """f(theta*x + (1-theta)*y) <= theta*f(x) + (1-theta)*f(y) 가 모든 theta에서 성립하는지 확인."""
    for theta in np.linspace(0, 1, n_thetas):
        lhs = f(theta * x + (1 - theta) * y)
        rhs = theta * f(x) + (1 - theta) * f(y)
        if lhs > rhs + 1e-9:
            return False
    return True


def f_convex(x):  # f(x) = x^2, 볼록함수
    return x ** 2


def f_nonconvex(x):  # f(x) = -x^2 + sin(4x)*3, 오목/비볼록 성격
    return -(x ** 2) + 3 * np.sin(4 * x)


x1, x2 = -2.0, 3.0
print(f"f(x)=x^2 은 [{x1},{x2}]에서 볼록? -> {is_convex_by_definition(f_convex, x1, x2)}")
print(f"f(x)=-x^2+3sin(4x) 는 [{x1},{x2}]에서 볼록? -> {is_convex_by_definition(f_nonconvex, x1, x2)}")


# 다변수: Hessian이 준정부호(고유값이 모두 0 이상)이면 볼록
def hessian_psd(H):
    eigenvalues = np.linalg.eigvalsh(H)
    return np.all(eigenvalues >= -1e-9), eigenvalues


# g(x,y) = x^2 + 2y^2 의 Hessian은 상수: [[2,0],[0,4]]
H_convex = np.array([[2.0, 0.0], [0.0, 4.0]])
psd, eigs = hessian_psd(H_convex)
print(f"\ng(x,y)=x^2+2y^2 의 Hessian 고유값 = {eigs} -> 준정부호(볼록)? {psd}")

# h(x,y) = x^2 - y^2 (안장점 형태) 의 Hessian
H_saddle = np.array([[2.0, 0.0], [0.0, -2.0]])
psd2, eigs2 = hessian_psd(H_saddle)
print(f"h(x,y)=x^2-y^2 의 Hessian 고유값 = {eigs2} -> 준정부호(볼록)? {psd2}")

Exercise

Derive the Hessian of the log-sum-exp function by hand and numerically verify vᵀHv ≥ 0 for arbitrary vectors v, then separately confirm the same conclusion using only the convexity-preservation rules.

Practical Connection

LMSR's cost function has the log-sum-exp form, so it's convex — and that convexity is exactly what guarantees prices are well-defined as per-outcome probabilities and that no risk-free arbitrage exists in the structure.

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 34 / 52 · 10월 — 미적분·최적화 (Day 27–34)

개념

집합 C가 볼록하다는 것은 C의 임의의 두 점을 잇는 선분이 통째로 C 안에 있다는 뜻이고, 함수 f가 볼록하다는 것은 정의역이 볼록이고 임의의 두 점과 0과 1 사이의 계수에 대해 f(θx + (1−θ)y) ≤ θf(x) + (1−θ)f(y)가 성립한다는 뜻이다. 동치 조건으로 f의 에피그래프가 볼록집합이라는 기하적 특징이 있고, 미분 가능하면 1차 조건(어느 점의 접평면이 함수의 전역 하계)으로, 두 번 미분 가능하면 Hessian이 준정부호(PSD)라는 2차 조건으로 판별한다. 볼록성은 연산에서 보존되며, 비음수 가중합, 아핀 사상과의 합성, 점별 상한(supremum), 볼록·비감소 함수와의 합성 등이 대표적인 보존 규칙이다. 볼록 문제에서는 국소 최소가 곧 전역 최소이고 강한 쌍대성이 성립하는 조건이 잘 알려져 있어, 문제를 볼록으로 만들 수 있느냐가 최적화 실무의 핵심 갈림길이 된다.

"볼록이냐"에 따라 전역 최적을 보장받는 solver를 쓸지, 초기값에 의존하는 휴리스틱을 쓸지가 갈리므로 모델링 단계에서 판별할 수 있어야 한다.

코드 · 수식

# Day 34 — 볼록집합/볼록함수 판별
# 정의(선분 부등식)와 2차 조건(Hessian이 준정부호)으로 볼록함수 여부를 판별한다.

import numpy as np


def is_convex_by_definition(f, x, y, n_thetas=11):
    """f(theta*x + (1-theta)*y) <= theta*f(x) + (1-theta)*f(y) 가 모든 theta에서 성립하는지 확인."""
    for theta in np.linspace(0, 1, n_thetas):
        lhs = f(theta * x + (1 - theta) * y)
        rhs = theta * f(x) + (1 - theta) * f(y)
        if lhs > rhs + 1e-9:
            return False
    return True


def f_convex(x):  # f(x) = x^2, 볼록함수
    return x ** 2


def f_nonconvex(x):  # f(x) = -x^2 + sin(4x)*3, 오목/비볼록 성격
    return -(x ** 2) + 3 * np.sin(4 * x)


x1, x2 = -2.0, 3.0
print(f"f(x)=x^2 은 [{x1},{x2}]에서 볼록? -> {is_convex_by_definition(f_convex, x1, x2)}")
print(f"f(x)=-x^2+3sin(4x) 는 [{x1},{x2}]에서 볼록? -> {is_convex_by_definition(f_nonconvex, x1, x2)}")


# 다변수: Hessian이 준정부호(고유값이 모두 0 이상)이면 볼록
def hessian_psd(H):
    eigenvalues = np.linalg.eigvalsh(H)
    return np.all(eigenvalues >= -1e-9), eigenvalues


# g(x,y) = x^2 + 2y^2 의 Hessian은 상수: [[2,0],[0,4]]
H_convex = np.array([[2.0, 0.0], [0.0, 4.0]])
psd, eigs = hessian_psd(H_convex)
print(f"\ng(x,y)=x^2+2y^2 의 Hessian 고유값 = {eigs} -> 준정부호(볼록)? {psd}")

# h(x,y) = x^2 - y^2 (안장점 형태) 의 Hessian
H_saddle = np.array([[2.0, 0.0], [0.0, -2.0]])
psd2, eigs2 = hessian_psd(H_saddle)
print(f"h(x,y)=x^2-y^2 의 Hessian 고유값 = {eigs2} -> 준정부호(볼록)? {psd2}")

연습

log-sum-exp 함수의 Hessian을 직접 구해 임의 벡터 v에 대해 vᵀHv ≥ 0임을 수치적으로 확인하고, 볼록성 보존 규칙만으로 같은 결론에 도달하는 경로도 적어 볼 것.

실무 · Verex 연결

LMSR의 비용 함수는 log-sum-exp 형태라 볼록이며, 이 볼록성 덕분에 가격이 각 결과 확률로 잘 정의되고 무위험 차익이 생기지 않는 구조가 보장된다.

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

← 33. 테일러 급수(1차 근사)35. 조건부확률·베이즈·기대값·정규분포 →