Optimization
(Project !)
2 due
Sunday
AssessPortfolio -
list of stocks
(the old project t)
" " # 1 unknown
symbols & number
/ -
list of allocations
( list sums to 1)
→
inks J start date , end date
↳
-
- cash to invest
start date allocations
Invest all cash as
per
.
on
No further trades
Calculate portfolio value each
day
especially
.
end date Return portfolio statistics ,
through Ratio
.
Sharpe .
and method)
( see end of lecture notes 4 for #
pseudocode .
calculation these dates, stocks,
That's a useful one-way
:
given
cash and allocations ,
what is the performance?
But what about the other direction?
← given → find
Dates ,
cash , stocks → best alloaations
7 → 100% in best stock
cumul return
W!fKgft%M?min
Max . .
?→ don't invest?
risk or volatility
2 best risk adjusted
Sharpe Ratio
-
→
Max .
return!
This is a
good job it searches parameter
for an optimizer :
find parameters
to
space trying or maximize some
function
that minimize
Actually just minimizing
,
is
enough !
find the vector X that minimizes
So some IX) y .
y
-
,
given
-
X This is all an optimizer does
What does an
optimizer
do
internally ? Local search
- -
works great on convex functions .
[Link]
any two values compute f④=y x ,
,
between CX g.) and CK yd
draw a line . , , .
? Examples
Is
-
entire line ab_wefCxS=y it
parabola?yes
If yes everywhere I cubic? no
x
the slope
-
? no
o
¥y
-
optimizer is multiple
If convex ,
to return minima ? no KW
guaranteedcorrect answer
a
"
"
unique is not atone
Cie one )
true minimum en f Cx)=y
epsilon E f
.
within some selected =y
neural network is X
Bythe way ,
a ,
¥
just a function ! Xz
'
sin: in:#em:c:#urate .
predictions optimizer
,
so the
should minimize the difference fly)=Y
between predictions
and
reality !
So the optimization function should use
ypredicted & Tactual
.is#n:[Link]. Y. YtIa=[Link] Iediotias
difference would betsad
a
→
The plain
er's:* differences
errors cancel
out weird
opposing
-
)
all errors proportional
1) sum of abs (errors ←
might
:
use
So we
2) sum of squared errors
worse errors
←
count more
3) root mean squared error (disproportionately)
Now project 2 is
easy
to explain .
I write
.
a
Python function : Halloa) SharpeRatio →
to find a Hoos that maximize SR
2 . use
optimizer .
the function from I
#
by repeatedly calling .
Just don't forget
:
allocate
can 't less than 08 or more
a) you
than 1008 to a stock
b) total allocations should be 100%
your( want to invest all your cash)
optimize minimize C) ← our optimizer
Scipy .
.
real defined function
" "
functionto minimize a lambda or a
f.
,
(local search needs
¥ an initial parameter
•
setting always
state) current we ask you to use YN
[
" -
an a
TD numpy array length number of stocks
. -
→ er
-
a ,
* it •
an optimization method
-
we use SLSQP
row )
(sequential least squares
most scipy
is bounds & constraints and
why? we need ,
both of those
↳ methods dont
optimization support .
[Link]
of 0.730.2 that's 1508 of
: 0.6 ,
- our cash .
So we need a constraint : sum Callas) -
- 1.0
Then our optimizer might return : 0.9 , 0.3 , -0.2
weird ( we haven't
Google
'' "
-
invest -20% in is
talked about shorting stocks yet
so each allocation needs bounds [o B ,
How can we
specify those to
scipy?
bounds : a sequence leg . list or
tuple)
of 2 tuples (pairs one per
unbounded)
- ,
"
in order ( None for
"
parameter ,
.
e.s
(( Max,) (Mine
)
Max,)
'
Ma mins ,
bounds = min ,
, ,
,
of diets (one diet per
)
constraint
a list
constraints :
in dict? fun , type
' '
' '
minimum keys required
defined function the constraint
(
'
fun
'
lambda
= or Python
'
' ' '
i
'
eg or neg
type
' =
t t
fun = o fun Z 0
If need e or constants
These two options you
the
only zero , adapt fan to work!
'
are .
besides
example constraint? X
,
the E I
←
[Link]
(X ,tXz) Z I ←
mathematically
-
equivalent
-
ntsisffemone
use
→ -
( x tutti
,
20
"
I can
'
ineg
'
and fun =
Cx,txz)t1
type
-
so I set
-_
pou¥q¥¥ X, Xz [Link]
It
?
If V
?
+ I
Zo ?
µ
,gversiM2-1iV-#
constraint X SAME z 2 4 - 4 X -3 X
fail ✓ V
* or l ✓
paradigm
O -
I I z
-
pass .
o V V V
o o o ,