SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 1.5.1 SequencesConcept · 25 min
      2. 1.5.2 Arithmetic and geometric seriesConcept · 20 min
      3. 1.5.3 Infinite series and when they convergeConcept · 25 min
      4. 1.5.4 Practice set: seriesPractice set · 20 min
      5. 1.5.5 Case study: annuities, perpetuities and bond pricesCase study · 35 min

Level 1, Precalculus 1.5 Sequences and series Lesson 4 of 5

Practice setLesson 1.5.4 in Level 1, Precalculus. About 20 minutes.

Practice set: series

Sequences

Sequences (lesson 1.5.1)

0 of 7 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Finite series

Arithmetic and geometric series (lesson 1.5.2)

0 of 7 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Infinite series

Infinite series (lesson 1.5.3)

0 of 7 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Coding exercise

Exercise

Geometric sums, finite and infinite

Write three functions.

  1. geometric_sum(a, r, n) returns a+ar+⋯+arn−1a + ar + \cdots + ar^{n-1}, the sum of nn terms. Handle r=1r = 1, where the formula divides by zero, and n=0n = 0, an empty sum.
  2. geometric_sum_infinite(a, r) returns a1−r\frac{a}{1 - r}. Raise ValueError when ∣r∣≥1|r| \ge 1, because the series diverges.
  3. terms_needed(a, r, tol) returns the smallest number of terms nn whose partial sum is within tol of the infinite sum. Check the ratio first and raise ValueError when ∣r∣≥1|r| \ge 1: there the gap never shrinks, so a loop would run for ever.

The tests check terms_needed against partial sums added up directly, including a negative ratio and a negative first term.

See the tests
def test_doubling():
    """5 + 10 + 20 + … to 10 terms is 5115"""
    expect_close(geometric_sum(5, 2, 10), 5115, what="geometric_sum(5, 2, 10)")


def test_matches_direct_sum():
    """The formula matches a direct sum: 1000 at ratio 1.05, 7 terms"""
    expect_close(geometric_sum(1000, 1.05, 7), sum(1000 * 1.05**k for k in range(7)), what="geometric_sum(1000, 1.05, 7)")


def test_ratio_one():
    """With ratio 1, every term is a: 7 terms of 3 make 21"""
    expect_close(geometric_sum(3, 1, 7), 21, what="geometric_sum(3, 1, 7)")


def test_no_terms():
    """Zero terms make an empty sum, 0"""
    expect_close(geometric_sum(4, 0.5, 0), 0, what="geometric_sum(4, 0.5, 0)")


def test_infinite():
    """1/2 + 1/4 + … adds to 1"""
    expect_close(geometric_sum_infinite(0.5, 0.5), 1.0, what="geometric_sum_infinite(0.5, 0.5)")


def test_infinite_negative_ratio():
    """5 − 5/2 + 5/4 − … adds to 10/3"""
    expect_close(geometric_sum_infinite(5, -0.5), 10 / 3, what="geometric_sum_infinite(5, -0.5)")


def test_diverges():
    """A ratio of 2 or −1 diverges: raise ValueError"""
    for r in (2, -1):
        try:
            geometric_sum_infinite(1, r)
        except ValueError:
            continue
        raise AssertionError(f"expected ValueError for r = {r}")


def _gap(a, r, n):
    """How far the partial sum of n terms, added up directly, is from a / (1 - r)."""
    return abs(a / (1 - r) - sum(a * r**k for k in range(n)))


def test_terms_needed_halves():
    """1/2 + 1/4 + … needs 20 terms to come within 0.000001 of 1"""
    expect_equal(terms_needed(0.5, 0.5, 1e-6), 20, what="terms_needed(0.5, 0.5, 1e-6)")


def test_terms_needed_slow_ratio():
    """1 + 0.9 + 0.81 + … needs 66 terms to come within 0.01 of 10"""
    expect_equal(terms_needed(1, 0.9, 0.01), 66, what="terms_needed(1, 0.9, 0.01)")


def test_terms_needed_is_smallest():
    """For a negative ratio and a negative first term, n terms are close enough and n − 1 are not"""
    for a, r, tol in [(1, -0.5, 0.001), (-2, 0.6, 0.001), (3, 0.25, 1e-5)]:
        n = terms_needed(a, r, tol)
        assert isinstance(n, int) and n >= 1, f"terms_needed({a}, {r}, {tol}) should be a whole number of terms, got {n!r}"
        assert _gap(a, r, n) < tol, f"terms_needed({a}, {r}, {tol}) returned {n}, but {n} terms still miss by {_gap(a, r, n):.3g}"
        assert _gap(a, r, n - 1) >= tol, f"terms_needed({a}, {r}, {tol}) returned {n}, but {n - 1} terms are already close enough"


def test_terms_needed_diverges():
    """A ratio of 1.5 has no sum to approach: raise ValueError"""
    try:
        terms_needed(1, 1.5, 0.01)
    except ValueError:
        return
    raise AssertionError("expected ValueError for r = 1.5")

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.

  • Series and Sequences

    Paul's Online Notes. Notes.

    Sequences, series, convergence tests and power series.