From afa3b668bf12ad5ecd3c12206daad1b2e53ed484 Mon Sep 17 00:00:00 2001 From: Morten Hjorth-Jensen Date: Thu, 14 Mar 2024 09:18:25 +0100 Subject: [PATCH] Update week9.do.txt --- doc/src/week9/week9.do.txt | 443 +++++++++---------------------------- 1 file changed, 101 insertions(+), 342 deletions(-) diff --git a/doc/src/week9/week9.do.txt b/doc/src/week9/week9.do.txt index 2c8c6e75..072352a9 100644 --- a/doc/src/week9/week9.do.txt +++ b/doc/src/week9/week9.do.txt @@ -1,15 +1,17 @@ TITLE: Week 11, March 11-15: Resampling Techniques, Bootstrap and Blocking AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} Email morten.hjorth-jensen@fys.uio.no at Department of Physics and Center fo Computing in Science Education, University of Oslo, Oslo, Norway & Department of Physics and Astronomy and Facility for Rare Ion Beams, Michigan State University, East Lansing, Michigan, USA -DATE: March 18-22 +DATE: March 11-15 !split ===== Overview of week 11, March 11-15 ===== !bblock Topics -* Resampling Techniques and statistics: Bootstrap and Blocking -* Discussion of onebody densities -* "Video of lecture TBA":"https://youtu.be/" -* "Handwritten notes":"https://github.com/CompPhysics/ComputationalPhysics2/blob/gh-pages/doc/HandWrittenNotes/2024/NotesMarch22.pdf" +o Reminder from last week about statistical observables, the central limit theorem and bootstrapping, see notes from last week +o Resampling TechniquesL Blocking +o Discussion of onebody densities +o Start discussion on optimization and parallelization +#* "Video of lecture TBA":"https://youtu.be/" +#* "Handwritten notes":"https://github.com/CompPhysics/ComputationalPhysics2/blob/gh-pages/doc/HandWrittenNotes/2024/NotesMarch22.pdf" !eblock !bblock Teaching Material, videos and written material @@ -25,20 +27,106 @@ DATE: March 18-22 * Our simulations can be treated as *computer experiments*. This is particularly the case for Monte Carlo methods * The results can be analysed with the same statistical tools as we would use analysing experimental data. * As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors. - - !eblock !split ===== Statistical analysis ===== !bblock * As in other experiments, many numerical experiments have two classes of errors: - o Statistical errors - o Systematical errors + o Statistical errors + o Systematical errors * Statistical errors can be estimated using standard tools from statistics * Systematical errors are method specific and must be treated differently from case to case. !eblock +!split +===== And why do we use such methods? ===== + +As you will see below, due to correlations between various +measurements, we need to evaluate the so-called covariance in order to +establish a proper evaluation of the total variance and the thereby +the standard deviation of a given expectation value. + +The covariance however, leads to an evaluation of a double sum over the various stochastic variables. This becomes computationally too expensive to evaluate. + +!split +===== Central limit theorem ===== + +Last week we derived the central limit theorem with the following assumptions: + +!bblock Measurement $i$ +We assumed that each individual measurement $x_{ij}$ is represented by stochastic variables which independent and identically distributed (iid). +This defined the sample mean of of experiment $i$ with $n$ samples as +!bt +\[ +\overline{x}_i=\frac{1}{n}\sum_{j} x_{ij}. +\] +!et +and the sample variance +!bt +\[ +\sigma^2_i=\frac{1}{n}\sum_{j} \left( (x_{ij}-\overline{x}_i\right)^2. +\] +!et +!eblock +Note that we use $n$ instead of $n-1$ in the definition of +variance. The sample variance and mean are not necessarily equal to +the exact values we would get if we knew the corresponding probability +distribution. + +!split +===== Running many measurements ===== + +!bblock Adding $m$ measurements $i$ +With the assumption that the average measurements $i$ are also defined as iid stochastic variables and have the same probability function $p$, +we defined the total average over $m$ experiments as +!bt +\[ +\overline{X}=\frac{1}{m}\sum_{i} \overline{x}_{i}. +\] +!et +and the total variance +!bt +\[ +\sigma^2_{m}=\frac{1}{m}\sum_{i} \left( \overline{x}_{i}-\overline{X}\right)^2. +\] +!et +!eblock +These are the quantities we used in showing that if the individual mean values are iid stochastic variables, then in the limit $m\rightarrow \infty$, the distribution for $\overline{X}$ is given by a Gaussian distribution with variance $\sigma^2_m$. + +!split +===== Adding more definitions ===== + +The total sample variance over the $mn$ measurements is defined as +!bt +\[ +\sigma^2=\frac{1}{mn}\sum_{i=1}^{m} \sum_{j=1}^{n}\left(x_{ij}-\overline{X}\right)^2. +\] +!et +We have from the equation for $\sigma_m^2$ +!bt +\[ +\overline{x}_i-\overline{X}=\frac{1}{n}\sum_{j=1}^{n}\left(x_{i}-\overline{X}\right), +\] +!et +and introducing the centered value $\tilde{x}_{ij}=x_{ij}-\overline{X}, we can rewrite $\sigma_m^2$ as +!bt +\[ +\sigma^2_{m}=\frac{1}{m}\sum_{i} \left( \overline{x}_{i}-\overline{X}\right)^2=\frac{1}{m}\sum_{i=1}^{m}\left[ \frac{i}{n}\sum_{j=1}^{n}\tilde{x}_{ij}\right]^2. +\] +!et + +!split +===== Further rewriting ===== +We can rewrite the latter in terms of a sum over diagonal elements only and another sum which contains the non-diagonal elements +!bt +\begin{align*} +\sigma^2_{m}& =\frac{1}{m}\sum_{i=1}^{m}\left[ \frac{i}{n}\sum_{j=1}^{n}\tilde{x}_{ij}\right]^2 \\ + & = \frac{1}{mn^2}\sum_{i=1}^{m} \sum_{j=1}^{n}\tilde{x}_ij}^2+\frac{2}{mn^2}\sum_{i=1}^{m} \sum_{j 0$. - - -The modified code here uses the BFGS algorithm but performs now a -production run and writes to file all average values of the -energy. +!split +===== Example code form last week ===== !bc pycod # 2-electron VMC code for 2dim quantum dot with importance sampling # Using gaussian rng for new positions and Metropolis- Hastings @@ -933,89 +769,12 @@ print(frame) !ec -Note that the _minimize_ function returns the final values for the -variable $\alpha=x0[0]$ and $\beta=x0[1]$ in the array $x$. - -When we have found the minimum, we use these optimal parameters to perform a production run of energies. -The output is in turn written to file and is used, together with resampling methods like the _blocking method_, -to obtain the best possible estimate for the standard deviation. The optimal minimum is, even with our guess, rather close to the exact value of $3.0$ a.u. - -The "sampling -functions":"https://github.com/CompPhysics/ComputationalPhysics2/tree/gh-pages/doc/Programs/Resampling" -can be used to perform both a blocking analysis, or a standard -bootstrap and jackknife analysis. - -===== How do we proceed? ===== - -There are several paths which can be chosen. One is to extend the -brute force gradient descent method with an adapative stochastic -gradient. There are several examples of this. A recent approach based -on "the Langevin equations":"https://arxiv.org/pdf/1805.09416.pdf" -seems like a promising approach for general and possibly non-convex -optimization problems. - -Here we would like to point out that our next step is now to use the -optimal values for our variational parameters and use these as inputs -to a production run. Here we would output values of the energy and -perform for example a blocking analysis of the results in order to get -a best possible estimate of the standard deviation. - - +!split ===== Resampling analysis ===== The next step is then to use the above data sets and perform a -resampling analysis, either using say the Bootstrap method or the -Blocking method. Since the data will be correlated, we would recommend -to use the non-iid Bootstrap code here. The theoretical background for these resampling methods is found in the "statistical analysis lecture notes":"http://compphysics.github.io/ComputationalPhysics2/doc/pub/statanalysis/html/statanalysis.html" - -Here we have tailored the codes to the output file from the previous example. We present first the bootstrap resampling with non-iid stochastic event. - -!bc pycod -# Common imports -import os - -# Where to save the figures and data files -DATA_ID = "Results/EnergyMin" - -def data_path(dat_id): - return os.path.join(DATA_ID, dat_id) - -infile = open(data_path("Energies.dat"),'r') - -from numpy import std, mean, concatenate, arange, loadtxt, zeros, ceil -from numpy.random import randint -from time import time - - -def tsboot(data,statistic,R,l): - t = zeros(R); n = len(data); k = int(ceil(float(n)/l)); - inds = arange(n); t0 = time() - - # time series bootstrap - for i in range(R): - # construct bootstrap sample from - # k chunks of data. The chunksize is l - _data = concatenate([data[j:j+l] for j in randint(0,n-l,k)])[0:n]; - t[i] = statistic(_data) - - # analysis - print ("Runtime: %g sec" % (time()-t0)); print ("Bootstrap Statistics :") - print ("original bias std. error") - print ("%8g %14g %15g" % (statistic(data), \ - mean(t) - statistic(data), \ - std(t) )) - return t -# Read in data -X = loadtxt(infile) -# statistic to be estimated. Takes two args. -# arg1: the data -def stat(data): - return mean(data) -t = tsboot(X, stat, 2**12, 2**10) - -!ec - +resampling analysis using the blocking method The blocking code, based on the article of "Marius Jonsson":"https://journals.aps.org/pre/abstract/10.1103/PhysRevE.98.043304" is given here !bc pycod