# The book "Machine Learning: Algorithms for Business" Hi, habrozhiteli! Marcos Lopez de Prado shares what they usually hide - the most profitable machine learning algorithms that he has used for two decades to manage large pools of funds of the most demanding investors.

Machine learning changes almost every aspect of our lives; MO algorithms perform tasks that, until recently, were trusted only by trusted experts. In the near future, machine learning will dominate finance, fortune telling will be a thing of the past, and investments will no longer be synonymous with gambling.

Take the chance to participate in the “machine revolution”, for this it’s enough to get acquainted with the first book, which provides a complete and systematic analysis of machine learning methods in relation to finance: starting with financial data structures, marking the financial series, weighing the sample, differentiating the time series ... and ending with the whole part dedicated to the correct backtesting of investment strategies.

### 15.1. Relevance

Investment strategies are often implemented in terms of positions held until one of the two conditions is met: 1) the condition for leaving the position with profits (taking profits) or 2) the condition for leaving the position with losses (stop loss). Even when the strategy does not explicitly declare a loss stop, there is always an implicit loss stop limit at which the investor can no longer finance his position (margin call) or incurs damage caused by an increase in unrealized loss. Since most strategies have (explicitly or implicitly) these two exit conditions, it makes sense to model the distribution of outcomes through a binomial process. This, in turn, will help us understand which combinations of betting rates, risks, and payouts are uneconomical. The purpose of this chapter is to help you evaluate

Consider a strategy that produces n identically distributed, mutually independent bets per year, where the outcome Xi of the bet i ∈ [1, n] represents profit π> 0 with probability P [Xi = π] = p and loss –π with probability P [Xi = –Π] = 1 - p. You can imagine p as the accuracy of a binary classifier, in which an affirmative outcome means betting on opportunity, and a negative outcome means missing opportunity: true statements are rewarded, false statements are punished, and negative outcomes (whether true or false) have no payoffs. Since the outcomes of bets {Xi} i = 1, ..., n are independent, we will calculate the expected moments per bet. The expected profit from one bet is E [Xi] = πp + (–π) (1 - p) = π (2p - 1). Variance is where = π2p + (–π) 2 (1 - p) = π2, therefore, V [Xi] = π2 - π2 (2p - 1) 2 = π2 [1– (2p - 1) 2] = 4π2p (1 - p ) For n identically distributed, mutually independent rates per year, the average annual Sharpe ratio (θ) is Notice how π balances the equation above because the payouts are symmetrical. As in the Gaussian case, θ [p, n] can be understood as a re-scaled t-value1. This illustrates the fact that even for a small Sharp coefficient can be made high for a sufficiently large n. This serves as the economic basis for high-frequency trading, where p can be slightly higher than .5, and the key to successful exchange activity is an increase in n. The Sharpe ratio is a function of accuracy, and not of correctness, because missing an opportunity (negative statement) is not rewarded or punished directly (although too many negative statements can lead to small n, which will reduce the Sharpe coefficient to zero).

For example, for and to reach an average annual Sharpe ratio of 2, 396 bets per year are required. Listing 15.1 verifies this result experimentally. Figure 15.1 shows the Sharpe ratio as a function of accuracy for different bet frequencies.

Listing 15.1. Sharpe ratio as a function of the number of bets

``````out,p=[],.55
for i in xrange(1000000):
rnd=np.random.binomial(n=1,p=p)
x=(1 if rnd==1 else -1)
out.append(x)
print np.mean(out),np.std(out),np.mean(out)/np.std(out)`````` This equation expresses quite clearly the trade-off between accuracy (p) and frequency (n) for a given Sharpe coefficient (θ). For example, in order to give an average annual Sharpe ratio of 2 for a strategy that produces only weekly rates (n = 52), a fairly high accuracy p = 0.6336 will be required. ### 15.3. Asymmetric Payments

Consider a strategy that produces n identically distributed, mutually independent rates per year, where the outcome Xi of the bet i ∈ [1, n] is π + with probability P [Xi = π +] = p, and the outcome π– (π– <π + ) happens with probability P [Xi = π_] = 1 - p. The expected profit from one bet is E [Xi] = pπ + + (1 - p) π– = (π + - π–) p + π–. The dispersion is V [Xi] =, where  Finally, we can solve the previous equation for 0 ≤ p ≤ 1 and get where:
???? a = (n + θ2) (π + - π–) 2;
???? b = [2nπ - θ2 (π + - π -)] (π + - π–); Note: Listing 15.2 verifies these symbolic operations using the Python SymPy Live shell running on the Google App Engine cloud service: live.sympy.org .

Listing 15.2. Using SymPy Library for Symbolic Operations

``````>>> from sympy import *
>>> init_printing(use_unicode=False,wrap_line=False,no_global=True)
>>> p,u,d=symbols('p u d')
>>> m2=p*u**2+(1-p)*d**2
>>> m1=p*u+(1-p)*d
>>> v=m2-m1**2
>>> factor(v)``````

The above equation answers the following question: for a given trading rule characterized by the parameters {π–, π +, n}, what is the degree of accuracy p necessary to achieve the Sharpe ratio equal to θ *? For example, in order to get θ = 2 for n = 260, π– = –.01, π + = .005, we need p = .72. Due to the large number of bets, a very small change in p (from p = .7 to p = .72) promoted the Sharp coefficient from θ = 1.173 to θ = 2. On the other hand, this also tells us that this strategy is vulnerable to small changes in p. Listing 15.3 implements the derivation of the expected accuracy. In fig. 15.2 shows the supposed accuracy as a function of n and π–, where π + = 0.1, and θ * = 1.5. As the threshold π– becomes more negative for a given n, a higher degree p is required to achieve θ * for a given threshold π +.

Listing 15.3. Calculation of Estimated Accuracy

``````def binHR(sl,pt,freq,tSR):
´´´``````

For a given trading rule characterized by the parameters {sl, pt, freq}, what is the minimum accuracy required to achieve the Sharpe ratio equal to tSR?

1) Inputs
sl: loss stop threshold
pt: profit threshold threshold
freq: bets per year
tSR: target annual average Sharpe ratio
2) Output
p: minimum accuracy p required to achieve tSR
´´´
a = (freq + tSR ** 2) * (pt-sl) ** 2
b = (2 * freq * sl-tSR ** 2 * (pt-sl)) * (pt-sl)
c = freq * sl ** 2
p = (- b + (b ** 2-4 * a * c) **. 5) / (2. * a)
return p Listing 15.4 solves θ [p, n, π–, π +] for the estimated betting frequency n. In fig. 15.3 shows the estimated frequency depending on p and π–, where π + = 0.1, and θ * = 1.5. As the threshold π– becomes more negative for a given degree p, a higher number n is required to achieve θ * for a given threshold π +. As the degree p becomes smaller for a given threshold π–, a higher number n is required to achieve θ * for a given threshold π +.

Listing 15.4. Calculating Estimated Betting Frequency

``````def binFreq(sl,pt,p,tSR):
´´´``````

For a given trading rule, characterized by the parameters {sl, pt, freq}, how many bets per year are necessary to achieve the Sharp coefficient tSR with a degree of accuracy p?

Note: equation with radicals, check for extraneous solutions.

1) Inputs
sl: threshold for stopping losses
pt: threshold for profit taking
p: degree of accuracy p
tSR: target average annual Sharpe ratio
2) Output
freq: number of required rates per year
´´´
freq = (tSR * (pt-sl)) ** 2 * p * (1-p) / ((pt-sl) * p + sl) ** 2 # possibly extraneous
if not np.isclose (binSR (sl, pt, freq, p), tSR): return
return freq 