# Set constant num_features = 2 batch_size = 512 epochs = 10000 lr = 1e-2 lr_decay = 0.999 flow_length = 6 true_density_choice = 2 # Generate sample from base distribution base_dist = D.MultivariateNormal(torch.zeros(num_features), torch.eye(num_features)) z_0 = base_dist.sample([batch_size]) flow = NormalizingFlow(num_features, flow_length) transformed_z, log_jacobs = flow(z_0) # Visualize before training with torch.no_grad(): visualize_flow(transformed_z) # Set up loss z_k = transformed_z[-1] if true_density_choice == 1: true_density = pot1 if true_density_choice == 2: true_density = pot2 def KL(base_dist, true_density, z_0, z_k, log_jacobs):
def generate_flows( start , end , period = 6 , duration = ppf.date_time.months , notional = 10000000 , accrual_basis = ppf.date_time.basis_act_360 , pay_currency = "USD" , pay_shift_method = ppf.date_time.shift_convention.modified_following , pay_holiday_centers = None , accrual_shift_method = ppf.date_time.shift_convention.modified_following , accrual_holiday_centers = None , observables = None , *arguments , **keywords): ''' >>> from ppf.date_time import * >>> flows = generate_flows( ... start = date(2007, Jun, 29) ... , end = date(2017, Jun, 29) ... , period = 6 ... , duration = ppf.date_time.months ... , notional = 1000000 ... , accrual_basis = basis_30360 ... , pay_currency = "JPY" ... , pay_shift_method = shift_convention.modified_following) >>> for f in flows: ... print f 1000000.000000, JPY, [2007-Jun-29, 2007-Dec-31], basis_30360, 2007-Dec-31, 1000000.000000, JPY, [2007-Dec-31, 2008-Jun-30], basis_30360, 2008-Jun-30, 1000000.000000, JPY, [2008-Jun-30, 2008-Dec-29], basis_30360, 2008-Dec-29, 1000000.000000, JPY, [2008-Dec-29, 2009-Jun-29], basis_30360, 2009-Jun-29, 1000000.000000, JPY, [2009-Jun-29, 2009-Dec-29], basis_30360, 2009-Dec-29, 1000000.000000, JPY, [2009-Dec-29, 2010-Jun-29], basis_30360, 2010-Jun-29, 1000000.000000, JPY, [2010-Jun-29, 2010-Dec-29], basis_30360, 2010-Dec-29, 1000000.000000, JPY, [2010-Dec-29, 2011-Jun-29], basis_30360, 2011-Jun-29, 1000000.000000, JPY, [2011-Jun-29, 2011-Dec-29], basis_30360, 2011-Dec-29, 1000000.000000, JPY, [2011-Dec-29, 2012-Jun-29], basis_30360, 2012-Jun-29, 1000000.000000, JPY, [2012-Jun-29, 2012-Dec-31], basis_30360, 2012-Dec-31, 1000000.000000, JPY, [2012-Dec-31, 2013-Jun-28], basis_30360, 2013-Jun-28, 1000000.000000, JPY, [2013-Jun-28, 2013-Dec-30], basis_30360, 2013-Dec-30, 1000000.000000, JPY, [2013-Dec-30, 2014-Jun-30], basis_30360, 2014-Jun-30, 1000000.000000, JPY, [2014-Jun-30, 2014-Dec-29], basis_30360, 2014-Dec-29, 1000000.000000, JPY, [2014-Dec-29, 2015-Jun-29], basis_30360, 2015-Jun-29, 1000000.000000, JPY, [2015-Jun-29, 2015-Dec-29], basis_30360, 2015-Dec-29, 1000000.000000, JPY, [2015-Dec-29, 2016-Jun-29], basis_30360, 2016-Jun-29, 1000000.000000, JPY, [2016-Jun-29, 2016-Dec-29], basis_30360, 2016-Dec-29, 1000000.000000, JPY, [2016-Dec-29, 2017-Jun-29], basis_30360, 2017-Jun-29, ''' i, day = 0, start flows = [] shift = ppf.date_time.shift while day < end: roll_start = start + duration(i*period) roll_end = start + duration((i + 1)*period) accrual_start = shift( roll_start , accrual_shift_method, accrual_holiday_centers) accrual_end = shift( roll_end , accrual_shift_method, accrual_holiday_centers) pay = shift( roll_end , pay_shift_method, pay_holiday_centers) flows.append( flow(notional , pay_currency , accrual_start , accrual_end , accrual_basis , pay) ) day = roll_end i += 1 if observables <> None: if len(observables) <> len(flows): raise RuntimeError, "too few or too many observables" for i in range(len(flows)): f = flows[i] obs = observables[i] f.set_observables(obs) return flows
/tot_length if rh_type == 4: rh = ones(wcs.x.size) rh[:al_L] = brh rh[al_L:al_L+101] = S_rh rh[al_L+101:] = brh # Set maximum velocity location -- centroid wcs.calcShapeParams() uMx, uMy = wcs.findCentroid() wcs.setUMPoint(uMx, uMy) # Calculate flow parameters wcs.genRL() F = flow(wcs, Q, rh) F.calcChezy(c_rh if rh_type == 4 else False) F.calcFlowParams() ## Get adjusted tau_b phi = arctan2( (wcs.umy - wcs.y), (wcs.umx - wcs.x) ) alpha = arctan2(wcs.yp-wcs.ym, wcs.xp-wcs.xm) F.regenT_b(phi, alpha) # Locate bed-load layer, if not enough capacity set to 0, -1 to garuntee # alluviation q_t = calcLSTC(F.T_b, rho_s, D_s, T_sc) xLs, xRs = findBedLoad(wcs, y_min, q_t, Q_s) # Alluviate by D_s if BLL thickness > 5*D_s
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # Please familiarize yourself with the license of this tool, available # in the distribution with the filename: /docs/license.txt # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # sample flow_rider test from flow import * if __name__ == '__main__': fl = flow() # read states, run intersections, and calc global mean #fl.states.read_states ('../df.csv') fl.states.read_states('../test/kayak2/cut_states.csv') fl.intersections.update(fl.states.df) fl.intersections.write_intersections('../test/kayak2/intersections.csv') flow_x_mean, flow_y_mean, flow_az, flow_vel = fl.calc_global_mean_flow() print('flow mean x ' + str(flow_x_mean)) print('flow mean y ' + str(flow_y_mean)) print('flow az ' + str(flow_az)) print('flow vel ' + str(flow_vel)) # assimilate to a grid print('performing assimilations') fl.assimilate()
g_h = h # use for next guess L, R = cs.findLR(h) # get indices for left and right of free-surface # New wetted cross-section from portion below free-surface wx = cs.x[L:R+1] wy = cs.y[L:R+1] wcs = CrossSection(wx, wy) widths[ts] = wx[-1]-wx[0] uMx = 0. # U_{max} ref point in center of f-s #uMx = (wcs.x[-1]+wcs.x[0])/2 uMy = wcs.y[0] wcs.setUMPoint(uMx, uMy) # set reference point to center of free-surface wcs.calcShapeParams() wcs.genRL() F = flow(wcs, Q, rh) # flow object to find shear stress F.setS(S) # we define slope in f-s case F.calcFlowParams() # Adjust onto bed-normal vector phi = fabs( arctan2( (wcs.umy - wcs.y), (wcs.umx - wcs.x) ) ) alpha = arctan2(wcs.yp-wcs.ym, wcs.xp-wcs.xm) alpha[0] = alpha[1] alpha[-1] = alpha[-2] F.regenT_b(phi, alpha) # adjust \tau_b with angles # Redraw cross-section with shear stress erosion law rl = zeros(cs.x.size) rl[L:R+1] = ff*( ( 1*10**(-1*(2*a+2)) )/50. )*F.T_b**(a) # power law erosion cs.redraw(rl, resample=True) # redraw cross-section w/ spline resample
def generate_flows( start, end, period=6, duration=ppf.date_time.months, notional=10000000, accrual_basis=ppf.date_time.basis_act_360, pay_currency="USD", pay_shift_method=ppf.date_time.shift_convention.modified_following, pay_holiday_centers=None, accrual_shift_method=ppf.date_time.shift_convention.modified_following, accrual_holiday_centers=None, observables=None, *arguments, **keywords): ''' >>> from ppf.date_time import * >>> flows = generate_flows( ... start = date(2007, Jun, 29) ... , end = date(2017, Jun, 29) ... , period = 6 ... , duration = ppf.date_time.months ... , notional = 1000000 ... , accrual_basis = basis_30360 ... , pay_currency = "JPY" ... , pay_shift_method = shift_convention.modified_following) >>> for f in flows: ... print f 1000000.000000, JPY, [2007-Jun-29, 2007-Dec-31], basis_30360, 2007-Dec-31, 1000000.000000, JPY, [2007-Dec-31, 2008-Jun-30], basis_30360, 2008-Jun-30, 1000000.000000, JPY, [2008-Jun-30, 2008-Dec-29], basis_30360, 2008-Dec-29, 1000000.000000, JPY, [2008-Dec-29, 2009-Jun-29], basis_30360, 2009-Jun-29, 1000000.000000, JPY, [2009-Jun-29, 2009-Dec-29], basis_30360, 2009-Dec-29, 1000000.000000, JPY, [2009-Dec-29, 2010-Jun-29], basis_30360, 2010-Jun-29, 1000000.000000, JPY, [2010-Jun-29, 2010-Dec-29], basis_30360, 2010-Dec-29, 1000000.000000, JPY, [2010-Dec-29, 2011-Jun-29], basis_30360, 2011-Jun-29, 1000000.000000, JPY, [2011-Jun-29, 2011-Dec-29], basis_30360, 2011-Dec-29, 1000000.000000, JPY, [2011-Dec-29, 2012-Jun-29], basis_30360, 2012-Jun-29, 1000000.000000, JPY, [2012-Jun-29, 2012-Dec-31], basis_30360, 2012-Dec-31, 1000000.000000, JPY, [2012-Dec-31, 2013-Jun-28], basis_30360, 2013-Jun-28, 1000000.000000, JPY, [2013-Jun-28, 2013-Dec-30], basis_30360, 2013-Dec-30, 1000000.000000, JPY, [2013-Dec-30, 2014-Jun-30], basis_30360, 2014-Jun-30, 1000000.000000, JPY, [2014-Jun-30, 2014-Dec-29], basis_30360, 2014-Dec-29, 1000000.000000, JPY, [2014-Dec-29, 2015-Jun-29], basis_30360, 2015-Jun-29, 1000000.000000, JPY, [2015-Jun-29, 2015-Dec-29], basis_30360, 2015-Dec-29, 1000000.000000, JPY, [2015-Dec-29, 2016-Jun-29], basis_30360, 2016-Jun-29, 1000000.000000, JPY, [2016-Jun-29, 2016-Dec-29], basis_30360, 2016-Dec-29, 1000000.000000, JPY, [2016-Dec-29, 2017-Jun-29], basis_30360, 2017-Jun-29, ''' i, day = 0, start flows = [] shift = ppf.date_time.shift while day < end: roll_start = start + duration(i * period) roll_end = start + duration((i + 1) * period) accrual_start = shift(roll_start, accrual_shift_method, accrual_holiday_centers) accrual_end = shift(roll_end, accrual_shift_method, accrual_holiday_centers) pay = shift(roll_end, pay_shift_method, pay_holiday_centers) flows.append( flow(notional, pay_currency, accrual_start, accrual_end, accrual_basis, pay)) day = roll_end i += 1 if observables <> None: if len(observables) <> len(flows): raise RuntimeError, "too few or too many observables" for i in range(len(flows)): f = flows[i] obs = observables[i] f.set_observables(obs) return flows