Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. function [dist, generations] = simulate_population(starting_couples, chance_of_extinction)
  2. % Sample input: [dist, generations] = simulate_population(2500, 1/2);
  3. % evaluate dist(1) to get the probability of extinction after generations
  4. transition_matrix = gen_transition_matrix(starting_couples);
  5. dist = zeros(1, starting_couples + 1);
  6. dist(end) = 1;
  7. generations = 0;
  8. while dist(1,1) <= chance_of_extinction
  9. dist = dist*transition_matrix;
  10. generations = generations + 1;
  11. end
  12.  
  13. function transition_matrix = gen_transition_matrix(starting_couples)
  14. transition_matrix = zeros(starting_couples + 1);
  15. for i = 0:starting_couples
  16. binom_dist = makedist('Binomial', 'N', 2*i, 'p', 1/2);
  17. temp = pdf(binom_dist, 0:i);
  18. temp(1:end - 1) = 2*temp(1:end - 1);
  19. transition_matrix(i + 1, 1: i+1) = temp;
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement