vimj1915 commited on
Commit
7aa0bfe
Β·
verified Β·
1 Parent(s): d983785

Update tools/math_tools.py

Browse files
Files changed (1) hide show
  1. tools/math_tools.py +29 -23
tools/math_tools.py CHANGED
@@ -20,41 +20,43 @@ def run_math_analysis(topic: str):
20
  elif topic == "markov":
21
  markov_process_demo()
22
  else:
23
- print("❌ Unknown topic. Available topics are:")
24
- print("linear_algebra, probability, optimization, game_theory, time_series, markov")
25
 
26
  def linear_algebra_demo():
27
- print("πŸ“ Linear Algebra: Matrix-vector multiplication (embeddings, transformations)")
28
  A = np.array([[1, 2], [3, 4]])
29
  B = np.array([[2], [1]])
30
  result = np.dot(A, B)
31
- print("Matrix A =\n", A)
32
- print("Vector B =\n", B)
33
- print("AΒ·B =\n", result)
34
 
35
  def probability_demo():
36
- print("πŸ“Š Probability & Bayesian Inference")
37
  prior = 0.3
38
  likelihood = 0.8
39
  evidence = 0.6
40
  posterior = (likelihood * prior) / evidence
41
- print(f"P(H|E) = (P(E|H) * P(H)) / P(E) = {posterior:.2f}")
42
 
43
  def optimization_demo():
44
- print("πŸ”§ Optimization: Linear Programming Example")
45
- c = [-3, -5] # Maximize 3x + 5y
46
  A = [[1, 0], [0, 2], [3, 2]]
47
  b = [4, 12, 18]
48
  result = linprog(c, A_ub=A, b_ub=b)
49
- print("Optimal solution (x, y):", result.x)
50
- print("Maximized objective value:", -result.fun)
 
 
 
51
 
52
  def game_theory_demo():
53
- print("🎲 Game Theory: Simple 2x2 Payoff Matrix")
54
- payoff_matrix = np.array([[3, 0], [5, 1]])
55
- print("Payoff Matrix (A vs B):")
56
- print(payoff_matrix)
57
- print("Interpretation: Rows = Player A strategies, Columns = Player B")
58
 
59
  def time_series_demo():
60
  print("πŸ“ˆ Time Series: ARIMA(1,1,1) Forecast")
@@ -63,17 +65,21 @@ def time_series_demo():
63
  model = ARIMA(series, order=(1, 1, 1))
64
  fit = model.fit()
65
  forecast = fit.forecast(steps=10)
66
- print("Forecast for next 10 steps:")
67
  print(forecast)
 
68
  series.plot(label="Historical")
69
  forecast.plot(label="Forecast", legend=True)
70
- plt.title("ARIMA Forecast")
 
 
 
71
  plt.show()
72
 
73
  def markov_process_demo():
74
- print("πŸ” Markov Model: Weather Simulation (Sunny/Rainy)")
75
- transition_matrix = np.array([[0.9, 0.1], [0.5, 0.5]])
76
- state = np.array([1, 0]) # Start with Sunny
77
  for day in range(5):
78
  state = np.dot(state, transition_matrix)
79
- print(f"Day {day+1} β†’ P(Sunny)={state[0]:.2f}, P(Rainy)={state[1]:.2f}")
 
20
  elif topic == "markov":
21
  markov_process_demo()
22
  else:
23
+ print("❌ Unknown topic. Available: linear_algebra, probability, optimization, game_theory, time_series, markov")
 
24
 
25
  def linear_algebra_demo():
26
+ print("πŸ“ Linear Algebra: Matrix-Vector Multiplication")
27
  A = np.array([[1, 2], [3, 4]])
28
  B = np.array([[2], [1]])
29
  result = np.dot(A, B)
30
+ print("Matrix A:\n", A)
31
+ print("Vector B:\n", B)
32
+ print("A Β· B:\n", result)
33
 
34
  def probability_demo():
35
+ print("πŸ“Š Probability & Bayesian Inference Example")
36
  prior = 0.3
37
  likelihood = 0.8
38
  evidence = 0.6
39
  posterior = (likelihood * prior) / evidence
40
+ print(f"Bayesian Posterior = P(H|E) = {posterior:.2f}")
41
 
42
  def optimization_demo():
43
+ print("πŸ”§ Optimization: Linear Programming (Maximize 3x + 5y)")
44
+ c = [-3, -5] # Maximize 3x + 5y β†’ minimize -3x -5y
45
  A = [[1, 0], [0, 2], [3, 2]]
46
  b = [4, 12, 18]
47
  result = linprog(c, A_ub=A, b_ub=b)
48
+ if result.success:
49
+ print("Optimal values of x and y:", result.x)
50
+ print("Maximum value of objective function:", -result.fun)
51
+ else:
52
+ print("❌ Optimization failed:", result.message)
53
 
54
  def game_theory_demo():
55
+ print("🎲 Game Theory: Payoff Matrix")
56
+ matrix = np.array([[3, 0], [5, 1]])
57
+ print("Player A vs Player B Payoff Matrix:")
58
+ print(matrix)
59
+ print("Interpret rows as Player A strategies and columns as Player B.")
60
 
61
  def time_series_demo():
62
  print("πŸ“ˆ Time Series: ARIMA(1,1,1) Forecast")
 
65
  model = ARIMA(series, order=(1, 1, 1))
66
  fit = model.fit()
67
  forecast = fit.forecast(steps=10)
68
+ print("Forecasted next 10 values:")
69
  print(forecast)
70
+
71
  series.plot(label="Historical")
72
  forecast.plot(label="Forecast", legend=True)
73
+ plt.title("Time Series Forecast (ARIMA)")
74
+ plt.xlabel("Time")
75
+ plt.ylabel("Value")
76
+ plt.grid(True)
77
  plt.show()
78
 
79
  def markov_process_demo():
80
+ print("πŸ” Markov Chain Simulation: Sunny vs Rainy")
81
+ transition_matrix = np.array([[0.9, 0.1], [0.5, 0.5]]) # [Sunny, Rainy]
82
+ state = np.array([1, 0]) # Start in Sunny
83
  for day in range(5):
84
  state = np.dot(state, transition_matrix)
85
+ print(f"Day {day + 1}: P(Sunny) = {state[0]:.2f}, P(Rainy) = {state[1]:.2f}")