Skip to content

Instantly share code, notes, and snippets.

@theXYZT
Created August 28, 2023 02:22
Show Gist options
  • Save theXYZT/d949034899318b51cbd1c3b161490eff to your computer and use it in GitHub Desktop.
Save theXYZT/d949034899318b51cbd1c3b161490eff to your computer and use it in GitHub Desktop.
Square of Sums / Sum of Squares
import numba as nb
import numpy as np
from tqdm.auto import tqdm
import dask.array as da
@nb.njit([nb.float64[:](nb.float64[:, :]),])
def g(x):
res = np.zeros(x.shape[0], dtype=np.float64)
for i in range(res.shape[0]):
b, c = 0, 0
for a in x[i]:
b += a
c += a**2
res[i] = b**2 / c
return res
def expected_g_uniform(n, N=1000000):
z = da.random.uniform(0, 1, (N, n), chunks=("auto", -1))
z = da.map_blocks(g, z, drop_axis=-1)
return z.mean().compute()
# Find G(100; U(0, 1))
G = expected_g_uniform(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment