import scipy.stats as st
from collections import namedtuple
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
percentil_1
reservas_1
percentil_2
reservas_2
percentil_desconocido
Percentile = namedtuple('Percentile', 'p value')
p1 = Percentile(1 - (float(percentil_1) / 100), float(reservas_1))
p2 = Percentile(1 - (float(percentil_2) / 100), float(reservas_2))
p_find = 1 - (float(percentil_desconocido) / 100)
z1 = st.norm.ppf(p1.p)
z2 = st.norm.ppf(p2.p)
print(f"z1: {z1}, z2: {z2}")
A = np.array([[1, z1], [1, z2]]) # Definición de la matrix A (2x2)
b = np.array(np.log([p1.value, p2.value])) # Definición del vector b (2x1)
# Resolvemos el sistema de ecuaciones
result = np.linalg.solve(A, b)
# Extraer resultados de mu y sigma de la solución
mu = result[0]
sigma = result[1]
print(f'mu: {mu}')
print(f'sigma: {sigma}')
M = np.exp(mu)
s = sigma
value = st.lognorm.ppf(p_find, s, scale=M)
print(f"P{percentil_desconocido}: {round(value, 2)}")
fig_0, ax_0 = plt.subplots(1, 1, figsize=(10, 8))
min = st.lognorm.ppf(0.01, s, scale=M)
max = st.lognorm.ppf(0.99, s, scale=M)
n_points = 100
x = np.linspace(min, max, n_points)
ax_0.plot(x, st.lognorm.cdf(x, s, scale=M), 'b-', lw=1, alpha=0.6, label='CDF')
x_scatter = [p1.value, p2.value, value]
y_scatter = [p1.p, p2.p, p_find]
colors = ['b', 'b', 'r']
ax_0.scatter(x_scatter[:2], y_scatter[:2], c=colors[:2], label="Percentiles Conocidos")
ax_0.scatter(x_scatter[-1], y_scatter[-1], c=colors[-1], label="Percentil Desconocido")
ax_0.set_xlabel("Reserves (bbl)")
ax_0.set_ylabel('CDF')
ax_0.set_title(f"Encontrar el percentil {int(100 - p_find * 100)} "
f"dados {int(100 - p1.p * 100), int(100 - p2.p * 100)}")
# Plot the pdf
ax_1 = ax_0.twinx()
ax_1.plot(x, st.lognorm.pdf(x, s, scale=M), 'k-', lw=1, alpha=0.6, label='PDF')
ax_1.set_ylabel('PDF')
ax_0.legend(loc="upper right")
ax_1.legend(loc="upper left")
plt.show()