[Tutor] Structure of my simulation / monte-carlo

Dave Angel davea at ieee.org
Sun Nov 1 13:02:24 CET 2009


Vincent Davis wrote:
> I ask this question in part because of a fee remarks from another question I
> ask "class attribute to initiate more classes"
>
> Basically I am simulation the process of applicants to schools and trying to
> ask/answer some questions like "what conditions do you need to have an
> optimal solution" Oh and to learn python. I basically had it working as a
> script rather than using a class structure but it was very inflexible and I
> still needed to learn about classes.
>
> What I have these classes
> class Applicant: has lots of attributes (self.gpa = random.gauss(50, 10)
> about the Applicant and def() defining how/why an applicant applies to an
> institution,
>
> class Institution: Lots of attributes (self.quality = random.gauss(50,
> 10)) about the Institution and def() defining how the institution considers
> the applicant.
>
> class Match: this defines the interaction of the population of Applicants
> and Institutions, i.e. the rules of the game and returns the outcome i.e.
> which Applicants went to which Institutions.
>
> As of now I have been running 1 Match at a time. Which is to say generate
> 8000 instances of Applicant and 300 instances of Institution and then run
> the match Match(app_list, inst_list) and I do this with a short script.
>
> So now I need to implement my monte-carlo. By that I mean that i want to set
> some of the initial condition such as GPA, and Quality and basically re-run
> the what I descried above, (generate applicant, institutions, match them)
>  Then save the results.
>
> So my plan way to make a new class. This class would define the Applicant
> characteristics "self.gpa = random.gauss(mean, SD)" and the
> institutions self.quality = random.gauss(mean, sd)
>
> so it would look something like this
>
>
> class RepeatMatch:
>     def __int__(self, app_mean, app_sd, inst_mean, inst_sd, match_ repeat)
>         self.app_mean = app_mean
>         ……..
>         self.match_repeat = match_repeat
>
>    def makeApplicants():
>
>    def makeInstitutions():
>
>    def runMatches(self)
>    # runs the match match_repeat number of times, saves results
>
> # then I calculate some characteristics of the results
>
>     def ratio_dist():
>          # returns the Mean and sd of GPA/Quality
> END OF CODE
>
> Does it make sense to do it this way? Is there a better/alternative way of
> thinking about this. In the end I want to compare the results of repeated
> simulations "RepeatMatch(50,2….) Compared to RepeatMatch(50,15….)"
> This is way I had ask the earlier question "class attribute to initiate more
> classes"
>
> Thanks
> Vincent Davis
>
>   
I worried that you might find my responses too complex, but nobody else 
has responded, and it's been almost a day.

I don't see anything wrong with your approach.  Since you're going to do 
multiple sets of data, it makes sense for an instance of a class 
(RepeatMatch) to hold the data for one such run.  In your original 
sample, it didn't make sense to me, but of course I didn't know where 
you were heading.  So I would add in instance attributes such as  
self.applicants=[]  to your __init__() method of RepeatMatch.  (I 
suspect you're planning to do exactly that)

I would caution you that each instance of RepeatMatch will then hold 
lots of the other members, so keeping them around could be expensive. So 
when run_matches() finishes its analysis, it might want to delete its 
lists of raw data (eg. self.applicants).  But other choices exist, and 
you can decide that when you see how the whole thing fits together.  
Perhaps you'll only have one such instance at a time.  But if you're 
going to do multiple things with the results, you may want this object 
to hang onto the results, but throw away the raw data when all the 
necessary results have been calculated.

Are you sure that the only distribution you're going to use is 
random.gauss() ?  If so, then you only need to pass mean and stddev to 
the RepeatMatch constructor, as you're doing.  But if you might need to 
compare that distribution with a different one, then you might want to 
use a function object, as I tried to describe earlier.


HTH
DaveA


More information about the Tutor mailing list