本文整理汇总了Python中numpy.cumprod函数的典型用法代码示例。如果您正苦于以下问题:Python cumprod函数的具体用法?Python cumprod怎么用?Python cumprod使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cumprod函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _indtosub_converter
def _indtosub_converter(dims, order='F', onebased=True):
"""Converter for changing linear indexing to subscript indexing
See also
--------
Series.indtosub
"""
_check_order(order)
def indtosub_inline_onebased(k, dimprod):
return tuple(map(lambda (x, y): int(mod(ceil(float(k)/y) - 1, x) + 1), dimprod))
def indtosub_inline_zerobased(k, dimprod):
return tuple(map(lambda (x, y): int(mod(ceil(float(k+1)/y) - 1, x)), dimprod))
inline_fcn = indtosub_inline_onebased if onebased else indtosub_inline_zerobased
if size(dims) > 1:
if order == 'F':
dimprod = zip(dims, append(1, cumprod(dims)[0:-1]))
else:
dimprod = zip(dims, append(1, cumprod(dims[::-1])[0:-1])[::-1])
converter = lambda k: inline_fcn(k, dimprod)
else:
converter = lambda k: (k,)
return converter
示例2: _update_attributes
def _update_attributes(self):
getitem_tuple = ()
values = []
self.signal_axes = ()
self.navigation_axes = ()
for axis in self._axes:
# Until we find a better place, take property of the axes
# here to avoid difficult to debug bugs.
axis.axes_manager = self
if axis.slice is None:
getitem_tuple += (axis.index,)
values.append(axis.value)
self.navigation_axes += (axis,)
else:
getitem_tuple += (axis.slice,)
self.signal_axes += (axis,)
self.signal_axes = self.signal_axes[::-1]
self.navigation_axes = self.navigation_axes[::-1]
self._getitem_tuple = getitem_tuple
self.signal_dimension = len(self.signal_axes)
self.navigation_dimension = len(self.navigation_axes)
if self.navigation_dimension != 0:
self.navigation_shape = tuple([axis.size for axis in self.navigation_axes])
else:
self.navigation_shape = ()
if self.signal_dimension != 0:
self.signal_shape = tuple([axis.size for axis in self.signal_axes])
else:
self.signal_shape = ()
self.navigation_size = np.cumprod(self.navigation_shape)[-1] if self.navigation_shape else 0
self.signal_size = np.cumprod(self.signal_shape)[-1] if self.signal_shape else 0
self._update_max_index()
示例3: plot2
def plot2(indx_Exposure, indx_MarketRet):
plt.clf()
MarketReturns = plt.subplot2grid((8,8), (0,0), colspan=6, rowspan=8)
t = np.array(DATEord)
if indx_Exposure == 2:
mRet = np.cumprod(1-marketRet[indx_MarketRet + cashOffset])
else:
mRet = np.cumprod(1+marketRet[indx_MarketRet + cashOffset])
MarketReturns.plot(t, mRet, 'b',linewidth=0.5)
statistics=stats(mRet)
MarketReturns.set_ylabel('Market Returns')
statsStr="Sharpe Ratio = {sharpe:.4f}\nSortino Ratio = {sortino:.4f}\n\nPerformance (%/yr) = {returnYearly:.4f}\nVolatility (%/yr) = {volaYearly:.4f}\n\nMax Drawdown = {maxDD:.4f}\nMAR Ratio = {mar:.4f}\n\n Max Time off peak = {maxTimeOffPeak}\n\n\n\n\n\n".format(**statistics)
MarketReturns.autoscale(tight=True)
MarketReturns.set_title('Market Returns of %s' %mRetMarkets[indx_MarketRet])
MarketReturns.set_xlabel('Date')
# Performance Numbers Textbox
f.text(.72,.58,statsStr)
plt.gcf().canvas.draw()
示例4: panda_index
def panda_index(labels, names=None, dtype='|S10'):
"""
Create a pandas.MultiIndex with row names contained in the nested
list `labels` and column names contained in the optional list
`names`.
Args:
labels: nested list of strings
names: list of strings
Example usage:
>>> labels = [['wine','water','beer'], [0.2','0.5'], ['to go','for here']]
>>> names = ['beverage','size','order']
>>> index = make_index(labels,names)
>>> index
"""
if names==None:
names = ['axis{0}'.format(i) for i in range(len(labels))]
else:
assert len(labels)==len(names)
sh = list_shape(labels)
n_axes = len(labels)
n_total = np.prod(sh)
ctile = np.concatenate( ([1],np.cumprod(sh)[:-1]) )
crep = np.concatenate( (np.cumprod(sh[::-1])[:-1][::-1],[1]) )
replabels = np.empty((n_axes,n_total), dtype=dtype)
for i,l in enumerate(labels):
replabels[i] = np.tile( np.repeat(l,crep[i]), ctile[i] )
tuples = zip(*replabels)
return pd.MultiIndex.from_tuples(tuples, names=names)
示例5: update_attributes
def update_attributes(self):
getitem_tuple = []
values = []
self.signal_axes = []
self.navigation_axes = []
for axis in self.axes:
if axis.slice is None:
getitem_tuple.append(axis.index)
values.append(axis.value)
self.navigation_axes.append(axis)
else:
getitem_tuple.append(axis.slice)
self.signal_axes.append(axis)
self._getitem_tuple = getitem_tuple
self.signal_dimension = len(self.signal_axes)
self.navigation_dimension = len(self.navigation_axes)
if self.navigation_dimension != 0:
self.navigation_shape = [
axis.size for axis in self.navigation_axes]
else:
self.navigation_shape = [0,]
if self.signal_dimension != 0:
self.signal_shape = [
axis.size for axis in self.signal_axes]
else:
self.signal_shape = [0,]
self.navigation_size = \
np.cumprod(self.navigation_shape)[-1]
self.signal_size = \
np.cumprod(self.signal_shape)[-1]
self._update_max_index()
示例6: build_reflector
def build_reflector(dataset, **kwargs):
'''
builds reflector
'''
#some shortcuts
vp = kwargs['model']['vp']
rho = kwargs['model']['rho']
R = kwargs['model']['R']
sz = kwargs['sz']
gz = kwargs['gz']
sx = kwargs['sx']
gx = kwargs['gx']
numpoints = 100 #used for interpolating through the model
for g in gx:
cmpx = np.floor((g + sx)/2.).astype(np.int) # nearest midpoint
h = cmpx - sx #half offset
#the next line extracts the non-zero reflection points at this midpoint
rp = np.nonzero(R[cmpx,:])[0]
#and iterates over them
for cmpz in (rp):
#~ print cmpx, cmpz
ds = np.sqrt(cmpz**2 + (h)**2)/float(numpoints) # line step distance
#predefine outputs
amp = 1.0
time = 0.0
#traveltime from source to cdp
vp_down = toolbox.find_points(sx, sz, cmpx, cmpz, numpoints, vp)
time += 0
#traveltime from cdp to geophone
vp_up = toolbox.find_points(cmpx, cmpz, g, gz, numpoints, vp)
time += 0
#loss due to spherical divergence
amp *= 0
#transmission losses from source to cdp
rho_down = toolbox.find_points(sx, sz, cmpx, cmpz, numpoints, rho)
z0s = rho_down * vp_down
z1s = toolbox.roll(z0s, 1)
correction = np.cumprod(transmission_coefficient(z0s, z1s) )[-1]
amp *= 0
#amplitude loss at reflection point
correction = R[cmpx,cmpz]
amp *= 0
#transmission loss from cdp to source
rho_up = toolbox.find_points(cmpx, cmpz, g, gz, numpoints, rho)
z0s = rho_up * vp_up
z1s = toolbox.roll(z0s, 1)
correction = np.cumprod(transmission_coefficient(z0s, z1s))[-1]
amp *= 0
#calculate coordinates
#write out data
return
示例7: main
def main(fund_tx_file, comparision_symbol):
fund_txn = pd.read_csv(fund_tx_file, parse_dates=[[0, 1, 2]], header=None, index_col=[0])
fund_txn.sort_index(inplace=True)
sorted_dates = fund_txn.index
start_date = sorted_dates[0]
end_date = sorted_dates[-1] + dt.timedelta(days=1)
total_daily_rets = fund_txn.iloc[:, -1].astype('f4')
# print total_daily_rets
daily_ret = tsu.returnize0(total_daily_rets.copy())
avg_daily_ret = np.mean(daily_ret)
std_dev = np.std(daily_ret)
sharpe = np.sqrt(252) * avg_daily_ret/std_dev
cum_ret = total_daily_rets[-1]/total_daily_rets[0]
comp_sym_vol, comp_sym_daily_ret, comp_sym_sharpe, comp_sym_cum_ret = optimizer.simulate(
start_date, end_date, [comparision_symbol], [1.0])
print("Details of the Performance of the portfolio :")
print("Data Range : {} to {}").format(str(start_date + dt.timedelta(hours=16)),
str(end_date + dt.timedelta(hours=16)))
print("Sharpe Ratio of Fund : {}").format(sharpe)
print("Sharpe Ratio of {} : {}").format(comparision_symbol,comp_sym_sharpe)
print("Total Return of Fund : {}").format(cum_ret)
print("Total Return of {} : {}").format(comparision_symbol, comp_sym_cum_ret)
print("Standard Deviation of Fund : {}").format(std_dev)
print("Standard Deviation of {} : {}").format(comparision_symbol, comp_sym_vol)
print("Average Daily Return of Fund : {}").format(avg_daily_ret)
print("Average Daily Return of {} : {}").format(comparision_symbol, comp_sym_daily_ret)
# Plot Fund vs comparing symbol
plt.clf()
fig = plt.figure(1)
ax = plt.subplot(111)
daily_ret_cummulative = np.cumprod(daily_ret + 1, axis=0)
# Calculate daily returns for comparing symbol
ldt_timestamps, na_price = optimizer.get_close_price_for_symbols(start_date,
end_date, [comparision_symbol])
na_normalized_price = na_price / na_price[0, :]
all_sum_daily = np.sum(na_normalized_price, 1)
comp_sym_daily_ret = tsu.returnize0(all_sum_daily.copy())
comp_sym_cummulative = np.cumprod(comp_sym_daily_ret + 1, axis=0)
plt.plot(sorted_dates, daily_ret_cummulative, label='Fund', alpha=0.4)
plt.plot(sorted_dates, comp_sym_cummulative, label=comparision_symbol)
plt.ylabel('Cumulative Returns')
plt.xlabel('Date')
fig.autofmt_xdate(rotation=45)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
示例8: cartesian
def cartesian(nodes, order='C'):
'''Cartesian product of a list of arrays
Parameters:
-----------
nodes: (list of 1d-arrays)
order: ('C' or 'F') order in which the product is enumerated
Returns:
--------
out: (2d-array) each line corresponds to one point of the product space
'''
nodes = [numpy.array(e) for e in nodes]
shapes = [e.shape[0] for e in nodes]
n = len(nodes)
l = numpy.prod(shapes)
out = numpy.zeros((l, n))
if order == 'C':
repetitions = numpy.cumprod([1] + shapes[:-1])
else:
shapes.reverse()
sh = [1] + shapes[:-1]
repetitions = numpy.cumprod(sh)
repetitions = repetitions.tolist()
repetitions.reverse()
for i in range(n):
_repeat_1d(nodes[i], repetitions[i], out[:,i])
return out
示例9: tiCarryPlot
def tiCarryPlot(getTIresult):
conCarry = getTIresult.ix[:,0]*getTIresult.ix[:,1]
disCarry = getTIresult.ix[:,0]*getTIresult.ix[:,2]
cumBetas = np.cumprod(getTIresult.ix[:,0]/100+1)-1
cumConBetas = np.cumprod(conCarry/100+1)-1
cumDisBetas = np.cumprod(disCarry/100+1)-1
fig = plt.figure()
ax1 = fig.add_subplot(311)
ax1.set_title('Cumulative Betas')
cumBetas.plot(style='r',label='Original Beta')
cumConBetas.plot(style='b', label='Discrete Weights')
cumDisBetas.plot(style='g', label='Digital Weights')
plt.legend(loc=2)
ax2 = fig.add_subplot(312)
ax2.set_title('Discrete Weights')
getTIresult.ix[:,1].plot(style='b')
plt.ylim([0, 1.2])
plt.yticks([0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2])
ax3 = fig.add_subplot(313)
ax3.set_title('Digital Weights')
getTIresult.ix[:,2].plot(style='g')
plt.ylim([-0.1, 1.1])
plt.yticks([-0.1, 0.1, 0.3, 0.5, 0.7, 0.9, 1.1])
fig.tight_layout()
plt.show()
示例10: p2_strat
def p2_strat():
data = util.load_insample()
SO = util.extract_SO(data)
SC = util.extract_SC(data)
SH = util.extract_SH(data)
SL = util.extract_SL(data)
TVL = util.extract_TVL(data)
RCC, avrRCC = p1_strat()
# 1...T
RCO = SO[1:] / SC[:-1] - 1
cumRCO = np.cumprod(1 + np.mean(RCO, axis=1))
powers = 1. / np.asarray(1 + np.arange(RCO.shape[0]))
avrRCO = np.power(cumRCO, powers) - 1
# 0...T
ROC = SC / SO - 1
cumROC = np.cumprod(1 + np.mean(ROC, axis=1))
powers = 1. / np.asarray(1 + np.arange(ROC.shape[0]))
avrROC = np.power(cumROC, powers) - 1
# 1...T
ROO = SO[1:] / SO[:-1] - 1
cumROO = np.cumprod(1 + np.mean(ROO, axis=1))
powers = 1. / np.asarray(1 + np.arange(ROO.shape[0]))
avrROO = np.power(cumROO, powers) - 1
# 0...T
RVP = (1 / (4 * np.log(2))) * (np.log(SH[1:]) - np.log(SL[1:]))**2
avrTVL = np.zeros(TVL.shape)
avrRVP = np.zeros(RVP.shape)
powers = 1. / np.asarray(1 + np.arange(TVL.shape[0]))
avrTVL[:200, :] = np.cumsum(TVL[:200, :], axis=0)
avrRVP[:200, :] = np.cumsum(RVP[:200, :], axis=0)
#avrRVP[:200, :] = np.cumprod(RVP[:200, :], axis=0)
print TVL.shape, RVP.shape
for i in np.arange(200, TVL.shape[0]):
avrTVL[i, :] = (avrTVL[i-1, :] - TVL[i-200, :] + TVL[i, :])
if i < RVP.shape[0]:
avrRVP[i, :] = (avrRVP[i-1, :] - RVP[i-200, :] + RVP[i, :])
#avrRVP[i, :] = np.multiply(np.divide(avrRVP[i-1, :], RVP[i-200, :]), RVP[i, :])
powers[200:] = 1. / 200
#avrTVL = np.power(avrTVL, powers[:, None])
avrTVL = np.multiply(avrTVL, powers[:, None])
avrRVP = np.multiply(avrRVP, powers[:-1, None])
#avrRVP = np.power(avrRVP, powers[:, None])
#print 'RCO ', RCO[:10, :10]
#print 'avrRCO ', avrRCO[:10]
#print 'ROC ', ROC[:10, :10]
#print 'avrROC ', avrROC[:10]
#print 'ROO ', ROO[:10, :10]
#print 'avrROO ', avrROO[:10]
print 'RVP ', RVP[:10, :10]
#print 'avrTVL ', avrTVL[:10, :10]
print 'avrRVP ', avrRVP[:10, :10]
return RCO, avrRCO, ROC, avrROC, ROO, avrROO, TVL, avrTVL, RVP, avrRVP
示例11: plot_backtest
def plot_backtest(config, algos, labels=None):
"""
@:param config: config dictionary
@:param algos: list of strings representing the name of algorithms or index of pgportfolio result
"""
results = []
for i, algo in enumerate(algos):
if algo.isdigit():
results.append(np.cumprod(_load_from_summary(algo, config)))
logging.info("load index "+algo+" from csv file")
else:
logging.info("start executing "+algo)
results.append(np.cumprod(execute_backtest(algo, config)))
logging.info("finish executing "+algo)
start, end = _extract_test(config)
timestamps = np.linspace(start, end, len(results[0]))
dates = [datetime.datetime.fromtimestamp(int(ts)-int(ts)%config["input"]["global_period"])
for ts in timestamps]
weeks = mdates.WeekdayLocator()
days = mdates.DayLocator()
rc("font", **{"family": "sans-serif", "sans-serif": ["Helvetica"],
"size": 8})
"""
styles = [("-", None), ("--", None), ("", "+"), (":", None),
("", "o"), ("", "v"), ("", "*")]
"""
fig, ax = plt.subplots()
fig.set_size_inches(9, 5)
for i, pvs in enumerate(results):
if len(labels) > i:
label = labels[i]
else:
label = NAMES[algos[i]]
ax.semilogy(dates, pvs, linewidth=1, label=label)
#ax.plot(dates, pvs, linewidth=1, label=label)
plt.ylabel("portfolio value $p_t/p_0$", fontsize=12)
plt.xlabel("time", fontsize=12)
xfmt = mdates.DateFormatter("%m-%d %H:%M")
ax.xaxis.set_major_locator(weeks)
ax.xaxis.set_minor_locator(days)
datemin = dates[0]
datemax = dates[-1]
ax.set_xlim(datemin, datemax)
ax.xaxis.set_major_formatter(xfmt)
plt.grid(True)
plt.tight_layout()
ax.legend(loc="upper left", prop={"size":10})
fig.autofmt_xdate()
plt.savefig("result.eps", bbox_inches='tight',
pad_inches=0)
plt.show()
示例12: f
def f(self,X):
X = reshape(X,self.input_dim)
n = X.shape[0]
fval = np.cumprod(np.sqrt(X),axis=1)[:,self.input_dim-1]*np.cumprod(np.sin(X),axis=1)[:,self.input_dim-1]
if self.sd ==0:
noise = np.zeros(n).reshape(n,1)
else:
noise = np.random.normal(0,self.sd,n).reshape(n,1)
return -fval.reshape(n,1) + noise
示例13: test_CumprodOp
def test_CumprodOp(self):
x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX)
f = theano.function([x], cumprod(x))
assert np.allclose(np.cumprod(a), f(a)) # Test axis=None
for axis in range(len(a.shape)):
f = theano.function([x], cumprod(x, axis=axis))
assert np.allclose(np.cumprod(a, axis=axis), f(a))
示例14: eqn8
def eqn8(N, B):
n = np.arange(N + 1, dtype=np.float64)
# Create an array containing the factorials. scipy.special.factorial
# requires SciPy 0.14 (#5064) therefore this is calculated by using
# numpy.cumprod. This could be replaced by factorial again as soon as
# older SciPy are not supported anymore but the cumprod alternative
# might also be a bit faster.
factorial_n = np.ones(n.shape, dtype=np.float64)
np.cumprod(n[1:], out=factorial_n[1:])
return 1. / (exp(-B) * np.sum(np.power(B, n) / factorial_n))
示例15: test_cumprod
def test_cumprod(self):
q1 = np.array([1, 2, 6]) * u.m
with pytest.raises(u.UnitsError) as exc:
q1.cumprod()
with pytest.raises(u.UnitsError) as exc:
np.cumprod(q1)
q2 = np.array([3, 4, 5]) * u.Unit(1)
assert np.all(q2.cumprod() == np.array([3, 12, 60]) * u.Unit(1))
assert np.all(np.cumprod(q2) == np.array([3, 12, 60]) * u.Unit(1))