Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Running Product Function

 From Yuval Arbel To statalist@hsphsun2.harvard.edu Subject Re: st: Running Product Function Date Mon, 10 Dec 2012 20:09:07 +0200

```As for the first example, I meant that in the 26th period the index
will still remains 1 as reduct_per=25 appears at the first time, and
starting from the 27th period it will be 2,3,.. etc.

On Mon, Dec 10, 2012 at 8:02 PM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
> Thanks Nick and Kit.
>
> Now I have a complementary question:
>
> I would like to generate an index variable for the calculation of the
> geometrical mean (namely, the exponent, which makes the product a
> geometrical mean). The problem is that for reduct_per==0 I would like
> the index to become a constant and remain the same until the next
> reduct_per, which is different from zero, comes.
>
> Consider the example above: during the first 25 periods the index
> should be one and and starting from the 26th period it should change
> to 2,3 etc.
>
> If, on the other hand, we would have opposite descending trail (namely
> 25 percent reduction rate from 1-25 and zero starting from the 26th
> period), the index should be 1, 2,..,25 and becomes a constant 25 from
> the 26th period.
>
> What is the simplest way to generate such an index? Can it be done
> without a loop?
>
> On Mon, Dec 10, 2012 at 12:57 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>> I mean I disagree with the idea of using a harmonic mean here.
>>
>> On Sun, Dec 9, 2012 at 10:54 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>> I disagree. The geometric mean fits with the idea that log scale is a
>>> natural scale for analysis, which is true of compound growth
>>> processes. The harmonic mean doesn't and it is not defined either for
>>> zeros.
>>>
>>> Nick
>>>
>>> On Sun, Dec 9, 2012 at 10:41 PM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>> So maybe I could try the harmonic mean as well.
>>>>
>>>> P.S. The geometric mean is excellent for my objectives:
>>>> psychologically if there is a positive momentum it is plausible that
>>>> more weight is given to the latter periods, while if there is a
>>>> negative momentum more weight is given to earlier periods (where the
>>>> market was high)
>>>>
>>>> On Mon, Dec 10, 2012 at 12:25 AM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>>> Thanks Nick. That was very helpful.
>>>>>
>>>>> On Mon, Dec 10, 2012 at 12:14 AM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>>>> Note also that the geometric mean of this series is 25. This implies
>>>>>> that while the arithmetic mean gives more weight to earlier periods,
>>>>>> the geometric mean gives more weight to latter periods.:
>>>>>>
>>>>>>  ameans(reduct_per)if appt==2862
>>>>>>
>>>>>>     Variable |    Type        Obs        Mean       [95% Conf. Interval]
>>>>>> -------------+----------------------------------------------------------
>>>>>>   reduct_per | Arithmetic      37    8.108108        4.152295   12.06392
>>>>>>              |  Geometric      12          25              25         25
>>>>>>              |   Harmonic      12          25              25         25
>>>>>> ------------------------------------------------------------------------
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Mon, Dec 10, 2012 at 12:06 AM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>>>>> Eventually, I improved the system and put:the following commands:
>>>>>>>
>>>>>>> by appt: replace reduct_per=1 if reduct_per==0
>>>>>>> by appt: gen y1=exp(sum(ln(reduct_per)))
>>>>>>>
>>>>>>> I got the following table, which is exactly what I need (I don't want
>>>>>>> the geometric mean to be set to zero):
>>>>>>>
>>>>>>> . list reduct_per y3 if appt==2862
>>>>>>>
>>>>>>>         +---------------------+
>>>>>>>         | reduct~r         y3 |
>>>>>>>         |---------------------|
>>>>>>>     30. |        1          1 |
>>>>>>>     31. |        1          1 |
>>>>>>>     32. |        1          1 |
>>>>>>>     33. |        1          1 |
>>>>>>>     34. |        1          1 |
>>>>>>>         |---------------------|
>>>>>>>     35. |        1          1 |
>>>>>>>     36. |        1          1 |
>>>>>>>     37. |        1          1 |
>>>>>>>     38. |        1          1 |
>>>>>>>     39. |        1          1 |
>>>>>>>         |---------------------|
>>>>>>>     40. |        1          1 |
>>>>>>>     41. |        1          1 |
>>>>>>>     42. |        1          1 |
>>>>>>>     43. |        1          1 |
>>>>>>>     44. |        1          1 |
>>>>>>>         |---------------------|
>>>>>>>     45. |        1          1 |
>>>>>>>     46. |        1          1 |
>>>>>>>     47. |        1          1 |
>>>>>>>     48. |        1          1 |
>>>>>>>     49. |        1          1 |
>>>>>>>         |---------------------|
>>>>>>>     50. |        1          1 |
>>>>>>>     51. |        1          1 |
>>>>>>>     52. |        1          1 |
>>>>>>>     53. |        1          1 |
>>>>>>>     54. |        1          1 |
>>>>>>>         |---------------------|
>>>>>>>     55. |       25         25 |
>>>>>>>     56. |       25        625 |
>>>>>>>     57. |       25      15625 |
>>>>>>>     58. |       25     390625 |
>>>>>>>     59. |       25    9765625 |
>>>>>>>         |---------------------|
>>>>>>>     60. |       25   2.44e+08 |
>>>>>>>     61. |       25   6.10e+09 |
>>>>>>>     62. |       25   1.53e+11 |
>>>>>>>     63. |       25   3.81e+12 |
>>>>>>>     64. |       25   9.54e+13 |
>>>>>>>         |---------------------|
>>>>>>>     65. |       25   2.38e+15 |
>>>>>>>     66. |       25   5.96e+16 |
>>>>>>>         +---------------------+
>>>>>>>
>>>>>>>
>>>>>>> On Sun, Dec 9, 2012 at 11:56 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>>>>> That is reasonable if and only if zero is in effect a code for missing
>>>>>>>>
>>>>>>>> (In terms of your earlier reference, -prod()- is a user-written -egen-
>>>>>>>> function which must be installed from
>>>>>>>>
>>>>>>>> STB-51  dm71  . . . . . . . . . . . .  Calculating the product of observations
>>>>>>>>         (help prod if installed)  . . . . . . . . . . . . . . . . . .  P. Ryan
>>>>>>>>         9/99    pp.3--4; STB Reprints Vol 9, pp.45--48
>>>>>>>>         extension to egen for producing the product of observations
>>>>>>>>
>>>>>>>> Please remember to explain _where_ you obtained user-written code.)
>>>>>>>>
>>>>>>>> Nick
>>>>>>>>
>>>>>>>> On Sun, Dec 9, 2012 at 9:40 PM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>>>>>>> I don't have negative values, but I have zeros, in which case I can
>>>>>>>>> replace them by one and then take the -ln()-
>>>>>>>>>
>>>>>>>>> On Sun, Dec 9, 2012 at 11:31 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>>>>>>> Oddly enough I was thinking earlier today about how you would
>>>>>>>>>> generalise this if any values were not positive.
>>>>>>>>>>
>>>>>>>>>> If any value is zero, then the product becomes zero; otherwise one
>>>>>>>>>> would need to separate out products of -abs()- and -sign()-.
>>>>>>>>>>
>>>>>>>>>> Nick
>>>>>>>>>>
>>>>>>>>>> On Sun, Dec 9, 2012 at 9:19 PM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>>>>>>>>
>>>>>>>>>>> Please ignore my previous e-mails regarding this question
>>>>>>>>>>>
>>>>>>>>>>> After a short additional search, I found a very nice (and well known)
>>>>>>>>>>> trick proposed by Nick Cox to address the problem (which, from some
>>>>>>>>>>> reason did not come to my mind):
>>>>>>>>>>>
>>>>>>>>>>> bysort group : gen prod = sum(ln(x))
>>>>>>>>>>> by group : replace prod = exp(prod[_N])
>>>>>>>>>>>
>>>>>>>>>>> On Sun, Dec 9, 2012 at 11:05 PM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>>>>>>>>>> P.S. According to stata's help, the details of the author of the
>>>>>>>>>>>> -prod()- function is:
>>>>>>>>>>>>
>>>>>>>>>>>> Philip Ryan
>>>>>>>>>>>> Department of Public Health
>>>>>>>>>>>> South Australia
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> On Sun, Dec 9, 2012 at 10:53 PM, Yuval Arbel <yuval.arbel@gmail.com> wrote:
>>>>>>>>>>
>>>>>>>>>>>>> I appreciate very much your assistance in the following question:,
>>>>>>>>>>>>>
>>>>>>>>>>>>> I'm looking for an equivalent function for -gen y1=sum()- which will
>>>>>>>>>>>>> calculate running product for each point in time
>>>>>>>>>>>>>
>>>>>>>>>>>>> In fact, what I would like to calculate is a running geometric mean
>>>>>>>>>>>>>
>>>>>>>>>>>>> Note also that -gen y2=prod()- does not work (i.e., stata does not
>>>>>>>>>>>>> identify the function). Only -egen y2=prod()- works, but it generates
>>>>>>>>>>>>> only one product for each panel, and this is not what I need.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Finally, I tried the -amean- command,, but it simply gives summary
>>>>>>>>>>>>> statistics of different means, and it is not a function..
>>>>>>>> *
>>>>>>>> *   For searches and help try:
>>>>>>>> *   http://www.stata.com/help.cgi?search
>>>>>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>>>>> *   http://www.ats.ucla.edu/stat/stata/
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Dr. Yuval Arbel
>>>>>>> 4 Shaar Palmer Street,
>>>>>>> Haifa 33031, Israel
>>>>>>> e-mail1: yuval.arbel@carmel.ac.il
>>>>>>> e-mail2: yuval.arbel@gmail.com
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Dr. Yuval Arbel
>>>>>> 4 Shaar Palmer Street,
>>>>>> Haifa 33031, Israel
>>>>>> e-mail1: yuval.arbel@carmel.ac.il
>>>>>> e-mail2: yuval.arbel@gmail.com
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Dr. Yuval Arbel
>>>>> 4 Shaar Palmer Street,
>>>>> Haifa 33031, Israel
>>>>> e-mail1: yuval.arbel@carmel.ac.il
>>>>> e-mail2: yuval.arbel@gmail.com
>>>>
>>>>
>>>>
>>>> --
>>>> Dr. Yuval Arbel
>>>> 4 Shaar Palmer Street,
>>>> Haifa 33031, Israel
>>>> e-mail1: yuval.arbel@carmel.ac.il
>>>> e-mail2: yuval.arbel@gmail.com
>>>> *
>>>> *   For searches and help try:
>>>> *   http://www.stata.com/help.cgi?search
>>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>>> *   http://www.ats.ucla.edu/stat/stata/
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>
>
>
> --
> Dr. Yuval Arbel
> 4 Shaar Palmer Street,
> Haifa 33031, Israel
> e-mail1: yuval.arbel@carmel.ac.il
> e-mail2: yuval.arbel@gmail.com

--
Dr. Yuval Arbel
4 Shaar Palmer Street,
Haifa 33031, Israel
e-mail1: yuval.arbel@carmel.ac.il
e-mail2: yuval.arbel@gmail.com
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```