Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
|
| 6 |
+
def simulate_scoring(total_qs, known_qs, guess_qs, marks_correct, marks_negative, num_options, trials=10000):
|
| 7 |
+
# 1. Calculate fixed score from known questions
|
| 8 |
+
base_score = known_qs * marks_correct
|
| 9 |
+
|
| 10 |
+
# 2. Simulate guesses
|
| 11 |
+
# Probability of getting a guess right
|
| 12 |
+
p_correct = 1 / num_options
|
| 13 |
+
|
| 14 |
+
# Each trial: number of correct guesses follows a Binomial Distribution
|
| 15 |
+
correct_guesses = np.random.binomial(guess_qs, p_correct, trials)
|
| 16 |
+
wrong_guesses = guess_qs - correct_guesses
|
| 17 |
+
|
| 18 |
+
# 3. Calculate total scores for all trials
|
| 19 |
+
guess_scores = (correct_guesses * marks_correct) - (wrong_guesses * marks_negative)
|
| 20 |
+
total_scores = base_score + guess_scores
|
| 21 |
+
|
| 22 |
+
# 4. Statistical Analysis
|
| 23 |
+
mean_score = np.mean(total_scores)
|
| 24 |
+
prob_negative_gain = np.mean(total_scores < base_score) * 100
|
| 25 |
+
prob_total_negative = np.mean(total_scores < 0) * 100
|
| 26 |
+
|
| 27 |
+
# 5. Plotting
|
| 28 |
+
plt.figure(figsize=(10, 6))
|
| 29 |
+
sns.histplot(total_scores, kde=True, color="skyblue", bins=30)
|
| 30 |
+
plt.axvline(base_score, color='red', linestyle='--', label=f'Score without guessing ({base_score})')
|
| 31 |
+
plt.axvline(mean_score, color='green', linestyle='-', label=f'Expected Mean Score ({mean_score:.2f})')
|
| 32 |
+
|
| 33 |
+
plt.title(f"Score Distribution across {trials:,} Simulations")
|
| 34 |
+
plt.xlabel("Total Score")
|
| 35 |
+
plt.ylabel("Frequency")
|
| 36 |
+
plt.legend()
|
| 37 |
+
|
| 38 |
+
return plt.gcf(), f"Expected Average Score: {mean_score:.2f}\nProbability that guessing HURTS your score: {prob_negative_gain:.2f}%\nProbability of an overall negative score: {prob_total_negative:.2f}%"
|
| 39 |
+
|
| 40 |
+
# Gradio UI Setup
|
| 41 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
+
gr.Markdown("# 🎯 Negative Marking Probability Simulator")
|
| 43 |
+
gr.Markdown("Visualize how guessing affects your final score using Monte Carlo simulations.")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
t_qs = gr.Number(label="Total Questions", value=100)
|
| 48 |
+
k_qs = gr.Slider(0, 100, label="Questions You Know (100% Correct)", value=50)
|
| 49 |
+
g_qs = gr.Slider(0, 100, label="Questions to Guess", value=20)
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
m_pos = gr.Number(label="Marks for Correct", value=4)
|
| 53 |
+
m_neg = gr.Number(label="Marks for Wrong (as positive value)", value=1)
|
| 54 |
+
|
| 55 |
+
opts = gr.Radio([2, 3, 4, 5], label="Number of Options per Question", value=4)
|
| 56 |
+
btn = gr.Button("Simulate Results", variant="primary")
|
| 57 |
+
|
| 58 |
+
with gr.Column():
|
| 59 |
+
plot_output = gr.Plot(label="Score Distribution")
|
| 60 |
+
stats_output = gr.Textbox(label="Statistical Summary")
|
| 61 |
+
|
| 62 |
+
btn.click(
|
| 63 |
+
fn=simulate_scoring,
|
| 64 |
+
inputs=[t_qs, k_qs, g_qs, m_pos, m_neg, opts],
|
| 65 |
+
outputs=[plot_output, stats_output]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|