|
| 1 | +import pandas as pd |
| 2 | +import numpy as np |
| 3 | +from sklearn.linear_model import LinearRegression |
| 4 | +import openpyxl |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +# Path to your Aura workbook |
| 8 | +AURA_FILE = Path("../data/Aura.xlsl") # treat .xlsl like .xlsx |
| 9 | + |
| 10 | +def load_data(sheet_name="SimulationInput"): |
| 11 | + """Load input parameters from the Aura workbook.""" |
| 12 | + df = pd.read_excel(AURA_FILE, sheet_name=sheet_name) |
| 13 | + return df |
| 14 | + |
| 15 | +def run_simulation(params_df, iterations=1000): |
| 16 | + """ |
| 17 | + Simple Monte Carlo simulation: |
| 18 | + Population growth with noise. |
| 19 | + """ |
| 20 | + results = [] |
| 21 | + for _, row in params_df.iterrows(): |
| 22 | + base_growth = row["growth_rate"] |
| 23 | + population = row["initial_population"] |
| 24 | + |
| 25 | + sims = [] |
| 26 | + for _ in range(iterations): |
| 27 | + pop = population |
| 28 | + for _ in range(row["years"]): |
| 29 | + # stochastic growth with random noise |
| 30 | + pop = pop * (1 + base_growth + np.random.normal(0, 0.01)) |
| 31 | + sims.append(pop) |
| 32 | + results.append({ |
| 33 | + "scenario": row["scenario"], |
| 34 | + "expected_population": np.mean(sims), |
| 35 | + "std_dev": np.std(sims) |
| 36 | + }) |
| 37 | + return pd.DataFrame(results) |
| 38 | + |
| 39 | +def train_ai_model(results_df, params_df): |
| 40 | + """ |
| 41 | + Train regression model to predict population outcome |
| 42 | + from growth rate & years. |
| 43 | + """ |
| 44 | + X = params_df[["growth_rate", "years"]].values |
| 45 | + y = results_df["expected_population"].values |
| 46 | + model = LinearRegression().fit(X, y) |
| 47 | + |
| 48 | + # Predictions |
| 49 | + preds = model.predict(X) |
| 50 | + results_df["ai_prediction"] = preds |
| 51 | + return results_df, model |
| 52 | + |
| 53 | +def export_results(results_df, sheet_name="SimulationResults"): |
| 54 | + """Write results back to the Aura workbook.""" |
| 55 | + with pd.ExcelWriter(AURA_FILE, engine="openpyxl", mode="a", if_sheet_exists="replace") as writer: |
| 56 | + results_df.to_excel(writer, sheet_name=sheet_name, index=False) |
| 57 | + |
| 58 | +def main(): |
| 59 | + print("Loading Aura data...") |
| 60 | + params_df = load_data() |
| 61 | + |
| 62 | + print("Running Monte Carlo simulation...") |
| 63 | + results_df = run_simulation(params_df) |
| 64 | + |
| 65 | + print("Training AI model...") |
| 66 | + results_df, model = train_ai_model(results_df, params_df) |
| 67 | + |
| 68 | + print("Exporting results back to Aura workbook...") |
| 69 | + export_results(results_df) |
| 70 | + |
| 71 | + print("Demo complete! Results in 'SimulationResults' sheet.") |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments