In [10]:
push!(LOAD_PATH, "$(homedir())/desktop/financial-opt-tools/src")
using JuPOT
# Generate synthetic data sets for Demonstration
############
# Assets
############
n = 10 # No. Of Assets
returns = rand(n)
covarariance = let
S = randn(n, n)
S'S + eye(n)
end
names = [randstring(3) for i in 1:n] # List of asset names
# Assets data structure containing, names, expected returns, covarariance
assets = AssetsCollection(names, returns, covarariance)
Out[10]:
In [11]:
######################################################
################### SIMPLE MVO #######################
######################################################
# Example 1: Basic
target_return = 0.2
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return; short_sale=false)
Out[11]:
In [12]:
optimize(mvo)
Out[12]:
In [55]:
##################################
# Example 2: Adding a Constraint #
##################################
function genTechIndicator()
[0,0,1,1,0,1,0,1,1,0]
end
# Adding a simple weight constraint
constraints = Dict((:constraint1 => :(dot(w,tech) <= tech_thresh)),
(:constraint2 => :(dot(w,fin) <= Fin_thresh)))
parameters = Dict(:tech=>genTechIndicator(),
:tech_thresh => 0.3,
:fin=> [1,1,0,0,1,0,1,0,0,0],
:Fin_thresh => 0.05)
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return, constraints; short_sale=false)
w = optimize(mvo, parameters)
# add checkers for constraints
Out[55]:
In [17]:
#######################################################
# Example 3: Changing a Constraint's parameter values #
#######################################################
# Changing values of an entered constraint
parameters[:tech_thresh] = 0.04
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return, constraints; short_sale=false)
optimize(mvo, parameters)
Out[17]:
In [19]:
####################################
# Example 4: Deleting a Constraint #
####################################
# Removing a previously defined constraint
delete!(constraints, :constraint2)
delete!(parameters, :Fin_thresh)
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return, constraints; short_sale=false)
optimize(mvo, parameters)
Out[19]:
In [23]:
##########################################
# Example 5: Adding multiple Constraints #
##########################################
# Adding a multiple weight constraints
# constraints = Dict(:constraint1 => :(w[1:5] .<= assets_1_5_max), :constraint2 => :(w[6:n] .<= assets_6_n_max))
# Adding a simple weight constraint
constraints_1 = [symbol("x$i") => :(min_thresh <= w[$i]) for i=1:n]
constraints_2 = [symbol("y$i") => :( w[$i] <= max_thresh) for i=1:n]
parameters_1 = Dict(:min_thresh => 0, :max_thresh => 0.5, :n => n)
# Different constraint sets can be merged to form new ones
constraints = merge(constraints,constraints_1,constraints_2)
parameters = merge(parameters,parameters_1)
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return, constraints; short_sale=false)
optimize(mvo, parameters)
Out[23]:
In [27]:
#####################################
# Example 6: Using Different Assets #
#####################################
############
# Asset #2 #
############
n = 10 # No. Of Assets
returns_new = rand(n)
covarariance_new = let
S = randn(n, n)
S'S + eye(n)
end
names_new = [randstring(3) for i in 1:n]
# Assets data structure containing, names, expected returns, covarariance
assets_new = AssetsCollection(names_new, returns_new, covarariance_new)
# Using the same previously defined constraints we can run the model on a different set of assets effortlessly
mvo = SimpleMVO(assets_new, target_return; short_sale=false)
optimize(mvo, parameters)
Out[27]:
In [28]:
# Forgot what constraints and parameters were defined for initial constraints? No Problem!
constraints # Prints the constraints
Out[28]:
In [29]:
parameters # prints the parameters
Out[29]:
In [30]:
# Using old constraints on new assets
mvo = SimpleMVO(assets_new, target_return, constraints; short_sale=false)
optimize(mvo, parameters)
Out[30]:
In [32]:
##############################
# Example 7 Using Robust MVO#
##############################
# refer to model definition for keyword arguments, etc
# If no uncertainty matrix is entered the model defaults
# to the ellipse whose axes are proportional to the
# individual variances of each asset
rmvo = RobustMVO(assets, target_return; short_sale=true)
optimize(rmvo, parameters)
Out[32]:
In [52]:
#################################################################
# Example 8 Creating Custom Functions (e.g efficient frontier) #
#################################################################
n = 20
risk = Array(Float32,n)
index = Array(Float32,n)
for i in 1:n
target_ret = i/21
mvo = SimpleMVO(assets, target_ret; short_sale=true)
optimize(mvo, parameters)
risk[i] = mvo.objVal
index[i] = target_ret
end
In [53]:
risk
Out[53]:
In [54]:
index
Out[54]:
In [ ]: