Montecarlo sim for acquiring collection of objects

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

Posts: 1
Joined: Sat Mar 02, 2019 7:48 pm

Montecarlo sim for acquiring collection of objects

Postby DisgruntledStudent » Sat Mar 02, 2019 9:25 pm

I'm currently trying to program a montecarlo sim to know the amount of unique objects acquired out of a collection in a given number of attempts. I have been using Eviews 10 for about 5 days and have no experience programming.
So far I have arrived at this

Code: Select all

!reps = 1000
!toy = 6
vector (!reps) right
scalar first = 0
scalar second = 1
scalar third = 2
scalar fourth = 3
scalar fifth = 4
scalar sixth = 5

for i = 1 to !reps
for !trial = 1 to 6
vector (!toy) toys
mtos(toys, toyss)
series dtoyss = d(toyss)

As far as I understand so far I've created a series that contains numbers from 0 to 5 depending on which toys were acquired that attempt. Now my problem lies in creating an if condition that checks each number in the series and marks a success for each draw if the number is unique thus a series containing (2, 0, 2, 3, 5 ,4) Would have 5 successes. I am not aware of any command that does this.

Maybe I'm approaching the problem wrong altogether, any help, suggestions or relevant examples would be deeply appreciated.

Sidenote: I'm running with the 'fors' commented because they also give me errors but that's a whole different issue. :P

EViews Matt
EViews Developer
Posts: 364
Joined: Thu Apr 25, 2013 7:48 pm

Re: Montecarlo sim for acquiring collection of objects

Postby EViews Matt » Mon Mar 04, 2019 11:52 am


EViews' @uniquevals function returns a vector containing only the unique values in a series (or vector, or matrix). The length of that vector, retrievable via the @rows function, would match the number of successes for your purposes. You can therefore create your vector of random values and immediately count the successes without having to manually examine the random values. For example,

Code: Select all

vector(!toy) toys
toys = @ceiling(6 * toys)
!successes = @rows(@uniquevals(toys))

A few other comments... if you examine the toys vector you'll see that you're generating integers in the range [1, 6], not [0, 5], due to your use of @ceiling rather than @floor. That difference doesn't change the number of successes, but future code you write will encounter problems if it's written assuming the [0, 5] range.

As you learn the EViews programming language, I believe you'll find the documentation sections covering Programming and the Function Reference helpful. For example, the section on for loops describes how each for loop must be terminated with the statement "next", which is absent from your example program. I suspect you may also be receiving an error regarding the "i" in your first for loop, perhaps you meant to use the program variable "!i" instead.

Return to “Programming”

Who is online

Users browsing this forum: No registered users and 4 guests