{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1.5" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bool[0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0]\n" ] } ], "source": [ "# Convert a number of the form 0.a₁a₂... to binary and truncate the result\n", "# after a number 'nbits' of bits. The function returns the bits b₁, b₂, b₃...\n", "# of the binary representation, from the most to the least significant.\n", "\n", "function to_binary(x, nbits)\n", " result = zeros(Bool, nbits)\n", " for i in 1:nbits\n", " result[i] = (2*x >= 1)\n", " x = 2*x - result[i]\n", " end\n", " return result\n", "end\n", "\n", "x, nbits = .1, 60\n", "bits = to_binary(x, nbits)\n", "println(bits)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Approximation: 0.1000000000000000055511151231257827021181583404541015625\n", "\n", "Float64 approximation of 0.1:\n", "0.1000000000000000055511151231257827021181583404541015625\n" ] } ], "source": [ "# Let us check that our function works\n", "approx = BigFloat(0)\n", "for (i, b) in enumerate(bits)\n", " approx += BigFloat(2.0)^(-i) * b\n", "end\n", "println(\"Approximation: $approx\")\n", "println(\"\\nFloat64 approximation of 0.1:\")\n", "println(\"$(BigFloat(0.1))\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1.14\n", "A better formula for computing the sample variance is the following:\n", "$$\n", "s^2 = \\frac{1}{N-1} \\sum_{n=1}^N \\bigl( x_n - \\bar x \\bigr)^2, \\qquad \\bar x = \\frac{1}{N} \\sum_{n=1}^N x_n\n", "$$" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Method 1: 134.2178622178622\n", "Method 2: 0.0833193095495609\n", "Exact value: 0.08331930954955580632570380781273146792330591312169203868088283519976023797377159\n" ] } ], "source": [ "import Random\n", "\n", "# This ensures that the same random numbers are generated every time the code is run\n", "Random.seed!(0)\n", "\n", "N, L = 10^6, 10^9\n", "x = L .+ rand(N)\n", "\n", "average = (sum(x)/N)\n", "\n", "s1 = 1/(N-1) * (sum(x.^2) - N*average^2)\n", "s2 = 1/(N-1) * (sum((x .- average).^2))\n", "println(\"Method 1: $s1\\nMethod 2:$s2\")\n", "\n", "# We use 'BigFloat' to calculate a very precise result\n", "x = BigFloat.(x)\n", "exact_average = (sum(x)/N)\n", "exact_value = 1/(N-1) * (sum(x.^2) - N*exact_average^2)\n", "println(\"Exact value: \$exact_value\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1.15\n", "For best accuracy, we can reverse the order of the summation" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.013651380840315e-9\n", "9.013651380840315e-9\n", "1.000000082740371e-9\n", "1.000000082740371e-10\n" ] } ], "source": [ "fun_naive(N) = sum(1/Float64(n)^2 for n in 1:N)\n", "fun_better(N) = sum(1/Float64(N+1-n)^2 for n in 1:N)\n", "\n", "println(abs(fun_naive(10^9) - π^2/6))\n", "println(abs(fun_naive(10^10) - π^2/6))\n", "println(abs(fun_better(10^9) - π^2/6))\n", "println(abs(fun_better(10^10) - π^2/6))" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all" }, "kernelspec": { "display_name": "Julia 1.5.3", "language": "julia", "name": "julia-1.5" } }, "nbformat": 4, "nbformat_minor": 2 }