本文整理汇总了Python中pysgpp.DataVector类的典型用法代码示例。如果您正苦于以下问题:Python DataVector类的具体用法?Python DataVector怎么用?Python DataVector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DataVector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: computeMoments
def computeMoments(self, ts=None):
names = ['time',
'iteration',
'grid_size',
'mean',
'meanDiscretizationError',
'var',
'varDiscretizationError']
# parameters
ts = self.__samples.keys()
nrows = len(ts)
ncols = len(names)
data = DataMatrix(nrows, ncols)
v = DataVector(ncols)
row = 0
for t in ts:
v.setAll(0.0)
v[0] = t
v[1] = 0
v[2] = len(self.__samples[t].values())
v[3], v[4] = self.mean(ts=[t])
v[5], v[6] = self.var(ts=[t])
# write results to matrix
data.setRow(row, v)
row += 1
return {'data': data,
'names': names}
示例2: testOperationTest_test
def testOperationTest_test(self):
from pysgpp import Grid, DataVector, DataMatrix
factory = Grid.createLinearBoundaryGrid(1)
gen = factory.createGridGenerator()
gen.regular(1)
alpha = DataVector(factory.getStorage().size())
data = DataMatrix(1,1)
data.setAll(0.25)
classes = DataVector(1)
classes.setAll(1.0)
testOP = factory.createOperationTest()
alpha[0] = 0.0
alpha[1] = 0.0
alpha[2] = 1.0
c = testOP.test(alpha, data, classes)
self.failUnless(c > 0.0)
alpha[0] = 0.0
alpha[1] = 0.0
alpha[2] = -1.0
c = testOP.test(alpha, data, classes)
self.failUnless(c == 0.0)
示例3: mean
def mean(self, grid, alpha, U, T):
r"""
Extraction of the expectation the given sparse grid function
interpolating the product of function value and pdf.
\int\limits_{[0, 1]^d} f_N(x) * pdf(x) dx
"""
# extract correct pdf for moment estimation
vol, W = self._extractPDFforMomentEstimation(U, T)
D = T.getTransformations()
# compute the integral of the product
gs = grid.getStorage()
acc = DataVector(gs.size())
acc.setAll(1.)
tmp = DataVector(gs.size())
err = 0
# run over all dimensions
for i, dims in enumerate(W.getTupleIndices()):
dist = W[i]
trans = D[i]
# get the objects needed for integration the current dimensions
gpsi, basisi = project(grid, dims)
if isinstance(dist, SGDEdist):
# if the distribution is given as a sparse grid function we
# need to compute the bilinear form of the grids
# accumulate objects needed for computing the bilinear form
gpsj, basisj = project(dist.grid, range(len(dims)))
# compute the bilinear form
bf = BilinearGaussQuadratureStrategy()
A, erri = bf.computeBilinearFormByList(gpsi, basisi,
gpsj, basisj)
# weight it with the coefficient of the density function
self.mult(A, dist.alpha, tmp)
else:
# the distribution is given analytically, handle them
# analytically in the integration of the basis functions
if isinstance(dist, Dist) and len(dims) > 1:
raise AttributeError('analytic quadrature not supported for multivariate distributions')
if isinstance(dist, Dist):
dist = [dist]
trans = [trans]
lf = LinearGaussQuadratureStrategy(dist, trans)
tmp, erri = lf.computeLinearFormByList(gpsi, basisi)
# print error stats
# print "%s: %g -> %g" % (str(dims), err, err + D[i].vol() * erri)
# import ipdb; ipdb.set_trace()
# accumulate the error
err += D[i].vol() * erri
# accumulate the result
acc.componentwise_mult(tmp)
moment = alpha.dotProduct(acc)
return vol * moment, err
示例4: generateLaplaceMatrix
def generateLaplaceMatrix(factory, level, verbose=False):
from pysgpp import DataVector
storage = factory.getStorage()
gen = factory.createGridGenerator()
gen.regular(level)
laplace = factory.createOperationLaplace()
# create vector
alpha = DataVector(storage.size())
erg = DataVector(storage.size())
# create stiffness matrix
m = DataVector(storage.size(), storage.size())
m.setAll(0)
for i in xrange(storage.size()):
# apply unit vectors
alpha.setAll(0)
alpha[i] = 1
laplace.mult(alpha, erg)
if verbose:
print erg, erg.sum()
m.setColumn(i, erg)
return m
示例5: estimate
def estimate(self, vol, grid, alpha, f, U, T):
r"""
Extraction of the expectation the given sparse grid function
interpolating the product of function value and pdf.
\int\limits_{[0, 1]^d} f(x) * pdf(x) dx
"""
# first: discretize f
fgrid, falpha, discError = discretize(grid, alpha, f, self.__epsilon,
self.__refnums, self.__pointsNum,
self.level, self.__deg, True)
# extract correct pdf for moment estimation
vol, W, pdfError = self.__extractDiscretePDFforMomentEstimation(U, T)
D = T.getTransformations()
# compute the integral of the product
gs = fgrid.getStorage()
acc = DataVector(gs.size())
acc.setAll(1.)
tmp = DataVector(gs.size())
for i, dims in enumerate(W.getTupleIndices()):
sgdeDist = W[i]
# accumulate objects needed for computing the bilinear form
gpsi, basisi = project(fgrid, dims)
gpsj, basisj = project(sgdeDist.grid, range(len(dims)))
A = self.__computeMassMatrix(gpsi, basisi, gpsj, basisj, W, D)
# A = ScipyQuadratureStrategy(W, D).computeBilinearForm(fgrid)
self.mult(A, sgdeDist.alpha, tmp)
acc.componentwise_mult(tmp)
moment = falpha.dotProduct(acc)
return vol * moment, discError[1] + pdfError
示例6: gradient_fun
def gradient_fun(self, params):
'''
Compute the gradient vector in the current state
'''
#import ipdb; ipdb.set_trace() #
gradient_array = np.empty((self.batch_size, self.grid.getSize()))
for sample_idx in xrange(self.batch_size):
x = self._lastseen[sample_idx, :self.dim]
y = self._lastseen[sample_idx, self.dim]
params_DV = DataVector(params)
gradient = DataVector(len(params_DV))
single_alpha = DataVector(1)
single_alpha[0] = 1
data_matrix = DataMatrix(x.reshape(1,-1))
mult_eval = createOperationMultipleEval(self.grid, data_matrix);
mult_eval.multTranspose(single_alpha, gradient);
residual = gradient.dotProduct(params_DV) - y;
gradient.mult(residual);
#import ipdb; ipdb.set_trace() #
gradient_array[sample_idx, :] = gradient.array()
return gradient_array
示例7: computeTrilinearFormByRow
def computeTrilinearFormByRow(self,
gpsk, basisk,
gpi, basisi,
gpj, basisj):
"""
Compute the trilinear form of two grid point with a list
of grid points
@param gpk: list of HashGridIndex
@param basisk: SG++ Basis for grid indices k
@param gpi: HashGridIndex
@param basisi: SG++ Basis for grid indices i
@param gpj: HashGridIndex
@param basisj: SG++ Basis for grid indices j
@return DataVector
"""
b = DataVector(len(gpsk))
b.setAll(1.0)
err = 0.
# run over all entries
for k, gpk in enumerate(gpsk):
# run over all dimensions
for d in xrange(gpi.dim()):
# compute trilinear form for one entry
value, erri = self.getTrilinearFormEntry(gpk, basisk,
gpi, basisi,
gpj, basisj,
d)
b[k] *= value
err += erri
return b, err
示例8: var
def var(self, grid, alpha, U, T, mean):
r"""
Estimate the expectation value using Monte-Carlo.
\frac{1}{N}\sum\limits_{i = 1}^N (f_N(x_i) - E(f))^2
where x_i \in \Gamma
"""
# init
_, W = self._extractPDFforMomentEstimation(U, T)
moments = np.zeros(self.__npaths)
vecMean = DataVector(self.__n)
vecMean.setAll(mean)
for i in xrange(self.__npaths):
samples = self.__getSamples(W, T, self.__n)
res = evalSGFunctionMulti(grid, alpha, samples)
res.sub(vecMean)
res.sqr()
# compute the moment
moments[i] = res.sum() / (len(res) - 1.)
# error statistics
err = np.Inf
# calculate moment
return np.sum(moments) / self.__npaths, err
示例9: plotSG3d
def plotSG3d(grid, alpha, n=50, f=lambda x: x):
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(0, 1, n)
Y = np.linspace(0, 1, n)
X, Y = np.meshgrid(X, Y)
Z = np.zeros(n * n).reshape(n, n)
for i in xrange(len(X)):
for j, (x, y) in enumerate(zip(X[i], Y[i])):
Z[i, j] = f(evalSGFunction(grid, alpha, DataVector([x, y])))
# get grid points
gs = grid.getStorage()
gps = np.zeros([gs.size(), 2])
p = DataVector(2)
for i in xrange(gs.size()):
gs.get(i).getCoords(p)
gps[i, :] = p.array()
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.scatter(gps[:, 0], gps[:, 1], np.zeros(gs.size()))
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# ax.set_zlim(0, 2)
fig.colorbar(surf, shrink=0.5, aspect=5)
return fig, ax, Z
示例10: currentDiagHess
def currentDiagHess(self, params):
#return np.ones(params.shape)
# if hasattr(self, 'H'):
# return self.H
# op_l2_dot = createOperationLTwoDotProduct(self.grid)
# self.H = np.empty((self.grid.getSize(), self.grid.getSize()))
# u = DataVector(self.grid.getSize())
# u.setAll(0.0)
# result = DataVector(self.grid.getSize())
# for grid_idx in xrange(self.grid.getSize()):
# u[grid_idx] = 1.0
# op_l2_dot.mult(u, result)
# self.H[grid_idx,:] = result.array()
# u[grid_idx] = 0.0
# self.H = np.diag(self.H).reshape(1,-1)
# return self.H
#import ipdb; ipdb.set_trace()
size = self._lastseen.shape[0]
data_matrix = DataMatrix(self._lastseen[:,:self.dim])
mult_eval = createOperationMultipleEval(self.grid, data_matrix);
params_DV = DataVector(self.grid.getSize())
params_DV.setAll(0.)
results_DV = DataVector(size)
self.H = np.zeros(self.grid.getSize())
for i in xrange(self.grid.getSize()):
params_DV[i] = 1.0
mult_eval.mult(params_DV, results_DV);
self.H[i] = results_DV.l2Norm()**2
params_DV[i] = 0.0
self.H = self.H.reshape(1,-1)/size
#import ipdb; ipdb.set_trace()
return self.H
示例11: testOperationB
def testOperationB(self):
from pysgpp import Grid, DataVector, DataMatrix
factory = Grid.createLinearBoundaryGrid(1)
gen = factory.createGridGenerator()
gen.regular(2)
alpha = DataVector(factory.getStorage().size())
p = DataMatrix(1,1)
beta = DataVector(1)
alpha.setAll(0.0)
p.set(0,0,0.25)
beta[0] = 1.0
opb = factory.createOperationB()
opb.mult(beta, p, alpha)
self.failUnlessAlmostEqual(alpha[0], 0.75)
self.failUnlessAlmostEqual(alpha[1], 0.25)
self.failUnlessAlmostEqual(alpha[2], 0.5)
self.failUnlessAlmostEqual(alpha[3], 1.0)
self.failUnlessAlmostEqual(alpha[4], 0.0)
alpha.setAll(0.0)
alpha[2] = 1.0
p.set(0,0, 0.25)
beta[0] = 0.0
opb.multTranspose(alpha, p, beta)
self.failUnlessAlmostEqual(beta[0], 0.5)
示例12: serializeToFile
def serializeToFile(self, memento, filename):
fstream = self.gzOpen(filename, "w")
try:
figure = plt.figure()
grid = memento
storage = grid.getStorage()
coord_vector = DataVector(storage.dim())
points = zeros([storage.size(), storage.dim()])
for i in xrange(storage.size()):
point = storage.get(i)
point.getCoords(coord_vector)
points[i] = [j for j in coord_vector.array()]
num_of_sublots = storage.dim()*(storage.dim()-1)/2
rows = int(ceil(sqrt(num_of_sublots)))
cols = int(floor(sqrt(num_of_sublots)))
i = 1
for x1 in xrange(1,storage.dim()):
for x2 in xrange(2,storage.dim()+1):
figure.add_subplot(rows*100 + cols*10 + i)
figure.add_subplot(rows, cols, i)
plt.xlabel('x%d'%x1, figure=figure)
plt.ylabel('x%d'%x2, figure=figure)
plt.scatter(points[:,x1-1], points[:,x2-1], figure=figure)
i +=1
plt.savefig(fstream, figure=figure)
plt.close(figure)
finally:
fstream.close()
示例13: setUp
def setUp(self):
self.grid = Grid.createLinearGrid(2) # a simple 2D grid
self.grid.createGridGenerator().regular(3) # max level 3 => 17 points
self.HashGridStorage = self.grid.getStorage()
alpha = DataVector(self.grid.getSize())
alpha.setAll(1.0)
for i in [9, 10, 11, 12]:
alpha[i] = 0.0
coarseningFunctor = SurplusCoarseningFunctor(alpha, 4, 0.5)
self.grid.createGridGenerator().coarsen(coarseningFunctor, alpha)
示例14: calc_indicator_value
def calc_indicator_value(self, index):
numData = self.trainData.getNrows()
numCoeff = self.grid.getSize()
seq = self.grid.getStorage().seq(index)
num = 0
denom = 0
tmp = DataVector(numCoeff)
self.multEval.multTranspose(self.errors, tmp)
num = tmp.__getitem__(seq)
num **= 2
alpha = DataVector(numCoeff)
col = DataVector(numData)
alpha.__setitem__(seq, 1.0)
self.multEval.mult(alpha, col)
col.sqr()
denom = col.sum()
if denom == 0:
print "Denominator is zero"
value = 0
else:
value = num/denom
return value
示例15: testDotProduct
def testDotProduct(self):
from pysgpp import DataVector
x = 0
d = DataVector(3)
for i in xrange(len(d)):
d[i] = i + 1
x += d[i] * d[i]
self.assertEqual(d.dotProduct(d), x)