0

I need the python equivalent code for the below MATLAB code:

[f,xi] = ksdensity(data,'Support','positive','Function','cdf');

I find the below python code, but I don't know how I can provide its cdf. I appreciate it if you could guide me.

from scipy import stats
kde = stats.gaussian_kde(data)
Mohammad
  • 163
  • 2
  • 8
  • Could you describe what `ksdensity` does? Or do we have to look it up? You may have studied the docs of both MATLAB and `scipy` in more detail than most of us. – hpaulj Jan 30 '21 at 22:22

2 Answers2

1

Try this, link.

def insert_size(insert_size_distribution):
    """Calculate cumulative distribution function from the raw insert size
    distributin. Uses 1D kernel density estimation.

    Args:
        insert_size_distribution (list): list of insert sizes from aligned
        read pairs

    Returns:
        1darray: a cumulative density function
    """
    kde = stats.gaussian_kde(
        insert_size_distribution,
        bw_method=0.2 / np.std(insert_size_distribution, ddof=1))
    x_grid = np.linspace(
        min(insert_size_distribution),
        max(insert_size_distribution), 1000)
    kde = kde.evaluate(x_grid)
    cdf = np.cumsum(kde)
    cdf = cdf / cdf[-1]
    return cdf 
pizzettix
  • 411
  • 2
  • 9
0

The following code works for me:

import scipy
kde = scipy.stats.gaussian_kde(data)
my_cdf = scipy.stats.norm.cdf(kde)
Mohammad
  • 163
  • 2
  • 8