cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Recall matrices into a matrix from a loop

ptc-5276671
1-Visitor

Recall matrices into a matrix from a loop

The first section of my programme is fine where it shows the progression of infection after every cycle however its this next part i am having a problem with. I am trying to see how the percentage of immunised population affects the final amount of infected people however I am having some trouble as to varying the immunised percent (x) in my programme loop. I only need to display the infected for the final stage of the loop not after every cycle which I have managed to do for a specifi x value however cant seem to get it for a range of x values from 0-1 at 0.01 intervals.

Any help would be appreciated.

in the long run i also want to average this out over a certain amount of runs. In otherwords i want to creat a matrix to hold the amount of infected population against the immuised percent x while varying that x value from 0-1. I then want to repeat that for say 1000 cycles and plot the average.

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:ptc-5276671)

My aim is to show how the percentage of immunisation affects the amount of infected.

Yes, but I still don't see why you would be interested in finding out by a long winded simulation that an immunisation rate of 37% results in an infection rate of approx. 63%. In other words - all persons not immunized will get infected sooner or later in the long run. Deviations from the ideal line are due to the mentioned effects when unvaccinated people or the first infected person are surrounded by immunized.

I haven't used your sheet to obtain the results and thats one reason for not attaching it. I would not be patient enough to wait until a simulation, where every immunization rate would be averaged over 1000 runs, finishes with a grid bigger than 10x10 (as in the first pic).

Did you already change your iteration/infection routine?

You should build more function which should be dependend on all variables you would like to change later and they should not rely on calculations made outside the program. That way you can easy use them in loops or plotting functions.

As a hint a list of the functions I used in my sheet:

  • make_grid(n,p,inf) ... returns a n x n matrix with p percent immunized and inf is the numer of initial infected people
  • step(grid) ... takes the matrix grid, applies the infection algorithmus and returns the newly derived matrix
  • steps(n,grid) ... routine which calls step() n-times; used for the animation
  • make_points(grid) ... creates the datastructures needed for the graphical representation of the grid; used for animation
  • infected_at_end(grid) ... will call step as long as the number of infected person changes and then returns the number of infected persons at the end
  • count(nr,M) ... returns how often nr is found in matrix M

To plot the infection rate at the end over the immunization rate, I used the following

infected2.png

BTW, I had an error in my Infected() routine and so the plots in my previous post are wrong. They would all look more or less like the above and it seems that the effect for higher immunization rates of groups of healthy people being fully surrounded by immunized ones is getting bigger with growing poulation size (see below for poulation size 400). So you can't cope this with averaging over 1000 runs as for higher immunization rates and greater population (say 75% and 30x30) it will happen nearly all of the time. The pic below with all those islands of healthy and islands of infected people being separated by immunized ones is typical, not an outlier. But then - would this have much meaning for real life?

infected1.png grid30.png

View solution in original post

9 REPLIES 9
Werner_E
25-Diamond I
(To:ptc-5276671)

To be able to vary the percentage of immunized persons you will have to turn all vital routines into function of that percentage. See if the attached helps.

I think the routine which simulates the infection of other people is not programmed correctly as the freshly to the right and below an infected person would immedeatly infect their neighbours in the same step.

So beginning with

1.png

you will end up with

2.png

which I guess is not intended. Ther come two ways into my mind to cope with that.

1) you could copy the matrix to a temporary one and use one for the conditions and set the value to 2 in the other.

2) Set the value to 3 or any other higher number or to a string in the first run and then go through the matrix a second time and set the value to 2 if its 3 or whatever you had set it before.

BTW, this could be a nice expample for an animation made with Mathcad.

Werner_E
25-Diamond I
(To:ptc-5276671)

Some additional thoughts:

in the long run i also want to average this out over a certain amount of runs. In otherwords i want to creat a matrix to hold the amount of infected population against the immuised percent x while varying that x value from 0-1. I then want to repeat that for say 1000 cycles and plot the average.

What do you expect to get? Basically you would end up with the function f(p):=1-p as at the end nearly all not immunized persons would be infected. With higher immunization rates some healthy, not immunized persons can be fully surrounded by immunized people and would never be infected (see in

the included animations). It can also happen, that the one infected person at the beginning is surrounded by immunized individuals and can only infect a very small amount of people being on the same "island". But these are effects specific to the way the simplified simulation is set up and in the long run we will end up with approx. all not immunized people being infected. You could add incubation time, declining vaccination, death, recovery, ... to make it a bit more realistic and interesting.

infected1.pnginfected2.pnginfection3.png


HI werner

Thanks for the reply. My aim is to show how the percentage of immunisation affects the amount of infected. Now i know what you were saying about sometimes the inital infected maybe completely surrounded by immunised people and therefore wouldnt spread but thats why i wanted to run it over say 1000 runs and take and avergae. Not sure how to get this data out from my loop to plot graphs like yours.

Werner_E
25-Diamond I
(To:ptc-5276671)

My aim is to show how the percentage of immunisation affects the amount of infected.

Yes, but I still don't see why you would be interested in finding out by a long winded simulation that an immunisation rate of 37% results in an infection rate of approx. 63%. In other words - all persons not immunized will get infected sooner or later in the long run. Deviations from the ideal line are due to the mentioned effects when unvaccinated people or the first infected person are surrounded by immunized.

I haven't used your sheet to obtain the results and thats one reason for not attaching it. I would not be patient enough to wait until a simulation, where every immunization rate would be averaged over 1000 runs, finishes with a grid bigger than 10x10 (as in the first pic).

Did you already change your iteration/infection routine?

You should build more function which should be dependend on all variables you would like to change later and they should not rely on calculations made outside the program. That way you can easy use them in loops or plotting functions.

As a hint a list of the functions I used in my sheet:

  • make_grid(n,p,inf) ... returns a n x n matrix with p percent immunized and inf is the numer of initial infected people
  • step(grid) ... takes the matrix grid, applies the infection algorithmus and returns the newly derived matrix
  • steps(n,grid) ... routine which calls step() n-times; used for the animation
  • make_points(grid) ... creates the datastructures needed for the graphical representation of the grid; used for animation
  • infected_at_end(grid) ... will call step as long as the number of infected person changes and then returns the number of infected persons at the end
  • count(nr,M) ... returns how often nr is found in matrix M

To plot the infection rate at the end over the immunization rate, I used the following

infected2.png

BTW, I had an error in my Infected() routine and so the plots in my previous post are wrong. They would all look more or less like the above and it seems that the effect for higher immunization rates of groups of healthy people being fully surrounded by immunized ones is getting bigger with growing poulation size (see below for poulation size 400). So you can't cope this with averaging over 1000 runs as for higher immunization rates and greater population (say 75% and 30x30) it will happen nearly all of the time. The pic below with all those islands of healthy and islands of infected people being separated by immunized ones is typical, not an outlier. But then - would this have much meaning for real life?

infected1.png grid30.png

Thanks werner, that really helped.

Werner_E
25-Diamond I
(To:ptc-5276671)

I think the plot of the number of cycles necessary unless there is no change in infections anymore over the immunization rate is even more interesting than that of the number of infected at last.

nrsteps10.pngnrsteps20.pngnrsteps50.png

In the attached videos of 512x512 grids you see the typical difference between 60% and 70% immunization rate. I had chosen jst 500 infected at the start which is much less than the 1% you had in your sheet. But you can clearly see the seoarated "islands" which stem from the arrangement and the infection mechanism used. Rreal people woul move around but would not infect EVERY person in their neighborhood.

Hi Werner,

Is it possible for you to go back at this solution and show how did you get the green grid with red dots showing the infected individuals?

Thanks

Daniel Crai wrote:

Hi Werner,

Is it possible for you to go back at this solution and show how did you get the green grid with red dots showing the infected individuals?

Thanks

Thats very optimistic to think that I would still find that worksheet after one and a half year 😉

But you have luck - here it is.

The grids with the lower resolutions are xy-plots while the larger one is a picture (again a feature not availale in Prime) created from a precalculated matrix.

Have fun with it.

attached is my idea just for reference.

forgive my broken english

Announcements

Top Tags