본문 바로가기

BITS

[Reinforcement Learning in Finance] MDP & RL: Action Value Function

이전 포스팅에서 value function 과 optimal value function 그리고 bellman optimality equation에서 numerical solution으로 해결할수 있는지 다뤘다. 

- optimal policy는 optimal value function 을통해서 구할수 있다.

- 그러나 더 action-value function을 이용하면 더 쉽게 구할수 있다.

$V_t^\pi (s) = E[\sum_n \gamma^n R(s_n, a_n) | s_0 = s,\pi]$

- 현재 state에만 관련된 policy 파이에 대한 value function는 Reinforcement Learning Agent에게 무엇을 해야하는지 알려주지 않는다. 

- 그러나 action-value function Q 는 현재상태인 $s_t$ 와 행해지는 action인 $a_t$를 모두 관여하고 있다.

$Q_0^\pi (s,a) = E[\sum_n \gamma^n R_n(s_n, a_n)|s_0 = s,a_0 = a, \pi] = R_0(s,a) + E[\sum_{n=1} \gamma^n R_n(s_n, a_n)|\pi]$

- 이는 value function과 마찬가지로 expected cumulative reward를 계산하지만, $s_t$와 $a_t$를 모두 반영하고 있다.

- value function 과 유사하게, action-value function Q 즉, Q-function은 policy pi로 계산되며, 첫번째 action은 주어진 A의 값으로 고정되었고, later action은 policy pi로 결정된다. 

- 이러한 정의를 사용하면, value function으로 bellman equation을 구한것처럼 bellman equation for q-function을 구할수 있다.

- 먼저 첫번째 term과 나머지를 분리한다. 여기서 첫번째 term은 action A를 현재 취했을때 오는 reward를 의미한다. action A는 q-function의 arguement로 고정되어있기때문에 reward 또한 고정되어있다.

- 두번째 term은 동일한 합의 과정이지만, next time stamp (미래)에 관련한다. 이는 A에 의존하지않고, policy pi에 의존한다. 

- 위의 같은 방식을 더하면 bellman function for the q-value function이 아래와 같이 만들어진다.

$Q_t^\pi (s_t, a_t) = R_t(s_t, a_t) + E_{s_{t+1}}[\gamma V_{t+1}^\pi(s_{t+1})]$

- 이는 time T에 reward에 관하여 T와 expected value를 discount하여 다음의 value 를 계산한다. 

- 2개의 unknown function이 있기 때문에 계산이 복잡하다. 그러나 optimal q-function의 경우는 더 간단하게 계산할수 있다.



Optimal Q-function:

$Q_t^*(s_t, a_t) = max_\pi Q_t^\pi (s_t, a_t)$

- optimal q-function은 optimal value function 과 동일한 방법으로 계산한다. 

- optimal action-value function q*는 모든 q functions과 가능한 모든 policy pi의 최대치이다. 

Optimal V-function:

$V_t^*(s_t) = max_{a_t \in A} Q_t^* (s_t, a_t)$

- optimal q-function은 action a 를 지금하고, policy p*를 나중에 따러야 하는 것을 의미한다. 이는 optimal value function에서 의미하는 optimal policy에 의해 결정되는 action을 현재 그리고 나중에 모두 적용해야한다는 것과 대조를 이룬다.

- 또 optimal value function V*는 a가 현재 할수 있는 모든 action 중 optimal action-value function Q* 의 최대값이다. 즉, Q*의 second argument에 대한 최대치이다. 


Bellman equation for the optimal q-function

- bellman equation for the q-function에 있는 모든 policies pi에 대한 최대치를 찾고, q-star의 정의를 이용한다.

$q_t^*(s_t, a_t) = R_t(s_t, a_t) + E_{s'}[\gamma V_{t+1}^*(s_{t+1}, a_t)]$


Bellman optimality equation for the action-state function Q

$q_t^*(s_t, a_t) = R_t(s_t, a_t) + max_{a_t \in A} E_{s'}[\gamma V_{t+1}^*(s_{t+1}, a_t)]$


Learning from experience

- state value function $V^\pi (s)$ 와 action-state value function $Q^\pi(s,a)$ 는 둘다  experience 통해서 값이 구해진다. 

- agent는 policy pi를 따르고, each state it visited 에 대해서 average reward를 가진다. 

- 각 state에 대한 visit이 충분하다면, 평균값은 value function for the state에 대해 converge하게 된다. 

- 각 단계에서 취해진 각 actions들의 평균들을 계산할 수 있는데, 평균들은 action-state value function의 값으로 converge 하게 된다. 

- 강화학습에서 이러한 방법을 Monte Carlo Methods 라고 부른다. 

- actions이나 states가 적은 경우에는 매우 간단하다. 그러나 states가 많다거나 action이 continuous 하다면, 각각의 단계에서 average를 갖는것이 실용적이지 않는 경우에 function approximation을 해야한다. 


Pole Balancing Problem

- 트랙위에 있는 cart에 힘을 가해서 균형을 맞춰 카트위의 pole이 떨어지는것을 막는 문제 pole이 일정각도이상으로 떨이지면 실패. 

- 이러한 문제는 episodic task인데, 주어진 시간동안이나 pole이 떨어질때까지 계속해서 pole의 균형을 맞추는 에피소드가 연속되는 것을 의미한다.

- 각 단계에서 failure가 일어나지 않으면 reward+1을 제공한다. failure가 발생하면 episode가 끝나고, agent는 새로운 episode를 시작한다.

- 이문제는 또 continuing task (no fixed end time) 으로 여겨질수 있는데, 이러한 경우에는 failure가 발생할때 -1 reward를 제공하고, 아닌 경우에는 0을 준다. discounting $\gamma$ 를 이용해서, value function의 정의에 대한 무한합을 다룬다. 

- MDP formulation: states는 카트와 pole의 포지션과 속도이고, action은 cart에게 가해지는 힘이다.



QUIZ: Select all correct answers

1. Episodic tasks continue until a Q-function keeps maximizing. Because the time it takes depends on initial conditions, different episodes generally take different times.

2. Monte Carlo methods in Reinforcement Learning directly estimate the Q-function by summing observed rewards along trajectories.

3. An optimal value function is obtained by maximization of a Q-function with respect to the action variable.

4. An action-value function (Q-function) is a function of a current state and current action, unlike a value function that is only a function of a current state.

5. An optimal value function is obtained by maximization of a Q-function with respect to the state variable.


Answer: 2,3,4


This is a translated note for the coursera course (linked below).  

https://www.coursera.org/learn/reinforcement-learning-in-finance/lecture