Skip to content

Commit

Permalink
clear notebook outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
alxlyj committed Nov 21, 2024
1 parent b87928b commit 1f58842
Show file tree
Hide file tree
Showing 5 changed files with 877,491 additions and 84 deletions.
94 changes: 48 additions & 46 deletions python/src/robyn/allocator/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,21 @@ def __init__(
self._initialize_data()

# Log initial model parameters
print("\nInitial model metrics:")
print(f"Total initial spend: {self.init_spend_total:,.2f}")
print(f"Total initial response: {np.sum(self.init_response):,.2f}")
print(f"Overall ROI: {np.sum(self.init_response)/self.init_spend_total:.4f}")

print("\nPareto to Allocator transfer:")
print(f"Selected model: {select_model}")
print("Media coefficients from Pareto:")
logger.debug("\nInitial model metrics:")
logger.debug(f"Total initial spend: {self.init_spend_total:,.2f}")
logger.debug(f"Total initial response: {np.sum(self.init_response):,.2f}")
logger.debug(
f"Overall ROI: {np.sum(self.init_response)/self.init_spend_total:.4f}"
)

logger.debug("\nPareto to Allocator transfer:")
logger.debug(f"Selected model: {select_model}")
logger.debug("Media coefficients from Pareto:")
for channel in self.media_spend_sorted:
coef = self.dt_best_coef[self.dt_best_coef["rn"] == channel]["coef"].values[
0
]
print(f"{channel}: {coef}")
logger.debug(f"{channel}: {coef}")

def _validate_inputs(self) -> None:
"""Validate input data and parameters."""
Expand Down Expand Up @@ -109,8 +111,8 @@ def _initialize_data(self) -> None:
(self.pareto_result.x_decomp_agg["solID"] == self.select_model)
& (self.pareto_result.x_decomp_agg["rn"].isin(self.paid_media_spends))
]
print("Model Coefficients:")
print(self.dt_best_coef)
logger.debug("Model Coefficients:")
logger.debug(self.dt_best_coef)

# Initialize hill parameters
self.hill_params = get_hill_params(
Expand All @@ -121,12 +123,12 @@ def _initialize_data(self) -> None:
self.media_spend_sorted,
self.select_model,
)
# Add debug prints after getting hill params:
print("Hill Parameters:")
print(f"Alphas: {self.hill_params.alphas}")
print(f"Gammas: {self.hill_params.gammas}")
print(f"Coefficients: {self.hill_params.coefs}")
print(f"Carryover: {self.hill_params.carryover}")
# Add debug logger.debugs after getting hill params:
logger.debug("Hill Parameters:")
logger.debug(f"Alphas: {self.hill_params.alphas}")
logger.debug(f"Gammas: {self.hill_params.gammas}")
logger.debug(f"Coefficients: {self.hill_params.coefs}")
logger.debug(f"Carryover: {self.hill_params.carryover}")

# Handle zero coefficients like R
self.exclude = np.array([coef == 0 for coef in self.hill_params.coefs])
Expand Down Expand Up @@ -241,9 +243,9 @@ def _setup_constraints(self) -> Constraints:
upper_bounds = self.init_spend_unit * self.params.channel_constr_up
budget_constraint = self.init_spend_total

print("\nOptimization constraints:")
print(f"Total budget: {budget_constraint:,.2f}")
print(f"Bounds multiplier: {self.params.channel_constr_multiplier}")
logger.debug("\nOptimization constraints:")
logger.debug(f"Total budget: {budget_constraint:,.2f}")
logger.debug(f"Bounds multiplier: {self.params.channel_constr_multiplier}")

return Constraints(
lower_bounds=lower_bounds,
Expand All @@ -261,18 +263,18 @@ def _setup_target_efficiency_constraints(self) -> Constraints:
if self.dep_var_type == "revenue":
initial_roas = np.sum(self.init_response) / np.sum(self.init_spend_unit)
target_value = initial_roas * 0.8 # Target 80% of initial ROAS
print(
logger.debug(
f"Target ROAS: {target_value:.4f} (80% of initial {initial_roas:.4f})"
)
else:
initial_cpa = np.sum(self.init_spend_unit) / np.sum(self.init_response)
target_value = initial_cpa * 1.2 # Target 120% of initial CPA
print(
logger.debug(
f"Target CPA: {target_value:.4f} (120% of initial {initial_cpa:.4f})"
)
else:
target_value = self.params.target_value
print(f"Using provided target value: {target_value:.4f}")
logger.debug(f"Using provided target value: {target_value:.4f}")

return Constraints(
lower_bounds=lower_bounds,
Expand All @@ -283,7 +285,7 @@ def _setup_target_efficiency_constraints(self) -> Constraints:

def optimize(self) -> AllocationResult:
"""Run the budget allocation optimization."""
print(f"\nStarting optimization for scenario: {self.params.scenario}")
logger.debug(f"\nStarting optimization for scenario: {self.params.scenario}")

# Initialize constraints based on scenario
if self.params.scenario == SCENARIO_TARGET_EFFICIENCY:
Expand All @@ -298,7 +300,7 @@ def optimize(self) -> AllocationResult:

def _run_optimization(self, bounded: bool = True) -> OptimizationResult:
"""Run optimization while respecting excluded channels."""
print(f"\nOptimization run (Bounded: {bounded})")
logger.debug(f"\nOptimization run (Bounded: {bounded})")

# Calculate bounds
if bounded:
Expand Down Expand Up @@ -398,16 +400,16 @@ def _run_optimization(self, bounded: bool = True) -> OptimizationResult:
final_solution = result.x.copy()
final_solution[self.exclude] = self.init_spend_unit[self.exclude]

print(f"\nNew best solution (attempt {i+1}):")
print(f"Objective value: {result.fun:,.2f}")
logger.debug(f"\nNew best solution (attempt {i+1}):")
logger.debug(f"Objective value: {result.fun:,.2f}")
total_response = np.sum(
[
self.calculate_response(spend, i)
for i, spend in enumerate(final_solution)
]
)
print(f"Total spend: {np.sum(final_solution):,.2f}")
print(f"Total response: {total_response:,.2f}")
logger.debug(f"Total spend: {np.sum(final_solution):,.2f}")
logger.debug(f"Total response: {total_response:,.2f}")

best_objective = result.fun
best_result = OptimizationResult(
Expand Down Expand Up @@ -475,20 +477,20 @@ def calculate_response(self, spend: float, channel_index: int) -> float:
x_saturated = (x_adstocked**alpha) / (x_adstocked**alpha + inflexion**alpha)
response = coef * x_saturated

print(f"\n{channel} Response Calculation:")
print(f"Input spend: {spend:,.2f}")
print(f"Adstocked value: {x_adstocked:,.2f}")
print(f"Saturated value: {x_saturated:.4f}")
print(f"Final response: {response:.4f}")
logger.debug(f"\n{channel} Response Calculation:")
logger.debug(f"Input spend: {spend:,.2f}")
logger.debug(f"Adstocked value: {x_adstocked:,.2f}")
logger.debug(f"Saturated value: {x_saturated:.4f}")
logger.debug(f"Final response: {response:.4f}")
# In calculate_response method
print(f"Raw spend: {spend}")
print(f"After adstock: {x_adstocked}")
print(f"After hill transform: {x_saturated}")

print("\nResponse calculation components:")
print(f"Alpha: {self.hill_params.alphas[channel_index]}")
print(f"Gamma: {self.hill_params.gammas[channel_index]}")
print(f"Coefficient: {self.hill_params.coefs[channel_index]}")
logger.debug(f"Raw spend: {spend}")
logger.debug(f"After adstock: {x_adstocked}")
logger.debug(f"After hill transform: {x_saturated}")

logger.debug("\nResponse calculation components:")
logger.debug(f"Alpha: {self.hill_params.alphas[channel_index]}")
logger.debug(f"Gamma: {self.hill_params.gammas[channel_index]}")
logger.debug(f"Coefficient: {self.hill_params.coefs[channel_index]}")
return response

def _process_optimization_results(
Expand Down Expand Up @@ -541,10 +543,10 @@ def _process_optimization_results(
)

# Log final results summary
print("\nOptimization Results Summary:")
print(f"Initial total response: {np.sum(self.init_response):,.2f}")
print(f"Optimized total response: {np.sum(bounded_response):,.2f}")
print(
logger.debug("\nOptimization Results Summary:")
logger.debug(f"Initial total response: {np.sum(self.init_response):,.2f}")
logger.debug(f"Optimized total response: {np.sum(bounded_response):,.2f}")
logger.debug(
f"Response lift: {((np.sum(bounded_response)/np.sum(self.init_response))-1)*100:,.2f}%"
)

Expand Down
2 changes: 2 additions & 0 deletions python/src/robyn/robyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ def optimize_budget(
):
select_model = pareto_solutions[1]

logger.info("Selected model for budget optimization: %s", select_model)

allocator = BudgetAllocator(
mmm_data=self.mmm_data,
featurized_mmm_data=self.featurized_mmm_data,
Expand Down
1 change: 1 addition & 0 deletions python/src/robyn/tutorials/tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
"robyn.train_models(trials_config=trials_config,\n",
" ts_validation=True, # changed from True to False -> deacitvate\n",
" add_penalty_factor=False,\n",
" rssd_zero_penalty=True,\n",
" cores=8,\n",
" nevergrad_algo=NevergradAlgorithm.TWO_POINTS_DE,\n",
" model_name=Models.RIDGE,\n",
Expand Down
9 changes: 0 additions & 9 deletions python/src/robyn/tutorials/tutorial1_src.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,6 @@
" plots=True,\n",
")\n",
"\n",
"\n",
"print(\"\\nInitial constraints:\")\n",
"for channel, low, up in zip(\n",
" mmm_data.mmmdata_spec.paid_media_spends,\n",
" [0.7] * len(mmm_data.mmmdata_spec.paid_media_spends), # Expand single value\n",
" [1.2, 1.5, 1.5, 1.5, 1.5], # Per channel values\n",
"):\n",
" print(f\"{channel}: {low:.1f}x - {up:.1f}x\")\n",
"\n",
"# Initialize budget allocator\n",
"max_response_allocator = BudgetAllocator(\n",
" mmm_data=mmm_data,\n",
Expand Down
Loading

0 comments on commit 1f58842

Please sign in to comment.