def _test_mesh(show=True): from weakly_admissable_meshes import polar_wam, box_wam from util.plot import Plot p = Plot("Weakly admissable mesh") p.add("Polar", *(polar_wam(8).T), color=p.color(0)) p.add("Box", *(box_wam(8).T), color=p.color(1)) if show: p.show(width=1000*.7, height=900*.7, append=True)
def test_random_cdf(display=True): if not display: return from util.plot import Plot p = Plot("") for nodes in range(1, 100): f = cdf() p.add_func(f"Random PDF {nodes}", f.derivative, f(), color=p.color(nodes - 1), group=nodes) # p.add(f"Points {nodes}", *list(zip(*f.nodes)), # color=p.color(nodes-1,alpha=.3), group=nodes) p.show(show=False) print() p = Plot("") for nodes in range(1, 30): f = cdf(nodes=nodes) p.add_func(f"Random {nodes} node CDF", f, f(), color=p.color(nodes - 1), group=nodes) p.add(f"Points {nodes}", *list(zip(*f.nodes)), color=p.color(nodes - 1, alpha=.3), group=nodes) p.show(append=True)
def test_support( model, low=0, upp=1, plot_points=3000, p=None, fun=lambda x: 3 * x[0] + .5 * np.cos(8 * x[0]) + np.sin(5 * x[-1]), N=20, D=2, random=True, seed=0): # Force D to be 2 D = 2 np.random.seed(seed) # Generate x points if random: x = np.random.random(size=(N, D)) else: N = int(round(N**(1 / D))) x = np.array( [r.flatten() for r in np.meshgrid(*[np.linspace(0, 1, N)] * D)]).T # Calculate response values y = np.array([fun(v) for v in x]) # Fit the model to the points model.fit(x, y) # Generate the plot from util.plotly import Plot if type(p) == type(None): p = Plot() p.add("Training Points", *x.T, color=p.color(len(x))) for i in range(len(x)): name = f"{i+1}" p.add(name, [x[i][0]], [x[i][1]], group=name) def supported(pt): pts, wts = model.points_and_weights(pt) return (i in pts) p.add_region(name + " region", supported, *([(low - .1, upp + .1)] * D), color=p.color(p.color_num), group=name, plot_points=plot_points, show_in_legend=False) # p.add_func(str(model), model, *([(low-.1,upp+.1)]*D), # plot_points=plot_points, vectorized=True) return p, x, y
def _test_mpca(display=False): if display: print() print("-" * 70) print("Begin tests for 'mpca'") GENERATE_APPROXIMATIONS = True BIG_PLOTS = False SHOW_2D_POINTS = False import random # Generate some points for testing. np.random.seed(4) # 4 random.seed(0) # 0 rgen = np.random.RandomState(1) # 10 n = 100 points = (rgen.rand(n, 2) - .5) * 2 # points *= np.array([.5, 1.]) # Create some testing functions (for learning different behaviors) funcs = [ lambda x: x[1], # Linear on y lambda x: abs(x[0] + x[1]), # "V" function on 1:1 diagonal lambda x: abs(2 * x[0] + x[1]), # "V" function on 2:1 diagonal lambda x: x[0]**2, # Quadratic on x lambda x: (x[0] + x[1])**2, # Quadratic on 1:1 diagonal lambda x: (2 * x[0] + x[1])**3, # Cubic on 2:1 diagonal lambda x: (x[0]**3), # Cubic on x lambda x: rgen.rand(), # Random function ] # Calculate the response values associated with each function. responses = np.vstack(tuple(tuple(map(f, points)) for f in funcs)).T # Reduce to just the first function choice = 3 func = funcs[choice] response = responses[:, choice] # Run the princinple response analysis function. components, values = mpca(points, response) values /= np.sum(values) conditioner = np.matmul(components, np.diag(values)) if display: print() print("Components") print(components) print() print("Values") print(values) print() print("Conditioner") print(conditioner) print() components = np.array([[1.0, 0.], [0., 1.]]) values = normalize_error(np.matmul(points, components.T), response, abs_diff) values /= np.sum(values) if display: print() print() print("True Components") print(components) print() print("True Values") print(values) print() # Generate a plot of the response surfaces. from util.plot import Plot, multiplot if display: print("Generating plots of source function..") # Add function 1 p1 = Plot() p1.add("Points", *(points.T), response, opacity=.8) p1.add_func("Surface", func, [-1, 1], [-1, 1], plot_points=100) if GENERATE_APPROXIMATIONS: from util.approximate import NearestNeighbor, Delaunay, condition p = Plot() # Add the source points and a Delaunay fit. p.add("Points", *(points.T), response, opacity=.8) p.add_func("Truth", func, [-1, 1], [-1, 1]) # Add an unconditioned nearest neighbor fit. model = NearestNeighbor() model.fit(points, response) p.add_func("Unconditioned Approximation", model, [-1, 1], [-1, 1], mode="markers", opacity=.8) # Generate a conditioned approximation model = condition(NearestNeighbor, method="MPCA")() model.fit(points, response) p.add_func("Best Approximation", model, [-1, 1], [-1, 1], mode="markers", opacity=.8) if display: p.plot(show=False, height=400, width=650) if display: print("Generating metric principle components..") # Return the between vectors and the differences between those points. def between(x, y, unique=True): vecs = [] diffs = [] for i1 in range(x.shape[0]): start = i1 + 1 if unique else 0 for i2 in range(start, x.shape[0]): if (i1 == i2): continue vecs.append(x[i2] - x[i1]) diffs.append(y[i2] - y[i1]) return np.array(vecs), np.array(diffs) # Plot the between slopes to verify they are working. # Calculate the between slopes vecs, diffs = between(points, response) vec_lengths = np.sqrt(np.sum(vecs**2, axis=1)) between_slopes = diffs / vec_lengths bs = ((vecs.T / vec_lengths) * between_slopes).T # Extrac a random subset for display size = 100 random_subset = np.arange(len(bs)) rgen.shuffle(random_subset) bs = bs[random_subset[:size], :] # Normalize the between slopes so they fit on the plot max_bs_len = np.max(np.sqrt(np.sum(bs**2, axis=1))) bs /= max_bs_len # Get a random subset of the between slopes and plot them. p2 = Plot("", "Metric PCA on Z", "") p2.add("Between Slopes", *(bs.T), color=p2.color(4, alpha=.4)) if SHOW_2D_POINTS: # Add the points and transformed points for demonstration. new_pts = np.matmul(np.matmul(conditioner, points), np.linalg.inv(components)) p2.add("Original Points", *(points.T)) p2.add("Transformed Points", *(new_pts.T), color=p2.color(6, alpha=.7)) # Add the principle response components for i, (vec, m) in enumerate(zip(components, values)): vec = vec * m p2.add(f"PC {i+1}", [0, vec[0]], [0, vec[1]], mode="lines") ax, ay = (vec / sum(vec**2)**.5) * 3 p2.add_annotation(f"{m:.2f}", vec[0], vec[1]) p3 = Plot("", "PCA on X", "") p3.add("Points", *(points.T), color=p3.color(4, alpha=.4)) # Add the normal principle components components, values = pca(points) values /= np.sum(values) for i, (vec, m) in enumerate(zip(components, values)): vec = vec * m p3.add(f"PC {i+1}", [0, vec[0]], [0, vec[1]], mode="lines") ax, ay = (vec / sum(vec**2)**.5) * 3 p3.add_annotation(f"{m:.2f}", vec[0], vec[1]) if BIG_PLOTS: if display: p1.plot(file_name="source_func.html", show=False) if display: p2.plot(append=True, x_range=[-8, 8], y_range=[-5, 5]) else: # Make the plots (with manual ranges) p1 = p1.plot(html=False, show_legend=False) p2 = p2.plot(html=False, x_range=[-1, 1], y_range=[-1, 1], show_legend=False) p3 = p3.plot(html=False, x_range=[-1, 1], y_range=[-1, 1], show_legend=False) # Generate the multiplot of the two side-by-side figures if display: multiplot([p1, p2, p3], height=126, width=650, append=True) if display: print("-" * 70)
print() print("Values") print(values) print() print("Conditioner") print(conditioner) print() # Generate a plot of the response surfaces. from util.plot import Plot, multiplot print("Generating plots of source function..") # Add function 1 p1 = Plot(font_family="times") p1.add("Points", *(points.T), response, opacity=.8) p1.add_func("Surface", func, [-1,1], [-1,1], plot_points=1000, color=p1.color(1)) if GENERATE_APPROXIMATIONS: from util.algorithms import NearestNeighbor model = NearestNeighbor() model.fit(points, response) p1.add_func("Unconditioned Approximation", model, [-1,1], [-1,1], mode="markers", opacity=.8) # Generate a conditioned approximation model = NearestNeighbor() model.fit(np.matmul(points, conditioner), response) approx = lambda x: model(np.matmul(x, conditioner)) p1.add_func("Best Approximation", approx, [-1,1], [-1,1], mode="markers", opacity=.8)
if (data_name == "yelp") and (a != "KNN10"): continue if (data_name == "mnist") and (a != "KNN1"): continue if (data_name == "cifar") and (a != "KNN10"): continue # Break up data by dimension y_axis = "Count" if data_name == "yelp" else "" p = Plot("", data_name + " errors", y_axis, font_size=20, font_family="times") plots.append(p) for method in ("MPCA", "PCA"): for dim in dims: # Color by method if method not in colors: colors[method] = p.color(len(colors)) color = colors[method] # Reduce data d = all_data[all_data["Data"] == data_name] d = d[d["Method"] == method] d = d[d["Sample Ratio"] == sample_ratio] d = d[d["Algorithm"] == a] d = d[d["Dimension"] == dim] # Skip empty data sets. if len(d) == 0: continue # cdf = cdf_fit_func(d[0,"Errors"]) # p.add_func(method+"-"+a+"-"+str(dim), cdf, cdf(), group=d) plot_name = method #+"-"+a+"-"+str(dim) show = (dim == dims[0]) and (data_name == "yelp") if data_name == "yelp":
# ============================================== # Display the boxes that were computed # ============================================== p = Plot() # Get the extreme points. min_x = np.min(x[:,0]) - .1 max_x = np.max(x[:,0]) + .1 min_y = np.min(x[:,1]) - .1 max_y = np.max(x[:,1]) + .1 # Get the box edges (about the centers). boxes = model.box_sizes.T boxes[:,0] = np.where(boxes[:,0] != -1, boxes[:,0], min_x) boxes[:,1] = np.where(boxes[:,1] != -1, boxes[:,1], min_y) boxes[:,2] = np.where(boxes[:,2] != -1, boxes[:,2], max_x) boxes[:,3] = np.where(boxes[:,3] != -1, boxes[:,3], max_y) # Add boxes to plot. for i,(pt,bounds) in enumerate(zip(x, boxes)): shifts = np.array([[- boxes[i,0], - boxes[i, 1]], [ boxes[i,2], - boxes[i, 1]], [ boxes[i,2], boxes[i, 3]], [- boxes[i,0], boxes[i, 3]], [- boxes[i,0], - boxes[i, 1]] ]) box = (pt + shifts) p.add(f"Box {i+1}", *(box.T), color=p.color(i), mode="lines") for i,(pt,bounds) in enumerate(zip(x, boxes)): p.add("", [pt[0]], [pt[1]], color=p.color(i), show_in_legend=False) p.show(append=True) exit()
def modes(data, confidence=.99, tol=1/1000): from util.optimize import zero num_samples = len(data) error = 2*samples(num_samples, confidence=confidence) cdf = cdf_fit(data, fit="cubic") print("error: ",error) # Find all of the zeros of the derivative (mode centers / dividers) checks = np.linspace(cdf.min, cdf.max, np.ceil(1/tol)) second_deriv = cdf.derivative.derivative deriv_evals = second_deriv(checks) modes = [i for i in range(1, len(deriv_evals)) if (deriv_evals[i-1] * deriv_evals[i] <= 0) and (deriv_evals[i-1] >= deriv_evals[i])] antimodes = [i for i in range(1, len(deriv_evals)) if (deriv_evals[i-1] * deriv_evals[i] <= 0) and (deriv_evals[i-1] < deriv_evals[i])] # Compute exact modes and antimodes using a zero-finding function. modes = [zero(second_deriv, checks[i-1], checks[i]) for i in modes] antimodes = [zero(second_deriv, checks[i-1], checks[i]) for i in antimodes] original_antimodes = antimodes[:] # Fix the bounds of the antimodes to match the distribution. if modes[0] < antimodes[0]: antimodes = [cdf.min] + antimodes else: antimodes[0] = cdf.min if modes[-1] > antimodes[-1]: antimodes += [cdf.max] else: antimodes[-1] = cdf.max # Make sure that there is an antimode between each mode. for i in range(len(modes)): if antimodes[i] > modes[i]: # Update the next antimode with this one (as long as it's not the max). if (i < len(modes)-1): antimodes[i+1] = (antimodes[i] + antimodes[i+1]) / 2 # Always update this antimode to properly be LESS than the mode. antimodes[i] = (modes[i] + modes[i-1]) / 2 print("len(modes): ",len(modes)) print("len(antimodes): ",len(antimodes)) # Define a function that counts the number of modes thta are too small. def count_too_small(): return sum( (cdf(upp) - cdf(low)) < error for (low,upp) in zip(antimodes[:-1],antimodes[1:]) ) # Show PDF from util.plot import Plot p = Plot() pdf = pdf_fit(cdf.inverse(np.random.random((1000,)))) # Loop until all modes are big enough to be accepted given error tolerance. step = 1 while count_too_small() > 0: print() print("step: ",step, (len(modes), len(antimodes))) f = len(modes) p.add_func("PDF", pdf, cdf(), color=p.color(1), frame=f, show_in_legend=(step==1)) # Generate the mode lines. mode_lines = [[],[]] for z in modes: mode_lines[0] += [z,z,None] mode_lines[1] += [0,.2,None] p.add("modes", *mode_lines, color=p.color(0), mode="lines", group="modes", show_in_legend=(z==modes[0] and step==1), frame=f) # Generate the antimode lines. anti_lines = [[],[]] for z in antimodes: anti_lines[0] += [z,z,None] anti_lines[1] += [0,.2,None] p.add("seperator", *anti_lines, color=p.color(3,alpha=.3), mode="lines", group="seperator", show_in_legend=(z==antimodes[0] and (step==1)), frame=f) step += 1 # Compute the densities and the sizes of each mode. sizes = [cdf(antimodes[i+1]) - cdf(antimodes[i]) for i in range(len(modes))] densities = [(cdf(antimodes[i+1]) - cdf(antimodes[i])) / (antimodes[i+1] - antimodes[i]) for i in range(len(modes))] # Compute those modes that have neighbors that are too small. to_grow = [i for i in range(len(modes)) if (i > 0 and sizes[i-1] < error) or (i < len(sizes)-1 and sizes[i+1] < error)] if len(to_grow) == 0: break print("modes: ",modes) print("antimodes: ",antimodes) print("sizes: ",sizes) print("densities: ",densities) print("to_grow: ",to_grow) # Sort the modes to be grown by their size, largest first. to_grow = sorted(to_grow, key=lambda i: -densities[i]) # Keep track of the modes that have already been absorbed. preference = {} taken = set() conflicts = set() modes_to_remove = [] anti_to_remove = [] while len(to_grow) > 0: i = to_grow.pop(0) # Pick which of the adjacent nodes to absorb. to_absorb = None if (i < len(modes)-1) and (sizes[i+1] < error): direction = 1 to_absorb = i + 1 if (i > 0) and (sizes[i-1] < error): # If there wasn't a right mode, take the left by default. if (to_absorb == None): direction = -1 to_absorb = i - 1 # Otherwise we have to pick based on the density similarity. elif (abs(modes[i-1]-modes[i]) < abs(modes[i+1]-modes[i])): # Take the other one if its density is more similar. direction = -1 to_absorb = i - 1 # If there is no good option to absorb, the skip. if (to_absorb in preference): continue # Record the preferred pick of this mode. preference[i] = (direction, to_absorb) # If this mode is already absorbed, then add it to conflict list. if to_absorb in taken: conflicts.add( to_absorb ) # Remove the ability to 'absorb' from modes getting absorbed. if to_absorb in to_grow: to_grow.remove(to_absorb) # Add the absorbed value to the set of "taken" modes. taken.add(to_absorb) # Resolve conflicts by giving absorbed modes to closer modes. for i in sorted(conflicts, key=lambda i: -densities[i]): if (abs(modes[i-1] - modes[i]) < abs(modes[i+1] - modes[i])): preference.pop(i+1) else: preference.pop(i-1) # Update the boundaries for i in sorted(preference, key=lambda i: -densities[i]): direction, to_absorb = preference[i] # Update the boundary of this mode. antimodes[i+(direction>0)] = antimodes[to_absorb + (direction>0)] # Update the "to_remove" lists. anti_to_remove.append( antimodes[to_absorb + (direction>0)] ) modes_to_remove.append( modes[to_absorb] ) # Remove the modes and antimodes that were merged. for m in modes_to_remove: modes.remove(m) for a in anti_to_remove: antimodes.remove(a) # Update the remaining antimodes to be nearest to the middle # of the remaining modes (making them representative dividers). for i in range(len(modes)-1): middle = (modes[i] + modes[i+1]) / 2 closest = np.argmin([abs(oam - middle) for oam in original_antimodes]) antimodes[i+1] = original_antimodes[closest] f = len(modes) p.add_func("PDF", pdf, cdf(), color=p.color(1), frame=f, show_in_legend=(step==1)) # Generate the mode lines. mode_lines = [[],[]] for z in modes: mode_lines[0] += [z,z,None] mode_lines[1] += [0,.2,None] p.add("modes", *mode_lines, color=p.color(0), mode="lines", group="modes", show_in_legend=(z==modes[0] and step==1), frame=f) # Generate the antimode lines. anti_lines = [[],[]] for z in antimodes: anti_lines[0] += [z,z,None] anti_lines[1] += [0,.2,None] p.add("seperator", *anti_lines, color=p.color(3,alpha=.3), mode="lines", group="seperator", show_in_legend=(z==antimodes[0] and (step==1)), frame=f) p.show(append=True, y_range=[0,.15]) p = Plot() p.add_func("CDF", cdf, cdf(), color=p.color(1)) for z in modes: p.add("modes", [z,z], [0,1], color=p.color(0), mode="lines", group="modes", show_in_legend=(z==modes[0])) for z in antimodes: p.add("seperator", [z,z], [0,1], color=p.color(3), mode="lines", group="sep", show_in_legend=(z==antimodes[0])) p.show(append=True)
train_x = train_mat[:, in_idxs] test_x = test_mat[:, out_idxs] m = Voronoi() m.fit(train_x) lengths += [len(ids) for ids, wts in m(test_x)] save(lengths) print("len(lengths): ", len(lengths)) from util.plot import Plot from util.stats import cdf_fit p = Plot("Distribution of Number of Influencers", "Number of records used to make prediction", "CDF value") cdf = cdf_fit(lengths) p.add_func("lengths", cdf, cdf(), show_in_legend=False, color=p.color(1)) p.show() exit() from util.approximate import Voronoi, NearestNeighbor, NeuralNetwork, DecisionTree from util.approximate import ShepMod, BoxMesh, LSHEP, Delaunay from util.approximate.testing import test_plot # model = NearestNeighbor() # model = Voronoi() # model = DecisionTree() # model = NeuralNetwork() # model = BoxMesh() # model = LSHEP()
def modes(data, confidence=.99, tol=1/1000): from util.optimize import zero num_samples = len(data) error = 2*samples(num_samples, confidence=confidence) print() print("Smallest allowed mode: ",error) print() # Get the CDF points (known to be true based on data). x, y = cdf_points(data) cdf = cdf_fit((x,y), fit="linear") x, y = x[1:], y[1:] # Generate the candidate break points based on linear interpolation. slopes = [(y[i+1] - y[i]) / (x[i+1] - x[i]) for i in range(len(x)-1)] candidates = [i for i in range(1,len(slopes)-1) if (slopes[i] < slopes[i-1]) and (slopes[i] < slopes[i+1])] # Sort candidates by their 'width', the widest being the obvious divisors. candidates = sorted(candidates, key=lambda i: -(x[i+1] - x[i])) print("candidates: ",candidates) print("slopes: ",[slopes[c] for c in candidates]) # Break the data at candidates as much as can be done with confidence. breaks = [min(x), max(x)] sizes = [1.] chosen = [] print() print("breaks: ",breaks) print("sizes: ",sizes) print("chosen: ",chosen) print() # Loop until there are no candidate break points left. while len(candidates) > 0: new_break_idx = candidates.pop(0) new_break = (x[new_break_idx + 1] + x[new_break_idx]) / 2 b_idx = np.searchsorted(breaks, new_break, side="right") # Compute the CDF values at the upper, break, and lower positions. upp = cdf(breaks[b_idx+1]) if (b_idx < len(breaks)-1) else cdf.max mid = cdf(new_break) low = cdf(breaks[b_idx-1]) print() print("new_break: ", new_break) print("b_idx: ",b_idx) print(" upp: ",upp, upp - mid) print(" mid: ",mid) print(" low: ",low, mid - low) # Compute the size of the smallest mode resulting from the break. smallest_result = min(upp - mid, mid - low) # Skip the break if it makes a mode smaller than error. if smallest_result < error: continue print() print("Num_modes: ", len(sizes)) print("breaks: ", breaks) print("sizes: ", sizes) print() # Update the "breaks" and "sizes" lists. breaks.insert(b_idx, new_break) sizes.insert(b_idx, upp - mid) sizes[b_idx-1] = mid - low chosen.append(new_break_idx) # From the "breaks" and "sizes", construct a list of "modes". # Consider the most dense point between breaks the "mode". modes = [] for i in range(len(chosen)): low = 0 if (i == 0) else chosen[i-1] upp = len(slopes)-1 if (i == len(chosen)-1) else chosen[i+1] mode_idx = low + np.argmax(slopes[low:upp]) modes.append( (x[mode_idx+1] + x[mode_idx]) / 2 ) from util.plot import Plot p = Plot() pdf = pdf_fit(data) p.add_func("PDF", pdf, pdf(), color=p.color(1)) # Generate the mode lines. mode_lines = [[],[]] for z in modes: mode_lines[0] += [z,z,None] mode_lines[1] += [0,.2,None] p.add("modes", *mode_lines, color=p.color(0), mode="lines", group="modes") # Generate the antimode lines. break_lines = [[],[]] for z in breaks: break_lines[0] += [z,z,None] break_lines[1] += [0,.2,None] p.add("seperator", *break_lines, color=p.color(3,alpha=.3), mode="lines", group="seperator") p.show() # Show CDF p = Plot() pdf = pdf_fit(data) p.add_func("CDF", cdf, cdf(), color=p.color(1)) # Generate the mode lines. mode_lines = [[],[]] for z in modes: mode_lines[0] += [z,z,None] mode_lines[1] += [0,1,None] p.add("modes", *mode_lines, color=p.color(0), mode="lines", group="modes") # Generate the antimode lines. break_lines = [[],[]] for z in breaks: break_lines[0] += [z,z,None] break_lines[1] += [0,1,None] p.add("seperator", *break_lines, color=p.color(3,alpha=.3), mode="lines", group="seperator") p.show(append=True)