모든 챌린지
쉬움 0명 해결 정답률 0%

Markov Chain State Distribution

Propagate a probability distribution through a Markov chain's transition matrix for a number of steps.

배우게 될 내용

  • Propagate a probability distribution
  • Multiply by a transition matrix
  • See long-run behavior emerge

문제

번역본 보기

A Markov chain moves between states at random, and the next state depends only on the current one. transition[i][j] is the probability of moving from state i to state j, so every row sums to 1.

If p is the probability of being in each state now, the distribution one step later is:

p_next[j] = Σ over i of p[i] × transition[i][j]

Implement markov_distribution(transition, start, steps) and apply this update steps times.

Input

transition: An n × n matrix whose rows sum to 1.

start: The starting distribution (n values summing to 1).

steps: How many steps to take.

Output

Return the distribution after steps steps.

제약 조건

1 ≤ n ≤ 50, 0 ≤ steps ≤ 1,000

0 ≤ each probability ≤ 1

예시

예시 1
입력:
start = [1.0, 0.0]
steps = 2
transition = [
    [0.9, 0.1],
    [0.5, 0.5]
]

예상 출력:
expected = [0.86, 0.14]
입력:
std::vector<double> start = {1.0, 0.0};
long long steps = 2;
std::vector<std::vector<double>> transition = {
    {0.9, 0.1},
    {0.5, 0.5}
};

예상 출력:
std::vector<double> expected = {0.86, 0.14};
입력:
double[] start = new double[] {1.0, 0.0};
long steps = 2;
double[][] transition = new double[][] {
    new double[] {0.9, 0.1},
    new double[] {0.5, 0.5}
};

예상 출력:
double[] expected = new double[] {0.86, 0.14};

설명

Think of sunny (0) and rainy (1) days, starting sunny. After one step: [0.9, 0.1]. After two: sunny = 0.9 × 0.9 + 0.1 × 0.5 = 0.86 and rainy = 0.9 × 0.1 + 0.1 × 0.5 = 0.14.

예시 2
입력:
start = [0.25, 0.75]
steps = 3
transition = [
    [0.0, 1.0],
    [1.0, 0.0]
]

예상 출력:
expected = [0.75, 0.25]
입력:
std::vector<double> start = {0.25, 0.75};
long long steps = 3;
std::vector<std::vector<double>> transition = {
    {0.0, 1.0},
    {1.0, 0.0}
};

예상 출력:
std::vector<double> expected = {0.75, 0.25};
입력:
double[] start = new double[] {0.25, 0.75};
long steps = 3;
double[][] transition = new double[][] {
    new double[] {0.0, 1.0},
    new double[] {1.0, 0.0}
};

예상 출력:
double[] expected = new double[] {0.75, 0.25};

설명

This chain always switches state, so each step swaps the two probabilities. After 3 swaps: [0.75, 0.25].

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.