AnyLogic variable for cumulative sum in system dynamics - anylogic

Good morning, in a System Dynamics model created on AnyLogic, I would like to compute the cumulative sum of a flow of the previous 7 days.
My purpose is to calculate the reproduction ratio of a disease starting from the infectious population at time t over the cumulative sum of the infectious in a fixed time interval. The formula is the following:
Formula
where:
I(t) = infectious population at time t --> I(t) is a flow in the model that changes a stock
I(t-s) = infectious population at time t-s
w(s) = gamma distribution
s represents the time interval of the previous 7 days
I have all the data but I am not able to calculate the sum of I(t-s).
Thanks.

You have to do this manually. Create a variable mySum of type double. Then, add a cyclic event that regularly adds to it from the stock (something like myVar += myStock).
You may need to use an additional variable that stores the temporary stock value from the last time you added, so you only add what was "new" since the last cycle.
In short: use a cyclic event to "approximate" your integral.

Related

How do I take an n-day average of data in Matlab to match another time series?

I have daily time series data and I want to calculate 5-day averages of that data while also retrieving the corresponding start date for each of the 5-day averages. For example:
x = [732099 732100 732101 732102 732103 732104 732105 732106 732107 732108];
y= [1 5 3 4 6 2 3 5 6 8];
Where x and y are actually size 92x1.
Firstly, how do I compute the 5-day mean when this time series data is not divisible by 5? Ultimately, I want to compute the 'jumping mean', where the average is not computed continuously (e.g., June 1-5, June 6-10, and so on).
I've tried doing the following:
Pentad_avg = mean(reshape(y(1:90),5,[]))'; %manually adjusted to be divisible by 5
Pentad_dt = x(1:5:90); %select every 5th day for time
However, Pentad_dt gives me dates 01-Jun-2004 and 06-Jun-2004 as output. And, that brings me to my second point.
I am looking to find 5-day averages for x and y that correspond to 5-day averages of another time series. This second time series has 5-day averaged data starting from 15-Jun-2004 until 29-Aug-2004 (instead of starting at 01-Jun-2004). Ultimately, how do I align the dates and 5-day averages between these two time series?
Synchronization between two time series can be accomplished using the timeseries object. Placing your data into an object allows Matlab to intelligently process it. The most useful thing is adds for your usage is the synchronize method.
You'll want to make sure to properly set the time vector on each of the timeseries objects.
An example of what this might look like is as follows:
ts1 = timeseries(y,datestr(x));
ts2 = timeseries(OtherData,OtherTimes);
[ts1 ts2] = synchronize(ts1,ts2,'Uniform','Interval',5);
This should return to you each timeseries aligned to be with the same times. You could also specify a specific time vector to align a timeseries to using the resample method.

Economic model and hazard damage in matlab

first, i want to say i am relatively new with matlab and so i am not yet very good in it.
I have the variables A,K and L and the constant alpha. Out of this, i want to model the income Y.
Y=A^alpha*K*L;
L changes at a growth rate of 0.09;
dL/dt= rl;
with L population growth; L0 (1950)=500;
I need to model this for 50 years, how can i do this in matlab? so, L has to grow every year, but with the stuff i tried i get always one output value, not 50 values (one for every year): how i have to code this in matlab?
at the moment, I have this, but it gives just the L0*(1+r) for every year
for i = 1:50
dL(i)=(1+r).*L
end
and the growth rate is continuus, but in one year I have due to an event (financial crisis for example) include a population decrease of 7% in one year, for example after year 30. Thereafther, the population will grow at same rate as before. How i can do this in matlab?
thanks for answering.
Actually it works, i had made a mistake by defining the loop from i:50, it must be from n:50

Can we code to calculate the difference of tick values in netlogo

Actually i need to calculate density increase/decrease rate of human population for my model, the model is same as i asked in unable to make non-stationary turtles change their direction if an obstacle a patch ahead (a specific area and a building within it, people are randomly visiting and going). What i thought that i will be needing to save the tick values for initial population value and after some time difference updated population value. Below is the procedure i want to plot graph for.
to density-increase-rate
store population-density at some initial time (ticks)
store updated-population-density at some later-time (ticks)
calculate density-increase-rate
( ( ( updated-pd - previous-pd ) / (updated-tick - previous-tick ) ) * 100 ) / 10
end
I am calculating population-density in my code as
total-density-inside-boundary count people with [inside-boundary?]
for any suggestion or code help i am very thankful.
If you just want to plot this change, there is no need to store it because the plot will update each tick.
globals [total-density-inside-boundary density-increase-rate]
to calc-plot-vars
let old-density total-density-inside-boundary
set total-density-inside-boundary count people with [inside-boundary?]
set density-increase-rate (total-density-inside-boundary - old-density) / 100
end
Then have a plot on the interface with plot total-density-inside-boundary and plot density-increase-rate. You may need to do some rescaling to have them both on the same plot.
If you want to have the rate based on total time, then create a variable to hold the initial value and calculate it at the specific time you think initial means (such as the end of the setup or at a specific tick).
globals [total-density-inside-boundary initial-density]
to setup
... (commands that create your people)
set initial-density count people with [inside-boundary?]
...
end
to go
...
if ticks = 1 [ set initial-density count people with [inside-boundary?] ]
...
end
Then have the rate plot in the interface have plot (total-density-inside-boundary - initial-density) / 100

Regarding Time scale issue in Netlogo

I am new user of netlogo. I have a system of reactions (converted to Ordinary Differential Equations), which can be solved using Matlab. I want to develop the same model in netlogo (for comparison with matlab results). I have the confusion regarding time/tick because netlogo uses "ticks" for increment in time, whereas Matlab uses time in seconds. How to convert my matlab sec to number of ticks? Can anyone help me in writing the code. The model is :
A + B ---> C (with rate constant k1 = 1e-6)
2A+ C ---> D (with rate constant k2 = 3e-7)
A + E ---> F (with rate constant k3 = 2e-5)
Initial values are A = B = C = 500, D = E = F = 10
Initial time t=0 sec and final time t=6 sec
I have a general comment first, NetLogo is intended for agent-based modelling. ABM has multiple entities with different characteristics interacting in some way. ABM is not really an appropriate methodology for solving ODEs. If your goal is to simply build your model in something other than Matlab for comparison rather than specifically requiring NetLogo, I can recommend Vensim as more appropriate. Having said that, you can build the model you want in NetLogo, it is just very awkward.
NetLogo handles time discretely rather than continuously. You can have any number of ticks per second (I would suggest 10 and then final time is 60 ticks). You will need to convert your equations into a discrete form, so your rates would be something like k1-discrete = k1 / 10. You may have precision problems with very small numbers.

Prediction of rainfall using nonhomogeneous Hidden Markov Model

I am new to HMM but I have gone through enough literature. I am working on a project in which I will be predicting rainfall using atmospheric parameters.
I have four observable characteristics of the atmosphere (humidity, temperature, wind, sea level height) for 10 years. I have also rainfall amount data with me.
As per I can understand, for each day a weather state will be specified on the basis of the spatial rainfall. So here goes the question. Lets suppose I have data for 100 days.
Rainfall = { 1,2,3,4... 100}. So if I want to generate weather states what should I do?
Lets suppose
temperature = { 30 to 45, some kind of distribution }
humidity = { 25 to 80 }
wind = { 60 to 100 }
sea level height = { 35 to 90 }
How to find
P(X_0) Initial parameter,
P(X_t|X_t-1) state transition matrix,
P(Y_t|X_t) dependence of observation on state
Do I need some clustering for generating states?
I am coding it in MATLAB.
You can come with your example or any source which can explain the procedure to implement in program.
An HMM has a discrete number of states, so your first step will be to define your states. Once you have well-defined states, come up with a numbering scheme for your states and write a function that can accept the data for a given time period, and output the state number that corresponds to that state.
Once you have a function (let's call it get_state) that maps data to a state number, you can create your state transition matrix as follows:
T = zeros(num_states);
for day = 2:num_days
s1 = get_state(data(day-1));
s2 = get_state(data(day));
T(s1,s2) = T(s1,s2) + 1;
end
The i,j-th element of the matrix T now gives you the transition counts from state i to j. You can turn this into transition probabilities as follows:
M = bsxfun(#rdivide,T+1,sum(T+1,2));
The dependence of the observation on the state is harder. You will have to figure out how you want to turn the observed data into a probability density function or probability mass function. You can have mutliple observed distributions from a single state instead of combining temperature, humidity, etc., into a single observation.
This is obviously not a full implementation, but hopefully it is enough to give you a starting point.