Spaces:
Runtime error
Runtime error
huggingface112
commited on
Commit
•
c0e0ce8
1
Parent(s):
6c25b38
added two methods
Browse files- script/processing.py +35 -5
script/processing.py
CHANGED
@@ -77,7 +77,8 @@ def get_processing_result_of_stocks_df(stock_df, profile_df):
|
|
77 |
stock_df['sector_pct'] = stock_df['pct'] * stock_df['prev_w_in_sectore']
|
78 |
|
79 |
# portfolio return
|
80 |
-
stock_df['portfolio_return'] = (stock_df.groupby(
|
|
|
81 |
# stock_df['cum_p_pct'] = stock_df.groupby(
|
82 |
# 'ticker')['portfolio_pct'].cumsum()
|
83 |
# stock_df['portfolio_return'] = np.exp(stock_df['cum_p_pct']) - 1
|
@@ -151,8 +152,6 @@ def get_portfolio_evaluation(portfolio_stock, benchmark_stock, profile_df):
|
|
151 |
return merged_df
|
152 |
|
153 |
|
154 |
-
|
155 |
-
|
156 |
def get_portfolio_sector_evaluation(portfolio_stock, benchmark_df):
|
157 |
# aggregate on sector and day
|
158 |
p_sector_df = portfolio_stock.groupby(['date', 'aggregate_sector'], as_index=False)\
|
@@ -291,8 +290,10 @@ def calculate_return(df, start, end):
|
|
291 |
# set the pct of first row to null
|
292 |
selected_df.iloc[0, selected_df.columns.get_indexer(
|
293 |
['portfolio_pct_p', 'portfolio_pct_b'])] = 0
|
294 |
-
selected_df['return_p'] = (
|
295 |
-
|
|
|
|
|
296 |
selected_df['active_return'] = selected_df.return_p - selected_df.return_b
|
297 |
return selected_df
|
298 |
|
@@ -367,3 +368,32 @@ def change_resolution(df, freq='W'):
|
|
367 |
'''
|
368 |
df['freq'] = pd.to_datetime(df['date']).dt.to_period(freq)
|
369 |
return df.groupby('freq').first().reset_index()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
stock_df['sector_pct'] = stock_df['pct'] * stock_df['prev_w_in_sectore']
|
78 |
|
79 |
# portfolio return
|
80 |
+
stock_df['portfolio_return'] = (stock_df.groupby(
|
81 |
+
'ticker')['portfolio_pct'].cumprod() + 1) - 1
|
82 |
# stock_df['cum_p_pct'] = stock_df.groupby(
|
83 |
# 'ticker')['portfolio_pct'].cumsum()
|
84 |
# stock_df['portfolio_return'] = np.exp(stock_df['cum_p_pct']) - 1
|
|
|
152 |
return merged_df
|
153 |
|
154 |
|
|
|
|
|
155 |
def get_portfolio_sector_evaluation(portfolio_stock, benchmark_df):
|
156 |
# aggregate on sector and day
|
157 |
p_sector_df = portfolio_stock.groupby(['date', 'aggregate_sector'], as_index=False)\
|
|
|
290 |
# set the pct of first row to null
|
291 |
selected_df.iloc[0, selected_df.columns.get_indexer(
|
292 |
['portfolio_pct_p', 'portfolio_pct_b'])] = 0
|
293 |
+
selected_df['return_p'] = (
|
294 |
+
1 + selected_df['portfolio_pct_p']).cumprod() - 1
|
295 |
+
selected_df['return_b'] = (
|
296 |
+
1 + selected_df['portfolio_pct_b']).cumprod() - 1
|
297 |
selected_df['active_return'] = selected_df.return_p - selected_df.return_b
|
298 |
return selected_df
|
299 |
|
|
|
368 |
'''
|
369 |
df['freq'] = pd.to_datetime(df['date']).dt.to_period(freq)
|
370 |
return df.groupby('freq').first().reset_index()
|
371 |
+
|
372 |
+
|
373 |
+
def calculate_weight_using_cash(df):
|
374 |
+
'''
|
375 |
+
patch df with current weight for each entry
|
376 |
+
use cash to calculate weight
|
377 |
+
|
378 |
+
Parameters
|
379 |
+
----------
|
380 |
+
df : dataframe
|
381 |
+
dataframe with processed cash column
|
382 |
+
|
383 |
+
'''
|
384 |
+
df['cur_w'] = float('nan')
|
385 |
+
grouped = df.groupby('time')
|
386 |
+
df.cur_w = grouped.cash.transform(lambda x: x / x.sum())
|
387 |
+
|
388 |
+
|
389 |
+
def calculate_cash(df):
|
390 |
+
'''
|
391 |
+
patch df with cash column
|
392 |
+
cash = shares * close
|
393 |
+
|
394 |
+
Parameters
|
395 |
+
----------
|
396 |
+
df : dataframe
|
397 |
+
dataframe with processed shares and close column
|
398 |
+
'''
|
399 |
+
df['cash'] = df['shares'] * df['close']
|