def optimize_portfolio(assets, weights, limit):
"""
Optimizes a portfolio by removing assets until the weighted average value is below a limit.
Args:
assets (list): A list of asset values.
weights (list): A list of corresponding weights for each asset.
limit (float): The target maximum weighted average value.
Returns:
tuple: A tuple containing:
- optimized_assets (list): The remaining assets after optimization.
- optimized_weights (list): The corresponding weights for the remaining assets.
- final_average (float): The final weighted average value.
"""
if len(assets) != len(weights):
raise ValueError("Assets and weights lists must be the same length")
combined = list(zip(assets, weights))
combined.sort(key=lambda x: x[0] * x[1], reverse= True) # Sort by weighted value (descending)
optimized_assets = []
optimized_weights = []
current_sum = 0
total_weight = 0
for asset, weight in combined:
current_sum += asset * weight
total_weight += weight
optimized_assets.append(asset)
optimized_weights.append(weight)
if total_weight > 0: # Avoid division by zero
current_average = current_sum / total_weight
if current_average <= limit:
break
final_average = current_sum / total_weight if total_weight > 0 else 0
return optimized_assets, optimized_weights, final_average
Explanation:
-
Input Validation: The function first checks if the lengths of assets and weights match.
-
Combining and Sorting: It combines assets and weights into tuples and sorts them in descending order based on their weighted value (asset * weight). This prioritizes removing assets with the highest impact on the average.
-
Iterative Removal: The code iterates through the sorted list, adding assets and their weights to the optimized_assets and optimized_weights lists while accumulating the current_sum and total_weight.
-
Average Calculation and Stopping Condition: After each addition, it calculates the current_average. If the current_average falls below or equals the limit, the loop breaks, as further additions would only decrease the average.
-
Final Average: The function calculates the final_average based on the remaining assets and weights.
-
Return Values: The function returns a tuple containing the optimized lists of assets and weights, along with the final weighted average.
Example Usage:
assets = [100, 150, 200, 50]
weights = [0.2, 0.3, 0.4, 0.1]
limit = 120
optimized_assets, optimized_weights, final_average = optimize_portfolio(assets, weights, limit)
print("Optimized Assets:", optimized_assets)
print("Optimized Weights:", optimized_weights)
print("Final Weighted Average:", final_average)
Key Points:
- Greedy Approach: This solution uses a greedy algorithm, removing the assets with the highest weighted value first. While not guaranteed to find the absolute optimal solution in all cases, it's efficient and often provides good results.
- Edge Cases: The code handles cases where the initial average is already below the limit and cases where all assets need to be removed.
- Flexibility: You can easily adapt this function to different asset types and weighting schemes by modifying the input data structures.