Spaces:
Runtime error
Runtime error
Daniel Varga commited on
Commit ·
ff93ece
1
Parent(s): 9933360
initial commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# N sequences, each with n coinflips.
|
| 7 |
+
# for each sequence, determine the length of the longest constant 0 interval.
|
| 8 |
+
def histogram(N, n):
|
| 9 |
+
data = np.random.binomial(size=(N, n + 2), n=1, p=0.5)
|
| 10 |
+
# putting 1s at both ends as barriers:
|
| 11 |
+
data[:, 0] = 1
|
| 12 |
+
data[:, -1] = 1
|
| 13 |
+
|
| 14 |
+
ks = []
|
| 15 |
+
for i in range(N):
|
| 16 |
+
seq = data[i]
|
| 17 |
+
ones = np.nonzero(seq)[0]
|
| 18 |
+
jumps = np.diff(ones)
|
| 19 |
+
k = np.max(jumps) - 1
|
| 20 |
+
ks.append(k)
|
| 21 |
+
return np.array(ks)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def visualize(x):
|
| 25 |
+
x_range = int(np.max(x) - np.min(x) + 1)
|
| 26 |
+
fig = go.Figure(data=[go.Histogram(x=x, marker=dict(color='#7A88CC'), nbinsx=x_range)])
|
| 27 |
+
fig.update_layout(title='length of longest row of heads', xaxis_title='length k', yaxis_title='count')
|
| 28 |
+
return fig
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def update(N, n):
|
| 32 |
+
return visualize(histogram(N, n))
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
demo = gr.Interface(update,
|
| 36 |
+
inputs = [
|
| 37 |
+
gr.Slider(0, 10000, value=1000, label="N, the number of simulations"),
|
| 38 |
+
gr.Slider(0, 1000, value=50, label="n, the number of coin flips in a simulation"),
|
| 39 |
+
],
|
| 40 |
+
outputs = ["plot"],
|
| 41 |
+
live=True)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
demo.launch(show_api=False)
|