Search code examples
pythonpandasspatialgaussianprobability-density

Normalise bivariate probability density function - python


I'm aiming to normalise a probability density function represented below Norm1. It works when I import the equation straight into ax.contour but not when calling the equation outside.

When trying to pass Norm1 into the ax.contour, I'm getting the following error:

    raise TypeError(f"Input z must be 2D, not {z.ndim}D")

TypeError: Input z must be 2D, not 3D

For context, I have two separate groups that contain various XY points. Each XY point has an identifier labeled as id. I assign a circle or radius to each unique point. I want to subtract the PDF from each group. The output is a normalised PDF between 0 to 1, where >0.5 is controlled by the group A and <0.5 is controlled by the group B.

df = pd.DataFrame({'Int_1': [1.0, 2.0, 1.0, 3.0, 1.0, 2.0, 3.0, 2.0], 

           'Int_2': [1.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 2.0],
           'Item_X': [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0],
           'Item_Y': [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0],               
           'Period': [1, 1, 1, 1, 2, 2, 2, 2],
           'Group': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'],
           'Item': ['Y', 'Y', 'A', 'B', 'A', 'B', 'A', 'B'],
           'id': ['1', '2', '3', '4', '1', '2', '3', '4']})

Group_A = [df[df['Group'] == 'A'][['Int_1','Int_2']].to_numpy()]
Group_B = [df[df['Group'] == 'B'][['Int_1','Int_2']].to_numpy()]
Item = [df[['Item_X','Item_Y']].to_numpy()]

period = df['Period'].drop_duplicates().reset_index(drop = True)


def impact_func(member_no, location, time_index, group):

  if group == 'A':
    data = Group_A.copy()

  elif group == 'B':
    data = Group_B.copy()

  else:

    return

  if np.all(np.isfinite(data[member_no][[time_index,time_index + 1],:])) & np.all(np.isfinite(Item[0][time_index,:])):

    sxy = (data[member_no][time_index + 1,:] - data[member_no][time_index,:]) / (period[time_index + 1] - period[time_index])

    mu = data[member_no][time_index,:] + sxy * 0.5

    out = mvn.pdf(location,mu) / mvn.pdf(data[member_no][time_index,:],mu)

  else:
    out = np.zeros(location.shape[0])

  return out

xx,yy = np.meshgrid(np.linspace(-10,10,200),np.linspace(-10,10,200))
Z_GA = np.zeros(40000)
Z_GB = np.zeros(40000)

for k in range(1):
  Z_GA += impact_func(k,np.c_[xx.flatten(),yy.flatten()],0,'A')
  Z_GB += impact_func(k,np.c_[xx.flatten(),yy.flatten()],0,'B')

fig, ax = plt.subplots(figsize=(8,8))
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)

Z_GA = Z_GA.reshape((200,200))
Z_GB = Z_GB.reshape((200,200))

Norm1 = xx,yy, 1 / (1 + np.exp(Z_GB - Z_GA))
Norm2 = Z_GA / (Z_GA + Z_GB)

#cfs = ax.contourf(Norm1, cmap = 'magma')
cfs = ax.contourf(xx,yy, 1 / (1 + np.exp(Z_GB - Z_GA)), cmap = 'magma')

fig.colorbar(cfs, ax = ax)

Solution

  • Solution

    You simply want to unpack Norm1:

    cfs = ax.contourf(*Norm1, cmap = 'magma')
    

    Result:

    contourf with unpacking

    Quick explanation:

    Norm1 is a tuple of 3 arrays. contourf needs one to three values [X, Y, Z] where Z holds the heights of each point.

    If you pass Norm1 as is, contourf will assume that it is solely the Z parameter, which would be interpreted as a 3D ndarray internally.

    Once unpacked using *, you will explicitely pass the three components of Nomr1 as three different parameters, ie X, Y and Z.