James McCool
commited on
Commit
·
4d1ad75
1
Parent(s):
232eb2a
Add trim slack parameter to portfolio trimming functionality in app.py and trim_portfolio.py, allowing for more flexible threshold adjustments during portfolio management.
Browse files- app.py +2 -1
- global_func/trim_portfolio.py +3 -3
app.py
CHANGED
@@ -912,6 +912,7 @@ with tab2:
|
|
912 |
with st.form(key='trim_form'):
|
913 |
performance_type = st.selectbox("Select Sorting variable", ['median', 'Finish_percentile'])
|
914 |
own_type = st.selectbox("Select trimming variable", ['Own', 'Geomean', 'Weighted Own'])
|
|
|
915 |
performance_threshold_high = st.number_input("Select performance threshold (will only trim if sorting variable is lower than this)", value=st.session_state['portfolio'][performance_type].max(), min_value=0.0, step=1.0)
|
916 |
performance_threshold_low = st.number_input("Select performance threshold (will only trim if sorting variable is higher than this)", value=0.0, min_value=0.0, step=1.0)
|
917 |
own_threshold_high = st.number_input("Select own threshold (will only trim if trimming variable is lower than this)", value=st.session_state['portfolio'][own_type].max(), min_value=0.0, step=1.0)
|
@@ -949,7 +950,7 @@ with tab2:
|
|
949 |
)
|
950 |
st.session_state['portfolio'] = st.session_state['portfolio'][lock_mask]
|
951 |
|
952 |
-
st.session_state['portfolio'] = trim_portfolio(st.session_state['portfolio'], performance_type, own_type, performance_threshold_high, performance_threshold_low, own_threshold_high, own_threshold_low)
|
953 |
st.session_state['portfolio'] = st.session_state['portfolio'].sort_values(by='median', ascending=False)
|
954 |
|
955 |
with col2:
|
|
|
912 |
with st.form(key='trim_form'):
|
913 |
performance_type = st.selectbox("Select Sorting variable", ['median', 'Finish_percentile'])
|
914 |
own_type = st.selectbox("Select trimming variable", ['Own', 'Geomean', 'Weighted Own'])
|
915 |
+
trim_slack_var = st.number_input("Select trim slack (percentile addition to trimming variable ceiling)", value=0.0, min_value=0.0, max_value=1.0, step=0.1)
|
916 |
performance_threshold_high = st.number_input("Select performance threshold (will only trim if sorting variable is lower than this)", value=st.session_state['portfolio'][performance_type].max(), min_value=0.0, step=1.0)
|
917 |
performance_threshold_low = st.number_input("Select performance threshold (will only trim if sorting variable is higher than this)", value=0.0, min_value=0.0, step=1.0)
|
918 |
own_threshold_high = st.number_input("Select own threshold (will only trim if trimming variable is lower than this)", value=st.session_state['portfolio'][own_type].max(), min_value=0.0, step=1.0)
|
|
|
950 |
)
|
951 |
st.session_state['portfolio'] = st.session_state['portfolio'][lock_mask]
|
952 |
|
953 |
+
st.session_state['portfolio'] = trim_portfolio(st.session_state['portfolio'], trim_slack_var, performance_type, own_type, performance_threshold_high, performance_threshold_low, own_threshold_high, own_threshold_low)
|
954 |
st.session_state['portfolio'] = st.session_state['portfolio'].sort_values(by='median', ascending=False)
|
955 |
|
956 |
with col2:
|
global_func/trim_portfolio.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
import pandas as pd
|
2 |
|
3 |
-
def trim_portfolio(portfolio: pd.DataFrame, performance_type: str, own_type: str, performance_threshold_high: float, performance_threshold_low: float, own_threshold_high: float, own_threshold_low: float):
|
4 |
if performance_type == 'Finish_percentile':
|
5 |
working_portfolio = portfolio.sort_values(by=performance_type, ascending = True).reset_index(drop=True)
|
6 |
else:
|
7 |
working_portfolio = portfolio.sort_values(by=performance_type, ascending = False).reset_index(drop=True)
|
8 |
rows_to_drop = []
|
9 |
-
curr_own_type_max = working_portfolio.loc[0, own_type]
|
10 |
|
11 |
for i in range(1, len(working_portfolio)):
|
12 |
if working_portfolio.loc[i, own_type] > curr_own_type_max and \
|
@@ -16,7 +16,7 @@ def trim_portfolio(portfolio: pd.DataFrame, performance_type: str, own_type: str
|
|
16 |
working_portfolio.loc[i, own_type] <= own_threshold_high:
|
17 |
rows_to_drop.append(i)
|
18 |
else:
|
19 |
-
curr_own_type_max = working_portfolio.loc[i, own_type]
|
20 |
|
21 |
working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
|
22 |
return working_portfolio
|
|
|
1 |
import pandas as pd
|
2 |
|
3 |
+
def trim_portfolio(portfolio: pd.DataFrame, trim_slack: float, performance_type: str, own_type: str, performance_threshold_high: float, performance_threshold_low: float, own_threshold_high: float, own_threshold_low: float):
|
4 |
if performance_type == 'Finish_percentile':
|
5 |
working_portfolio = portfolio.sort_values(by=performance_type, ascending = True).reset_index(drop=True)
|
6 |
else:
|
7 |
working_portfolio = portfolio.sort_values(by=performance_type, ascending = False).reset_index(drop=True)
|
8 |
rows_to_drop = []
|
9 |
+
curr_own_type_max = working_portfolio.loc[0, own_type] + (trim_slack * working_portfolio.loc[0, own_type])
|
10 |
|
11 |
for i in range(1, len(working_portfolio)):
|
12 |
if working_portfolio.loc[i, own_type] > curr_own_type_max and \
|
|
|
16 |
working_portfolio.loc[i, own_type] <= own_threshold_high:
|
17 |
rows_to_drop.append(i)
|
18 |
else:
|
19 |
+
curr_own_type_max = working_portfolio.loc[i, own_type] + (trim_slack * working_portfolio.loc[0, own_type])
|
20 |
|
21 |
working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
|
22 |
return working_portfolio
|