2

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.

user1142937
  • 314
  • 5
  • 19
  • 2
    You've given the error message but not where the error occurs. If you paste the full stack trace then you are much more likely to get an answer. As a general piece of advice the stack trace is usually a great place when you're starting out trying to diagnose a problem, as you can locate where in your own code an error occurs. – monk Jan 02 '13 at 13:53

1 Answers1

2

I think the arguments to interp1d are reversed. It should be

invcdf = interp1d(x, obscdf)
sizzzzlerz
  • 4,277
  • 3
  • 27
  • 35