Search code examples
c++qt-creatorcdf

implement empirical cdf c++


I want to define empirical cdf in C++ according to intervals fixed by N samples received and I should save the 2 vectors (T and Y)! I made this code but it didn't work:

double cum_dist_funct::real_cdf_function(vec X,double x,int N,int Ndft,vec &T, vec &Z)
{
    Z.set_size(X.length());
    Z.zeros();
    vec V;
    V.set_size(N);
    V.zeros();
    double f0=0;
    T.set_size(X.length());
    T.zeros();
    T=X;
    for (int i=0; i<Ndft;i++)
    {
        V=X.get(i*N,(i+1)*N-1);
        for(int j=0;j<N;j++)
        {
            if(x<V[0]) f0=0;
            else if (V[j]<=x && x<V[j+1]) f0=j/N;
            else if (x>=V[N-1])      f0=1;
            Z[i*N+j]=f0;
        }
    }
    return f0;
}

Thanks in advance for any help!


Solution

    1. It is unclear why you need N, Ndft and X.length(). Aren't one value to be enough?

    2. As mentioned by someone, in integer division j/N you're getting zeroes. Please make it to be double(j)/double(N)