-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbirthprocess.py
More file actions
77 lines (66 loc) · 2.42 KB
/
birthprocess.py
File metadata and controls
77 lines (66 loc) · 2.42 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
# Models
def birthProcess(birth_rate,total_time):
time=0
population=0
times=[time]
populations=[population]
while time<total_time:
time_to_next_birth = np.random.exponential(1 / birth_rate)
time += time_to_next_birth
population+=1
times.append(time)
populations.append(population)
return times,populations
# Visualization
def visualize_birth(times, populations):
plt.figure(figsize=(10, 5))
plt.step(times, populations, where='post')
plt.title('Pure Birth Process Simulation')
plt.xlabel('Time')
plt.ylabel('Population Size')
plt.grid()
st.pyplot(plt)
# Diagram
def plot_state_transition_diagram_birth(max_population, birth_rate):
G = nx.DiGraph()
for i in range(max_population + 1):
G.add_node(i)
if i < max_population:
G.add_edge(i, i + 1) # Just create the edges without weights
pos = nx.spring_layout(G) # Calculate positions without weights
plt.figure(figsize=(10, 8))
nx.draw(G, pos, with_labels=True, node_size=1800, node_color='lightblue', font_size=12)
# Draw edge labels with birth rate
edge_labels = {(i, i + 1): f'λ={birth_rate}' for i in range(max_population)}
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color='red', font_size=6)
plt.title("State Transition Diagram")
plt.xlabel("Population Size")
plt.ylabel("Next Population Size")
plt.grid()
st.pyplot(plt)
# Statistics
def display_statistics(populations):
# Calculate statistics
final_population = populations[-1]
mean_population = np.mean(populations)
std_population = np.std(populations)
# Create a DataFrame for more statistics if needed
df = pd.DataFrame({'Time': range(len(populations)), 'Population': populations})
# Display statistics in Streamlit
st.write("### Statistics")
st.write(f"Final Population: {final_population}")
st.write(f"Mean Population: {mean_population:.2f}")
st.write(f"Standard Deviation of Population: {std_population:.2f}")
# Plot population over time
plt.figure(figsize=(10, 5))
plt.plot(df['Time'], df['Population'], marker='o', linestyle='-', color='blue')
plt.title("Population Over Time")
plt.xlabel("Time")
plt.ylabel("Population")
plt.grid()
st.pyplot(plt)