tiantian-paris commited on
Commit
0d1e757
·
verified ·
1 Parent(s): 6ba5983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -18
app.py CHANGED
@@ -12,27 +12,59 @@ from Gradio_UI import GradioUI
12
 
13
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
  @tool
15
- def calculate_historical_var(returns: np.ndarray, var_level: float = 0.95) -> float:
 
 
 
 
 
 
16
  """
17
- Calculate the Value at Risk (VaR) using the historical method.
18
-
19
  Args:
20
- returns: Array of daily returns.
21
- var_level: VaR level (e.g., 0.95 for 95%).
22
-
 
 
 
23
  Returns:
24
- float: VaR value.
25
  """
26
- # Sort the returns in ascending order
27
- sorted_returns = np.sort(returns)
28
-
29
- # Determine the index for the VaR level
30
- index = int((1 - var_level) * len(returns))
31
-
32
- # Get the VaR value
33
- var_value = sorted_returns[index]
34
-
35
- return var_value
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  @tool
38
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -73,7 +105,7 @@ with open("prompts.yaml", 'r') as stream:
73
 
74
  agent = CodeAgent(
75
  model=model,
76
- tools=[final_answer,calculate_historical_var,get_current_time_in_timezone,visit_webpage,web_search], ## add your tools here (don't remove final answer)
77
  max_steps=6,
78
  verbosity_level=1,
79
  grammar=None,
 
12
 
13
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
  @tool
15
+ def calculate_risk_metrics(
16
+ returns: np.ndarray,
17
+ var_level: float = 0.95,
18
+ n_simulations: int = 10000,
19
+ bootstrap: bool = False,
20
+ random_seed: Optional[int] = None
21
+ ) -> Tuple[float, float]:
22
  """
23
+ Calculate Value at Risk (VaR) and Expected Shortfall (ES) using the historical method, with an option for bootstrapped historical simulation.
24
+
25
  Args:
26
+ returns: Array of daily returns. Each value represents the percentage return for a single day.
27
+ var_level: VaR level (e.g., 0.95 for 95% confidence). Defaults to 0.95.
28
+ n_simulations: Number of bootstrap simulations. Defaults to 10000.
29
+ bootstrap: If True, use bootstrapped historical simulation. Defaults to False.
30
+ random_seed: Seed for random number generation to ensure reproducibility. Defaults to None.
31
+
32
  Returns:
33
+ Tuple[float, float]: A tuple containing the VaR and Expected Shortfall (ES) values.
34
  """
35
+ if random_seed is not None:
36
+ np.random.seed(random_seed)
37
+
38
+ if bootstrap:
39
+ # Perform bootstrapped historical simulation
40
+ simulated_var = np.zeros(n_simulations)
41
+ simulated_es = np.zeros(n_simulations)
42
+
43
+ for i in range(n_simulations):
44
+ # Resample returns with replacement
45
+ resampled_returns = np.random.choice(returns, size=len(returns), replace=True)
46
+ # Sort the resampled returns
47
+ sorted_returns = np.sort(resampled_returns)
48
+ # Determine the index for the VaR level
49
+ index = int((1 - var_level) * len(sorted_returns))
50
+ # Calculate VaR for this simulation
51
+ simulated_var[i] = sorted_returns[index]
52
+ # Calculate ES for this simulation (average of returns below VaR)
53
+ simulated_es[i] = np.mean(sorted_returns[:index])
54
+
55
+ # Calculate the average VaR and ES across all simulations
56
+ var_value = np.mean(simulated_var)
57
+ es_value = np.mean(simulated_es)
58
+ else:
59
+ # Use the standard historical method
60
+ sorted_returns = np.sort(returns)
61
+ index = int((1 - var_level) * len(returns))
62
+ # Calculate VaR
63
+ var_value = sorted_returns[index]
64
+ # Calculate ES (average of returns below VaR)
65
+ es_value = np.mean(sorted_returns[:index])
66
+
67
+ return var_value, es_value
68
 
69
  @tool
70
  def get_current_time_in_timezone(timezone: str) -> str:
 
105
 
106
  agent = CodeAgent(
107
  model=model,
108
+ tools=[final_answer,calculate_risk_metrics,get_current_time_in_timezone,visit_webpage,web_search], ## add your tools here (don't remove final answer)
109
  max_steps=6,
110
  verbosity_level=1,
111
  grammar=None,