|
1 | 1 | """Methods for fine-tuning parameters of alpha.""" |
2 | 2 |
|
3 | | -from itertools import product |
| 3 | +import random |
4 | 4 |
|
| 5 | +from brain.agent import agent |
| 6 | +from brain.agent_config import DEFAULT_CONFIG |
5 | 7 | from brain.alpha_class import Alpha |
6 | | -from brain.api import BrainAPI |
7 | | - |
8 | | - |
9 | | -def generate_alpha_grid(regular: str): |
10 | | - """Generate a grid of alpha parameters for fine-tuning.""" |
11 | | - # Define the grid of parameters to explore |
12 | | - param_options = { |
13 | | - # "universe": ["TOP3000", "TOP1000", "TOP500", "TOP200", "TOPSP500"], |
14 | | - "universe": ["TOP3000", "TOP1000", "TOP500"], |
15 | | - "neutralization": ["INDUSTRY", "SECTOR", "MARKET", "NONE", "SUBINDUSTRY"], |
16 | | - "decay": [4, 8, 16, 32], |
17 | | - "truncation": [0.01, 0.05, 0.1], |
18 | | - # "pasteurization": ["ON", "OFF"], |
19 | | - "pasteurization": ["ON"], |
| 8 | +from brain.alpha_storage import Storage |
| 9 | +from brain.genetic_algorithm import genetic_algorithm |
| 10 | +from brain.score import get_score |
| 11 | +from brain.tools.ideas import get_random_idea |
| 12 | +from brain.tools.simulation import StopException |
| 13 | + |
| 14 | +MAIN_ALPHA = Alpha( |
| 15 | + regular="ts_corr(fnd6_newqv1300_lltq, fnd6_newqv1300_aociotherq, 40) * zscore(ts_mean(pcr_vol_120, 40))", |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def create_alpha_simulation(storage: Storage): |
| 20 | + """Create a new alpha based on the given ID.""" |
| 21 | + |
| 22 | + formatted_alphas = { |
| 23 | + cat: "\n".join(alpha.prompt_format() for alpha in storage.get_top_k(cat, 10)) |
| 24 | + for cat in storage.categories |
20 | 25 | } |
21 | 26 |
|
22 | | - # Generate all combinations of parameters |
23 | | - combinations = list(product(*param_options.values())) |
24 | | - params = [dict(zip(param_options.keys(), values)) for values in combinations] |
25 | | - alphas = [Alpha.create_alpha(regular=regular, **p) for p in params] |
| 27 | + if random.random() < 0.05: |
| 28 | + prompt = "Create a completely new alpha by random data fields." |
| 29 | + else: |
| 30 | + prompt = f""" |
| 31 | +Your task is to fine-tune the parameters of the following alpha: |
| 32 | +{MAIN_ALPHA.prompt_format()} |
| 33 | +Create a new alphas by adding or removing data fields, changing parameters, or modifying the logic. |
| 34 | +You can add/remove data fields, change parameters, add operators like neutralization |
| 35 | +or modify the logic. |
| 36 | +HOWEVER, THE CORE LOGIC OF THE ALPHA SHOULD REMAIN THE SAME. |
| 37 | +
|
| 38 | +PASSING |
| 39 | +------- |
| 40 | +{formatted_alphas['passing']} |
| 41 | +
|
| 42 | +FAILING |
| 43 | +------- |
| 44 | +{formatted_alphas['failing']} |
| 45 | +
|
| 46 | +PENDING |
| 47 | +------- |
| 48 | +{formatted_alphas['pending']} |
| 49 | +
|
| 50 | +{get_random_idea() if random.random() < 0.3 else ''} |
| 51 | +""" |
26 | 52 |
|
27 | | - return alphas |
| 53 | + print(f"Prompt:\n{prompt}") |
28 | 54 |
|
| 55 | + alphas_store = [] |
| 56 | + while not alphas_store: |
| 57 | + try: |
| 58 | + agent.invoke( |
| 59 | + { |
| 60 | + "messages": [ |
| 61 | + { |
| 62 | + "role": "user", |
| 63 | + "content": prompt, |
| 64 | + } |
| 65 | + ] |
| 66 | + }, |
| 67 | + config={ |
| 68 | + "recursion_limit": 100, |
| 69 | + "configurable": { |
| 70 | + **DEFAULT_CONFIG, |
| 71 | + "alphas": alphas_store, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ) |
| 75 | + except StopException: |
| 76 | + continue |
29 | 77 |
|
30 | | -def get_fitness(result): |
31 | | - """Get the fitness of the alpha from result.""" |
32 | | - stats = result["is_stats"] |
33 | | - if "fitness" in stats: |
34 | | - return stats["fitness"][0] |
| 78 | + print(f"Alphas store: {alphas_store}") |
| 79 | + return alphas_store[-1] |
35 | 80 |
|
36 | | - return -1 |
37 | 81 |
|
| 82 | +def main(): |
| 83 | + """Main function to run the agent.""" |
| 84 | + storage = Storage(score_func=get_score, max_size=50) |
| 85 | + genetic_algorithm(storage, create_alpha_simulation) |
38 | 86 |
|
39 | | -def fine_tune_alpha(regular: str): |
40 | | - alphas = generate_alpha_grid(regular) |
41 | | - results = BrainAPI.simulate_alpha_list(alphas) |
42 | | - sorted_results = sorted(results, key=get_fitness, reverse=True) |
43 | 87 |
|
44 | | - print("Best alpha parameters:") |
45 | | - for i, result in enumerate(sorted_results): |
46 | | - print(f"Rank {i + 1}:") |
47 | | - print(f"Alpha: {result['alpha']}") |
48 | | - print(f"Fitness: {get_fitness(result)}") |
49 | | - print(f"Parameters: {result['simulate_data']}") |
50 | | - print() |
| 88 | +if __name__ == "__main__": |
| 89 | + main() |
0 commit comments