from pylab import * import current as pc rc("figure", figsize=[8., 4.]) rc("figure.subplot", left=.08, top=.93, right=.98) seed(1000) dist = pc.Iid(pc.Normal(), 2) nodes, weights = pc.quadgen(1, dist, rule="G") print nodes # [[-1. -1. 1. 1.] # [-1. 1. -1. 1.]] print weights # [ 0.25 0.25 0.25 0.25] #end dist = pc.Iid(pc.Uniform(), 2) nodes1, weights1 = pc.quadgen([3, 5], dist, rule="C", sparse=True) print len(weights1) # 105 nodes2, weights2 = pc.quadgen([3, 5], dist, rule="C", growth=True) print len(weights2) # 297 #end subplot(122) scatter(nodes2[0], nodes2[1], marker="s", s=50 * sqrt(weights2),
from pylab import * import current as pc rc("figure", figsize=[8.,4.]) rc("figure.subplot", left=.08, top=.95, right=.98) seed(1000) dist = pc.Laplace(pi, .5) f = pc.lazy_eval(dist.pdf) #end q0, w0 = pc.quadgen(2, [-10,10]) print q0 # [[-10. 0. 10.]] #end comp = [-10, 0, 10] q, w = pc.quadgen(2, [-10,10], composite=comp) print q # [[-10. -5. 0. 5. 10.]] #end def eval_errors(q, f): errors = [0.] for i in xrange(1, len(q[0])-1): a,b,c = q[0,i-1:i+2] f_approx = ((f(c)-f(a))*b + f(a)*c-f(c)*a)/(c-a) f_diff = abs(f(q[0,i]) - f_approx)
from pylab import * import current as pc dist = pc.Iid(pc.Normal(), 2) # end nodes, weights = pc.quadgen(2, dist, rule="G") print nodes # [[-1.73205081 -1.73205081 -1.73205081 0. 0. # 0. 1.73205081 1.73205081 1.73205081] # [-1.73205081 0. 1.73205081 -1.73205081 0. # 1.73205081 -1.73205081 0. 1.73205081]] print weights # [ 0.02777778 0.11111111 0.02777778 0.11111111 0.44444444 # 0.11111111 0.02777778 0.11111111 0.02777778] # end orth = pc.orth_ttr(2, dist) print orth # [1.0, q1, q0, q1^2-1.0, q0q1, q0^2-1.0] # end def model_solver(q): return [q[0] * q[1], q[0] * e ** -q[1] + 1] solves = [model_solver(q) for q in nodes.T] # end approx = pc.fitter_quad(orth, nodes, weights, solves)
# [ 0.14367935 0.22228764] # [ 0.28735869 0.44457529]] #end mu = [-1.0, -2.0, -1.0, -2.0] Cov = [[1.0, 0.5, 0.5, 0.5], [0.5, 1.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.5], [0.5, 0.5, 0.5, 1.0]] Q = pc.MvLognormal(mu, Cov) #end Z = pc.Iid(pc.Normal(), 4) orth_poly = pc.orth_ttr(2, Z) #end nodes, weights = pc.quadgen(3, Z, gaussian=True, sparse=True) print len(weights) # 137 #end mapped_nodes = Q.inv(Z.fwd(nodes)) t = linspace(0, 10, 100) evaluations = [model_solver(t, q) for q in mapped_nodes.T] #end model_approx = pc.fitter_quad(orth_poly, nodes, weights, evaluations) #end samples_Z = Z.sample(100) samples_Y = model_approx(*samples_Z)
from pylab import * import current as pc rc("figure", figsize=[8.,4.]) rc("figure.subplot", left=.08, top=.93, right=.98) seed(1000) dist = pc.Iid(pc.Normal(), 2) nodes, weights = pc.quadgen(1, dist, rule="G") print nodes # [[-1. -1. 1. 1.] # [-1. 1. -1. 1.]] print weights # [ 0.25 0.25 0.25 0.25] #end dist = pc.Iid(pc.Uniform(), 2) nodes1, weights1 = pc.quadgen([3,5], dist, rule="C", sparse=True) print len(weights1) # 105 nodes2, weights2 = pc.quadgen([3,5], dist, rule="C", growth=True) print len(weights2) # 297 #end subplot(122) scatter(nodes2[0], nodes2[1], marker="s", s=50*sqrt(weights2), alpha=.7, color="k") xlabel(r"$q_0$")
model_solver = pc.lazy_eval(model_solver) #end current = array([1, 1, 1, 1, 1]) current_error = inf #end for step in range(10): for direction in eye(len(dist), dtype=int): #end orth = pc.orth_ttr(current + direction, dist) nodes, weights = pc.quadgen(current + direction, dist, rule="C", growth=True) vals = [model_solver(q) for q in nodes.T] #end residuals = pc.cross_validate(orth, nodes, vals, folds=5) error = sum(residuals**2) #end if error < current_error: current_error = error proposed_dir = current + direction #end current = proposed_dir #end
from pylab import * import current as pc rc("figure", figsize=[8.,4.]) rc("figure.subplot", left=.08, top=.95, right=.98) seed(1000) nodes, weights = pc.quadgen(2, [0,1]) print nodes # [[ 0. 0.5 1. ]] print weights # [ 0.16666667 0.66666667 0.16666667] #end dist = pc.Beta(2,2) nodes, weights = pc.quadgen(3, dist) print nodes # [[ 0.14644661 0.5 0.85355339]] print weights # [ 0.2 0.6 0.2] #end nodes, weights = pc.quadgen(2, dist, rule="G") print nodes # [[ 0.17267316 0.5 0.82732684]] print weights # [ 0.23333333 0.53333333 0.23333333] #end nodes, weights = pc.quadgen(1, [0,1], rule="E", composite=2)
#end mu = [-1.0, -2.0, -1.0, -2.0] Cov = [[1.0, 0.5, 0.5, 0.5], [0.5, 1.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.5], [0.5, 0.5, 0.5, 1.0]] Q = pc.MvLognormal(mu, Cov) #end Z = pc.Iid(pc.Normal(), 4) orth_poly = pc.orth_ttr(2, Z) #end nodes, weights = pc.quadgen(3, Z, gaussian=True, sparse=True) print len(weights) # 137 #end mapped_nodes = Q.inv(Z.fwd(nodes)) t = linspace(0,10,100) evaluations = [model_solver(t, q) for q in mapped_nodes.T] #end model_approx = pc.fitter_quad(orth_poly, nodes, weights, evaluations) #end samples_Z = Z.sample(100) samples_Y = model_approx(*samples_Z)
from pylab import * import current as pc rc("figure", figsize=[8., 4.]) rc("figure.subplot", left=.08, top=.95, right=.98) seed(1000) dist = pc.Laplace(pi, .5) f = pc.lazy_eval(dist.pdf) #end q0, w0 = pc.quadgen(2, [-10, 10]) print q0 # [[-10. 0. 10.]] #end comp = [-10, 0, 10] q, w = pc.quadgen(2, [-10, 10], composite=comp) print q # [[-10. -5. 0. 5. 10.]] #end def eval_errors(q, f): errors = [0.] for i in xrange(1, len(q[0]) - 1): a, b, c = q[0, i - 1:i + 2] f_approx = ((f(c) - f(a)) * b + f(a) * c - f(c) * a) / (c - a)
from pylab import * import current as pc dist_main = pc.MvNormal([0, 0], [[1, .5], [.5, 1]]) #end dist_aux = pc.Iid(pc.Normal(), 2) #end orth, norms = pc.orth_ttr(2, dist_aux, retall=True) print orth # [1.0, q1, q0, q1^2-1.0, q0q1, q0^2-1.0] #end nodes_aux, weights = pc.quadgen(3, dist_aux, rule="G") #end function = lambda q: q[0] * e**-q[1] + 1 #end nodes_main = dist_main.inv(dist_aux.fwd(nodes_aux)) solves = [function(q) for q in nodes_main.T] #end approx = pc.fitter_quad(orth, nodes_aux, weights, solves, norms=norms) print pc.E(approx, dist_aux) # 0.175824752014 #end
def model_solver(q): return q[0]*e**-q[1]/(q[2]+1) + sin(q[3]) model_solver = pc.lazy_eval(model_solver) #end current = array([1,1,1,1,1]) current_error = inf #end for step in range(10): for direction in eye(len(dist), dtype=int): #end orth = pc.orth_ttr(current+direction, dist) nodes, weights = pc.quadgen(current+direction, dist, rule="C", growth=True) vals = [model_solver(q) for q in nodes.T] #end residuals = pc.cross_validate(orth, nodes, vals, folds=5) error = sum(residuals**2) #end if error < current_error: current_error = error proposed_dir = current+direction #end current = proposed_dir #end