HakanBrian / CoopConflStoch

1 stars 0 forks source link

`reproduce!` is not correctly sampling parents for the offspring #4

Closed HakanBrian closed 3 months ago

HakanBrian commented 3 months ago

I added some tests to ensure sampling was working correctly and guess what it was not working as expected. There may be something wrong with the test itself, but theoretical, the test should be testing correctly.

Which leaves us with the possibility that the sample function or weights are not being called correctly. Another possibility is the keys and payoffs I feed sample are not in the correct order. By this, I mean some keys I give may be aligned in correctly such that the order of payoffs does not match the individual it originates from when sampling. However, in this case there may be some indication this is happening when we run the tests, but so far I have not seen anything.

A definite effect of this sampling issue is the evolution of traits and payoff will be weird, which is what we may be seeing when plotting some simulation runs. This is especially the case when examining the payoff to see if changes in traits make sense.

Although maybe my test is just wrong, but either way this needs some investigation.

HakanBrian commented 3 months ago

Fixed errors in both the test and function.

The test used an incorrect population size. reproduce! was modified to use wsample with weights calculated using ProbabliiltyWeights. I haven't seen much difference between sample and wsample other than one being a more specific algorithm. However, when running reproduce! for multiple iterations, wsample provided a distribution of parent traits that made more sense. With wsample eventually the traits with higher probability of being inherited eventually dominated the population whereas sample seemed to stagnated more. However, these observations may just be due to chance, so more testing is needed to be conclusive. But for now it should be ok to leave things with these changes.

vancleve commented 3 months ago

huh, this is odd. I don't think that they should produce results. I'll look into it more, but you might do a little googling too to see if folks have run into a difference between the two methods before.

vancleve commented 3 months ago

Both of these things should produce the same results. You can see here that the wsample in the end just calls sample:

https://github.com/JuliaStats/StatsBase.jl/blob/c022f8227064a701115455888871be776ede5fe2/src/sampling.jl#L973

HakanBrian commented 3 months ago

Yes, you are right. Then I guess everything is working fine. The only issue was the weird results from the test, which I ended up fixing.