analytics
Browse files
aiOpt.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
class Asset():
|
5 |
+
def __init__(self, cost, prob):
|
6 |
+
"""Create a new state space with given dimensions."""
|
7 |
+
self.cost = cost
|
8 |
+
self.prob = prob
|
9 |
+
self.volume = []
|
10 |
+
print("cost: ", cost)
|
11 |
+
print("prob: ", prob)
|
12 |
+
print("Assume volume is bound by 0 to 100")
|
13 |
+
print("Objective: max SUM [ vol_s * prob_s - cost_s ]")
|
14 |
+
|
15 |
+
def hill_climb(self, maximum=None, log=False):
|
16 |
+
"""Performs hill-climbing to find a solution."""
|
17 |
+
count = 0
|
18 |
+
|
19 |
+
self.volume=np.random.randint(0,100,len(self.prob))
|
20 |
+
|
21 |
+
if log:
|
22 |
+
print("Initial state: profit", self.get_profit(self.volume))
|
23 |
+
|
24 |
+
# Continue until we reach maximum number of iterations
|
25 |
+
while maximum is None or count < maximum:
|
26 |
+
count += 1
|
27 |
+
best_neighbors = []
|
28 |
+
best_neighbor_profit = None
|
29 |
+
|
30 |
+
for i,vol_s in enumerate(self.volume):
|
31 |
+
|
32 |
+
for replacement in self.get_neighbors(vol_s):
|
33 |
+
neighbor = self.volume.copy()
|
34 |
+
neighbor[i] = replacement
|
35 |
+
|
36 |
+
# Check if neighbor is best so far
|
37 |
+
profit = self.get_profit(neighbor)
|
38 |
+
if best_neighbor_profit is None or profit > best_neighbor_profit:
|
39 |
+
best_neighbor_profit = profit
|
40 |
+
best_neighbors = [neighbor]
|
41 |
+
elif best_neighbor_profit == profit:
|
42 |
+
best_neighbors.append(neighbor)
|
43 |
+
|
44 |
+
# None of the neighbors are better than the current state
|
45 |
+
if best_neighbor_profit <= self.get_profit(self.volume):
|
46 |
+
return self.volume
|
47 |
+
|
48 |
+
# Move to a highest-valued neighbor
|
49 |
+
else:
|
50 |
+
if log:
|
51 |
+
print(f"Found better neighbor: cost {best_neighbor_profit}")
|
52 |
+
self.volume = random.choice(best_neighbors)
|
53 |
+
|
54 |
+
|
55 |
+
def random_restart(self, maximum, image_prefix=None, log=False):
|
56 |
+
"""Repeats hill-climbing multiple times."""
|
57 |
+
best_volume = None
|
58 |
+
best_profit = None
|
59 |
+
|
60 |
+
# Repeat hill-climbing a fixed number of times
|
61 |
+
for i in range(maximum):
|
62 |
+
volume = self.hill_climb()
|
63 |
+
profit = self.get_profit(volume)
|
64 |
+
if best_profit is None or profit > best_profit:
|
65 |
+
best_profit = profit
|
66 |
+
best_volume = volume
|
67 |
+
if log:
|
68 |
+
print(f"{i}: Found new best state: profit {profit} with volume {volume}")
|
69 |
+
else:
|
70 |
+
if log:
|
71 |
+
print(f"{i}: Found state: profit {profit}")
|
72 |
+
|
73 |
+
return best_volume
|
74 |
+
|
75 |
+
def get_profit(self, volume):
|
76 |
+
profit = 0
|
77 |
+
for i,vol_s in enumerate(self.volume):
|
78 |
+
profit += vol_s * self.prob[i] - self.cost[i]
|
79 |
+
return profit
|
80 |
+
|
81 |
+
def get_neighbors(self, vol_s):
|
82 |
+
candidates = [
|
83 |
+
vol_s-5, vol_s+5
|
84 |
+
]
|
85 |
+
neighbors = []
|
86 |
+
for c in candidates:
|
87 |
+
if 0 <= c < 100:
|
88 |
+
neighbors.append(c)
|
89 |
+
return neighbors
|
90 |
+
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
|
94 |
+
num_assets=10
|
95 |
+
s = Asset(np.random.randint(0, 10, num_assets),
|
96 |
+
np.random.rand(num_assets))
|
97 |
+
|
98 |
+
s.random_restart(10, log=True)
|
app.py
CHANGED
@@ -7,6 +7,16 @@ https://huggingface.co/spaces/kevinhug/clientX
|
|
7 |
https://hits.seeyoufarm.com/
|
8 |
'''
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
'''
|
11 |
TIME SERIES ANALYTICS
|
12 |
'''
|
@@ -257,13 +267,14 @@ By identifying growth and decline industries, traders can make informed investme
|
|
257 |
we can use this to filter out blacklisted firms for compliance
|
258 |
|
259 |
- objective function can identify potential industry
|
|
|
260 |
|
261 |
#### Personalize UI to fit each trader
|
262 |
-
here is my https://public.tableau.com/app/profile/kevin1619
|
263 |
-
|
264 |
-
customize UI for secret sauce formula for stock picking:
|
265 |
-
- metric: moving average for the price, moving average for volume, ...etc
|
266 |
-
- timeframe chain: monthly, weekly, daily, 4h, 15 min
|
267 |
|
268 |
#### Personalize Alert with Twilio
|
269 |
The trader can set their price to buy at the right price at the right time, without missing the right entry in a high stress environment
|
@@ -271,5 +282,52 @@ The trader can set their price to buy at the right price at the right time, with
|
|
271 |
|
272 |
""")
|
273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
demo.launch()
|
275 |
|
|
|
7 |
https://hits.seeyoufarm.com/
|
8 |
'''
|
9 |
|
10 |
+
'''
|
11 |
+
PORTFOLIO OPTIMIZATION
|
12 |
+
'''
|
13 |
+
from .aiOpt import Asset
|
14 |
+
import numpy as np
|
15 |
+
def optimize(cost, prob, its):
|
16 |
+
s = Asset(np.asfarray(cost.split()),
|
17 |
+
np.asfarray(prob.split()))
|
18 |
+
|
19 |
+
return s.random_restart(int(its))
|
20 |
'''
|
21 |
TIME SERIES ANALYTICS
|
22 |
'''
|
|
|
267 |
we can use this to filter out blacklisted firms for compliance
|
268 |
|
269 |
- objective function can identify potential industry
|
270 |
+
|
271 |
|
272 |
#### Personalize UI to fit each trader
|
273 |
+
here is my https://public.tableau.com/app/profile/kevin1619
|
274 |
+
|
275 |
+
customize UI for secret sauce formula for stock picking:
|
276 |
+
- metric: moving average for the price, moving average for volume, ...etc
|
277 |
+
- timeframe chain: monthly, weekly, daily, 4h, 15 min
|
278 |
|
279 |
#### Personalize Alert with Twilio
|
280 |
The trader can set their price to buy at the right price at the right time, without missing the right entry in a high stress environment
|
|
|
282 |
|
283 |
""")
|
284 |
|
285 |
+
|
286 |
+
|
287 |
+
with gr.Tab("Portfolio Optimization"):
|
288 |
+
in_p_cost = gr.Textbox(placeholder="4 30 2 3 5",
|
289 |
+
label="Cost",
|
290 |
+
info="cost for the asset"
|
291 |
+
)
|
292 |
+
in_p_prob = gr.Textbox(placeholder="0.3 0.4 0.5 0.6 0.7",
|
293 |
+
label="Probabilities",
|
294 |
+
info="P(success) for the asset"
|
295 |
+
)
|
296 |
+
in_p_its = gr.Textbox(placeholder="10",
|
297 |
+
label="Number of Iteration",
|
298 |
+
info="number of trial for optimal"
|
299 |
+
)
|
300 |
+
out_p = gr.Textbox(label="Asset Allocation Approx Optimization using AI")
|
301 |
+
|
302 |
+
btn_p = gr.Button("Optimize Asset Allocation")
|
303 |
+
btn_p.click(fn=optimize, inputs=[in_p_cost, in_p_prob, in_p_its], outputs=out_p)
|
304 |
+
|
305 |
+
gr.Markdown("""
|
306 |
+
Objective: To allocate assets in a way that maximizes expected profit while minimizing costs and risks.
|
307 |
+
|
308 |
+
Inputs:
|
309 |
+
- List of available assets and their associated probabilities and costs.
|
310 |
+
|
311 |
+
Outputs:
|
312 |
+
- Allocation of assets that maximizes expected profit while minimizing costs and risks.
|
313 |
+
|
314 |
+
Constraints:
|
315 |
+
Assume volume is bound by 0 to 100
|
316 |
+
Objective: max SUM [ vol_s * prob_s - cost_s ]
|
317 |
+
|
318 |
+
- The total cost of the allocation must not exceed the available budget.
|
319 |
+
- The risk level of the allocation must not exceed the maximum allowable risk level.
|
320 |
+
- The profit margin of the allocation must not fall below the minimum allowable profit margin.
|
321 |
+
|
322 |
+
Method:
|
323 |
+
Using search algorithm to find the approx. optimial allocation
|
324 |
+
|
325 |
+
Assumptions:
|
326 |
+
- The probabilities and costs of the assets are known with certainty.
|
327 |
+
- The expected return and risk level of the allocation are calculated using historical data and statistical models.
|
328 |
+
|
329 |
+
|
330 |
+
"""
|
331 |
+
|
332 |
demo.launch()
|
333 |
|