2
$\begingroup$

In the arithmetic-geometric mean, we combine the arithmetic mean and the geometric mean like so, $$a_0=a,~b_0=b$$ $$a_1=\frac{a_0+b_0}{2},~b_1=\sqrt{a_0 b_0}$$ $$\vdots$$ $$a_{n+1}=\frac{a_n+b_n}{2},~b_{n+1}=\sqrt{a_n b_n}$$ $$AGM(a, b)=\lim_{n\to\infty} a_n=\lim_{n\to\infty} b_n$$ I would like to have a more general purpose function, something like

ComboMean[f,g,x]

gives the a similar kind of mean, but instead of combining the arithmetic mean and the geometric mean, we combine any two functions f and g using a similar iteration.

But I'm stumped on the syntax on how to accomplish this. Right now, I'm writing a specific function for each mean I'm trying to find.

For example:

f2[x_] := {Mean[x], Total[x^2]/Total[x]}
M2[x_] := Nest[f2, x, 10][[1]] // N
$\endgroup$
3
  • $\begingroup$ NestList[{Mean[#], GeometricMean[#]} &, {2., 3.}, 40] $\endgroup$
    – cvgmt
    Commented Jul 21, 2023 at 7:30
  • 6
    $\begingroup$ There is ArithmeticGeometricMean in the Wolfram Language. $\endgroup$
    – Artes
    Commented Jul 21, 2023 at 8:59
  • $\begingroup$ Oh, I know. I'm interested in taking the combination of functions we see with the AGM and using it to build a other means. Like what's between the AGM and the Geometric Mean, and so on. $\endgroup$
    – David Elm
    Commented Jul 21, 2023 at 16:33

2 Answers 2

5
$\begingroup$

This is a simple implementation:

ComboMean[f_, g_, x_] := FixedPoint[{f@#,g@#}&, x][[1]];
(* define our own AGM function *)
agm[a_, b_] := ComboMean[ Mean, GeometricMean, {a, b}];
(* example of use *)
agm[1, 2.]
(* 1.45679103104691 *)

Notice that to avoid infinite loops you could use the third argument to FixedPoint to provide a limit for the number of iterations.

$\endgroup$
1
  • $\begingroup$ Thanks! That's exactly what I needed. $\endgroup$
    – David Elm
    Commented Jul 21, 2023 at 16:35
1
$\begingroup$

Since it's a recurrence, you can use

RecurrenceTable[
{
a[n] == (a[n-1] + b[n-1]) /2,
b[n] == Sqrt@(a[n-1] b[n-1]),
a[0] == a0,
b[0] == b0
},{a,b},{n,1,3}
]

for example

RecurrenceTable[{a[n] == (a[n-1]+ b[n-1]) /2, b[n] == Sqrt@(a[n-1] b[n-1]), a[0] ==1.,b[0] == 2.
},{a,b},{n,1,10}]

gives

{{1.5, 1.4142135623730951}, {1.4571067811865475, 1.4564753151219703}, {1.4567910481542587, 1.456791013939555}, {1.4567910310469068, 1.4567910310469068}, {1.4567910310469068, 1.4567910310469068}, {1.4567910310469068, 1.4567910310469068}, {1.4567910310469068, 1.4567910310469068}, {1.4567910310469068, 1.4567910310469068}, {1.4567910310469068, 1.4567910310469068}, {1.4567910310469068, 1.4567910310469068}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.