I am trying to find the inverse CDF of gamma function for the observed data, in order to find the transfer function. The whole aim is to correct the bias in the simulated data by CDF matching CDFobs(y) = CDFsim(x). I tried to do it with the below approach which throws and error.
The image below shows the CDF's and the the Transfer function in plot(c). I am trying to replicate the same. Please help me in achieving the plot.
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import scipy.stats as st
sim = st.gamma(1,loc=0,scale=0.8) # Simulated
obs = st.gamma(2,loc=0,scale=0.7) # Observed
x = np.linspace(0,4,1000)
simpdf = sim.pdf(x)
obspdf = obs.pdf(x)
plt.plot(x,simpdf,label='Simulated')
plt.plot(x,obspdf,'r--',label='Observed')
plt.title('PDF of Observed and Simulated Precipitation')
plt.legend(loc='best')
plt.show()
plt.figure(1)
simcdf = sim.cdf(x)
obscdf = obs.cdf(x)
plt.plot(x,simcdf,label='Simulated')
plt.plot(x,obscdf,'r--',label='Observed')
plt.title('CDF of Observed and Simulated Precipitation')
plt.legend(loc='best')
plt.show()
# Inverse CDF
invcdf = interp1d(obscdf,x)
transfer_func = invcdf(simcdf)
plt.figure(2)
plt.plot(transfer_func,x,'g-')
plt.show()
Traceback (most recent call last): File "/home/bias_correction.py", line 54, in transfer_func = invcdf(simcdf)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 357, in _call_ out_of_bounds = self._check_bounds(x_new)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 415, in _check_bounds raise ValueError("A value in x_new is above the interpolation " ValueError: A value in x_new is above the interpolation range.