Created
February 6, 2025 19:03
-
-
Save nickelulz/6d926a7021af5a5f0ab87c204988b7e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
# Generate a hidden matrix of probabilities (true values) | |
def generate_hidden_matrix(N, M, seed=42): | |
np.random.seed(seed) | |
return np.random.rand(N, M) # Random probabilities between 0 and 1 | |
# Generate observed coin flips based on the hidden probabilities | |
def generate_observations(hidden_matrix, num_samples): | |
N, M = hidden_matrix.shape | |
observations = np.random.rand(num_samples, N, M) < hidden_matrix # 1 for heads, 0 for tails | |
return observations.astype(int) | |
# Train a simple model using gradient descent to estimate the hidden matrix | |
def train_model(observations, lr=0.1, epochs=1000): | |
num_samples, N, M = observations.shape | |
estimated_matrix = np.random.rand(N, M) # Initialize randomly | |
for epoch in range(epochs): | |
gradients = np.mean(observations - estimated_matrix, axis=0) # Compute gradient | |
estimated_matrix += lr * gradients # Gradient descent update | |
if epoch % 100 == 0: | |
loss = np.mean((estimated_matrix - np.mean(observations, axis=0))**2) | |
print(f"Epoch {epoch}: Loss = {loss:.4f}") | |
return estimated_matrix | |
# Main execution | |
N, M = 5, 5 # Size of hidden matrix | |
num_samples = 1000 # Number of observed trials | |
hidden_matrix = generate_hidden_matrix(N, M) | |
observations = generate_observations(hidden_matrix, num_samples) | |
print(observations) | |
estimated_matrix = train_model(observations) | |
print("Hidden matrix:") | |
print(hidden_matrix) | |
print("\nEstimated matrix:") | |
print(estimated_matrix) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment