#NEWLINE# def importanceMixing( oldpoints , oldpdf , newpdf , newdistr , forcedRefresh = 0.01 ) : #NEWLINE# #INDENT# ' Implements importance mixing. Given a set of points, an old and a new pdf-function for them\n    and a generator function for new points, it produces a list of indices of the old points to be reused and a list of new points.\n    Parameter (optional): forced refresh rate.\n    ' #NEWLINE# reuseindices = [ ] #NEWLINE# batch = len ( oldpoints ) #NEWLINE# for ( i , sample ) in enumerate ( oldpoints ) : #NEWLINE# #INDENT# r = uniform ( 0 , 1 ) #NEWLINE# if ( r < ( ( ( 1 - forcedRefresh ) * newpdf ( sample ) ) / oldpdf ( sample ) ) ) : #NEWLINE# #INDENT# reuseindices . append ( i ) #NEWLINE# #UNINDENT# if ( ( batch - len ( reuseindices ) ) <= max ( 1 , ( batch * forcedRefresh ) ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# newpoints = [ ] #NEWLINE# while ( ( len ( reuseindices ) + len ( newpoints ) ) < batch ) : #NEWLINE# #INDENT# r = uniform ( 0 , 1 ) #NEWLINE# sample = newdistr ( ) #NEWLINE# if ( r < forcedRefresh ) : #NEWLINE# #INDENT# newpoints . append ( sample ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( r < ( 1 - ( oldpdf ( sample ) / newpdf ( sample ) ) ) ) : #NEWLINE# #INDENT# newpoints . append ( sample ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return ( reuseindices , newpoints )