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run("rm -rf /home/user/.cache/pip/*", shell=True)
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
os.system("pip install --no-cache-dir matplotlib")
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import pandas as pd
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
os.system("pip install --no-cache-dir numpy==1.23.5 spacy==3.5.3 thinc==8.1.8 pydantic<2.0")
|
| 18 |
+
import spacy
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def extract_equation_params(text):
|
| 22 |
+
"""
|
| 23 |
+
Extracts demand and supply equation parameters from a structured natural language query.
|
| 24 |
+
"""
|
| 25 |
+
doc = nlp(text.lower())
|
| 26 |
+
numbers = [float(token.text) for token in doc if token.like_num]
|
| 27 |
+
|
| 28 |
+
if len(numbers) < 14:
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
demand_intercept_corn, demand_slope_corn, supply_intercept_corn, supply_slope_corn, \
|
| 32 |
+
supply_intercept_prod_pay, supply_slope_prod_pay, \
|
| 33 |
+
demand_intercept_cleaned, demand_slope_cleaned, supply_intercept_cleaned, supply_slope_cleaned, \
|
| 34 |
+
demand_intercept_no_loading, demand_slope_no_loading, supply_intercept_no_loading, supply_slope_no_loading = numbers[:14]
|
| 35 |
+
|
| 36 |
+
demand_slope_corn = -abs(demand_slope_corn)
|
| 37 |
+
demand_slope_cleaned = -abs(demand_slope_cleaned)
|
| 38 |
+
demand_slope_no_loading = -abs(demand_slope_no_loading)
|
| 39 |
+
|
| 40 |
+
return demand_intercept_corn, demand_slope_corn, supply_intercept_corn, supply_slope_corn, \
|
| 41 |
+
supply_intercept_prod_pay, supply_slope_prod_pay, \
|
| 42 |
+
demand_intercept_cleaned, demand_slope_cleaned, supply_intercept_cleaned, supply_slope_cleaned, \
|
| 43 |
+
demand_intercept_no_loading, demand_slope_no_loading, supply_intercept_no_loading, supply_slope_no_loading
|
| 44 |
+
|
| 45 |
+
def calculate_equilibrium(demand_intercept, demand_slope, supply_intercept, supply_slope):
|
| 46 |
+
"""
|
| 47 |
+
Computes Competitive Equilibrium: Quantity and Price.
|
| 48 |
+
Also calculates CS, PS, and SW for the competitive market.
|
| 49 |
+
"""
|
| 50 |
+
quantity_eq = (demand_intercept - supply_intercept) / (supply_slope - demand_slope)
|
| 51 |
+
price_eq = demand_intercept + demand_slope * quantity_eq
|
| 52 |
+
|
| 53 |
+
cs_eq = (demand_intercept - price_eq) * quantity_eq / 2
|
| 54 |
+
ps_eq = (price_eq - supply_intercept) * quantity_eq / 2
|
| 55 |
+
sw_eq = cs_eq + ps_eq
|
| 56 |
+
|
| 57 |
+
return round(quantity_eq, 2), round(price_eq, 2), round(cs_eq, 2), round(ps_eq, 2), round(sw_eq, 2)
|
| 58 |
+
|
| 59 |
+
def plot_market(demand_intercept, demand_slope, supply_intercept, supply_slope, quantity_eq, price_eq, market_name,
|
| 60 |
+
supply_intercept_alt=None, supply_slope_alt=None):
|
| 61 |
+
"""
|
| 62 |
+
Plots the supply and demand curves for a given market.
|
| 63 |
+
"""
|
| 64 |
+
q_range = np.linspace(0, quantity_eq * 1.5, 100)
|
| 65 |
+
demand_curve = demand_intercept + demand_slope * q_range
|
| 66 |
+
supply_curve_primary = supply_intercept + supply_slope * q_range
|
| 67 |
+
supply_curve_alt = supply_intercept_alt + supply_slope_alt * q_range if supply_intercept_alt is not None else None
|
| 68 |
+
|
| 69 |
+
plt.figure(figsize=(8, 6))
|
| 70 |
+
plt.plot(q_range, demand_curve, label="Demand Curve", color="blue")
|
| 71 |
+
plt.plot(q_range, supply_curve_primary, label="Supply Curve (Original)", color="green")
|
| 72 |
+
if supply_curve_alt is not None:
|
| 73 |
+
plt.plot(q_range, supply_curve_alt, label="Supply Curve (Alternative)", color="red", linestyle="dashed")
|
| 74 |
+
|
| 75 |
+
plt.xlabel("Quantity")
|
| 76 |
+
plt.ylabel("Price")
|
| 77 |
+
plt.title(f"Market Equilibrium: {market_name}")
|
| 78 |
+
plt.legend()
|
| 79 |
+
st.pyplot(plt)
|
| 80 |
+
|
| 81 |
+
# Streamlit Interface
|
| 82 |
+
st.title("Market Equilibrium Solver")
|
| 83 |
+
|
| 84 |
+
st.write("Enter a question describing supply and demand equations for corn and water.")
|
| 85 |
+
user_query = st.text_area(
|
| 86 |
+
"Type your question here:",
|
| 87 |
+
value="Corn market demand: intercept 100, slope -2. Original Corn supply (water consumers pay): intercept 20, slope 3. Intervene Corn supply (corn producer pays): intercept 30, slope 3.\n"
|
| 88 |
+
"Original Sceanrio (water consumer pays): Water Demand has intercept 200, slope -4. Water Supply has intercept 64.38, slope 2.1.\n"
|
| 89 |
+
"Intervene Scenario (corn producer pays): Water demand has intercept 200, slope -4. Water supply has intercept 44.91, slope 2.1."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if st.button("Solve"):
|
| 94 |
+
params = extract_equation_params(user_query)
|
| 95 |
+
|
| 96 |
+
if params:
|
| 97 |
+
demand_intercept_corn, demand_slope_corn, supply_intercept_corn, supply_slope_corn, \
|
| 98 |
+
supply_intercept_prod_pay, supply_slope_prod_pay, \
|
| 99 |
+
demand_intercept_cleaned, demand_slope_cleaned, supply_intercept_cleaned, supply_slope_cleaned, \
|
| 100 |
+
demand_intercept_no_loading, demand_slope_no_loading, supply_intercept_no_loading, supply_slope_no_loading = params
|
| 101 |
+
|
| 102 |
+
# Compute Equilibria for Corn
|
| 103 |
+
quantity_eq_corn, price_eq_corn, cs_eq_corn, ps_eq_corn, sw_eq_corn = \
|
| 104 |
+
calculate_equilibrium(demand_intercept_corn, demand_slope_corn, supply_intercept_corn, supply_slope_corn)
|
| 105 |
+
|
| 106 |
+
quantity_eq_corn_prod_pay, price_eq_corn_prod_pay, cs_eq_corn_prod_pay, ps_eq_corn_prod_pay, sw_eq_corn_prod_pay = \
|
| 107 |
+
calculate_equilibrium(demand_intercept_corn, demand_slope_corn, supply_intercept_prod_pay, supply_slope_prod_pay)
|
| 108 |
+
|
| 109 |
+
# Compute Equilibria for Water
|
| 110 |
+
quantity_eq_cleaned, price_eq_cleaned, cs_eq_cleaned, ps_eq_cleaned, sw_eq_cleaned = \
|
| 111 |
+
calculate_equilibrium(demand_intercept_cleaned, demand_slope_cleaned, supply_intercept_cleaned, supply_slope_cleaned)
|
| 112 |
+
|
| 113 |
+
quantity_eq_no_loading, price_eq_no_loading, cs_eq_no_loading, ps_eq_no_loading, sw_eq_no_loading = \
|
| 114 |
+
calculate_equilibrium(demand_intercept_no_loading, demand_slope_no_loading, supply_intercept_no_loading, supply_slope_no_loading)
|
| 115 |
+
|
| 116 |
+
# Display Equilibrium Prices and Quantities
|
| 117 |
+
st.write("\n**Equilibrium Prices and Quantities:**")
|
| 118 |
+
st.write(f"- **Corn Mkt(Original:Water Consumer Pay):** Price: ${price_eq_corn}, Quantity: {quantity_eq_corn} units")
|
| 119 |
+
st.write(f"- **Corn Mkt(Intervene:Corn Producer Pays):** Price: ${price_eq_corn_prod_pay}, Quantity: {quantity_eq_corn_prod_pay} units")
|
| 120 |
+
st.write(f"- **Water Market(Original:Water Consumer Pays):** Price: ${price_eq_cleaned}, Quantity: {quantity_eq_cleaned} units")
|
| 121 |
+
st.write(f"- **Water Mkt:(intervene:Corn Producer Pays):** Price: ${price_eq_no_loading}, Quantity: {quantity_eq_no_loading} units")
|
| 122 |
+
|
| 123 |
+
# Plot Graphs for Corn with both supply curves
|
| 124 |
+
plot_market(demand_intercept_corn, demand_slope_corn, supply_intercept_corn, supply_slope_corn,
|
| 125 |
+
quantity_eq_corn, price_eq_corn, "Corn Market", supply_intercept_prod_pay, supply_slope_prod_pay)
|
| 126 |
+
|
| 127 |
+
# Plot Graphs for Water with both supply curves
|
| 128 |
+
plot_market(demand_intercept_cleaned, demand_slope_cleaned, supply_intercept_cleaned, supply_slope_cleaned,
|
| 129 |
+
quantity_eq_cleaned, price_eq_cleaned, "Water Market", supply_intercept_no_loading, supply_slope_no_loading)
|
| 130 |
+
|
| 131 |
+
# Display Surplus Tables
|
| 132 |
+
st.write("\n**Surplus Tables:**")
|
| 133 |
+
st.table(pd.DataFrame({
|
| 134 |
+
"Scenario": ["Orig Corn Mkt:Water Cons Pay", "Intervene Corn Mkt:Corn Prod Pay)", "Difference"],
|
| 135 |
+
"Consumer Surplus": [cs_eq_corn, cs_eq_corn_prod_pay, cs_eq_corn_prod_pay - cs_eq_corn],
|
| 136 |
+
"Producer Surplus": [ps_eq_corn, ps_eq_corn_prod_pay, ps_eq_corn_prod_pay - ps_eq_corn],
|
| 137 |
+
"Total Social Welfare": [sw_eq_corn, sw_eq_corn_prod_pay, sw_eq_corn_prod_pay - sw_eq_corn]
|
| 138 |
+
}))
|
| 139 |
+
|
| 140 |
+
st.table(pd.DataFrame({
|
| 141 |
+
"Scenario": ["Orig Water Mkt:Water Cons Pay", "Intervene Water Mkt:Corn Prod Pay", "Difference"],
|
| 142 |
+
"Consumer Surplus": [cs_eq_cleaned, cs_eq_no_loading, cs_eq_no_loading - cs_eq_cleaned],
|
| 143 |
+
"Producer Surplus": [ps_eq_cleaned, ps_eq_no_loading, ps_eq_no_loading - ps_eq_cleaned],
|
| 144 |
+
"Total Social Welfare": [sw_eq_cleaned, sw_eq_no_loading, sw_eq_no_loading - sw_eq_cleaned]
|
| 145 |
+
}))
|