def constructIntegrandFunctionObject(marginalPair, eta, k, l, N): """ Input: marginalPair, e.g. [0.5,0.3] eta, k,l, N Output: integrand function which object maps t to Estimated emission probability of p(baseMarginals)(t) from p^\eta and which has some additional methods/properties, facilitating binary search, associated with it. """ #functionFromTwoMarginalsAndParameterToIntegrand(x,y,t) = Estimated emission probability of p(x,y,t) from p^\eta functionFromTwoMarginalsAndParameterToIntegrand = epc.emissionProbabilityCalculator( eta, k, l, N).RobbinsEstimateOfEmissionProbability t = 0.001 #used for logger logging.info("For marginals %s, Robbins function takes value %s at t=%s" % (marginalPair, functionFromTwoMarginalsAndParameterToIntegrand( marginalPair[0], marginalPair[1], t), t)) functionFromParameterToIntegrandObject = fa.functionAlgorithms( functionFromTwoMarginalsAndParameterToIntegrand) functionFromParameterToIntegrandObject.setFixedArgumentList(marginalPair) logging.info( "Fixed argument list set to %s" % (str(functionFromParameterToIntegrandObject.fixedArgumentList))) #functionFromParameterToIntegrand(t) = Estimated emission probability of p(baseMarginals)(t) from p^\eta functionFromParameterToIntegrand = functionFromParameterToIntegrandObject.functionOfOneVariable logging.info("As func. of one variable, takes value %s at t=%s" % (functionFromParameterToIntegrand(t), t)) return fa.functionAlgorithms(functionFromParameterToIntegrand)
def testBinarySearch(self): newFunctionAlgorithmObject = fa.functionAlgorithms(np.sin) np.testing.assert_almost_equal( newFunctionAlgorithmObject.searchArgWhereIncreasingFunctionTakesVal(0, -np.pi/2.0, np.pi/2.0),0.0) np.testing.assert_almost_equal( newFunctionAlgorithmObject.searchArgWhereIncreasingFunctionTakesVal(-1, -np.pi/2.0, np.pi/2.0), -1.57079632094) np.testing.assert_almost_equal( newFunctionAlgorithmObject.searchArgWhereIncreasingFunctionTakesVal(1, -np.pi/2.0, np.pi/2.0), 1.57079632094)
def testBinarySearch(self): newFunctionAlgorithmObject = fa.functionAlgorithms(np.sin) np.testing.assert_almost_equal( newFunctionAlgorithmObject.searchArgWhereIncreasingFunctionTakesVal(0, -np.pi / 2.0, np.pi / 2.0), 0.0 ) np.testing.assert_almost_equal( newFunctionAlgorithmObject.searchArgWhereIncreasingFunctionTakesVal(-1, -np.pi / 2.0, np.pi / 2.0), -1.57079632094, ) np.testing.assert_almost_equal( newFunctionAlgorithmObject.searchArgWhereIncreasingFunctionTakesVal(1, -np.pi / 2.0, np.pi / 2.0), 1.57079632094, )
def testSearchArgWhereIncreasingFunctionTakesProportionOfMaxVal(self): theProportion = stats.norm.pdf(1)/stats.norm.pdf(0) N_list = list(10*np.array(range(1,10))) N_list.extend((100*np.array(range(1,10)))) k,l=2,2 eta=0.01 gamma = 0.001 logger.debug("Set eta=%s, gamma=%s"%(eta,gamma)) #firstMarginalsDist = stats.uniform(loc = .4,scale = .2) #secondMarginalsDist = stats.uniform(loc = .4,scale = .2) for N in N_list: for iteration in range(1): #firstMarginal, secondMarginal = [firstMarginalsDist.rvs(), secondMarginalsDist.rvs()] firstMarginal, secondMarginal = 1.0/l, 1.0/k #0.5, 0.5 for binary-binary logger.debug("Randomly chosen marginals: (%s,%s)"%(firstMarginal, secondMarginal)) functionFromTwoMarginalsAndParameterToIntegrand = epc.emissionProbabilityCalculator(eta, k, l, N).RobbinsEstimateOfEmissionProbability logger.debug("For marginals (%s,%s), Robbins function takes value %s at t=%s"%(firstMarginal, secondMarginal, functionFromTwoMarginalsAndParameterToIntegrand(firstMarginal, secondMarginal, 0.001), 0.001)) functionFromParameterToIntegrandObject = fa.functionAlgorithms(functionFromTwoMarginalsAndParameterToIntegrand) functionFromParameterToIntegrandObject.setFixedArgumentList([firstMarginal, secondMarginal]) logger.debug("Fixed argument list set to %s"%(str(functionFromParameterToIntegrandObject.fixedArgumentList))) functionFromParameterToIntegrand = functionFromParameterToIntegrandObject.functionOfOneVariable logger.debug("As func. of one variable, takes value %s at t=%s"%(functionFromParameterToIntegrand(0.001), 0.001)) probDistPath = pdpf.probabilityDistributionPathFactory([firstMarginal, secondMarginal], k, l).construct() t_gamma_plus = probDistPath.largestPos_t_atWhichKLDivergenceFromBaseIsLessThanEta(gamma) t_gamma_minus = probDistPath.smallestNeg_t_atWhichKLDivergenceFromBaseIsLessThanEta(gamma) logger.debug("Robbins function takes value %s at t_gamma_plus=%s"%(functionFromParameterToIntegrandObject.theFunction(firstMarginal, secondMarginal, t_gamma_plus), t_gamma_plus)) logger.debug("Robbins function takes value %s at t_gamma_minus=%s"%(functionFromParameterToIntegrandObject.theFunction(firstMarginal, secondMarginal, t_gamma_minus), t_gamma_minus)) integrandFunctionObject = fa.functionAlgorithms(functionFromParameterToIntegrand) logger.info("Searching for where the function is proportion %s of the max between %s and %s"%(theProportion, t_gamma_minus, t_gamma_plus)) computedScale = integrandFunctionObject.searchArgWhereIncreasingFunctionTakesProportionOfMaxVal(theProportion, t_gamma_minus, t_gamma_plus) logger.info("For marginals (%s,%s), N=%s, computed scale is %s"%(firstMarginal, secondMarginal, N, computedScale))
def constructIntegrandFunctionObject(marginalPair, eta, k,l,N): """ Input: marginalPair, e.g. [0.5,0.3] eta, k,l, N Output: integrand function which object maps t to Estimated emission probability of p(baseMarginals)(t) from p^\eta and which has some additional methods/properties, facilitating binary search, associated with it. """ #functionFromTwoMarginalsAndParameterToIntegrand(x,y,t) = Estimated emission probability of p(x,y,t) from p^\eta functionFromTwoMarginalsAndParameterToIntegrand = epc.emissionProbabilityCalculator(eta, k, l, N).RobbinsEstimateOfEmissionProbability t=0.001 #used for logger logging.info("For marginals %s, Robbins function takes value %s at t=%s"%( marginalPair, functionFromTwoMarginalsAndParameterToIntegrand(marginalPair[0], marginalPair[1], t), t)) functionFromParameterToIntegrandObject = fa.functionAlgorithms(functionFromTwoMarginalsAndParameterToIntegrand) functionFromParameterToIntegrandObject.setFixedArgumentList(marginalPair) logging.info("Fixed argument list set to %s"%(str(functionFromParameterToIntegrandObject.fixedArgumentList))) #functionFromParameterToIntegrand(t) = Estimated emission probability of p(baseMarginals)(t) from p^\eta functionFromParameterToIntegrand = functionFromParameterToIntegrandObject.functionOfOneVariable logging.info("As func. of one variable, takes value %s at t=%s"%(functionFromParameterToIntegrand(t), t)) return fa.functionAlgorithms(functionFromParameterToIntegrand)
def testSearchArgWhereIncreasingFunctionTakesProportionOfMaxVal(self): theProportion = stats.norm.pdf(1) / stats.norm.pdf(0) N_list = list(10 * np.array(range(1, 10))) N_list.extend((100 * np.array(range(1, 10)))) k, l = 2, 2 eta = 0.01 gamma = 0.001 logger.debug("Set eta=%s, gamma=%s" % (eta, gamma)) # firstMarginalsDist = stats.uniform(loc = .4,scale = .2) # secondMarginalsDist = stats.uniform(loc = .4,scale = .2) for N in N_list: for iteration in range(1): # firstMarginal, secondMarginal = [firstMarginalsDist.rvs(), secondMarginalsDist.rvs()] firstMarginal, secondMarginal = 1.0 / l, 1.0 / k # 0.5, 0.5 for binary-binary logger.debug("Randomly chosen marginals: (%s,%s)" % (firstMarginal, secondMarginal)) functionFromTwoMarginalsAndParameterToIntegrand = epc.emissionProbabilityCalculator( eta, k, l, N ).RobbinsEstimateOfEmissionProbability logger.debug( "For marginals (%s,%s), Robbins function takes value %s at t=%s" % ( firstMarginal, secondMarginal, functionFromTwoMarginalsAndParameterToIntegrand(firstMarginal, secondMarginal, 0.001), 0.001, ) ) functionFromParameterToIntegrandObject = fa.functionAlgorithms( functionFromTwoMarginalsAndParameterToIntegrand ) functionFromParameterToIntegrandObject.setFixedArgumentList([firstMarginal, secondMarginal]) logger.debug( "Fixed argument list set to %s" % (str(functionFromParameterToIntegrandObject.fixedArgumentList)) ) functionFromParameterToIntegrand = functionFromParameterToIntegrandObject.functionOfOneVariable logger.debug( "As func. of one variable, takes value %s at t=%s" % (functionFromParameterToIntegrand(0.001), 0.001) ) probDistPath = pdpf.probabilityDistributionPathFactory( [firstMarginal, secondMarginal], k, l ).construct() t_gamma_plus = probDistPath.largestPos_t_atWhichKLDivergenceFromBaseIsLessThanEta(gamma) t_gamma_minus = probDistPath.smallestNeg_t_atWhichKLDivergenceFromBaseIsLessThanEta(gamma) logger.debug( "Robbins function takes value %s at t_gamma_plus=%s" % ( functionFromParameterToIntegrandObject.theFunction(firstMarginal, secondMarginal, t_gamma_plus), t_gamma_plus, ) ) logger.debug( "Robbins function takes value %s at t_gamma_minus=%s" % ( functionFromParameterToIntegrandObject.theFunction( firstMarginal, secondMarginal, t_gamma_minus ), t_gamma_minus, ) ) integrandFunctionObject = fa.functionAlgorithms(functionFromParameterToIntegrand) logger.info( "Searching for where the function is proportion %s of the max between %s and %s" % (theProportion, t_gamma_minus, t_gamma_plus) ) computedScale = integrandFunctionObject.searchArgWhereIncreasingFunctionTakesProportionOfMaxVal( theProportion, t_gamma_minus, t_gamma_plus ) logger.info( "For marginals (%s,%s), N=%s, computed scale is %s" % (firstMarginal, secondMarginal, N, computedScale) )