Spaces:
Sleeping
Sleeping
skanderovitch
commited on
Commit
β’
606728b
1
Parent(s):
14a6dc4
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import plotly.graph_objects as go
|
|
7 |
import plotly.express as px
|
8 |
import numpy as np
|
9 |
import pandas as pd
|
|
|
10 |
|
11 |
|
12 |
st.set_page_config(page_title='Can you be truly random ?', layout = 'wide', page_icon = 'favicon.jpg', initial_sidebar_state = 'auto')
|
@@ -47,6 +48,7 @@ if 'stage' not in st.session_state:
|
|
47 |
reset_game()
|
48 |
|
49 |
st.title('Can your brain be random?')
|
|
|
50 |
|
51 |
if st.session_state.stage == 0:
|
52 |
st.button('Begin', on_click=set_state, args=[1],use_container_width=True)
|
@@ -55,14 +57,15 @@ if st.session_state.stage == 0:
|
|
55 |
|
56 |
st.markdown(f'You will be presented with {st.session_state.n_buttons} buttons to randomly choose from')
|
57 |
|
58 |
-
|
59 |
-
st.session_state.max_history = st.slider(label="How much memory can I use?", min_value=2, max_value=20,value=10)
|
60 |
st.markdown(f"At each round, I will try to predict which button you click")
|
|
|
|
|
61 |
st.markdown(f"I will examine {st.session_state.max_history}-long sequences you played in the past")
|
62 |
st.markdown('This will help me make a guess')
|
63 |
st.markdown(f"If I get it right, I earn {st.session_state.n_buttons-1} point(s), otherwise you earn 1 point")
|
64 |
-
st.
|
65 |
-
st.
|
|
|
66 |
|
67 |
|
68 |
|
@@ -132,6 +135,8 @@ if st.session_state.stage == 1:
|
|
132 |
pred,data = make_pred()
|
133 |
|
134 |
choice = max(pred,key=pred.get)
|
|
|
|
|
135 |
|
136 |
cols = st.columns(st.session_state.n_buttons)
|
137 |
for i,col in enumerate(cols):
|
@@ -141,25 +146,32 @@ if st.session_state.stage == 1:
|
|
141 |
col1,col2 = st.columns(2)
|
142 |
|
143 |
win,sharpe = compute_perf()
|
144 |
-
if sharpe > 0.1:
|
145 |
-
emoticon = "π"
|
146 |
-
elif sharpe < 0:
|
147 |
-
emoticon = "π"
|
148 |
-
else:
|
149 |
-
emoticon = "π§ "
|
150 |
-
text = f'%age win={win:.0%} Sharpe={sharpe:.3f} {emoticon}'
|
151 |
-
col1.subheader('My earnings so far...')
|
152 |
-
col1.write(text)
|
153 |
-
fig = px.line(st.session_state.pnl)
|
154 |
-
fig.update_layout(showlegend=False)
|
155 |
-
fig.update_layout(xaxis={'visible': True, 'showticklabels': False})
|
156 |
-
col1.plotly_chart(fig, use_container_width=True)
|
157 |
-
|
158 |
-
cols = ['you_played','similarity','weight','how_long_ago'] + [f'same_{i}' for i in range(st.session_state.max_history)]
|
159 |
-
data = pd.DataFrame(data,columns=cols).set_index('how_long_ago').sort_values('weight',ascending=False)[:10]
|
160 |
-
col2.subheader(f'In the past, you played')
|
161 |
-
col2.dataframe(data.style.background_gradient(cmap=cm), use_container_width=True)
|
162 |
-
col2.subheader(f'So I chose {choice}, p={pred[choice]:.0%}')
|
163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
st.button('Start over', on_click=reset_game, args=[],
|
165 |
use_container_width=True)
|
|
|
7 |
import plotly.express as px
|
8 |
import numpy as np
|
9 |
import pandas as pd
|
10 |
+
from scipy.stats import norm
|
11 |
|
12 |
|
13 |
st.set_page_config(page_title='Can you be truly random ?', layout = 'wide', page_icon = 'favicon.jpg', initial_sidebar_state = 'auto')
|
|
|
48 |
reset_game()
|
49 |
|
50 |
st.title('Can your brain be random?')
|
51 |
+
n_plays = 50
|
52 |
|
53 |
if st.session_state.stage == 0:
|
54 |
st.button('Begin', on_click=set_state, args=[1],use_container_width=True)
|
|
|
57 |
|
58 |
st.markdown(f'You will be presented with {st.session_state.n_buttons} buttons to randomly choose from')
|
59 |
|
|
|
|
|
60 |
st.markdown(f"At each round, I will try to predict which button you click")
|
61 |
+
st.session_state.max_history = st.slider(label="How much memory can I use?", min_value=2, max_value=20,value=10)
|
62 |
+
|
63 |
st.markdown(f"I will examine {st.session_state.max_history}-long sequences you played in the past")
|
64 |
st.markdown('This will help me make a guess')
|
65 |
st.markdown(f"If I get it right, I earn {st.session_state.n_buttons-1} point(s), otherwise you earn 1 point")
|
66 |
+
st.subheader(f'This is a fair game, as you have {(1-1/st.session_state.n_buttons):.0%} chances of winning')
|
67 |
+
st.markdown(f"Play as long as you want and try to beat me!")
|
68 |
+
st.markdown(f"I will start showing you my strategy from {n_plays} rounds")
|
69 |
|
70 |
|
71 |
|
|
|
135 |
pred,data = make_pred()
|
136 |
|
137 |
choice = max(pred,key=pred.get)
|
138 |
+
progress = min(1.,float(len(st.session_state.history))/n_plays)
|
139 |
+
my_bar = st.progress(progress)
|
140 |
|
141 |
cols = st.columns(st.session_state.n_buttons)
|
142 |
for i,col in enumerate(cols):
|
|
|
146 |
col1,col2 = st.columns(2)
|
147 |
|
148 |
win,sharpe = compute_perf()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
+
if progress >= 1.:
|
151 |
+
|
152 |
+
if sharpe > 0.1:
|
153 |
+
emoticon = "π"
|
154 |
+
elif sharpe < 0:
|
155 |
+
emoticon = "π"
|
156 |
+
else:
|
157 |
+
emoticon = "π§ "
|
158 |
+
text = f'%age win={win:.0%} Sharpe={sharpe:.3f} {emoticon}'
|
159 |
+
col1.subheader('My earnings so far...')
|
160 |
+
col1.write(text)
|
161 |
+
dev = abs(st.session_state.pnl[-1] / st.session_state.max[-1])
|
162 |
+
chance = 1 - norm.cdf(dev)
|
163 |
+
col1.write(f'There is only {chance:.2%} chances of achieving this by chance')
|
164 |
+
fig = px.line(st.session_state.pnl)
|
165 |
+
fig.update_layout(showlegend=False)
|
166 |
+
fig.update_layout(xaxis={'visible': True, 'showticklabels': False})
|
167 |
+
col1.plotly_chart(fig, use_container_width=True)
|
168 |
+
|
169 |
+
cols = ['you_played','similarity','weight','how_long_ago'] + [f'same_{i}' for i in range(st.session_state.max_history)]
|
170 |
+
data = pd.DataFrame(data,columns=cols).set_index('how_long_ago').sort_values('weight',ascending=False)[:10]
|
171 |
+
col2.subheader(f'In the past, you played')
|
172 |
+
col2.dataframe(data.style.background_gradient(cmap=cm), use_container_width=True)
|
173 |
+
won = choice == st.session_state.history[-1]
|
174 |
+
col2.write(f'So I chose {choice} (p={pred[choice]:.0%}) and ' + ('won π' if won else 'lost π'))
|
175 |
+
|
176 |
st.button('Start over', on_click=reset_game, args=[],
|
177 |
use_container_width=True)
|