# FxPaul

Math in finance or vice versa

## Order book temperature

The temperature is one of principal quantities in thermodynamics and it is a macroscopic intensive variable because it is independent of the bulk amount of elementary entities contained inside. Let’s try to move physical definition to trading world. Thermodynamics defines temperature as:
$\frac{dS}{dE} = \frac{1}{T}$
where $S$ is entropy and $E$ is internal energy of the system.

In statistical mechanics, entropy is a measure of the number of ways in which a system may be arranged, often taken to be a measure of “disorder” (the higher the entropy, the higher the disorder). This definition describes the entropy as being proportional to the natural logarithm of the number of possible microscopic configurations (microstates) which could give rise to the observed macroscopic state (macrostate) of the system. For sake of simplicity we assume the constant of proportionality equal to one:
$S = - \sum_{n} {p_n \ln p_n}$

Order book is in fact a set of all buy/sell orders. Let’s denote it as $\left\{ \left\{ b_i : B_i \right\}, \left\{s_i : S_i \right\} \right\}$ where b (s) is price and B (S) is amount of contracts at given price of buy (or sell) orders. Let’s normalise it by total buy (Tb) and sell (Ts) contracts:
$\left\{\left\{b_i : p_i = \frac{B_i}{Tb} \right\}, \left\{s_i : q_i = \frac{S_i}{Ts} \right\} \right\}$
Thus the entropy becomes a sum of entropy of buy and sell sides:
$S = -\sum_{b} p_i \ln p_i - \sum_{s} q_i \ln q_i$

The internal energy is the total energy contained by thermodynamical system. It is the energy needed to create the system, but excludes the energy to displace the system’s surroundings, any energy associated with a move as a whole, or due to external force fields. Thus to create the order book one needs to have all money of buy side and to own securities of sell side. There could be doubts how to price securities of sell side but we’ll take the easiest approach:
$E = \sum_s s_i S_i - \sum_b b_i B_i = \sum_s s_i q_i Ts - \sum_b b_i p_i Tb$

Let’s try to derive a formula of temperature under the given assumption. At first, the total differentials of entropy and internal energy should be obtained:
$dS = -\sum_{b} {(1 + \ln p_i)dp_i} - \sum_{s} {(1+\ln q_i) dq_i}$
$dE = d\left( \sum_s s_i q_i Tb - \sum_b b_i p_i Ts \right) = \sum_s s_i Ts dq_i - \sum_b b_i Tp dp_i$

Then we can find derivative of entropy by energy by total derivative definition:
$\frac{dS}{dE} = \frac{\partial S}{\partial E} + \sum_b {\frac{\partial S}{\partial p_i}\frac{dp_i}{dE}} + \sum_s {\frac{\partial S}{\partial q_i}\frac{dq_i}{dE}}$
where
$\frac{\partial S}{\partial E} = 0$
$\frac{\partial S}{\partial p_i} = 1 + \ln p_i$
$\frac{\partial S}{\partial q_i} = 1 + \ln q_i$
$\frac{dp_i}{dE} = \left(\frac{dE}{dp_i}\right)^{-1} = - \left(b_i Tb\right)^{-1}$
$\frac{dq_i}{dE} = \left(\frac{dE}{dq_i}\right)^{-1} = \left(s_i Ts\right)^{-1}$
And substitution into the total derivative yields the formula for temperature:
$\frac{1}{T} = \frac{dS}{dE} = \sum_s \frac{1 + \ln q_i}{s_i Ts} - \sum_b \frac{1 + \ln p_i}{b_i Tb}$

Written by fxpaul

November 19, 2012 at 16:05

Posted in thoughts

Tagged with ,

## Hidden Markov Model for application store ratings

Hidden Markov model (HMM) is a statistical model in which the system is assumed to be a Markov process with hidden states. Those states can be recovered by outputs, observed sequences. In other words, it is possible to infer some probabilistic properties of the system by outputs.

As an off-topic, application stores usually give ranking to apps by user comments and rankings. The simplest way to derive an app rating is to calculate average or median, i.e. some statistical property based on rating samples. For average rating not being a robust statistics, its value is affected by outliers, for instance, by deviant rankings submitted by users. Thus a robust procedure might be used to improve ranking.

In fact we can apply HMM mechanics to infer real application rating by the most likely explanation of observed user rankings. Let’s see how to do that.

Written by fxpaul

November 2, 2011 at 17:10

Posted in thoughts

Tagged with , ,