PDF from copula


I have two discrete distributions and I wish to introduce dependence between them using a copula.

I can write the copula myself, but the output is P(X<=x, Y<=y). I wish to back out P(X=x, Y=y) = P(X<=x, Y<=y)- P(X<=x-1, Y<=y) - P(X<=x, Y<=y-1) + P(X<=x-1, Y<=y-1).

Previously I have done these using labelled structs in an e.g. vector. I could use a spare matrix of size M(max(x), max(y)) but I think it is inefficient in memory.

Is there a better way in Stan?


Maybe I am missing something, but why don’t you just write something as:

functions {
  real my_copula_cumulative(int x, int y, real param) {
    <copula code>
  real my_copula(int x, int y, real param) {
    return my_copula(x,y, param) - my_copula(x-1,y, param) - my_copula(x, y -1, param) + my_copula(x-1,y-1, param);

model { 
   target += log(my_copula(...));

Or are you trying to reuse computation or something?