Update tools/math_tools.py
Browse files- 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
|
| 24 |
-
print("linear_algebra, probability, optimization, game_theory, time_series, markov")
|
| 25 |
|
| 26 |
def linear_algebra_demo():
|
| 27 |
-
print("π Linear Algebra: Matrix-
|
| 28 |
A = np.array([[1, 2], [3, 4]])
|
| 29 |
B = np.array([[2], [1]])
|
| 30 |
result = np.dot(A, B)
|
| 31 |
-
print("Matrix A
|
| 32 |
-
print("Vector B
|
| 33 |
-
print("AΒ·
|
| 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"
|
| 42 |
|
| 43 |
def optimization_demo():
|
| 44 |
-
print("π§ Optimization: Linear Programming
|
| 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 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def game_theory_demo():
|
| 53 |
-
print("π² Game Theory:
|
| 54 |
-
|
| 55 |
-
print("
|
| 56 |
-
print(
|
| 57 |
-
print("
|
| 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("
|
| 67 |
print(forecast)
|
|
|
|
| 68 |
series.plot(label="Historical")
|
| 69 |
forecast.plot(label="Forecast", legend=True)
|
| 70 |
-
plt.title("
|
|
|
|
|
|
|
|
|
|
| 71 |
plt.show()
|
| 72 |
|
| 73 |
def markov_process_demo():
|
| 74 |
-
print("π Markov
|
| 75 |
-
transition_matrix = np.array([[0.9, 0.1], [0.5, 0.5]])
|
| 76 |
-
state = np.array([1, 0]) # Start
|
| 77 |
for day in range(5):
|
| 78 |
state = np.dot(state, transition_matrix)
|
| 79 |
-
print(f"Day {day+1}
|
|
|
|
| 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}")
|