-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (47 loc) · 1.86 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pandas as pd
import scipy.stats
import streamlit as st
import time
st.header('Tossing a Coin')
number_of_trials = st.slider('Number of trials?', 1, 1000, 10, key='trials_slider')
start_button = st.button('Run')
if start_button:
st.write(f'Running the experient of {number_of_trials} trials.')
st.write('It is not a functional application yet. Under construction.')
# these are stateful variables which are preserved as Streamlin reruns this script
if 'experiment_no' not in st.session_state:
st.session_state['experiment_no'] = 0
if 'df_experiment_results' not in st.session_state:
st.session_state['df_experiment_results'] = pd.DataFrame(columns=['no', 'iterations', 'mean'])
st.header('Tossing a Coin')
chart = st.line_chart([0.5])
def toss_coin(n):
trial_outcomes = scipy.stats.bernoulli.rvs(p=0.5, size=n)
mean = None
outcome_no = 0
outcome_1_count = 0
for r in trial_outcomes:
outcome_no +=1
if r == 1:
outcome_1_count += 1
mean = outcome_1_count / outcome_no
chart.add_rows([mean])
time.sleep(0.05)
return mean
number_of_trials = st.slider('Number of trials?', 1, 1000, 10)
start_button = st.button('Run')
if start_button:
st.write(f'Running the experient of {number_of_trials} trials.')
st.session_state['experiment_no'] += 1
mean = toss_coin(number_of_trials)
st.session_state['df_experiment_results'] = pd.concat([
st.session_state['df_experiment_results'],
pd.DataFrame(data=[[st.session_state['experiment_no'],
number_of_trials,
mean]],
columns=['no', 'iterations', 'mean'])
],
axis=0)
st.session_state['df_experiment_results'] = \
st.session_state['df_experiment_results'].reset_index(drop=True)
st.write(st.session_state['df_experiment_results'])