RE: [Numpy-discussion] USING NUMPY FOR AN ENROLLMENT PROJECTION PROBLEM
I posted this problem - arising from my work at the local university - some years back in comp.lang.python and received a solution that did not require the use of matrix algebra. The solution was: def enrollment(year, intake, survivalrate): return sum([intake[year-i]*rate for i, rate in enumerate(survivalrate)]) I would welcome help in formulating a solution using NumPy. Thanks in advance for any suggestion. The Problem: The enrolment E(n) of an institution at the beginning of year n is the sum of the intake for year n, I(n), and the survivors from the intakes of previous r years. Thus, if s(1) is the 1-year intake survival rate, s(2) is the 2-year survival rate, etc, we have: E(n)= I(n)+I(n-1)*s(1)+ I(n-2)*s(2)+...+I(n-r)*s(r) E(n+1)= I(n+1)+I(n)*s(1)+I(n-1)*s(2)+... +I(n-r-1)*s(r) . . . E(n+k)= I(n+k)+I(n+k-1)*s(1)+I(n+k-2)*s(2)+...+I(n+k-r)*s(r) Given: (a) the actual intakes for the current and previous r years, I(n), I(n-1),I(n-2),..,I(n-r), and the planned intakes for the next n+k years: I(n+1), I(n+2),..., I(n+k), we have the intake vector I = (I(n-r), I(n-r-1),...,I(n),I(n+1),..., I(n+k)); and (b) the survival rate vector, s = (1,s(1), s(2),...,s(r)) Find: The k*1 enrolment projection column vector, E = (E(n+1),E(n+2),...,E(n+k)) in terms of a k*(r+1) matrix P (derived from I) and the (r+1)*1 column vector, s. I = P*s Is there a compact Python representation of the relevant matrix P where: P = [I(n+1) I(n) I(n-1).. . I(n-r) I(n+2) I(n+1) I(n)... I(n-r-1) . . I(n+k) I(n+k-1) I(n+k-2)... I(n+k-r)]
El Sábado 25 Marzo 2006 13:59, Ajith Prasad (OFN) escribió:
I posted this problem - arising from my work at the local university - some years back in comp.lang.python and received a solution that did not require the use of matrix algebra. The solution was:
def enrollment(year, intake, survivalrate): return sum([intake[year-i]*rate for i, rate in enumerate(survivalrate)])
I would welcome help in formulating a solution using NumPy. Thanks in advance for any suggestion.
The Problem:
The enrolment E(n) of an institution at the beginning of year n is the sum of the intake for year n, I(n), and the survivors from the intakes of previous r years. Thus, if s(1) is the 1-year intake survival rate, s(2) is the 2-year survival rate, etc, we have:
E(n)= I(n)+I(n-1)*s(1)+ I(n-2)*s(2)+...+I(n-r)*s(r) E(n+1)= I(n+1)+I(n)*s(1)+I(n-1)*s(2)+... +I(n-r-1)*s(r) . . . E(n+k)= I(n+k)+I(n+k-1)*s(1)+I(n+k-2)*s(2)+...+I(n+k-r)*s(r)
Given: (a) the actual intakes for the current and previous r years, I(n), I(n-1),I(n-2),..,I(n-r), and the planned intakes for the next n+k years: I(n+1), I(n+2),..., I(n+k), we have the intake vector I = (I(n-r), I(n-r-1),...,I(n),I(n+1),..., I(n+k)); and (b) the survival rate vector, s = (1,s(1), s(2),...,s(r)) Find: The k*1 enrolment projection column vector, E = (E(n+1),E(n+2),...,E(n+k)) in terms of a k*(r+1) matrix P (derived from I) and the (r+1)*1 column vector, s.
I = P*s
Is there a compact Python representation of the relevant matrix P where:
P = [I(n+1) I(n) I(n-1).. . I(n-r) I(n+2) I(n+1) I(n)... I(n-r-1) . . I(n+k) I(n+k-1) I(n+k-2)... I(n+k-r)]
If I've understood properly you simply want to multiply two matrices. Just store your intakes in an array, your survival rates in another one and call matrixmultiply (or dot). The following interactive session shows it: In [1]: import numarray In [2]: p_matrix = numarray.array([1,2,3,4,5,10,1,2,3,4,20,10,1,2,3,30,20,10,1,2], ...: shape=(4, 5), type="Int8") In [3]: sr_array = numarray.array([1, 0.2, 0.15, 0.25, 0.3]) In [4]: enrolment_array = numarray.matrixmultiply(p_matrix, sr_array) In [5]: print p_matrix [[ 1 2 3 4 5] [10 1 2 3 4] [20 10 1 2 3] [30 20 10 1 2]] In [6]: print sr_array [ 1. 0.2 0.15 0.25 0.3 ] In [7]: print enrolment_array [ 4.35 12.45 23.55 36.35] Regards -- :: \ / Vicent Mas http://www.carabos.com 0;0 / \ Cárabos Coop. Enjoy Data V V " "
participants (2)
-
Ajith Prasad (OFN)
-
Vicent Mas (V+)