def fZ(z): """ Compute f(z) with a simpson rule @ In, z, float, the coordinate @ Out, fZ, the f(z) """ return mathUtils.simpson(lambda x: pdfs[0](x)*pdfs[1](x-z), lowLow, highHigh, 1000)
def firstMomentSimpson(f, a, b, n): """ Compute the first simpson method @ In, f, method, the function f(x) @ In, a, float, lower bound @ In, b, float, upper bound @ In, n, int, the number of discretizations @ Out, firstMomentSimpson, float, the moment """ return mathUtils.simpson(lambda x: x * f(x), a, b, n)
testSkewNormal = mathUtils.skewNormal(x,alpha,xi,omega) checkAnswer('skewNormal (%f,%f,%f,%f)' %(x,alpha,xi,omega),testSkewNormal,val,1e-5) ### check "createInterp" # TODO ### check "simpson" def f(x): """ Simple squaring function. @ In, x, float, value @ Out, f, float, square value """ return x*x simp = mathUtils.simpson(f,-1,1,5) checkAnswer('simpson',simp,0.677333333333,1e-5) ### check "getGraphs" # TODO I don't know what this does. Documentation is poor. ### check "countBins" data = [0.1,0.2, 1.1,1.2,1.3, 2.1,2.2,2.3,2.4, 3.1,3.2,3.3] boundaries = [1,2,3] counted = mathUtils.countBins(data,boundaries) checkArray('countBins',counted,[2,3,4,3],1e-5) ### check "log2"
def _getGraphs(functions, fZStats=False): """ Returns the graphs of the functions. The functions are a list of (dataStats, cdf_function, pdf_function,name) It returns a dictionary with the graphs and other statistics calculated. @ In, functions, list, list of functions (data_stats_dict, cdf_function, pdf_function,name) @ In, fZStats, bool, optional, true if the F(z) (cdf) needs to be computed @ Out, retDict, dict, the return dictionary """ retDict = {} dataStats = [x[0] for x in functions] means = [x["mean"] for x in dataStats] stdDevs = [x["stdev"] for x in dataStats] cdfs = [x[1] for x in functions] pdfs = [x[2] for x in functions] names = [x[3] for x in functions] low = min([m - 3.0 * s for m, s in zip(means, stdDevs)]) high = max([m + 3.0 * s for m, s in zip(means, stdDevs)]) lowLow = min([m - 5.0 * s for m, s in zip(means, stdDevs)]) highHigh = max([m + 5.0 * s for m, s in zip(means, stdDevs)]) minBinSize = min([x["minBinSize"] for x in dataStats]) n = int(math.ceil((high - low) / minBinSize)) interval = (high - low) / n #Print the cdfs and pdfs of the data to be compared. origCdfAndPdfArray = [] origCdfAndPdfArray.append(["x"]) for name in names: origCdfAndPdfArray.append([name + '_cdf']) origCdfAndPdfArray.append([name + '_pdf']) for i in range(n): x = low + interval * i origCdfAndPdfArray[0].append(x) k = 1 for stats, cdf, pdf, name in functions: origCdfAndPdfArray[k].append(cdf(x)) origCdfAndPdfArray[k + 1].append(pdf(x)) k += 2 retDict["cdf_and_pdf_arrays"] = origCdfAndPdfArray if len(means) < 2: return cdfAreaDifference = mathUtils.simpson( lambda x: abs(cdfs[1](x) - cdfs[0](x)), lowLow, highHigh, integrationSegments) def firstMomentSimpson(f, a, b, n): """ Compute the first simpson method @ In, f, method, the function f(x) @ In, a, float, lower bound @ In, b, float, upper bound @ In, n, int, the number of discretizations @ Out, firstMomentSimpson, float, the moment """ return mathUtils.simpson(lambda x: x * f(x), a, b, n) #print a bunch of comparison statistics pdfCommonArea = mathUtils.simpson(lambda x: min(pdfs[0](x), pdfs[1](x)), lowLow, highHigh, integrationSegments) for i in range(len(pdfs)): pdfArea = mathUtils.simpson(pdfs[i], lowLow, highHigh, integrationSegments) retDict['pdf_area_' + names[i]] = pdfArea dataStats[i]["pdf_area"] = pdfArea retDict['cdf_area_difference'] = cdfAreaDifference retDict['pdf_common_area'] = pdfCommonArea dataStats[0]["cdf_area_difference"] = cdfAreaDifference dataStats[0]["pdf_common_area"] = pdfCommonArea if fZStats: def fZ(z): """ Compute f(z) with a simpson rule @ In, z, float, the coordinate @ Out, fZ, the f(z) """ return mathUtils.simpson(lambda x: pdfs[0](x) * pdfs[1](x - z), lowLow, highHigh, 1000) midZ = means[0] - means[1] lowZ = midZ - 3.0 * max(stdDevs[0], stdDevs[1]) highZ = midZ + 3.0 * max(stdDevs[0], stdDevs[1]) #print the difference function table. fZTable = [["z"], ["f_z(z)"]] zN = 20 intervalZ = (highZ - lowZ) / zN for i in range(zN): z = lowZ + intervalZ * i fZTable[0].append(z) fZTable[1].append(fZ(z)) retDict["f_z_table"] = fZTable sumFunctionDiff = mathUtils.simpson(fZ, lowZ, highZ, 1000) firstMomentFunctionDiff = firstMomentSimpson(fZ, lowZ, highZ, 1000) varianceFunctionDiff = mathUtils.simpson( lambda x: ((x - firstMomentFunctionDiff)**2) * fZ(x), lowZ, highZ, 1000) retDict['sum_function_diff'] = sumFunctionDiff retDict['first_moment_function_diff'] = firstMomentFunctionDiff retDict['variance_function_diff'] = varianceFunctionDiff return retDict