Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

simple question

3 views
Skip to first unread message

Francisco Gutierrez

unread,
Nov 24, 2009, 6:11:04 AM11/24/09
to
Dear List:
I have the following list:
ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},{1,0,2,0,1},{2,2,0,1,1},{2,1,1,1,2},{0,1,1,0,1}};
I want to group it, so that the sublists of ejemplo1 that have identical values at positions 4 and 5 are gathered together. So I did the following code:
Split[ Sort[ejemplo1,#1[[4]]>=#2[[4]] && #1[[5]]>=#2[[5]] &],Take[#1,{4,5}]==Take[#2,{4,5}]&]

Works! The output in effect is:
{{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{{1,0,2,0,1},{0,1,1,0,1}},{{1,0,2,2,0}}},
precisely what I wanted.

Now, how can I create a function for the general case (instead of fixed positions 4 and 5, an arbitrary number of positions that act as "gathering parameter")?
Fg

--- On Mon, 11/23/09, Petri T=F6tterman <petri.t...@hanken.fi> wrote:


From: Petri T=F6tterman <petri.t...@hanken.fi>
Subject: ML estimators for Box-Cox Extreme Value distribution
Date: Monday, November 23, 2009, 6:53 AM


Dear all,

I have a set of data, and I want to fit a distribution on this data. I
am particularily interested in the Box-Cox General Extreme Value
distribution, for which I have the CDF and PDF:
---
BoxCoxGEVDistribution /:

CDF[BoxCoxGEVDistribution[\[Mu]_, \[Sigma]_, \[Xi]_, \[Phi]_], x_]:=
1 + (((Exp[-(1 + \[Xi] ( (x - \
\[Mu])/\[Sigma]))^(-1/\[Xi])])^\[Phi]) - 1)/\[Phi];

BoxCoxGEVDistribution /:

PDF[BoxCoxGEVDistribution[\[Mu]_, \[Sigma]_, \[Xi]_, \[Phi]_], x_]=
D[CDF[BoxCoxGEVDistribution[\[Mu], \[Sigma], \[Xi], \[Phi]], x],
x];
---

I do also have a Log likelihood function from (Bali, 2007):

---
LLdBoxCoxGEV[\[Mu]_, \[Sigma]_, \[Xi]_, \[Phi]_, M_] :=
Module[{k = Length[M]}, -k Log[\[Sigma]] -
k ((1 + \[Xi])/\[Xi]) Sum[
Log[1 + \[Xi] ((M[[i]] - \[Mu])/\[Sigma])], {i, 1, k}] -
k \[Phi] Sum[(1 + \[Xi] ((M[[i]] - \[Mu])/\[Sigma]))^(-(
1/\[Xi])), {i, 1, k}] ];
---

Obviously, \[Mu]_, \[Sigma]_, \[Xi]_, \[Phi]_ are parameters which I
need to estimate, and M is a list of values from the dataset,
exceeding a predefined limit.

I would be grateful for advice, how should I continue to find the
Maximum Likelihood estimators for this distribution, using Mathematica?

Best regards,
/petri

Bill Rowe

unread,
Nov 25, 2009, 2:31:02 AM11/25/09
to
On 11/24/09 at 5:48 AM, fgutie...@yahoo.com (Francisco Gutierrez)
wrote:

>Dear List: I have the following list:
>ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},{1,0,2,0,1
>},{2,2,0,1,1},{2,1,1,1,2},{0,1,1,0,1}}; I want to group it, so that
>the sublists of ejemplo1 that have identical values at positions 4
>and 5 are gathered together. So I did the following code: Split[
>Sort[ejemplo1,#1[[4]]>=#2[[4]] && #1[[5]]>=#2[[5]]
>&],Take[#1,{4,5}]==Take[#2,{4,5}]&]

>Works! The output in effect is:
>{{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{
>{1,0,2,0,1},{0,1,1,0,1}},{{1,0,2,2,0}}}, precisely what I wanted.

Using version 7 of Mathematica, you can achieve the same
grouping using GatherBy although the resulting groups will be
returned in a different order. Given positions 4 and 5 are the
last two positions

GatherBy[ejemplo1,#[[-2;;]]&]

results in the desired grouping which is confirmed by

In[7]:= Sort[
Split[Sort[ejemplo1, #1[[4]] >= #2[[4]] && #1[[5]] >= #2[[5]] &],
Take[#1, {4, 5}] == Take[#2, {4, 5}] &]] ==
Sort[GatherBy[ejemplo1, #[[-2 ;;]] &]]

Out[7]= True

>Now, how can I create a function for the general case (instead of
>fixed positions 4 and 5, an arbitrary number of positions that act
>as "gathering parameter")?

If the positions to use as the "gathering parameter" are always
the last n, then the following will work

group[data_,n_]:=GatherBy[data,#[[-n;;]]&]


Raffy

unread,
Nov 25, 2009, 2:31:36 AM11/25/09
to
On Nov 24, 6:11 am, Francisco Gutierrez <fgutiers2...@yahoo.com>
wrote:

> Dear List:
> I have the following list:
> ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},{1,0,2,0,1},{2,2, 0,1,1},{2,1,1,1,2},{0,1,1,0,1}};

> I want to group it, so that the sublists of ejemplo1 that have identical values at positions 4 and 5 are gathered together. So I did the following code:
> Split[ Sort[ejemplo1,#1[[4]]>=#2[[4]] && #1[[5]]>=#2[[5]] &],Take[#1, {4,5}]==Take[#2,{4,5}]&]
>
> Works! The output in effect is:
> {{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{{1,0, 2, 0,1},{0,1,1,0,1}},{{1,0,2,2,0}}},

> precisely what I wanted.
>
> Now, how can I create a function for the general case (instead of fixed positions 4 and 5, an arbitrary number of positions that act as "gathering parameter")?
> Fg
>

GatherBy[ejemplo1, #[[{4,5}]] &]

Replace {4,5} with any number/combination of indices.

Sjoerd C. de Vries

unread,
Nov 25, 2009, 2:32:23 AM11/25/09
to
Hi Fransisco,

It can be done rather easy using Gather:

In[31]:= myGather[list_List, places_List] :=
Gather[list, (#1[[places]] == #2[[places]]) &]

In[32]:= myGather[ejemplo1, {4, 5}]

Out[32]= {{{1, 0, 2, 2, 0}}, {{0, 1, 1, 1, 2}, {2, 1, 1, 1, 2}}, {{2,
0, 0, 1, 1}, {2, 2, 0, 1, 1}}, {{1, 1, 0, 2, 2}}, {{1, 0, 2, 0,
1}, {0, 1, 1, 0, 1}}}

In[33]:= myGather[ejemplo1, {1, 4, 5}]

Out[33]= {{{1, 0, 2, 2, 0}}, {{0, 1, 1, 1, 2}}, {{2, 0, 0, 1, 1}, {2,
2, 0, 1, 1}}, {{1, 1, 0, 2, 2}}, {{1, 0, 2, 0, 1}}, {{2, 1, 1, 1,
2}}, {{0, 1, 1, 0, 1}}}

In[34]:= myGather[ejemplo1, {1}]

Out[34]= {{{1, 0, 2, 2, 0}, {1, 1, 0, 2, 2}, {1, 0, 2, 0, 1}}, {{0, 1,
1, 1, 2}, {0, 1, 1, 0, 1}}, {{2, 0, 0, 1, 1}, {2, 2, 0, 1, 1}, {2,
1, 1, 1, 2}}}

Cheers -- Sjoerd


On Nov 24, 1:11 pm, Francisco Gutierrez <fgutiers2...@yahoo.com>
wrote:


> Dear List:
> I have the following list:
> ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},{1,0,2,0,1},{2,2,0,1,1},{2,1,1,1,2},{0,1,1,0,1}};
> I want to group it, so that the sublists of ejemplo1 that have identical values at positions 4 and 5 are gathered together. So I did the following code:
> Split[ Sort[ejemplo1,#1[[4]]>=#2[[4]] && #1[[5]]>=#2[[5]] &],Take[#1, {4,5}]==Take[#2,{4,5}]&]
>
> Works! The output in effect is:
> {{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{{1,0,2,0,1},{0,1,1,0,1}},{{1,0,2,2,0}}},
> precisely what I wanted.
>
> Now, how can I create a function for the general case (instead of fixed positions 4 and 5, an arbitrary number of positions that act as "gathering parameter")?
> Fg
>

> --- On Mon, 11/23/09, Petri T=F6tterman <petri.totter...@hanken.fi> wrote:

Bob Hanlon

unread,
Nov 25, 2009, 2:33:53 AM11/25/09
to

groupBy[mat_, positions_] :=
GatherBy[mat, Part[#, positions] &]


Bob Hanlon

---- Francisco Gutierrez <fgutie...@yahoo.com> wrote:

=============

Leonid Shifrin

unread,
Nov 25, 2009, 2:35:26 AM11/25/09
to
Hi Francisco,

The simplest is probably to use GatherBy:

Clear[gatherByPositions];
gatherByPositions[x_List, plist_List] :=
GatherBy[x, #[[plist]] &];

For example:

In[1]:= gatherByPositions[ejemplo1,{4,5}]

Out[1]=
{{{1,0,2,2,0}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{{1,1,0,2,2}},{{1,0,2,0,1},{0,1,1,0,1}}}

In[2]:= gatherByPositions[ejemplo1,{1,2}]

Out[2]=
{{{1,0,2,2,0},{1,0,2,0,1}},{{0,1,1,1,2},{0,1,1,0,1}},{{2,0,0,1,1}},{{1,1,0,2,2}},{{2,2,0,1,1}},{{2,1,1,1,2}}}


Regards,
Leonid

On Tue, Nov 24, 2009 at 1:48 PM, Francisco Gutierrez <fgutie...@yahoo.com
> wrote:

> Dear List:
> I have the following list:
>
> ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},{1,0,2,0,1},{2,2,0,1,1},{2,1,1,1,2},{0,1,1,0,1}};
> I want to group it, so that the sublists of ejemplo1 that have identical
> values at positions 4 and 5 are gathered together. So I did the following
> code:
> Split[ Sort[ejemplo1,#1[[4]]>=#2[[4]] && #1[[5]]>=#2[[5]]
> &],Take[#1,{4,5}]==Take[#2,{4,5}]&]
>
> Works! The output in effect is:
>
> {{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{{1,0,2,0,1},{0,1,1,0,1}},{{1,0,2,2,0}}},
> precisely what I wanted.
>
> Now, how can I create a function for the general case (instead of fixed
> positions 4 and 5, an arbitrary number of positions that act as "gathering
> parameter")?
> Fg
>

> --- On Mon, 11/23/09, Petri T=F6tterman <petri.t...@hanken.fi> wrote:
>
>
> From: Petri T=F6tterman <petri.t...@hanken.fi>

Valeri Astanoff

unread,
Nov 25, 2009, 2:35:47 AM11/25/09
to
On 24 nov, 12:11, Francisco Gutierrez <fgutiers2...@yahoo.com> wrote:
> Dear List:
> I have the following list:
> ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},{1,0,2,0,1},{2,2,0,1,1},{2,1,1,1,2},{0,1,1,0,1}};
> I want to group it, so that the sublists of ejemplo1 that have identical values at positions 4 and 5 are gathered together. So I did the following code:
> Split[ Sort[ejemplo1,#1[[4]]>=#2[[4]] && #1[[5]]>=#2[[5]] &],Take[#1,{4,5}]==Take[#2,{4,5}]&]
>
> Works! The output in effect is:
> {{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},{{1,0,2,0,1},{0,1,1,0,1}},{{1,0,2,2,0}}},
> precisely what I wanted.
>
> Now, how can I create a function for the general case (instead of fixed positions 4 and 5, an arbitrary number of positions that act as "gathering parameter")?
> Fg

Good day,

It could be done this way :

In[1]:= orden[l1_List, l2_List, parm_List]:=
And@@((l1[[#]] >= l2[[#]])& /@ parm);

In[2]:= gathering[l1_List, parm_List]:=
Split[Sort[l1,orden[#1,#2,parm]&],(#1[[parm]]==#2[[parm]])&];

In[3]:= ejemplo1={{1,0,2,2,0},{0,1,1,1,2},{2,0,0,1,1},{1,1,0,2,2},
{1,0,2,0,1},{2,2,=AD0,1,1},{2,1,1,1,2},{0,1,1,0,1}};

In[4]:= gathering[ejemplo1,{4,5}]
Out[4]= {{{1,1,0,2,2}},{{0,1,1,1,2},{2,1,1,1,2}},{{2,0,0,1,1},{2,2,0,1,1}},


{{1,0,2,0,1},{0,1,1,0,1}},{{1,0,2,2,0}}}

hth

--
Valeri Astanoff

Francisco Gutierrez

unread,
Nov 27, 2009, 6:44:27 AM11/27/09
to
Many thanks to Leonid, Valeri, Bob, and all the other friends who sent enlightening solutions to my simple problem.
As always, the variety of methods of attacking the problem is quite astonishing.
Best regards,
Fg

--- On Wed, 11/25/09, Bob Hanlon <han...@cox.net> wrote:


From: Bob Hanlon <han...@cox.net>
Subject: Re: simple question
Date: Wednesday, November 25, 2009, 2:33 AM

groupBy[mat_, positions_] :=
GatherBy[mat, Part[#, positions] &]


Bob Hanlon

---- Francisco Gutierrez <fgutie...@yahoo.com> wrote:

=============

0 new messages