Untitled

 avatar
unknown
python
a year ago
818 B
116
Indexable
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}")
Editor is loading...
Leave a Comment