Describing the right recurrence - discrete-mathematics

I've to describe a recurrence for l_n, the number of lobsters caught in year n
The task says: A hobby fisherman estimates the number of lobsters he will catch in a year as the average of the number he caught in the two previous years
Describe a recurrence for l_n, the number of lobsters caught in year n
I've tried something. It's degree-D homogeneous LRR so the recurrence is:
l_n = l_n-1 + l_n-2 as it's for 2 years. Have I solved ít correctly?

It's the average of the two, so you'd want to add them and divide by 2.

Related

How can I partition data based on date column to get subsets of max 1 year (365 consecutive days)

I'm quite new to R, so I still struggle a bit with what may be some of the basics. I have movement data for several individuals, and some of them have been monitored for over a year. In these cases, I'm trying to partition the data into bins of max. one year (i.e. 365 consecutive days) per individual. For example, if one individual was tracked for 800 days, I would need to partition the data into the first 365 days, the second 365 days, and the remaining 70 days. Or, if one individual started on 2019/04/17, I'd like to subset the data until 2020/04/16, with the next subset beginning on 2020/04/17.
Individuals were added in different moments so not all monitoring periods begin on the same day, and per individual there can be more than one observation per day, so many rows share the same date. Naturally I want to use the timestamp column for this, but have been looking for ways and can't seem to find one. Is there a way to tell R to pick the first date and extract the next 365 days?
I could manually calculate each bin and try to partition it by hand, but I was wondering if there was a simpler way for this. I can however separate the data per individual.
Thanks!
My data looks something like this
Date.and.time Ind Lat Long
2019-04-02 08:54:03 Animal_1 Y X
2019-04-02 09:01:13 Animal_2 Y X
2019-04-02 15:45:22 Animal_1 Y X
2019-04-03 17:31:50 Animal_1 Y X
.
.
.
2021-10-14 12:34:56 Animal_1 Y X
2021-10-15 16:05:50 Animal_20 Y X
2021-10-15 22:29:37 Animal_15 Y X

Calculating Running Balances

I am struggling with how to calculate a running balance that is dynamic. Here's what I have.
A range of periods with an intersection between total duration and each month in the duration.
A rate that is applied at each intersection.
An initial amount
A need to calculate a running balance based on the initial amount less the rate applied in that period.
For example, I have a project worth $2,500,000 that is 8 months long. The rates for each interval are as follows: 1. 8.10% 2. 14.04% 3. 26.8% 4. 29.1% 5. 33.4% 6. 30.4% 7. 47.4% 8. 100%
For period 1 I have $202,500 (8.10% × $2.5 million), For period 2, I have $322,500 (14.04% × $2,297,500 ($2.5 - $202,500)), for period 3, I have $530,000 (26.8% × $1,974,999 ($2.5 - sum of first two periods ($525,000)). At the end of period 8, my balance is $0 and my earned amount = $2.5 million.
Can I use something like RunningTotal = Sum(MonthlyAmts) OVER (ORDER BY XX ROWS UNBOUNDED PRECEDING), ORDER BY Period? Or is this a candidate for a cursor?
Thanks in advance!
It depends on your table structure. But it sounds like you should use function. If you would like get all years use table valued function, if you are interested only on result use scalar function. Or better way with recursive CTE.

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

Group of consecutive minimum values within data - MATLAB

I have daily river flow data for 1975-2009 and I am asked to find the 7 consecutive days within each year that have the smallest flows.
Any advice how to start this? I've only been using MATLAB for a couple weeks.
Thanks!
You could convolve the data with ones(1,7) and look for the minimum, which will yield the starting day of your dry period:
[~,startingDay] = min(conv(flow,ones(1,7),'valid'))
(This is basically a moving average filter without the normalization).
Loop through the years to get each year's result.
Start by finding cumulative sum with cumsum. The difference between cumulative sums 7 days apart will give you the total for those 7 days. Then pick the minimum of those.
a = cumsum(flow);
b = a(8:end) - a(1:end-7);
[m,i] = min(b);
Here m holds the smallest total over 7 consecutive days, and i is a vector of indices telling you when they occurred.

How do I loop a portfolio return calculation for multiple periods in matlab?

I have the following data/sets (simplified version to make the example clear):
3*10 matrix of stocks identified by a number for a given period (3 stocks (my portfolio) in rows * by 10 days (in columns) named indexBest.
10*10 matrix of returns for each period for each security in my universe named dailyret (my universe of stocks is 10).
I want to create a loop where I am able to calculate the sum returns of each portfolio for each period into one matrix ideally 1*10 or 10*1 (returns * date or vice versa).
I have done this for a single period for a portfolio, see below: but I want to be able to automate this process instead of doing all this 10* over for each portfolio for each period. Please help!
Portfolio_1_L= indexBest(:,1); %helps me get the portfolio constituents for period one (3 stocks basically).
Returns_1_L= dailyret(Portfolio_1(:,1));%to calculate returns of the portfolio for period 1 I have referenced the extraction of returns to the portfolio constituents.
Sum_ret_Port_1_L=sum(Returns_1_L); %sum return of the portfolio for period one
How do I loop this process for all other 9 periods?
Use a for loop and use the index variable in place of hardcoded 1 in your example code. Also index the output to store the result for each day.
for day = 1:10
Portfolio_1_L = indexBest(:,day);
Returns_1_L = dailyret(Portfolio_1_L);
Sum_ret_Port_1_L(day) = sum(Returns_1_L);
end