Can anyone help me to unnderstand concave convex optimize?

i'm novice in this area i just a trader who interest in algorithmic trading system, then i bought Quantitative money management course. In the Kelly Criterion part i stucked with cvxpy library that use concave, affine expression method and i'm not clear at all please give me some advice or link of an easy explain for dummy like me to understand what i've learned. Please.



Thank in advance.

Hello Tanapat, thanks for your query. That's all right, convex optimisation isn't an easy topic and needs some effort and time. So you're good.



Let's start with the meaning of the word affine. So, in common parlance affine refers to things that are related. What could that mean in terms of math? Have a look at this very easy explanation. Basically, you move a set of points around to a different place in the same space but the relationship between them continues to remain. A relationship where the lines between the various points continue to remain parallel. 



Now imagine this, in a 2 dimensioned coordinate space, you have a line between (0,0) and (1,1). This would include all points between (0,0) and (1,1). You decide to move all the points on the line by 5 units on the x-coordinate. Now, all the points on the line lie between (0,5) and (1,6). This is an example of an affine movement. The relationship between the points is preserved! 



So, what will be an affine expression in this case? It's simple! It will just be a mathematical equation that describes this movement which keeps the relationship ( affine ) alive. So, the mathematical equation that describes all points between (0,0) and (1,1) will be, X=Y ( because for every point X and Y are equal  , right ? ) ----- Now we had decided to move the points on the X-axis by 5 points. So, how can we express this movement? Simple! We'll just say that X and Y were equal before X was moved by 5 points. So how to move back X by 5 units? Easy! We'll just subtract 5 from X. X-5. And so it's now equal to Y. Which makes the expression: Y = X - 5. 



The mathematical equation, Y = X - 5, is an example of an affine expression



Now, while doing the course you would have come across ( especially in the notebooks while using cvxpy )  something like:  



>>>> constraints = [ cp.sum(weights)==1 ]

Equality(Expression(AFFINE, UNKNOWN, ()), Constant(CONSTANT, NONNEGATIVE, ()))




Let's go line by line. The first line basically says something very simple, the sum of weights of all assets in your portfolio should be 1. weights is a variable created using cvxpy just for holding the weights ( or fraction of money)  you will assign to each asset in your portfolio. sum(weights) == 1 means that all these fractions should add to 1. Say, you invest 50% money in MSFT and 50% in AAPL. In that case, the fractions will be 1/2 and 1/2 right? And they should sum to 1. That is all we're saying in line 1!  



In the 2nd line, which is output by python. All the line is saying is, Equality means the sum of weights should be equal to 1. And in this equation, cp.sum(weights) == 1, on one side you have an expression ( cp.sum(weights) ) and on the other side, you have a constant (1) which is NONNEGATIVE. The expression is AFFINE, yes the same affine that we had discussed before! So, all its saying is, the sum of weights or fraction of money you put in different assets when summed together is just an affine expression. Meaning, that because we are yet to decide on how many fractions of our money we will put in each asset the weights or fractions are variable ( yet to be decided and so can vary). And hence, it will be some expression like X + Y + Z  = 1. Which looks just like the affine expression we had created earlier. 



Concave expressions are another kind of expressions just like the affine ones we discussed above. When we say expressions we basically mean some sort of mathematical operation(s) on a group of numbers or variables etc



>>>> portfolio_returns = (np.array(df_change)weights)

>>>> cp.sum(cp.log(1+portfolio_returns))

Expression(CONCAVE, UNKNOWN, ())




So, the first line is very simple we're basically multiplying the matrix of returns of each stock for multiple days with the vector of weights/fractions we put in that stock. For each day we will get one value of total returns, this we put in a vector called portfolio_returns. 



In the second line, we use this portfolio_returns vector add one to the returns of each day. And then taking the log of the value for each day in this vector: cp.log(1+portfolio_returns). Thereafter, all these log values are added cp.sum(cp.log(1+portfolio_returns)) to get the total.This adding on log values creates a concave expression. But why? 



Because normally to get total returns we would have not added but multiplied the portfolio returns. But because we did a log operation on each value here we added them. Because of log( A * B ) = log(A) + log(B). Hence, the total return would actually be some expression like x
yzabc where x, y, z, a, b, c are all daily returns. xyzab*c is not a linear expression that preserves the relationship between points, unlike affine. Turns out the sum of log values has a concave shape.  You can read more about them in this simple documentation



So, that's about it. Do tell me if you need more info or explanation Tanapat.