# Homework 2 -- Comparison of series by a limit of ratios

Let $(a_n)$ and $(b_n)$ be two sequences of non-negative real numbers. Prove
that if
$$
\lim_{n\to\infty} \frac{a_n}{b_n} = c \in (0,\infty),
$$
then $\sum_n a_n$ and $\sum_n b_n$ are either both convergent or both divergent.

Use this result to show that, given any sequence $(a_n)$ of non-negative real
numbers, $\sum a_n$ converges if and only if $\sum \frac{a_n}{1+a_n}$ converges.

**Course material.**

- Lecture 4: [Cauchy sequences and series](../index.html#lecture04)
  (Abbott §2.6–2.7).
- Formalisation: [`theories/Series.v`](../theories/Series.v). The results used
  below are `series_comparison_test`, `series_convergent_terms_zero` and
  `series_algebraic_limit`; `partial_sums a n` is $\sum_{k=0}^{n} a_k$, and
  "$\sum a_n$ converges" is written `∃ l1 ∈ ℝ, partial_sums a ⟶ l1`.

Notes:

- The ratio $a_n / b_n$ only makes sense when $b_n \neq 0$, so we assume
  $b_n > 0$ throughout. (In Rocq `x / 0` is `0`, which would make the statement
  false as written.)
- **Invoke lemmas together with their hypotheses**, as in
  `By (my_lemma x Hx) it holds that ...` rather than `By my_lemma it holds that ...`.
  Otherwise the automation has to search for the arguments itself, and checking
  becomes way too slow.

<hint title="Import libraries (click to open/close)">

Run `dune build` once before opening this file, so that the `RUG.Analysis.*`
modules of the course library are available.

```coq
Require Import Stdlib.Reals.Reals.
Require Import Stdlib.Reals.PartSum.
Require Import Waterproof.Tactics.
Require Import Waterproof.Notations.Common.
Require Import Waterproof.Notations.Reals.
Require Import Waterproof.Notations.Sets.
Require Import Waterproof.Chains.
Require Import Waterproof.Automation.
Require Import Waterproof.Libs.Analysis.Series.
Require Import Waterproof.Libs.Analysis.Sequences.
Require Import RUG.Analysis.Series.

Waterproof Enable Automation RealsAndIntegers.
Waterproof Enable Automation Intuition.

Open Scope R_scope.
Open Scope subset_scope.

Set Default Goal Selector "!".
Set Bullet Behavior "Waterproof Relaxed Subproofs".
```
</hint>

## Part 1: comparison from a certain index onwards

If $0 \le a_n$, $0 \le b_n$, and $a_n \le b_n$ for all $n > N$, then
convergence of $\sum b_n$ implies convergence of $\sum a_n$.

This variant of `series_comparison_test` is given to you, since its proof is
long and is not the point of the assignment. You will use it in Parts 3 and 4.
You do not need to read the proof, but it is a good example of an induction in
Waterproof: in the induction step the goal is stated for `(n + 1)%nat`, not
`S n`, and in the base case one writes `0%nat`, because a plain `0` is read as
a real number.

```coq
Lemma tail_comparison (a b : ℕ → ℝ) (Nn : ℕ) (L : ℝ)
    (Ha : ∀ n : ℕ, 0 ≤ a n)
    (Hb0 : ∀ n : ℕ, 0 ≤ b n)
    (Hab : ∀ n : ℕ, (n > Nn)%nat → a n ≤ b n)
    (HL : partial_sums b ⟶ L) :
    ∃ l1 ∈ ℝ, partial_sums a ⟶ l1.
Proof.
(* Step 1: the partial sums of a are non-decreasing. *)
We claim that ∀ n ∈ ℕ, a n ≥ 0 as (Ha').
{ Take n ∈ ℕ. It holds that 0 ≤ a n. We conclude that a n ≥ 0. }
By partial_sums_pos_incr it holds that Un_growing (partial_sums a) as (Hgrow).

(* Step 2: compare the partial sums: up to index Nn we use monotonicity,
   after Nn we use a n ≤ b n. *)
We claim that ∀ n : ℕ, partial_sums a n ≤ partial_sums a Nn + partial_sums b n
  as (Hcomp).
{
  We use induction on n.
  + We first show the base case
      (partial_sums a 0 ≤ partial_sums a Nn + partial_sums b 0).
    By (growing_prop (partial_sums a) Nn O) it holds that
      partial_sums a Nn ≥ partial_sums a 0 as (H0).
    It holds that partial_sums b 0%nat = b 0%nat as (Hb00).
    It holds that 0 ≤ b 0%nat as (Hb0pos).
    We conclude that partial_sums a 0 ≤ partial_sums a Nn + partial_sums b 0%nat.
  + We now show the induction step.
    Take n : ℕ.
    Assume that partial_sums a n ≤ partial_sums a Nn + partial_sums b n as (IH).
    We need to show that
      partial_sums a (n + 1)%nat ≤ partial_sums a Nn + partial_sums b (n + 1)%nat.
    It holds that (n + 1 = S n)%nat as (Hn1).
    By Hn1 it holds that partial_sums a (n + 1)%nat = partial_sums a (S n) as (Ea).
    By Hn1 it holds that partial_sums b (n + 1)%nat = partial_sums b (S n) as (Eb).
    It holds that partial_sums a (S n) = partial_sums a n + a (S n) as (Hsa).
    It holds that partial_sums b (S n) = partial_sums b n + b (S n) as (Hsb).
    Either (S n ≤ Nn)%nat or (S n > Nn)%nat.
    - Case (S n ≤ Nn)%nat.
      By (growing_prop (partial_sums a) Nn (S n)) it holds that
        partial_sums a Nn ≥ partial_sums a (S n) as (Hmon).
      By (cond_pos_sum b (S n)) it holds that 0 ≤ partial_sums b (S n) as (Hpos).
      We conclude that
        partial_sums a (n + 1)%nat ≤ partial_sums a Nn + partial_sums b (n + 1)%nat.
    - Case (S n > Nn)%nat.
      By Hab it holds that a (S n) ≤ b (S n) as (Hk).
      We conclude that
        partial_sums a (n + 1)%nat ≤ partial_sums a Nn + partial_sums b (n + 1)%nat.
}

(* Step 3: the partial sums of b are bounded by their limit L. *)
We claim that ∀ n ∈ ℕ, b n ≥ 0 as (Hb').
{ Take n ∈ ℕ. It holds that 0 ≤ b n. We conclude that b n ≥ 0. }
By partial_sums_pos_incr it holds that Un_growing (partial_sums b) as (Hgrowb).
By convergence_equivalence it holds that
  (partial_sums b ⟶ L ⇔ Un_cv (partial_sums b) L) as (Hb_iff).
By Hb_iff it holds that Un_cv (partial_sums b) L as (HbL).
By growing_ineq it holds that ∀ n : ℕ, partial_sums b n ≤ L as (Hb_le).

(* Step 4: hence the partial sums of a are bounded ... *)
We claim that bound (EUn (partial_sums a)).
{
  We need to show that ∃ m : ℝ, ∀ x : ℝ, EUn (partial_sums a) x ⇒ x ≤ m.
  Choose m := partial_sums a Nn + L.
  Take x : ℝ.
  Assume that EUn (partial_sums a) x as (Hx).
  It holds that ∃ i : ℕ, x = partial_sums a i as (Hi).
  Obtain i according to (Hi).
  By Hcomp it holds that partial_sums a i ≤ partial_sums a Nn + partial_sums b i.
  By Hb_le it holds that partial_sums b i ≤ L.
  We conclude that x ≤ m.
}

(* Step 5: ... and the monotone convergence theorem concludes. *)
By Un_cv_crit it holds that ∃ l : ℝ, Un_cv (partial_sums a) l as (Hl).
Obtain l according to (Hl).
By convergence_equivalence it holds that
  (partial_sums a ⟶ l ⇔ Un_cv (partial_sums a) l) as (Ha_iff).
By Ha_iff it holds that partial_sums a ⟶ l as (Hal).
Choose l1 := l.
{ Indeed, l1 ∈ ℝ. }
We conclude that partial_sums a ⟶ l1.
Qed.
```

## Part 2: the bounds coming from the limit of the ratios

From $\lim a_n / b_n = c > 0$, applied with $\varepsilon = c/2$, deduce that from
some index onwards
$$
\frac c2 < \frac{a_n}{b_n} < \frac{3c}{2},
\qquad\text{hence}\qquad
a_n \le \frac{3c}{2} b_n \quad\text{and}\quad b_n \le \frac2c a_n .
$$

**Waterproof remark.** The automation treats `/ u` as an unknown. To pass from
`a n / b n` to a statement about `a n`, give it `b n * / b n = 1`, and likewise
`c * / c = 1`, using `Rinv_r : r ≠ 0 → r * / r = 1`.
The automation also does not remove absolute values from hypotheses on its
own. To turn `|t| < a` into two inequalities without absolute values, use
`Rabs_def2 : |x| < a → x < a ∧ - a < x`, e.g.
`By Rabs_def2 it holds that t < a ∧ - a < t as (H).`

<hint title="💡 Hint (click to open/close)">
Use the definition of the limit with $\varepsilon = c/2$, remove the absolute
value, then multiply by $b_n > 0$.
</hint>

```coq
Lemma ratio_bounds (a b : ℕ → ℝ) (c : ℝ) (Hc : c > 0)
    (Hbpos : ∀ n : ℕ, b n > 0)
    (Hlim : (fun n : ℕ => a n / b n) ⟶ c) :
    ∃ Nn ∈ ℕ, ∀ n : ℕ, (n ≥ Nn)%nat →
      a n ≤ (3*c/2) * b n ∧ b n ≤ (2/c) * a n.
Proof.
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```

Since the bounds of Part 2 involve a constant factor, we also need that
rescaling a series does not affect convergence. Here `sprod b K` is the sequence
$n \mapsto K \cdot b_n$ (see `theories/Series.v`). This is the first half of
`series_algebraic_limit` from the lectures, with both sequences equal to `b`,
so it is given to you.

```coq
Lemma scaled_series_converges (b : ℕ → ℝ) (K L : ℝ) (HL : partial_sums b ⟶ L) :
    partial_sums (sprod b K) ⟶ (K * L).
Proof.
By (series_algebraic_limit b b L L HL HL) it holds that
  (∀ c ∈ ℝ, partial_sums (sprod b c) ⟶ (c * L)) ∧
    partial_sums (ssum b b) ⟶ (L + L) as (Halg).
Because (Halg) both (∀ c ∈ ℝ, partial_sums (sprod b c) ⟶ (c * L)) as (Hscal)
  and (partial_sums (ssum b b) ⟶ (L + L)) as (Hsum).
It holds that K ∈ ℝ as (HK).
By (Hscal K HK) we conclude that partial_sums (sprod b K) ⟶ (K * L).
Qed.
```

## Part 3: the main theorem

Both series converge, or neither does.

**Waterproof remark.** A statement about `sprod a K n` is easier to prove after
unfolding it with `We need to show that 0 ≤ K * a n.` (or similar).

<hint title="💡 Hint (click to open/close)">
Prove the two implications separately. They are symmetric: combine Part 2,
`scaled_series_converges` and Part 1.
</hint>

```coq
Theorem series_ratio_comparison (a b : ℕ → ℝ) (c : ℝ) (Hc : c > 0)
    (Ha0 : ∀ n : ℕ, 0 ≤ a n) (Hbpos : ∀ n : ℕ, b n > 0)
    (Hlim : (fun n : ℕ => a n / b n) ⟶ c) :
    (∃ l1 ∈ ℝ, partial_sums a ⟶ l1) ⇔ (∃ l1 ∈ ℝ, partial_sums b ⟶ l1).
Proof.
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```

## Part 4: the application

For the second half of the assignment we need three elementary facts about the
map $x \mapsto \frac{x}{1+x}$ on $[0,\infty)$:
$$
0 \le \frac{x}{1+x}, \qquad
\frac{x}{1+x} \le x, \qquad
\frac{x}{1+x} \le \frac12 \;\Rightarrow\; x \le 2\cdot\frac{x}{1+x}.
$$

**These three lemmas are optional.** They are admitted, so you can use them in
the proof of the equivalence below straight away. Prove them in your own time,
after finishing the rest: replace `Admitted.` by your proof and end it with
`Qed.`.

**Waterproof remark.** As in Part 2, the automation needs `(1 + x) * / (1 + x) = 1`
(`Rinv_r`) before it can handle `x / (1 + x)`. It also does not cancel a
common factor in an inequality on its own: for that use
`Rmult_le_reg_r : 0 < r → r1 * r ≤ r2 * r → r1 ≤ r2`, e.g. once you know
`0 < 1 + x` and `u * (1 + x) ≤ v * (1 + x)`, write
`By Rmult_le_reg_r we conclude that u ≤ v.`

<hint title="💡 Hint (click to open/close)">
Clear the denominator: multiply by `1 + x` and cancel the common factor.
For the third fact, first show `x ≤ 1`.
</hint>

```coq
Lemma frac_nonneg (x : ℝ) (Hx : 0 ≤ x) : 0 ≤ x / (1 + x).
Proof.
```
<input-area>
```coq
Admitted.
```
</input-area>

```coq
Lemma frac_le (x : ℝ) (Hx : 0 ≤ x) : x / (1 + x) ≤ x.
Proof.
```
<input-area>
```coq
Admitted.
```
</input-area>

```coq
Lemma frac_ge (x : ℝ) (Hx : 0 ≤ x) (Hhalf : x / (1 + x) ≤ 1/2) :
    x ≤ 2 * (x / (1 + x)).
Proof.
```
<input-area>
```coq
Admitted.
```
</input-area>

Now prove the equivalence.

**Waterproof remark.** The hypotheses of `series_comparison_test` are stated with
`∀ n ∈ ℕ`, those of Part 1 with `∀ n : ℕ`; state them in exactly that form.
As in Part 2, use `Rabs_def2` to remove an absolute value from a hypothesis.

<hint title="💡 Hint (click to open/close)">
(⇒) Use `series_comparison_test`.

(⇐) The terms of a convergent series tend to $0$
(`series_convergent_terms_zero`), so eventually
$\frac{a_n}{1+a_n} \le \frac12$, and then `frac_ge` applies. Conclude with
Part 1.
</hint>

```coq
Theorem series_a_over_one_plus_a (a : ℕ → ℝ) (Ha : ∀ n : ℕ, 0 ≤ a n) :
    (∃ l1 ∈ ℝ, partial_sums a ⟶ l1) ⇔
    (∃ l1 ∈ ℝ, partial_sums (fun n : ℕ => a n / (1 + a n)) ⟶ l1).
Proof.
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```

## Declaration of own work

Please declare that the work above is your own work by writing `Yes.` below.
In particular, this means the proofs are typed by yourself and you could
explain them when asked.
```coq
Inductive answer : Type := Yes : answer.

Definition own_work : answer. exact
```
<input-area>
```coq

```
</input-area>
```coq
Qed.
```
