eagle0504 commited on
Commit
0fb1235
·
verified ·
1 Parent(s): 97aadea

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -57,7 +57,7 @@ def calculate_metrics(a, b, c, d, price_control=None):
57
  Q_supplied_at_pc = (price_control - a) / b
58
 
59
  # The actual quantity traded is the minimum of Qd and Qs at the controlled price,
60
- # ensuring it\'s non-negative (cannot trade negative quantity).
61
  Q_traded = min(max(0, Q_demanded_at_pc), max(0, Q_supplied_at_pc))
62
 
63
  # Calculate DWL if trade is restricted from equilibrium
@@ -104,7 +104,7 @@ col1, col2 = st.columns([1, 2])
104
  with col1:
105
  st.header("📈 Economic Metrics")
106
  if Q_eq == 0 and P_eq == 0 and CS == 0: # Indicates invalid parameters from calculate_metrics
107
- st.error("Invalid parameters: Please adjust \'c\' to be greater than \'a\' to ensure a valid equilibrium.")
108
  else:
109
  st.metric("Equilibrium Quantity (Qe)", f"{Q_eq:.2f}")
110
  st.metric("Equilibrium Price (Pe)", f"${P_eq:.2f}")
@@ -122,7 +122,7 @@ with col1:
122
  else:
123
  st.info("No Deadweight Loss with current enforced price (or price is ineffective).")
124
  else:
125
- st.info("Set \'Enforced Price\' to calculate Deadweight Loss.")
126
 
127
 
128
  with col2:
@@ -141,16 +141,16 @@ with col2:
141
  P_supply = a + b * Q_values
142
 
143
  # Filter out negative prices/quantities for plotting realism
144
- P_demand[P_demand < 0] = np.nan # Don\'t plot negative prices
145
- P_supply[P_supply < 0] = np.nan # Don\'t plot negative prices
146
- Q_values[Q_values < 0] = np.nan # Don\'t plot negative quantities
147
 
148
- ax.plot(Q_values, P_demand, label=f\'Demand: P = {c:.1f} - {d:.1f}Q\', color=\'blue\', linewidth=2)
149
- ax.plot(Q_values, P_supply, label=f\'Supply: P = {a:.1f} + {b:.1f}Q\', color=\'red\', linewidth=2)
150
 
151
  # Plot Equilibrium Point and lines
152
  if Q_eq > 0 and P_eq > 0:
153
- ax.plot(Q_eq, P_eq, 'go', markersize=8, label=f\'Equilibrium (Q={Q_eq:.2f}, P=${P_eq:.2f})\')
154
  ax.vlines(Q_eq, 0, P_eq, linestyle=':', color='gray', linewidth=1)
155
  ax.hlines(P_eq, 0, Q_eq, linestyle=':', color='gray', linewidth=1)
156
 
@@ -166,7 +166,7 @@ with col2:
166
 
167
  # Plot Price Control and Deadweight Loss
168
  if price_control > 0 and Q_traded < Q_eq:
169
- ax.hlines(pc_used, 0, Q_traded, linestyle='--', color='purple', label=f\'Enforced Price (${pc_used:.2f})\', linewidth=1.5)
170
  ax.vlines(Q_traded, 0, pc_used, linestyle='--', color='purple', linewidth=1.5)
171
 
172
  # Identify the points for the DWL triangle
@@ -185,8 +185,8 @@ with col2:
185
  ax.fill_between(Q_dwl_fill, P_supply_dwl_fill, P_demand_dwl_fill, color='red', alpha=0.5, label='Deadweight Loss')
186
 
187
  # Mark points relevant to price control
188
- # ax.plot(Q_traded, c - d * Q_traded, 'kx', markersize=8, label=f\'Demand at PC ({Q_traded:.2f})\')
189
- # ax.plot(Q_traded, a + b * Q_traded, 'bx', markersize=8, label=f\'Supply at PC ({Q_traded:.2f})\')
190
 
191
  ax.set_xlabel("Quantity (Q)", fontsize=12)
192
  ax.set_ylabel("Price (P)", fontsize=12)
 
57
  Q_supplied_at_pc = (price_control - a) / b
58
 
59
  # The actual quantity traded is the minimum of Qd and Qs at the controlled price,
60
+ # ensuring it's non-negative (cannot trade negative quantity).
61
  Q_traded = min(max(0, Q_demanded_at_pc), max(0, Q_supplied_at_pc))
62
 
63
  # Calculate DWL if trade is restricted from equilibrium
 
104
  with col1:
105
  st.header("📈 Economic Metrics")
106
  if Q_eq == 0 and P_eq == 0 and CS == 0: # Indicates invalid parameters from calculate_metrics
107
+ st.error("Invalid parameters: Please adjust 'c' to be greater than 'a' to ensure a valid equilibrium.")
108
  else:
109
  st.metric("Equilibrium Quantity (Qe)", f"{Q_eq:.2f}")
110
  st.metric("Equilibrium Price (Pe)", f"${P_eq:.2f}")
 
122
  else:
123
  st.info("No Deadweight Loss with current enforced price (or price is ineffective).")
124
  else:
125
+ st.info("Set 'Enforced Price' to calculate Deadweight Loss.")
126
 
127
 
128
  with col2:
 
141
  P_supply = a + b * Q_values
142
 
143
  # Filter out negative prices/quantities for plotting realism
144
+ P_demand[P_demand < 0] = np.nan # Don't plot negative prices
145
+ P_supply[P_supply < 0] = np.nan # Don't plot negative prices
146
+ Q_values[Q_values < 0] = np.nan # Don't plot negative quantities
147
 
148
+ ax.plot(Q_values, P_demand, label=f'Demand: P = {c:.1f} - {d:.1f}Q', color='blue', linewidth=2)
149
+ ax.plot(Q_values, P_supply, label=f'Supply: P = {a:.1f} + {b:.1f}Q', color='red', linewidth=2)
150
 
151
  # Plot Equilibrium Point and lines
152
  if Q_eq > 0 and P_eq > 0:
153
+ ax.plot(Q_eq, P_eq, 'go', markersize=8, label=f'Equilibrium (Q={Q_eq:.2f}, P=${P_eq:.2f})')
154
  ax.vlines(Q_eq, 0, P_eq, linestyle=':', color='gray', linewidth=1)
155
  ax.hlines(P_eq, 0, Q_eq, linestyle=':', color='gray', linewidth=1)
156
 
 
166
 
167
  # Plot Price Control and Deadweight Loss
168
  if price_control > 0 and Q_traded < Q_eq:
169
+ ax.hlines(pc_used, 0, Q_traded, linestyle='--', color='purple', label=f'Enforced Price (${pc_used:.2f})', linewidth=1.5)
170
  ax.vlines(Q_traded, 0, pc_used, linestyle='--', color='purple', linewidth=1.5)
171
 
172
  # Identify the points for the DWL triangle
 
185
  ax.fill_between(Q_dwl_fill, P_supply_dwl_fill, P_demand_dwl_fill, color='red', alpha=0.5, label='Deadweight Loss')
186
 
187
  # Mark points relevant to price control
188
+ # ax.plot(Q_traded, c - d * Q_traded, 'kx', markersize=8, label=f'Demand at PC ({Q_traded:.2f})')
189
+ # ax.plot(Q_traded, a + b * Q_traded, 'bx', markersize=8, label=f'Supply at PC ({Q_traded:.2f})')
190
 
191
  ax.set_xlabel("Quantity (Q)", fontsize=12)
192
  ax.set_ylabel("Price (P)", fontsize=12)