modeling overlapping generations

by punkish (Priest)
 on Nov 26, 2011 at 17:40 UTC Need Help??
punkish has asked for the wisdom of the Perl Monks concerning the following question:

Caveat lector, my subject line may be inappropriate for the question below, but it seems to come the closest to my understanding (http://en.wikipedia.org/wiki/Overlapping_generations_model).

Update: The above link to modeling financial instruments may be confusing, but the following sentence from the above link resonated with me, "model in which agents live a finite length of time and live long enough to endure into at least one period of the next generation's lives."

I am looking for a way to model overlapping generations where 'a' lies within 'b' which starts in 'c' and ends in 'd' whereby 'c' lies within 'e' and so on. Here is my attempt to draw it in ascii

```        +--
|
+--  |
|    |d +---
a--+ b  |  |
|  --+--+
+--  |  |
|c |e
|  |
+--|
+---

Update: Here is a real example: geologic time is made up of opinions. "The opinion is that the given level in a section at a locality corresponds to zone G. When this is compared to other opinions, it will come out that zone G is in the regional Tulean stage. When that is compared to other opinions, it will come out that the Tulean straddles the Tremadoc and Arenig. But the Arenig spans the Floian through lower Darriwilian. So, without the proper precautions, one could end up thinking the level in the section is somewhere between the Tremadoc and Darriwilian, when in fact it is much better constrained than that."

It could be done in SQL, and that is how we are tackling it right now, but it is messy, and the coding is always complicated. I am thinking of alternatives, perhaps as graphs (RDF triples), or some other way.

I am sure knowledgable monks will have several ideas. I look forward to your thoughts.

Update 2: Set::IntervalTree looks very promising.

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re: modeling overlapping generations
by BrowserUk (Pope) on Nov 26, 2011 at 17:55 UTC

I can't extract a question from your post?

Neither can I relate anything in your post -- which seems to be concerned with overlapping 2-dimension regions -- and the wikipedia link you (almost) gave which relates to finacial instruments in overlapping time periods.

You're much, much more likely to get useful responses, if you add some details about what format your data is in when you get it; and what information you hope to extract from it.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
I have updated the original question with more text. Hopefully that helps convey my question across.

when small people start casting long shadows, it is time to go to bed

I still can't see the question. Presumably you wish to take one or more of these opinions for a given strata and use them to categorise or rate or bracket the period(s) that strata is most likely to be a part of, but without you telling us what form those opinions are expressed in, suggesting anything is pretty much impossible.

This link might help others grasp a little of what you are talking about.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: modeling overlapping generations
by TJPride (Pilgrim) on Nov 26, 2011 at 22:22 UTC
Not sure if this is what you're looking for, but here's a "best range" calculator. The first part calculates all unique, valid range combinations (this part should probably be worked on a bit further if you have many ranges), the second chooses the best range. If you just want a simple 1:1 match and aren't worried about combinations of multiple ranges, you can eliminate that part of the code.

```use strict;
use warnings;

my (@r1, %r1, @r2, \$i, \$j, \$inp, \$start, \$end, \$choice);

@r1 = (
{ 'name' => 'b', 'start' => 70, 'end' => 110 },
{ 'name' => 'c', 'start' => 40, 'end' => 80 },
{ 'name' => 'd', 'start' => 80, 'end' => 140 },
{ 'name' => 'e', 'start' => 30, 'end' => 100 },
);

for (@r1) {
\$r1{\$_->{'name'}}++;
\$_->{'stages'} = 1;
}

### Generate combinations of overlapping ranges
do {
@r2 = ();
for \$i (0..\$#r1) {
for \$j (0..\$#r1) {
### Ranges are same, skip
next if \$i == \$j;

### One range is completely inside other, skip
next if \$r1[\$i]{'start'} <= \$r1[\$j]{'start'} &&
\$r1[\$i]{'end'} >= \$r1[\$j]{'end'} ||
\$r1[\$j]{'start'} <= \$r1[\$i]{'start'} &&
\$r1[\$j]{'end'} >= \$r1[\$i]{'end'};

### i is not earlier range, skip
next if \$r1[\$j]{'start'} < \$r1[\$i]{'start'};

### Ranges do not overlap at all, skip
next if \$r1[\$i]{'end'} < \$r1[\$j]{'start'};

### Combined range already exists, skip
next if \$r1{"\$r1[\$i]{'name'}-\$r1[\$j]{'name'}"}++;

### Combine and add
push @r2, {
name => "\$r1[\$i]{'name'}-\$r1[\$j]{'name'}",
start => \$r1[\$i]{'start'},
end => \$r1[\$j]{'end'},
stages => \$r1[\$i]{'stages'} + \$r1[\$j]{'stages'}
};
}
}
push @r1, @r2;
}
while (\$#r2 != -1);

while (1) {
print "\n";
exit if !getInputYes('Do you want to match a range?');
\$start = int getInput('Enter a start value');
\$end = int getInput('Enter an end value');

@r2 = ();
for (@r1) {
\$_->{'diff'} = \$start - \$_->{'start'} + \$_->{'end'} - \$end;
push @r2, \$_
if \$start >= \$_->{'start'} &&
\$end <= \$_->{'end'};
}

if (\$#r2 == -1) {
print "No matches found.\n";
next;
}

for (sort {
\$a->{'diff'} <=> \$b->{'diff'} ||
\$a->{'stages'} <=> \$b->{'stages'}
} @r2) {
print "\$_->{'name'} [\$_->{'start'}-\$_->{'end'}] is best match,
+ with \$_->{'diff'} difference and \$_->{'stages'} stages.\n";
last;
}
}

sub getInput {
my \$inp;
while (1) {
print "\$_[0] : ";
chomp(\$inp = <STDIN>);
return \$inp if \$inp;
}
}
sub getInputYes {
my \$inp;
while (1) {
\$inp = uc substr(getInput("\$_[0] (Y/N)"), 0, 1);
return 1 if \$inp eq 'Y';
return 0 if \$inp eq 'N';
};
}
Re: modeling overlapping generations
by TJPride (Pilgrim) on Nov 26, 2011 at 18:09 UTC
Still no idea what you're talking about. Give us input data and tell us what you want it to come out looking like. Supplying your SQL queries might be helpful as well. Are you modeling date ranges? Or taking logical rules about what starts or ends within what or lies within what and graphing the relationships?
Sorry, don't have ready access to my sql queries as I am in a cafe for the next several hours. But, the way we store the data are as "periods" which have starting age and ending age. Once again, the following may describe the dataset well -- 'a' lies completely within 'b'. However, 'b' started sometime during 'c' and ended sometime during 'd'. 'c', in turn, lies completely within 'e'... and so on.

I am looking for alternative ways to store such data, and then use Perl to query it so that given a "period", I can find the most nearest period that completely contains it.

If the above is not enough to convey the question clearly, I will submit more information, probably toward the end of the day, or tomorrow, with hopefully some of the sql queries.

when small people start casting long shadows, it is time to go to bed
Re: modeling overlapping generations
by sundialsvc4 (Abbot) on Nov 28, 2011 at 12:44 UTC

A range (aLo..aHi) overlaps the range (bLo,bHi) when (aLo <= bHi) && (bLo <= aHi).   (I think...)

This can be put into an SQL query that would return to you all of the potentially-overlapping ranges.   The “distance” between the two could then be calculated e.g. max(0, (aLo-bLo)) + min(0, (aHi-bHi)) (maybe...) and the minimum taken.

You might even be able to conjure up a way to do this using a nested SQL query, i.e. select from (subquery).   Choose the shortest “distance” from the overlapping-ranges returned by the inner query.   An INNER JOIN could be used to map “soft” phrases like Jurassic to “hard” numbers like 14 billions-and-billions.™   (Sigh... RIP, Carl Sagan.)

You describe the ranges in a hierarchy, but I don’t know whether or not that hierarchy is strictly necessary for results.   Can a range be decided to overlap based strictly on its upper- and lower-range values?   Or must the decision be made by navigating a tree?   If the latter, might an SQL query nevertheless be able to at least weed-out candidates for further consideration?

Food for thought, and nothing more.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://940186]
Approved by Perlbotics
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-03-19 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (245 votes). Check out past polls.

Notices?