In [90]:
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)*0.4
covariance = 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, covariance)
Out[90]:
In [2]:
######################################################
################### 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[2]:
In [3]:
optimize(mvo)
Out[3]:
In [63]:
##################################
# 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((:techClassWeightConstraint => :(dot(w,tech) <= tech_thresh)),
(:finClassWeightConstraint => :(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.5)
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return, constraints; short_sale=false)
w = optimize(mvo, parameters)
Out[63]:
In [64]:
# Constraint Checks
print("Tech Class Constraint: \n")
techClassWeight = dot(w, parameters[:tech])
print(techClassWeight, ", ", techClassWeight <= parameters[:tech_thresh])
print("\n")
print("Fin Class Constraint: \n")
finClassWeight = dot(w, parameters[:fin])
print(finClassWeight, ", ", finClassWeight <= parameters[:fin_thresh])
In [65]:
#######################################################
# 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.
w = optimize(mvo, parameters)
Out[65]:
In [66]:
# Constraint Checks
print("Tech Class Constraint: \n")
techClassWeight = dot(w, parameters[:tech])
print(techClassWeight, ", ", techClassWeight <= parameters[:tech_thresh])
print("\n")
print("Fin Class Constraint: \n")
finClassWeight = dot(w, parameters[:fin])
print(finClassWeight, ", ", finClassWeight <= parameters[:fin_thresh])
In [67]:
####################################
# Example 4: Deleting a Constraint #
####################################
# Removing a previously defined constraint
delete!(constraints, :techClassWeightConstraint)
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return, constraints; short_sale=false)
w = optimize(mvo, parameters)
Out[67]:
In [68]:
#Display Current Constraint Container
constraints
Out[68]:
In [69]:
# Constraint Checks
print("Tech Class Constraint: \n")
techClassWeight = dot(w, parameters[:tech])
print(techClassWeight, ", ", techClassWeight <= parameters[:tech_thresh])
print("\n")
print("Fin Class Constraint: \n")
finClassWeight = dot(w, parameters[:fin])
print(finClassWeight, ", ", finClassWeight <= parameters[:fin_thresh])
In [71]:
##########################################
# Example 5: Adding multiple Constraints #
##########################################
# Adding a multiple weight constraints
# Adding a simple weight constraint
assetClassThresholds = [0.5, 0.5, 0.5, 0.01, 0.01, 0.01]
assetsWeightConstraints = [symbol("assetWeightConstraint$i") => :(w[$i] <= $(assetClassThresholds[i])) for i=1:6]
# Different constraint sets can be merged to form new ones
new_constraints = merge(constraints, assetsWeightConstraints)
# refer to model definition for keyword arguments, etc.
mvo = SimpleMVO(assets, target_return, new_constraints; short_sale=false)
w = optimize(mvo, parameters)
Out[71]:
In [73]:
print("Fin Class Constraint: \n")
finClassWeight = dot(w, parameters[:fin])
print(finClassWeight, ", ", finClassWeight <= parameters[:fin_thresh])
print("\n")
print("Weights 1 - 3 Constraint: \n")
for i=1:3
print(w[i], ", ", w[i] <= 0.5)
print("\n")
end
print("Weights 4 - 6 Constraint: \n")
for i=4:6
print(w[i], ", ", w[i] <= 0.01)
print("\n")
end
In [74]:
#####################################
# Example 6: Using Different Assets #
#####################################
############
# Asset #2 #
############
n = 10 # No. Of Assets
returns_new = rand(n)
covariance_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, covariance_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[74]:
In [75]:
# Forgot what constraints and parameters were defined for initial constraints? No Problem!
constraints # Prints the constraints
Out[75]:
In [76]:
parameters # prints the parameters
Out[76]:
In [78]:
# It's good practice to remove unnecessary parameters
delete!(parameters, :tech)
delete!(parameters, :tech_thresh)
parameters
Out[78]:
In [ ]:
##############################
# 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)
In [92]:
#################################################################
# Example 8 Creating Custom Functions (e.g efficient frontier) #
#################################################################
n = 20
variance = Array(Float32,n)
returns = Array(Float32,n)
target_returns = linspace(0,0.4,20)
for i in 1:n
target_ret = target_returns[i]
mvo = SimpleMVO(assets, target_ret; short_sale=true)
w = optimize(mvo, parameters)
variance[i] = mvo.objVal
returns[i] = dot(w, JuPOT.getReturns(assets))
end
In [93]:
variance
Out[93]:
In [94]:
returns
Out[94]:
In [83]:
mean(JuPOT.getReturns(assets))
Out[83]:
In [89]:
mean(rand(10)*0.4)
Out[89]:
In [ ]: