Initialize Hall of Fame to store the best individual
hof = tools.HallOfFame(HALL_OF_FAME_SIZE)
Begin the evolution
print("\nStarting Genetic Algorithm Evolution...\n")
for gen in tqdm(range(1, MAX_GENERATIONS + 1), desc="Generations"):
# Select the next generation individuals
offspring = toolbox.select(population, len(population))
offspring = list(map(toolbox.clone, offspring))
code
# Apply crossover on the offspring
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < P_CROSSOVER:
toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
# Apply mutation on the offspring
for mutant in offspring:
if random.random() < P_MUTATION:
toolbox.mutate(mutant)
del mutant.fitness.values
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# Replace population with offspring
population[:] = offspring
# Update the hall of fame with the generated individuals
hof.update(population)
# Gather all the fitnesses in one list
fits = [ind.fitness.values[0] for ind in population]
correct the code so that it doesnt use cumulative return, use CAGR instead
To replace the cumulative return calculations with the Compound Annual Growth Rate (CAGR), you need to adjust the code to compute and display CAGR instead of cumulative return in the analysis and plots. Here is the relevant portion of your code corrected to use CAGR:
python
def calculate_cagr(returns):
"""
Calculate the Compound Annual Growth Rate (CAGR) given returns.
"""
total_return = (1 + returns).prod()
n = len(returns) / 252 # Assuming daily returns with 252 trading days per year
return total_return**(1/n) - 1
# Retrieve and display the best portfolio found
if len(hof) > 0:
best_ind = hof[0]
selected = best_ind[:NUM_ASSETS]
weights = np.array(best_ind[NUM_ASSETS:])
# Training Metrics
portfolio_returns_train = train_returns[selected].dot(weights)
portfolio_cagr_train = calculate_cagr(portfolio_returns_train)
portfolio_volatility_train = portfolio_returns_train.std() * np.sqrt(252)
sharpe_ratio_train = (portfolio_returns_train.mean() * 252 - RISK_FREE_RATE) / portfolio_volatility_train if portfolio_volatility_train != 0 else 0
sortino_growth_train = sortino_ratio(portfolio_returns_train[train_labels == 'Growth'])
sortino_recession_train = sortino_ratio(portfolio_returns_train[train_labels == 'Recession'])
annualized_return_train = portfolio_returns_train.mean() * 252
portfolio_cum_returns_growth_train = (1 + portfolio_returns_train[train_labels == 'Growth']).cumprod()
portfolio_cum_returns_recession_train = (1 + portfolio_returns_train[train_labels == 'Recession']).cumprod()
portfolio_max_drawdown_growth_train = max_drawdown(portfolio_cum_returns_growth_train)
portfolio_max_drawdown_recession_train = max_drawdown(portfolio_cum_returns_recession_train)
# Validation Metrics
try:
portfolio_returns_val = validation_returns[selected].dot(weights)
portfolio_cagr_val = calculate_cagr(portfolio_returns_val)
portfolio_volatility_val = portfolio_returns_val.std() * np.sqrt(252)
sharpe_ratio_val = (portfolio_returns_val.mean() * 252 - RISK_FREE_RATE) / portfolio_volatility_val if portfolio_volatility_val != 0 else 0
sortino_growth_val = sortino_ratio(portfolio_returns_val[validation_labels == 'Growth'])
sortino_recession_val = sortino_ratio(portfolio_returns_val[validation_labels == 'Recession'])
annualized_return_val = portfolio_returns_val.mean() * 252
portfolio_cum_returns_growth_val = (1 + portfolio_returns_val[validation_labels == 'Growth']).cumprod()
portfolio_cum_returns_recession_val = (1 + portfolio_returns_val[validation_labels == 'Recession']).cumprod()
portfolio_max_drawdown_growth_val = max_drawdown(portfolio_cum_returns_growth_val)
portfolio_max_drawdown_recession_val = max_drawdown(portfolio_cum_returns_recession_val)
except:
portfolio_cagr_val = 0
portfolio_volatility_val = 0
sharpe_ratio_val = 0
sortino_growth_val = 0
sortino_recession_val = 0
annualized_return_val = 0
portfolio_max_drawdown_growth_val = 0
portfolio_max_drawdown_recession_val = 0
# Testing Metrics
try:
portfolio_returns_test = test_returns[selected].dot(weights)
portfolio_cagr_test = calculate_cagr(portfolio_returns_test)
portfolio_volatility_test = portfolio_returns_test.std() * np.sqrt(252)
sharpe_ratio_test = (portfolio_returns_test.mean() * 252 - RISK_FREE_RATE) / portfolio_volatility_test if portfolio_volatility_test != 0 else 0
sortino_test_growth = sortino_ratio(portfolio_returns_test[test_labels == 'Growth'])
sortino_test_recession = sortino_ratio(portfolio_returns_test[test_labels == 'Recession'])
annualized_return_test = portfolio_returns_test.mean() * 252
portfolio_cum_returns_growth_test = (1 + portfolio_returns_test[test_labels == 'Growth']).cumprod()
portfolio_cum_returns_recession_test = (1 + portfolio_returns_test[test_labels == 'Recession']).cumprod()
portfolio_max_drawdown_growth_test = max_drawdown(portfolio_cum_returns_growth_test)
portfolio_max_drawdown_recession_test = max_drawdown(portfolio_cum_returns_recession_test)
except:
portfolio_cagr_test = 0
portfolio_volatility_test = 0
sharpe_ratio_test = 0
sortino_test_growth = 0
sortino_test_recession = 0
annualized_return_test = 0
portfolio_max_drawdown_growth_test = 0
portfolio_max_drawdown_recession_test = 0
# Composite Fitness Score on Training and Validation Sets
fitness_train = (
WEIGHT_SHARPE * sharpe_ratio_train +
WEIGHT_SORTINO_GROWTH * sortino_growth_train +
WEIGHT_SORTINO_RECESSION * sortino_recession_train +
WEIGHT_VOLATILITY * (-portfolio_volatility_train) +
WEIGHT_ANNUALIZED_RETURN * annualized_return_train +
WEIGHT_MAX_DRAWDOWN * (portfolio_max_drawdown_growth_train + portfolio_max_drawdown_recession_train) / 2
)
fitness_val = (
WEIGHT_SHARPE * sharpe_ratio_val +
WEIGHT_SORTINO_GROWTH * sortino_growth_val +
WEIGHT_SORTINO_RECESSION * sortino_recession_val +
WEIGHT_VOLATILITY * (-portfolio_volatility_val) +
WEIGHT_ANNUALIZED_RETURN * annualized_return_val +
WEIGHT_MAX_DRAWDOWN * (portfolio_max_drawdown_growth_val + portfolio_max_drawdown_recession_val) / 2
)
fitness = (fitness_train + fitness_val) / 2
portfolio_df = pd.DataFrame({
'Ticker': selected,
'Allocation': [f"{w*100:.2f}%" for w in weights]
})
print("Optimal Portfolio Found:")
display(portfolio_df)
print(f"--- Training Set Performance ---")
print(f"CAGR: {portfolio_cagr_train*100:.2f}%")
print(f"Annualized Volatility: {portfolio_volatility_train*100:.2f}%")
print(f"Sharpe Ratio: {sharpe_ratio_train:.2f}")
print(f"Sortino Ratio (Growth): {sortino_growth_train:.2f}")
print(f"Sortino Ratio (Recession): {sortino_recession_train:.2f}")
print(f"Annualized Return: {annualized_return_train*100:.2f}%")
print(f"Maximum Drawdown (Growth): {portfolio_max_drawdown_growth_train:.2%}")
print(f"Maximum Drawdown (Recession): {portfolio_max_drawdown_recession_train:.2%}")
print(f"--- Validation Set Performance ---")
print(f"CAGR: {portfolio_cagr_val*100:.2f}%")
print(f"Annualized Volatility: {portfolio_volatility_val*100:.2f}%")
print(f"Sharpe Ratio: {sharpe_ratio_val:.2f}")
print(f"Sortino Ratio (Growth): {sortino_growth_val:.2f}")
print(f"Sortino Ratio (Recession): {sortino_recession_val:.2f}")
print(f"Annualized Return: {annualized_return_val*100:.2f}%")
print(f"Maximum Drawdown (Growth): {portfolio_max_drawdown_growth_val:.2%}")
print(f"Maximum Drawdown (Recession): {portfolio_max_drawdown_recession_val:.2%}")
print(f"--- Testing Set Performance ---")
print(f"CAGR: {portfolio_cagr_test*100:.2f}%")
print(f"Annualized Volatility: {portfolio_volatility_test*100:.2f}%")
print(f"Sharpe Ratio: {sharpe_ratio_test:.2f}")
print(f"Sortino Ratio (Growth): {sortino_test_growth:.2f}")
print(f"Sortino Ratio (Recession): {sortino_test_recession:.2f}")
print(f"Annualized Return: {annualized_return_test*100:.2f}%")
print(f"Maximum Drawdown (Growth): {portfolio_max_drawdown_growth_test:.2%}")
print(f"Maximum Drawdown (Recession): {portfolio_max_drawdown_recession_test:.2%}")
# Assess overfitting
print("\n--- Overfitting Assessment ---")
# Define thresholds for acceptable performance differences
threshold_cagr = 0.05 # Threshold for CAGR difference
train_val_cagr_diff = portfolio_cagr_train - portfolio_cagr_val
train_test_cagr_diff = portfolio_cagr_train - portfolio_cagr_test if 'portfolio_cagr_test' in locals() else 0
train_val_vol_diff = portfolio_volatility_train - portfolio_volatility_val
train_test_vol_diff = portfolio_volatility_train - portfolio_volatility_test if 'portfolio_volatility_test' in locals() else 0
overfitting_cagr = train_test_cagr_diff > threshold_cagr
overfitting_vol = train_test_vol_diff < -threshold_volatility
if overfitting_cagr and overfitting_vol:
print("Warning: Overfitting detected! The portfolio performs significantly better on training and validation data compared to testing data.")
else:
print("No significant overfitting detected. The portfolio generalizes well to unseen data.")
# Plot cumulative returns for visual assessment
plt.figure(figsize=(14, 7))
# Training Cumulative Returns
plt.subplot(1, 2, 1)
if not portfolio_cum_returns_growth_train.empty:
plt.plot(portfolio_cum_returns_growth_train.index, portfolio_cum_returns_growth_train, label='Training Growth')
if not portfolio_cum_returns_recession_train.empty:
plt.plot(portfolio_cum_returns_recession_train.index, portfolio_cum_returns_recession_train, label='Training Recession')
if not portfolio_cum_returns_growth_val.empty:
plt.plot(portfolio_cum_returns_growth_val.index, portfolio_cum_returns_growth_val, label='Validation Growth')
if not portfolio_cum_returns_recession_val.empty:
plt.plot(portfolio_cum_returns_recession_val.index, portfolio_cum_returns_recession_val, label='Validation Recession')
if 'portfolio_cum_returns_growth_test' in locals() and not portfolio_cum_returns_growth_test.empty:
plt.plot(portfolio_cum_returns_growth_test.index, portfolio_cum_returns_growth_test, label='Testing Growth')
if 'portfolio_cum_returns_recession_test' in locals() and not portfolio_cum_returns_recession_test.empty:
plt.plot(portfolio_cum_returns_recession_test.index, portfolio_cum_returns_recession_test, label='Testing Recession')
plt.title('Cumulative Returns')
plt.xlabel('Date')
plt.ylabel('Cumulative Return')
plt.legend()
# Drawdowns
plt.subplot(1, 2, 2)
if not portfolio_cum_returns_growth_train.empty:
plt.plot(portfolio_cum_returns_growth_train.index, (portfolio_cum_returns_growth_train / portfolio_cum_returns_growth_train.cummax()) - 1, label='Training Growth Drawdown')
if not portfolio_cum_returns_recession_train.empty:
plt.plot(portfolio_cum_returns_recession_train.index, (portfolio_cum_returns_recession_train / portfolio_cum_returns_recession_train.cummax()) - 1, label='Training Recession Drawdown')
if not portfolio_cum_returns_growth_val.empty:
plt.plot(portfolio_cum_returns_growth_val.index, (portfolio_cum_returns_growth_val / portfolio_cum_returns_growth_val.cummax()) - 1, label='Validation Growth Drawdown')
if not portfolio_cum_returns_recession_val.empty:
plt.plot(portfolio_cum_returns_recession_val.index, (portfolio_cum_returns_recession_val / portfolio_cum_returns_recession_val.cummax()) - 1, label='Validation Recession Drawdown')
if 'portfolio_cum_returns_growth_test' in locals() and not portfolio_cum_returns_growth_test.empty:
plt.plot(portfolio_cum_returns_growth_test.index, (portfolio_cum_returns_growth_test / portfolio_cum_returns_growth_test.cummax()) - 1, label='Testing Growth Drawdown')
if 'portfolio_cum_returns_recession_test' in locals() and not portfolio_cum_returns_recession_test.empty:
plt.plot(portfolio_cum_returns_recession_test.index, (portfolio_cum_returns_recession_test / portfolio_cum_returns_recession_test.cummax()) - 1, label='Testing Recession Drawdown')
plt.title('Drawdowns')
plt.xlabel('Date')
plt.ylabel('Drawdown')
plt.legend()
plt.tight_layout()
plt.show()
Key Changes
CAGR Calculation: Replace cumulative return calculations with the calculate_cagr function.
Performance Metrics Display: Update the print statements to display CAGR instead of cumulative returns.
Overfitting Assessment: Use differences in CAGR rather than cumulative returns for overfitting checks.
The plotting of cumulative returns does not change, as that's used for visualization. The primary metric showcased in the print statements and calculations is now CAGR. Adjust any additional summary output as needed to reflect CAGR usage consistently in your application.
Define Genetic Algorithm parameters
POPULATION_SIZE = 500 # Adjusted for memory constraints
P_CROSSOVER = 0.8 # Probability for crossover
P_MUTATION = 0.2 # Probability for mutating an individual
MAX_GENERATIONS = 100 # Increased number of generations for better convergence
HALL_OF_FAME_SIZE = 1 # Number of best individuals to keep
Define the weights for each optimization objective
pool = multiprocessing.Pool(processes=num_processes)
toolbox.register("map", pool.map)
refine the above code as well
To refine the provided genetic algorithm code and incorporate the use of CAGR instead of cumulative returns, we will adjust several parts of the code to improve clarity, correctness, and ensure that the Compound Annual Growth Rate is used appropriately. Here's the refined code:
python
# Define Genetic Algorithm parameters
POPULATION_SIZE = 500 # Adjusted for memory constraints
P_CROSSOVER = 0.8 # Probability for crossover
P_MUTATION = 0.2 # Probability for mutating an individual
MAX_GENERATIONS = 100 # Increased number of generations for better convergence
HALL_OF_FAME_SIZE = 1 # Number of best individuals to keep
# Define the weights for each optimization objective
WEIGHT_SHARPE = 0.30
WEIGHT_SORTINO_GROWTH = 0.15
WEIGHT_SORTINO_RECESSION = 0.15
WEIGHT_VOLATILITY = 0.10
WEIGHT_ANNUALIZED_RETURN = 0.20
WEIGHT_MAX_DRAWDOWN = 0.10
# Function to calculate Compound Annual Growth Rate (CAGR)
def calculate_cagr(returns):
total_return = (1 + returns).prod()
n = len(returns) / 252 # Assuming daily data
return total_return**(1/n) - 1
# Define the evaluation (fitness) function
def evaluate_portfolio(individual):
selected_tickers = individual[:NUM_ASSETS]
weights = np.array(individual[NUM_ASSETS:])
# Ensure all selected tickers are unique and weights are valid
if len(set(selected_tickers)) != NUM_ASSETS or not np.isclose(weights.sum(), 1.0) or \
not all(LOWER_WEIGHT <= w <= UPPER_WEIGHT for w in weights) or \
not all(ticker in returns.columns for ticker in selected_tickers):
return -np.inf,
# Calculate portfolio metrics using Training Set
try:
portfolio_returns_train = train_returns[selected_tickers].dot(weights)
portfolio_cagr_train = calculate_cagr(portfolio_returns_train)
portfolio_volatility_train = portfolio_returns_train.std() * np.sqrt(252)
sharpe_ratio_train = (portfolio_returns_train.mean() * 252 - RISK_FREE_RATE) / portfolio_volatility_train if portfolio_volatility_train != 0 else 0
sortino_growth_train = sortino_ratio(portfolio_returns_train[train_labels == 'Growth'])
sortino_recession_train = sortino_ratio(portfolio_returns_train[train_labels == 'Recession'])
portfolio_max_drawdown_growth_train = max_drawdown(portfolio_returns_train[train_labels == 'Growth'].cumprod())
portfolio_max_drawdown_recession_train = max_drawdown(portfolio_returns_train[train_labels == 'Recession'].cumprod())
except:
return -np.inf,
# Combine the metrics into a single fitness score
fitness = (
WEIGHT_SHARPE * sharpe_ratio_train +
WEIGHT_SORTINO_GROWTH * sortino_growth_train +
WEIGHT_SORTINO_RECESSION * sortino_recession_train +
WEIGHT_VOLATILITY * (-portfolio_volatility_train) +
WEIGHT_ANNUALIZED_RETURN * portfolio_cagr_train +
WEIGHT_MAX_DRAWDOWN * (portfolio_max_drawdown_growth_train + portfolio_max_drawdown_recession_train) / 2
)
return fitness,
# Create the DEAP framework
creator.create("FitnessMax", base.Fitness, weights=(1.0,)) # Single objective, maximize
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
# Attribute generators
def select_tickers():
return random.sample(successful_tickers, NUM_ASSETS)
def allocate_weights():
return generate_weights(NUM_ASSETS, LOWER_WEIGHT, UPPER_WEIGHT).tolist()
def create_individual():
tickers_selected = select_tickers()
weights_allocated = allocate_weights()
return creator.Individual(tickers_selected + weights_allocated)
toolbox.register("individual", create_individual)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# Custom Crossover
def cxTickersWeights(ind1, ind2):
# Separate tickers and weights
tickers1, weights1 = ind1[:NUM_ASSETS], ind1[NUM_ASSETS:]
tickers2, weights2 = ind2[:NUM_ASSETS], ind2[NUM_ASSETS:]
# Crossover tickers using one-point crossover
cx_point = random.randint(1, NUM_ASSETS - 1)
new_tickers1 = tickers1[:cx_point] + [ticker for ticker in tickers2[cx_point:] if ticker not in tickers1[:cx_point]]
new_tickers2 = tickers2[:cx_point] + [ticker for ticker in tickers1[cx_point:] if ticker not in tickers2[:cx_point]]
# Fill the remaining tickers to maintain NUM_ASSETS
available_tickers1 = list(set(successful_tickers) - set(new_tickers1))
available_tickers2 = list(set(successful_tickers) - set(new_tickers2))
while len(new_tickers1) < NUM_ASSETS:
new_tickers1.append(random.choice(available_tickers1))
available_tickers1.remove(new_tickers1[-1])
while len(new_tickers2) < NUM_ASSETS:
new_tickers2.append(random.choice(available_tickers2))
available_tickers2.remove(new_tickers2[-1])
ind1[:NUM_ASSETS], ind2[:NUM_ASSETS] = new_tickers1, new_tickers2
# Crossover weights using arithmetic crossover
new_weights1 = (np.array(weights1) + np.array(weights2)) / 2
new_weights2 = (np.array(weights1) + np.array(weights2)) / 2
# Enforce weight constraints
try:
new_weights1 = generate_weights(NUM_ASSETS, LOWER_WEIGHT, UPPER_WEIGHT)
except ValueError:
new_weights1 = allocate_weights()
try:
new_weights2 = generate_weights(NUM_ASSETS, LOWER_WEIGHT, UPPER_WEIGHT)
except ValueError:
new_weights2 = allocate_weights()
ind1[NUM_ASSETS:], ind2[NUM_ASSETS:] = new_weights1.tolist(), new_weights2.tolist()
return ind1, ind2
toolbox.register("mate", cxTickersWeights)
# Genetic operators
toolbox.register("evaluate", evaluate_portfolio)
def mutate_portfolio(individual, indpb=0.2):
mutation_type = random.choice(['ticker', 'weight'])
if mutation_type == 'ticker':
idx = random.randint(0, NUM_ASSETS - 1)
current_ticker = individual[idx]
available_tickers = list(set(successful_tickers) - set(individual[:NUM_ASSETS]))
if available_tickers:
new_ticker = random.choice(available_tickers)
individual[idx] = new_ticker
else:
try:
new_weights = generate_weights(NUM_ASSETS, LOWER_WEIGHT, UPPER_WEIGHT)
individual[NUM_ASSETS:] = new_weights.tolist()
except ValueError:
pass
return (individual,)
toolbox.register("mutate", mutate_portfolio, indpb=0.2)
toolbox.register("select", tools.selTournament, tournsize=3)
# Define the callback to print instant best portfolio
class PrintBestPortfolio:
def __init__(self, hof):
self.hof = hof
self.best_fitness = -np.inf
def __call__(self, gen, population, fitnesses):
current_best = max(fitnesses)
if current_best > self.best_fitness:
self.best_fitness = current_best
best_ind = tools.selBest(population, 1)[0]
selected = best_ind[:NUM_ASSETS]
weights = np.array(best_ind[NUM_ASSETS:])
try:
portfolio_returns_train = train_returns[selected].dot(weights)
portfolio_cagr_train = calculate_cagr(portfolio_returns_train)
portfolio_volatility_train = portfolio_returns_train.std() * np.sqrt(252)
sharpe_ratio_train = (portfolio_returns_train.mean() * 252 - RISK_FREE_RATE) / portfolio_volatility_train if portfolio_volatility_train != 0 else 0
sortino_growth_train = sortino_ratio(portfolio_returns_train[train_labels == 'Growth'])
sortino_recession_train = sortino_ratio(portfolio_returns_train[train_labels == 'Recession'])
portfolio_max_drawdown_growth_train = max_drawdown(portfolio_returns_train[train_labels == 'Growth'].cumprod())
portfolio_max_drawdown_recession_train = max_drawdown(portfolio_returns_train[train_labels == 'Recession'].cumprod())
print(f"\nGeneration {gen}: New Best Portfolio Found!")
# Display the metrics
print(f"CAGR (Train): {portfolio_cagr_train*100:.2f}%")
print(f"Volatility (Train): {portfolio_volatility_train*100:.2f}%")
print(f"Sharpe Ratio (Train): {sharpe_ratio_train:.2f}")
print(f"Sortino Ratio (Growth): {sortino_growth_train:.2f}")
print(f"Sortino Ratio (Recession): {sortino_recession_train:.2f}")
print(f"Max Drawdown (Growth): {portfolio_max_drawdown_growth_train:.2%}")
print(f"Max Drawdown (Recession): {portfolio_max_drawdown_recession_train:.2%}")
portfolio_df = pd.DataFrame({
'Ticker': selected,
'Allocation': [f"{w*100:.2f}%" for w in weights]
})
print(portfolio_df)
except Exception as e:
print(f"Error in calculating metrics: {e}")
# Set up multiprocessing pool
num_processes = max(1, multiprocessing.cpu_count() - 1) # Leave one core free
pool = multiprocessing.Pool(processes=num_processes)
toolbox.register("map", pool.map)
Key Improvements:
CAGR Calculation: Replaced cumulative returns with CAGR calculation using the calculate_cagr function.
Validation of Individuals: Ensured that tickers are unique, weights sum to 1, and tickers exist in the dataset.
Code Organization: Streamlined crossover and mutation functions to focus on ensuring valid configurations of tickers and weights.
Metric Output: Updated print statements and error handling to improve clarity and robustness.
Use of Multiprocessing: The multiprocessing setup is maintained to speed up the evaluation process.
This refined code should now correctly incorporate CAGR into the evaluation and output stages, allowing for a more accurate assessment of portfolio performance.