turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-04-2017 04:52 PM

Hi,

I am recreating Rick Wicklin's blog post Simulating the Coupon Collector's Problem. In the last section there is a code that creates a CDF:

```
/** has event occurred by roll j? (j=K..L) **/
cdf = j(L,1,0); /** allocate **/
do j = K to L;
c = countunique(x[,1:j], "row");
cdf[j] = (c=K)[:];
end;
```

What I would like is to have the actual count for each j and not the proportion, but when I did :

cdf[j] = c, I got an error message that the matrices do not conform to the operation. Is there a way to see the actual count for each j?

Thank you

Accepted Solutions

Solution

02-05-2017
10:21 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ilikesas

02-04-2017 06:59 PM

The notation

(c=K)

creates a 0/1 matrix which has the value 1 in cells for which c[i,j]=K.

The notation

(c=K)[:]

takes the mean of those numbers by using a subscript reduction operator.

If you want the sum instead of the mean, just use

(c=K)[+]

or

sum(c=K)

```
cdf = j(L,1,0); /** allocate **/
do j = K to L;
c = countunique(x[,1:j], "row");
cdf[j] = (c=K)[+];
end;
call scatter(K:L, cdf);
```

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ilikesas

02-04-2017 05:55 PM

cdf[j] is one cell, whereas c is a vector with 10,000 elements, which is why you are getting an error.

I don't understand what "count" you want. I suggest you set NSim=5 and L=8 so you can print out x and other matrices and tell us what you are looking for:

```
/* generate NSim trials of L rolls */
NSim = 5;
L = 8;
...
print x;
/* count of what? */
c1 = (x=k)[+,]; /* number of times for each column that K appeared */
c2 = (x=k)[,+]; /* number of times for each row that K appeared */
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

02-04-2017 06:08 PM

Hi Rick (I knew you would reply!)

By count I mean simply the total number of trials for which all 6 faces appear in 6 rolls, in 7 rolls, 8 rolls etc.

I know that I can multiply each cdf by 10000 and then calculate the difference between consecutive cdf's, but I just wanted to see how to do it with the code.

Thanks!

Solution

02-05-2017
10:21 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ilikesas

02-04-2017 06:59 PM

The notation

(c=K)

creates a 0/1 matrix which has the value 1 in cells for which c[i,j]=K.

The notation

(c=K)[:]

takes the mean of those numbers by using a subscript reduction operator.

If you want the sum instead of the mean, just use

(c=K)[+]

or

sum(c=K)

```
cdf = j(L,1,0); /** allocate **/
do j = K to L;
c = countunique(x[,1:j], "row");
cdf[j] = (c=K)[+];
end;
call scatter(K:L, cdf);
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

02-04-2017 07:10 PM

thanks! that is what I meant, but couldn't get because I just didn't know about the code

`(c=K)[+]`

A small side note, this code gets the cummulative count, which is the cummulative distribution multiplied by the sample size. Is it also possible to get the discrete count, like the pdf times the sample size?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ilikesas

02-05-2017 06:44 AM

The pdf is the difference between adjacent values of the cdf. Therfore you can use the DIF function to compute the pdf from the cdf. Something like

pdf = dif(cdf);

pdf[1] = 0;