I would appreciate advice on how best to formulate the following
problem in Python. I have originally posted the problem to the J
Programming forum and received a one-line formulation ((#s)
(|.s)&(+/@:*)\ I)! I was wondering what the equivalent Python
formulation would be.
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) 3 1807
Donald 'Paddy' McCarthy <pa*******@blueyonder.co.ukNOTthisBIT> wrote in message news:<3E**************@blueyonder.co.ukNOTthisBIT> ... Ajith Prasad wrote: I would appreciate advice on how best to formulate the following problem in Python. I have originally posted the problem to the J Programming forum and received a one-line formulation ((#s) (|.s)&(+/@:*)\ I)! I was wondering what the equivalent Python formulation would be.
Wow! that one line formulation is dense. Why do you *want* a compact answer? I'm curious, if you put it down, do you have problems undestanding the above program in a weeks time? Or in a days time?
Cheers, Pad.
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)]
Alternatively, a non-matrix formulation of the problem would be acceptable. Thanks in advance for any suggestions on how to proceeed.
I do not require an equally compact Python formulation - any solution
that works will do. The J solution works but I do not why! It was
formulated by a J expert and if one is very competent in J, I suppose
one could follow the logic. In Python, I look forward to a readable
and comprehensible solution. Thanks.
Quoth Ajith Prasad: I would appreciate advice on how best to formulate the following problem in Python. [...]
If you really want to bring out the linear algebra guns, I'm sure
Numeric has everything you need. But for a problem this simple,
I'd just write
def enrollment(year, intake, survivalrate):
return sum([intake[year-i]*rate
for i, rate in enumerate(survivalrate)])
That's for Python 2.3. In 2.2 you could write
def enrollment(year, intake, survivalrate):
sum = 0
for i in range(len(survivalrate)):
sum = sum + intake[year-i]*survivalrate[i]
return sum
In either case, using it might look something like this:
# survivalrate[n] is proportion of students who survive n years.
survivalrate = [1, 0.5, 0.25, 0.1]
# intake[n] is the number of students intook in year n.
actualintake = {
1993: 980, 1994: 1019, 1995: 1038, 1996: 1046, 1997: 1043,
1998: 970, 1999: 954, 2000: 980, 2001: 952, 2002: 1047,
}
plannedintake = {2003: 1000, 2004: 1000, 2005: 1100, 2006: 1200}
intake = actualintake.copy()
intake.update(plannedintake)
print enrollment(2004, intake, survivalrate)
Note that the intake vectors are dicts, not lists; I do this so I
can avoid index-twiddling. I find the code to be easier to read
and write this way.
--
Steven Taschuk st******@telusplanet.net
Every public frenzy produces legislation purporting to address it.
(Kinsley's Law)
Steven Taschuk <st******@telusplanet.net> wrote in message news:<ma**********************************@python. org>... Quoth Ajith Prasad: I would appreciate advice on how best to formulate the following problem in Python. [...]
If you really want to bring out the linear algebra guns, I'm sure Numeric has everything you need. But for a problem this simple, I'd just write
def enrollment(year, intake, survivalrate): return sum([intake[year-i]*rate for i, rate in enumerate(survivalrate)])
That's for Python 2.3. In 2.2 you could write
def enrollment(year, intake, survivalrate): sum = 0 for i in range(len(survivalrate)): sum = sum + intake[year-i]*survivalrate[i] return sum
In either case, using it might look something like this:
# survivalrate[n] is proportion of students who survive n years. survivalrate = [1, 0.5, 0.25, 0.1]
# intake[n] is the number of students intook in year n. actualintake = { 1993: 980, 1994: 1019, 1995: 1038, 1996: 1046, 1997: 1043, 1998: 970, 1999: 954, 2000: 980, 2001: 952, 2002: 1047, } plannedintake = {2003: 1000, 2004: 1000, 2005: 1100, 2006: 1200}
intake = actualintake.copy() intake.update(plannedintake)
print enrollment(2004, intake, survivalrate)
Note that the intake vectors are dicts, not lists; I do this so I can avoid index-twiddling. I find the code to be easier to read and write this way.
Thank you very much. This does what I require. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kostatus |
last post by:
I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class...
|
by: Peter Olcott |
last post by:
www.halting-problem.com
|
by: Peter Bailey |
last post by:
I have a student table with studentid as pk and an enrolment date field as
date.
I have made a qry to group on date and count the number of enrolments for a
particular day.
What this doesnt...
|
by: anirbanab |
last post by:
Hi,
I am trying to take a parallel projection of a real-life
surface(fractal surface) on series of 2D planes(that is X-Y, Y-Z, Z-X
planes). The idea then is to concentrate on any particular patch...
|
by: Adam Wawrzyniak |
last post by:
Hi,
this is my problem:
-------------------
public class Rectangle : Shape
{}
....
Shape shape;
shape = graph.AddShape(uid, pointXY);
----------------------------
| |
by: genc_ymeri |
last post by:
Hi over there,
We are a team of 6 developping web applications
(ASP.Net/C#/VB.Net/SQLServer). What happens is that randomly, each of us
get the below error in (PS). We believe that the "acess is...
|
by: Garmt de Vries-Uiterweerd |
last post by:
X-posted to opera.page-authoring, because this is a bit Opera-specific for
the moment. F'up to ciwas.
I am playing around with dedicated styling for the projection media type..
Currently the...
|
by: TommyC |
last post by:
Hi all,
Do anyone of u know how to write the codes for horizontal and vertical integral projection in C?
I decide to use this method for automated mouth detection.
Any helps will be apprecated. ...
|
by: xiao |
last post by:
Can anyone give some link about the link projection in C? Thank you :)
I am trying to adjust the data in a map,
Thank you :)
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |