forked from dwave-training/portfolio-optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_1.py
104 lines (70 loc) · 3.16 KB
/
exercise_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright 2021 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: Import any required packages here
import utilities
def define_variables(stockcodes):
"""Define the variables to be used for the CQM.
Args:
stockcodes (list): List of stocks under consideration
Returns:
stocks (list):
List of variables named 's_{stk}' for each stock stk in stockcodes, where stk is replaced by the stock code.
"""
# TODO: Define your list of variables and call it stocks
## Hint: Remember to import the required package at the top of the file for Binary variables
return stocks
def define_cqm(stocks, num_stocks_to_buy, returns):
"""Define a CQM for the exercise.
Requirements:
Objective: Maximize returns
Constraints:
- Choose exactly num_stocks_to_buy stocks
Args:
stocks (list):
List of variables named 's_{stk}' for each stock in stockcodes
num_stocks_to_buy (int): Number of stocks to purchase
returns (list):
List of average monthly returns for each stock in stocks
where returns[i] is the average returns for stocks[i]
Returns:
cqm (ConstrainedQuadraticModel)
"""
# TODO: Initialize the ConstrainedQuadraticModel called cqm
## Hint: Remember to import the required package at the top of the file for ConstrainedQuadraticModels
# TODO: Add a constraint to choose exactly num_stocks_to_buy stocks
## Important: Use the label 'choose k stocks', this label is case sensitive
# TODO: Add an objective function maximize returns
## Hint: Use the information in returns, and remember to convert to minimization
return cqm
def sample_cqm(cqm):
# TODO: Define your sampler as LeapHybridCQMSampler
## Hint: Remember to import the required package at the top of the file
# TODO: Sample the ConstrainedQuadraticModel cqm and store the result in sampleset
return sampleset
if __name__ == '__main__':
# 10 stocks used in this program
stockcodes=["T", "SFL", "PFE", "XOM", "MO", "VZ", "IBM", "TSLA", "GILD", "GE"]
# Compute relevant statistics like price, average returns, and covariance
price, returns, variance = utilities.get_stock_info()
# Number of stocks to buy
num_stocks_to_buy = 2
# Add binary variables for stocks
stocks = define_variables(stockcodes)
# Build CQM
cqm = define_cqm(stocks, num_stocks_to_buy, returns)
# Run CQM on hybrid solver
sampleset = sample_cqm(cqm)
# Process and print solution
print("\nPart 1 solution:\n")
utilities.process_sampleset(sampleset, stockcodes)