Untitled

mail@pastecode.io avatar
unknown
python
a month ago
818 B
67
Indexable
Never
import numpy as np
from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression

# Parameters
X = 200  # Lambda (mean and variance) for the Poisson distribution
N = 1000  # Number of samples

# Step 1: Sample from a Poisson distribution N times
samples = np.random.poisson(X, N)

# Step 2: Calculate the cumulative sum of the array
cumulative_sum = np.cumsum(samples)

# Step 3: Calculate the R^2 of the cumulative sum
# The independent variable will be the indices, and the dependent variable will be the cumulative sum
indices = np.arange(1, N + 1).reshape(-1, 1)  # Reshape for sklearn
model = LinearRegression().fit(indices, cumulative_sum)
predicted_cumulative_sum = model.predict(indices)

r_squared = r2_score(cumulative_sum, predicted_cumulative_sum)

print(f"R^2 value: {r_squared}")
Leave a Comment