Erroe Metrics
Erroe Metrics
df1 = pd.read_csv('D:\\pythonProject1\\venv\\Rain_imd\\IMD_DataSET\\annual.csv')
d2 = np.array(df1)
rainfall_data = d2[:, 7]
print("Observed Rainfall Data:")
print(rainfall_data, len(rainfall_data))
# ------------------------------
# 2. Compute transition probabilities
# ------------------------------
n = len(rainfall_data) - 1 # number of transitions
dry_prev = rainfall_data[:-1] == 0
wet_prev = rainfall_data[:-1] > 0
dry_curr = rainfall_data[1:] == 0
wet_curr = rainfall_data[1:] > 0
print("\nTransition Probabilities:")
print(f"P_00 (Dry-Dry): {P_00:.3f}")
print(f"P_01 (Dry-Wet): {P_01:.3f}")
print(f"P_10 (Wet-Dry): {P_10:.3f}")
print(f"P_11 (Wet-Wet): {P_11:.3f}")
# ------------------------------
# 3. Compute empirical CDFs
# ------------------------------
def empirical_cdf(data, x):
return np.count_nonzero(data <= x) / len(data)
# ------------------------------
# 4. Gumbel Copula Functions
# ------------------------------
# Conditional CDF using Gumbel copula for a given x (if x > 0) and rainfall value y
def O_Y_given_X_x_and_X_positive(y, x):
u, v = H_x(x), H_y(y)
C_uv_partial = gumbel_copula_partial_u(u, v, theta)
h_x = np.count_nonzero(rainfall_data[:-1] == x) / len(rainfall_data[:-1]) if
len(rainfall_data[:-1]) > 0 else 0
f_y = np.count_nonzero(rainfall_data[1:] == y) / len(rainfall_data[1:]) if
len(rainfall_data[1:]) > 0 else 0
denom = P_10 * h_x + P_11 * f_y
return (P_10 * h_x + P_11 * f_y * C_uv_partial) / denom if denom > 0 else
H_y(y)
# ------------------------------
# 5. Generate Plots for Conditional CDF
# ------------------------------
y_values = np.linspace(0, max(rainfall_data), 100)
cdf_0 = np.array([O_Y_given_X_0(y) for y in y_values])
cdf_x_positive = np.array([O_Y_given_X_x_and_X_positive(y, 3) for y in y_values])
plt.figure(figsize=(10, 6))
plt.plot(y_values, cdf_0, label="O_Y_given_X=0", linestyle="--", linewidth=2)
plt.plot(y_values, cdf_x_positive, label="O_Y_given_X=x and X>0", linewidth=2)
plt.scatter(rainfall_data[1:], [O_Y_given_X_x_and_X_positive(y, 3) for y in
rainfall_data[1:]],
color='red', alpha=0.5, label="Observed Rainfall")
plt.xlabel('Rainfall (mm)')
plt.ylabel('Conditional CDF')
plt.legend()
plt.title('Conditional CDF of Y given X')
plt.grid()
# plt.show()
# ------------------------------
# 6. Simulate rainfall using the Markov Chain with Copula
# ------------------------------
simulated_rainfall = np.zeros(n, dtype=float)
simulated_rainfall[0] = rainfall_data[0] # start with the observed first day
# For wet days, sample intensities from the observed wet days
wet_intensities = rainfall_data[rainfall_data > 0]
if len(wet_intensities) == 0:
raise ValueError("No wet days found in observed data; cannot sample
intensities.")
# ------------------------------
# 7. Calculate Performance Metrics
# ------------------------------
# For the performance metrics, we compare the simulated series with the
corresponding observed series.
# Note: simulated_rainfall has length n (which is len(rainfall_data)-1). We compare
it with the first n values.
observed = rainfall_data[:n]
simulated = simulated_rainfall
print("\nPerformance Metrics:")
print(f"Nash-Sutcliffe Efficiency (NSE): {nse_val:.3f}")
print(f"Root Mean Square Error (RMSE): {rmse_val:.3f}")
print(f"Percent Bias (PBIAS): {pbias_val:.3f}%")
print(f"R-squared (R²): {r2_val:.3f}")
print(f"Kling-Gupta Efficiency (KGE): {kge_val:.3f}")
plt.show()