L,n = rft1d.geom.bwlabel(b) return n #(0) Simulation: np.random.seed(0) nNodes = 101 FWHM = [5, 10, 25] nIterations = 100 #increase this to 1000 to reproduce the results from the paper heights = np.linspace(0, 4, 21) ### simulate random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nNodes, W, pad=True) ec = np.array([[here_ec(yy>u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) #(1) Expected EC: E0,E1 = [],[] for W in FWHM: e0 = np.array([here_expected_ec_0d(u) for u in heights]) e1 = np.array([here_expected_ec_1d(nNodes, W, u) for u in heights]) E0.append(e0) E1.append(e1) #(2) Plot results:
from matplotlib import pyplot from spm1d import rft1d eps = np.finfo(float).eps #smallest float #(0) Set parameters: np.random.seed(0) nResponses = 2000 nNodes = 101 FWHM = 10.0 interp = True wrap = True heights = [2.2, 2.4, 2.6, 2.8] ### generate data: y = rft1d.randn1d(nResponses, nNodes, FWHM) calc = rft1d.geom.ClusterMetricCalculator() rftcalc = rft1d.prob.RFTCalculator(STAT='Z', nodes=nNodes, FWHM=FWHM) #(1) Maximum region size: K0 = np.linspace(eps, 15, 21) K = np.array([[calc.max_cluster_extent(yy, h, interp, wrap) for yy in y] for h in heights]) P = np.array([(K>=k0).mean(axis=1) for k0 in K0]).T P0 = np.array([[rftcalc.p.cluster(k0, h) for k0 in K0/FWHM] for h in heights])
nNodes = 101 FWHM = [5.0, 10.0, 25.0] nResponses = 20 heights = np.linspace(1.0, 3.0, 11) ### generate mask: nodes = np.array([True]*nNodes) nodes[20:35] = False nodes[60:80] = False ### assemble the field's geometric characteristics: nSegments = rft1d.geom.bwlabel(nodes)[1] #number of unbroken field segments (here: 3) nNodesTotal = nodes.sum() #number of nodes in the unbroken field segments fieldSize = nNodesTotal - nSegments #(1) Generate broken random fields: y0 = rft1d.randn1d(nResponses, nodes, FWHM[0], pad=True) y1 = rft1d.randn1d(nResponses, nodes, FWHM[1], pad=True) y2 = rft1d.randn1d(nResponses, nodes, FWHM[2], pad=True) #(2) Plot results: pyplot.close('all') axx = np.linspace(0.04, 0.695, 3) AX = [pyplot.axes([xx,0.18,0.29,0.8]) for xx in axx] ax0,ax1,ax2 = AX ### plot fields: colors = scalar2color(range(nResponses+3), cmap=cm.RdPu) [ax0.plot(yy, color=color) for yy,color in zip(y0,colors)]
tri_c = np.sum(((marker2[frame]) - (marker1[frame]))**2)**0.5 cos = (tri_a**2 + tri_b**2 - tri_c**2) / (2 * tri_a * tri_b) ang = np.degrees(np.arccos(cos)) return ang def get_weights(Q, rate=4): x = np.linspace(0, 10, Q) w0 = 1.0 / (1 + np.exp(5 - rate * x)) w1 = w0[::-1] return (w0 + w1) - 1 # Create a random trajectory: np.random.seed(1234567) y0 = rft1d.randn1d(1, 41, 15) w = get_weights(41, rate=8) y = y0 * w #t = range(40) xi = 161.35 xf = 96.85 #t=DPhalanx[:,0] t = np.linspace(0, 40, 41) #Create an array of linear values from 0 to 2 (101 values) x = trajectory(t, xi, xf) #Create an array of minimum jerk trajectory x_noise = rft1d.randn1d(1, 41, 5) x_noise_clean = x_noise * w amp = 2 xnew = x + amp * x_noise_clean
from matplotlib import pyplot from spm1d import rft1d eps = np.finfo(float).eps #smallest float #(0) Set parameters: np.random.seed(0) nResponses = 2000 nNodes = 101 FWHM = 8.5 interp = True wrap = True heights = [2.0, 2.2, 2.4] c = 2 ### generate data: y = rft1d.randn1d(nResponses, nNodes, FWHM) calc = rft1d.geom.ClusterMetricCalculator() rftcalc = rft1d.prob.RFTCalculator(STAT='Z', nodes=nNodes, FWHM=FWHM) #(1) Maximum region size: K0 = np.linspace(eps, 8, 21) K = [[calc.cluster_extents(yy, h, interp, wrap) for yy in y] for h in heights] ### compute number of upcrossings above a threshold: C = np.array([[[sum([kkk >= k0 for kkk in kk]) for kk in k] for k in K] for k0 in K0]) P = np.mean(C >= c, axis=2).T P0 = np.array([[rftcalc.p.set(c, k0, h) for h in heights] for k0 in K0 / FWHM]).T #(2) Plot results: pyplot.close('all')
nNodes = 101 FWHM = [5.0, 10.0, 25.0] nResponses = 20 heights = np.linspace(1.0, 3.0, 11) ### generate mask: nodes = np.array([True] * nNodes) nodes[20:35] = False nodes[60:80] = False ### assemble the field's geometric characteristics: nSegments = rft1d.geom.bwlabel(nodes)[ 1] #number of unbroken field segments (here: 3) nNodesTotal = nodes.sum() #number of nodes in the unbroken field segments fieldSize = nNodesTotal - nSegments #(1) Generate broken random fields: y0 = rft1d.randn1d(nResponses, nodes, FWHM[0], pad=True) y1 = rft1d.randn1d(nResponses, nodes, FWHM[1], pad=True) y2 = rft1d.randn1d(nResponses, nodes, FWHM[2], pad=True) #(2) Plot results: pyplot.close('all') axx = np.linspace(0.04, 0.695, 3) AX = [pyplot.axes([xx, 0.18, 0.29, 0.8]) for xx in axx] ax0, ax1, ax2 = AX ### plot fields: colors = scalar2color(range(nResponses + 3), cmap=cm.RdPu) [ax0.plot(yy, color=color) for yy, color in zip(y0, colors)] [ax1.plot(yy, color=color) for yy, color in zip(y1, colors)] [ax2.plot(yy, color=color) for yy, color in zip(y2, colors)] ### adjust axes: pyplot.setp(AX, xlim=(0, 100), ylim=(-3.8, 3.8))
from matplotlib import pyplot from spm1d import rft1d #(0) Set parameters: np.random.seed(0) nResponses = 10000 nNodes = 101 FWHM = 13.1877 ### generate a field mask: nodes_full = np.array([True] * nNodes) #nothing masked out nodes = nodes_full.copy() nodes[20:45] = False #this region will be masked out nodes[60:80] = False #(1) Generate Gaussian 1D fields and extract maxima:: y_full = rft1d.randn1d(nResponses, nNodes, FWHM) np.random.seed(0) y_broken = rft1d.randn1d(nResponses, nodes, FWHM) ymax_full = y_full.max(axis=1) ymax_broken = np.nanmax(y_broken, axis=1) #(2) Survival functions for field maximum: heights = np.linspace(2.0, 4, 21) sf_full = np.array([(ymax_full >= h).mean() for h in heights]) sf_broken = np.array([(ymax_broken >= h).mean() for h in heights]) ### expected: sfE_full = rft1d.norm.sf(heights, nNodes, FWHM) #theoretical sfE_broken = rft1d.norm.sf(heights, nodes, FWHM) #theoretical #(3) Plot results: pyplot.close('all')
### actual EC: def here_ec(b): L, n = rft1d.geom.bwlabel(b) return n ### simulate: np.random.seed(0) nNodes = 101 FWHM = [5, 10, 25] nIterations = 100 #increase this to 1000 to reproduce the results from the paper heights = np.linspace(0, 4, 21) ### simulate random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nNodes, W, pad=True) ec = np.array([[here_ec(yy > u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) ### expected EC: E0, E1 = [], [] for W in FWHM: e0 = np.array([here_expected_ec_0d(u) for u in heights]) e1 = np.array([here_expected_ec_1d(nNodes, W, u) for u in heights]) E0.append(e0) E1.append(e1) ### plot: pyplot.figure(3) ax = pyplot.axes([0.11, 0.14, 0.86, 0.84]) colors = ['b', 'g', 'r']
### actual EC: def here_ec(b): L, n = rft1d.geom.bwlabel(b) return n #(0) Simulation: np.random.seed(0) nNodes = 101 FWHM = [5, 10, 25] nIterations = 100 #increase this to 1000 to reproduce the results from the paper heights = np.linspace(0, 4, 21) ### simulate random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nNodes, W, pad=True) ec = np.array([[here_ec(yy > u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) #(1) Expected EC: E0, E1 = [], [] for W in FWHM: e0 = np.array([here_expected_ec_0d(u) for u in heights]) e1 = np.array([here_expected_ec_1d(nNodes, W, u) for u in heights]) E0.append(e0) E1.append(e1) #(2) Plot results: pyplot.close('all')
#(0) Set parameters: np.random.seed(123456789) nResponsesA = 5 nResponsesB = 5 nNodes = 101 FWHM = 10.0 nIterations = 5000 ### derived parameters: nA, nB = nResponsesA, nResponsesB nTotal = nA + nB df = nA + nB - 2 #(1) Generate Gaussian 1D fields, compute test stat, store field maximum: T = [] for i in range(nIterations): y = rft1d.randn1d(nTotal, nNodes, FWHM) #compute test stat: yA, yB = y[:nA], y[nA:] mA, mB = yA.mean(axis=0), yB.mean(axis=0) sA, sB = yA.std(ddof=1, axis=0), yB.std(ddof=1, axis=0) s = np.sqrt(((nA - 1) * sA * sA + (nB - 1) * sB * sB) / df) t = (mA - mB) / (s * np.sqrt(1.0 / nA + 1.0 / nB)) T.append(t.max()) T = np.asarray(T) #(2) Survival functions: heights = np.linspace(2, 5, 21) sf = np.array([(T > h).mean() for h in heights]) sfE = rft1d.t.sf(heights, df, nNodes, FWHM) #theoretical sf0D = rft1d.t.sf0d(heights, df) #theoretical (0D)
#(0) Set parameters: np.random.seed(0) nResponses = 10000 nNodes = 101 FWHM = 13.1877 ### generate a field mask: nodes_full = np.array([True]*nNodes) #nothing masked out nodes = nodes_full.copy() nodes[20:45] = False #this region will be masked out nodes[60:80] = False #(1) Generate Gaussian 1D fields and extract maxima:: y_full = rft1d.randn1d(nResponses, nNodes, FWHM) np.random.seed(0) y_broken = rft1d.randn1d(nResponses, nodes, FWHM) ymax_full = y_full.max(axis=1) ymax_broken = np.nanmax(y_broken, axis=1) #(2) Survival functions for field maximum: heights = np.linspace(2.0, 4, 21) sf_full = np.array( [ (ymax_full>=h).mean() for h in heights] ) sf_broken = np.array( [ (ymax_broken>=h).mean() for h in heights] ) ### expected: sfE_full = rft1d.norm.sf(heights, nNodes, FWHM) #theoretical sfE_broken = rft1d.norm.sf(heights, nodes, FWHM) #theoretical
np.random.seed(123456789) nResponsesA = 5 nResponsesB = 5 nNodes = 101 FWHM = 10.0 nIterations = 5000 ### derived parameters: nA,nB = nResponsesA, nResponsesB nTotal = nA + nB df = nA + nB - 2 #(1) Generate Gaussian 1D fields, compute test stat, store field maximum: T = [] for i in range(nIterations): y = rft1d.randn1d(nTotal, nNodes, FWHM) #compute test stat: yA,yB = y[:nA], y[nA:] mA,mB = yA.mean(axis=0), yB.mean(axis=0) sA,sB = yA.std(ddof=1, axis=0), yB.std(ddof=1, axis=0) s = np.sqrt( ((nA-1)*sA*sA + (nB-1)*sB*sB) / df ) t = (mA-mB) / ( s *np.sqrt(1.0/nA + 1.0/nB)) T.append( t.max() ) T = np.asarray(T) #(2) Survival functions: heights = np.linspace(2, 5, 21) sf = np.array( [ (T>h).mean() for h in heights] ) sfE = rft1d.t.sf(heights, df, nNodes, FWHM) #theoretical sf0D = rft1d.t.sf0d(heights, df) #theoretical (0D)
def here_expected_ec_1d(Q, FWHM, u): return (Q-1)/FWHM * sqrt(4*log(2)) / (2*pi) * exp(-0.5*(u*u)) ### actual EC: def here_ec(b): L,n = rft1d.geom.bwlabel(b) return n ### simulate: np.random.seed(0) nNodes = 101 FWHM = [5, 10, 25] nIterations = 100 #increase this to 1000 to reproduce the results from the paper heights = np.linspace(0, 4, 21) ### simulate random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nNodes, W, pad=True) ec = np.array([[here_ec(yy>u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) ### expected EC: E0,E1 = [],[] for W in FWHM: e0 = np.array([here_expected_ec_0d(u) for u in heights]) e1 = np.array([here_expected_ec_1d(nNodes, W, u) for u in heights]) E0.append(e0) E1.append(e1) ### plot: pyplot.figure(3) ax = pyplot.axes([0.11,0.14,0.86,0.84]) colors = ['b', 'g', 'r'] for color,e0,e1,ec in zip(colors, E0, E1, EC):
import ci1d #(0) Initialize parameters: np.random.seed(0) #seed the random number generator to replicate results alpha = 0.05 #Type I error rate J = 10 #sample size Q = 101 #number of continuum nodes fwhm = 20 #smoothness mu = np.zeros(Q) #true population mean niterations = 100 #number of datasets / experiments to simulate in_ci = [] #list that will hold one True or False value for each iteration in_pi = [] #list that will hold one True or False value for each iteration #(1) Simulate: for i in range(niterations): y = mu + rft1d.randn1d(J, Q, fwhm) #Gaussian data ynew = mu + rft1d.randn1d(1, Q, fwhm) #an additional random observation ds = ci1d.UnivariateDataset1D(y) ci = ds.get_confidence_region(alpha=alpha) pi = ds.get_prediction_region(alpha=alpha) in_ci.append(ci.isinside(mu)) in_pi.append(pi.isinside(ynew)) #(2) Report: ### confidence region: prop_in = np.mean( in_ci) #proportion of experiments where the true mean lies inside the CI prop_out = 1 - prop_in #proportion of experiments where the true mean lies outside the CI print('Proportion of random datasets with mu inside CI: %.3f' % prop_in) print('Proportion of random datasets with mu outside CI: %.3f' % prop_out) ### prediction region:
Note: When FWHM gets large (2FWHM>nNodes), the data should be padded using the *pad* keyword. ''' import numpy as np from matplotlib import pyplot from spm1d import rft1d #(0) Set parameters: np.random.seed(12345) nResponses = 5 nNodes = 101 FWHM = 20.0 #(1) Generate Gaussian 1D fields: y = rft1d.randn1d(nResponses, nNodes, FWHM, pad=False) #(2) Plot: pyplot.close('all') pyplot.plot(y.T) pyplot.plot([0,100], [0,0], 'k:') pyplot.xlabel('Field position', size=16) pyplot.ylabel('z', size=20) pyplot.title('Random (Gaussian) fields', size=20) pyplot.show()
def gauss1dns(J, Q, fwhm=10, s=1): ''' Nonstationary, smooth Gaussian 1D noise ''' e = rft1d.randn1d(J, Q, FWHM=fwhm, pad=True) return s * e