codelion's picture
Create app.py
18ee127 verified
raw
history blame
4.17 kB
import gradio as gr
# Define stocks with their events and impacts
stocks = {
"AAPL": {
"events": [
{"name": "New iPhone release", "impact_if_happens": 0.10, "impact_if_not": -0.05},
{"name": "Positive earnings report", "impact_if_happens": 0.08, "impact_if_not": -0.03},
]
},
"TSLA": {
"events": [
{"name": "Achieves production target", "impact_if_happens": 0.15, "impact_if_not": -0.10},
{"name": "New model announcement", "impact_if_happens": 0.12, "impact_if_not": -0.02},
]
},
"GOOG": {
"events": [
{"name": "AI breakthrough", "impact_if_happens": 0.20, "impact_if_not": -0.05},
{"name": "Regulatory approval", "impact_if_happens": 0.05, "impact_if_not": -0.15},
]
},
}
def calculate_portfolio(selected_stocks, total_investment,
aapl_event1_prob, aapl_event2_prob,
tsla_event1_prob, tsla_event2_prob,
goog_event1_prob, goog_event2_prob):
# Check if any stocks are selected
if not selected_stocks:
return "Please select at least one stock."
# Map user-input probabilities to stocks (convert from percentage to decimal)
probs = {
"AAPL": [aapl_event1_prob / 100, aapl_event2_prob / 100],
"TSLA": [tsla_event1_prob / 100, tsla_event2_prob / 100],
"GOOG": [goog_event1_prob / 100, goog_event2_prob / 100],
}
# Calculate expected return for each selected stock
expected_returns = {}
for stock in selected_stocks:
events = stocks[stock]["events"]
exp_return = 0
for i, event in enumerate(events):
p = probs[stock][i]
impact_happens = event["impact_if_happens"]
impact_not = event["impact_if_not"]
exp_return += p * impact_happens + (1 - p) * impact_not
expected_returns[stock] = exp_return
# Calculate portfolio metrics assuming equal investment
num_stocks = len(selected_stocks)
investment_per_stock = total_investment / num_stocks
portfolio_expected_return = sum(expected_returns[stock] for stock in selected_stocks) / num_stocks
expected_future_value = sum(investment_per_stock * (1 + expected_returns[stock]) for stock in selected_stocks)
# Format the output
output = "Expected Returns:\n"
for stock in selected_stocks:
output += f"{stock}: {expected_returns[stock]*100:.2f}%\n"
output += f"\nPortfolio Expected Return: {portfolio_expected_return*100:.2f}%\n"
output += f"Expected Future Value: ${expected_future_value:.2f}"
return output
# Define Gradio inputs
selected_stocks = gr.CheckboxGroup(choices=["AAPL", "TSLA", "GOOG"], label="Select Stocks")
total_investment = gr.Number(label="Total Investment ($)", value=10000)
# Probability sliders for each event
aapl_event1_prob = gr.Slider(0, 100, label="AAPL: Probability of New iPhone release (%)", value=50)
aapl_event2_prob = gr.Slider(0, 100, label="AAPL: Probability of Positive earnings report (%)", value=50)
tsla_event1_prob = gr.Slider(0, 100, label="TSLA: Probability of Achieving production target (%)", value=50)
tsla_event2_prob = gr.Slider(0, 100, label="TSLA: Probability of New model announcement (%)", value=50)
goog_event1_prob = gr.Slider(0, 100, label="GOOG: Probability of AI breakthrough (%)", value=50)
goog_event2_prob = gr.Slider(0, 100, label="GOOG: Probability of Regulatory approval (%)", value=50)
# Define output
output = gr.Textbox(label="Results")
# Create Gradio interface
iface = gr.Interface(
fn=calculate_portfolio,
inputs=[selected_stocks, total_investment,
aapl_event1_prob, aapl_event2_prob,
tsla_event1_prob, tsla_event2_prob,
goog_event1_prob, goog_event2_prob],
outputs=output,
title="Investment Decision Analysis POC",
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."
)
# Launch the app
if __name__ == "__main__":
iface.launch()