codelion commited on
Commit
18ee127
·
verified ·
1 Parent(s): 8ef3413

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # Define stocks with their events and impacts
4
+ stocks = {
5
+ "AAPL": {
6
+ "events": [
7
+ {"name": "New iPhone release", "impact_if_happens": 0.10, "impact_if_not": -0.05},
8
+ {"name": "Positive earnings report", "impact_if_happens": 0.08, "impact_if_not": -0.03},
9
+ ]
10
+ },
11
+ "TSLA": {
12
+ "events": [
13
+ {"name": "Achieves production target", "impact_if_happens": 0.15, "impact_if_not": -0.10},
14
+ {"name": "New model announcement", "impact_if_happens": 0.12, "impact_if_not": -0.02},
15
+ ]
16
+ },
17
+ "GOOG": {
18
+ "events": [
19
+ {"name": "AI breakthrough", "impact_if_happens": 0.20, "impact_if_not": -0.05},
20
+ {"name": "Regulatory approval", "impact_if_happens": 0.05, "impact_if_not": -0.15},
21
+ ]
22
+ },
23
+ }
24
+
25
+ def calculate_portfolio(selected_stocks, total_investment,
26
+ aapl_event1_prob, aapl_event2_prob,
27
+ tsla_event1_prob, tsla_event2_prob,
28
+ goog_event1_prob, goog_event2_prob):
29
+ # Check if any stocks are selected
30
+ if not selected_stocks:
31
+ return "Please select at least one stock."
32
+
33
+ # Map user-input probabilities to stocks (convert from percentage to decimal)
34
+ probs = {
35
+ "AAPL": [aapl_event1_prob / 100, aapl_event2_prob / 100],
36
+ "TSLA": [tsla_event1_prob / 100, tsla_event2_prob / 100],
37
+ "GOOG": [goog_event1_prob / 100, goog_event2_prob / 100],
38
+ }
39
+
40
+ # Calculate expected return for each selected stock
41
+ expected_returns = {}
42
+ for stock in selected_stocks:
43
+ events = stocks[stock]["events"]
44
+ exp_return = 0
45
+ for i, event in enumerate(events):
46
+ p = probs[stock][i]
47
+ impact_happens = event["impact_if_happens"]
48
+ impact_not = event["impact_if_not"]
49
+ exp_return += p * impact_happens + (1 - p) * impact_not
50
+ expected_returns[stock] = exp_return
51
+
52
+ # Calculate portfolio metrics assuming equal investment
53
+ num_stocks = len(selected_stocks)
54
+ investment_per_stock = total_investment / num_stocks
55
+ portfolio_expected_return = sum(expected_returns[stock] for stock in selected_stocks) / num_stocks
56
+ expected_future_value = sum(investment_per_stock * (1 + expected_returns[stock]) for stock in selected_stocks)
57
+
58
+ # Format the output
59
+ output = "Expected Returns:\n"
60
+ for stock in selected_stocks:
61
+ output += f"{stock}: {expected_returns[stock]*100:.2f}%\n"
62
+ output += f"\nPortfolio Expected Return: {portfolio_expected_return*100:.2f}%\n"
63
+ output += f"Expected Future Value: ${expected_future_value:.2f}"
64
+ return output
65
+
66
+ # Define Gradio inputs
67
+ selected_stocks = gr.CheckboxGroup(choices=["AAPL", "TSLA", "GOOG"], label="Select Stocks")
68
+ total_investment = gr.Number(label="Total Investment ($)", value=10000)
69
+
70
+ # Probability sliders for each event
71
+ aapl_event1_prob = gr.Slider(0, 100, label="AAPL: Probability of New iPhone release (%)", value=50)
72
+ aapl_event2_prob = gr.Slider(0, 100, label="AAPL: Probability of Positive earnings report (%)", value=50)
73
+ tsla_event1_prob = gr.Slider(0, 100, label="TSLA: Probability of Achieving production target (%)", value=50)
74
+ tsla_event2_prob = gr.Slider(0, 100, label="TSLA: Probability of New model announcement (%)", value=50)
75
+ goog_event1_prob = gr.Slider(0, 100, label="GOOG: Probability of AI breakthrough (%)", value=50)
76
+ goog_event2_prob = gr.Slider(0, 100, label="GOOG: Probability of Regulatory approval (%)", value=50)
77
+
78
+ # Define output
79
+ output = gr.Textbox(label="Results")
80
+
81
+ # Create Gradio interface
82
+ iface = gr.Interface(
83
+ fn=calculate_portfolio,
84
+ inputs=[selected_stocks, total_investment,
85
+ aapl_event1_prob, aapl_event2_prob,
86
+ tsla_event1_prob, tsla_event2_prob,
87
+ goog_event1_prob, goog_event2_prob],
88
+ outputs=output,
89
+ title="Investment Decision Analysis POC",
90
+ description="Select stocks and adjust event probabilities to analyze expected returns based on simulated prediction market data. Event impacts are predefined. Click 'Submit' to see results."
91
+ )
92
+
93
+ # Launch the app
94
+ if __name__ == "__main__":
95
+ iface.launch()