def movement_v_max(path, n, frames, num_agents, num_sick, immunity): """Sweeps the moves_per_step parameter and creates a graph of the number of agents who are sick or contagious versus time for each immunity value. """ maxes = [] movement_range = np.linspace(0, 20, num=21) for movement in movement_range: print(movement) grid = MovingWorld(n=n, immunity=immunity, num_agents=num_agents, num_sick=num_sick, moves_per_step=int(movement)) # the World step method must return a value for this to work. In # this case, it returns the number that is sick or contagious. maxes.append(max([grid.step() for i in range(frames)])) thinkplot.plot(movement_range, maxes) thinkplot.config( xlabel='Moves Per Step', ylabel='max sick', loc='lower right', title="Number of Moves per Turn vs. Max Sick, num_agents=%d, n=%d" % (num_agents, n)) plt.savefig(path + 'movement_v_max_sweep.pdf') plt.clf() print("Immunity v. max sweep saved!")
def plot_facebook(): """Plot Facebook prices and a smoothed time series. """ names = ['date', 'open', 'high', 'low', 'close', 'volume'] df = pd.read_csv('fb.csv', header=0, names=names, parse_dates=[0]) close = df.close.values[::-1] dates = df.date.values[::-1] days = (dates - dates[0]) / np.timedelta64(1,'D') M = 30 window = np.ones(M) window /= sum(window) smoothed = np.convolve(close, window, mode='valid') smoothed_days = days[M//2: len(smoothed) + M//2] thinkplot.plot(days, close, color=GRAY, label='daily close') thinkplot.plot(smoothed_days, smoothed, label='30 day average') last = days[-1] thinkplot.config(xlabel='Time (days)', ylabel='Price ($)', xlim=[-7, last+7], legend=True, loc='lower right') thinkplot.save(root='convolution1')
def immunity_v_max(path, n, frames, num_agents, num_sick, moves_per_step): """Sweeps the moves_per_step parameter and creates a graph of the number of agents who are sick or contagious versus time for each immunity value. """ maxes = [] immunity_range = np.linspace(0.01, 1.0, num=100) for immunity in immunity_range: print(immunity) avg = [] for run in range(5): grid = MovingWorld(n=n, immunity=immunity, num_agents=num_agents, num_sick=num_sick, moves_per_step=moves_per_step) # the World step method must return a value for this to work. In # this case, it returns the number that is sick or contagious. avg.append(max([grid.step() for i in range(frames)])) maxes.append(np.mean(avg)) thinkplot.plot(immunity_range, maxes) thinkplot.config(xlabel='Immunity', ylabel='max sick', loc='lower right') plt.savefig(path + 'immunity_v_max_sweep.pdf') plt.clf() print("Immunity v. max sweep saved!")
def sweep_immunity(path, n, frames, num_agents, num_sick, moves_per_step): """Sweeps the immunity parameter and creates a graph of the number of agents who are sick or contagious versus time for each immunity value. """ for immunity in [0.1, 0.3, 0.5, 0.7, 0.8, 0.9]: grid = MovingWorld(n=n, immunity=immunity, num_agents=num_agents, num_sick=num_sick, moves_per_step=moves_per_step) # the World step method must return a value for this to work. In this # case, it returns the number that is either sick or contagious. segs = [grid.step() for i in range(frames)] thinkplot.plot(segs, label='immunity = %.2f' % immunity) thinkplot.config( xlabel='Time steps', ylabel='num_sick + num_contagious', title='Sick Agents for Different Immunities, num_agents=%d, n=%d' % (num_agents, n), loc='upper right') plt.savefig(path + 'immunity_sweep.pdf') plt.clf() print("Immunity sweep saved!")
def plot(self, low=0, high=None, **options): """Plots amplitude vs frequency. low: int index to start at high: int index to end at """ thinkplot.plot(self.fs[low:high], self.amps[low:high], **options)
def single_plot(dfs): """Make the plot. """ for i, age_group in enumerate(age_groups): print(i, age_group) series = [df.loc[age_group] for df in dfs] models = [fit_row(s).fittedvalues for s in series] xs = series[0].index + 2 rows = thinkstats2.PercentileRows(models, [5, 95]) thinkplot.fill_between(xs, rows[0], rows[1], color=colors[i], alpha=0.3) rows = thinkstats2.PercentileRows(series, [50]) thinkplot.plot(xs, rows[0], label=labels[i], color=colors[i], alpha=0.6) thinkplot.config(xlabel=xlabel, ylabel=ylabel, loc='upper left', axis=axis) plt.gca().get_legend().set(title='Age group') thinkplot.save(root='age_religion2')
def plot_power(self, low=0, high=None, **options): """Plots power vs frequency. low: int index to start at high: int index to end at """ thinkplot.plot(self.fs[low:high], self.power[low:high], **options)
def plot_derivative(wave, wave2): # compute the derivative by spectral decomposition spectrum = wave.make_spectrum() spectrum3 = wave.make_spectrum() spectrum3.differentiate() # plot the derivative computed by diff and differentiate wave3 = spectrum3.make_wave() wave2.plot(color="0.7", label="diff") wave3.plot(label="derivative") thinkplot.config(xlabel="days", xlim=[0, 1650], ylabel="dollars", loc="upper left") thinkplot.save(root="systems4") # plot the amplitude ratio compared to the diff filter amps = spectrum.amps amps3 = spectrum3.amps ratio3 = amps3 / amps thinkplot.preplot(1) thinkplot.plot(ratio3, label="ratio") window = numpy.array([1.0, -1.0]) padded = zero_pad(window, len(wave)) fft_window = numpy.fft.rfft(padded) thinkplot.plot(abs(fft_window), color="0.7", label="filter") thinkplot.config( xlabel="frequency (1/days)", xlim=[0, 1650 / 2], ylabel="amplitude ratio", ylim=[0, 4], loc="upper left" ) thinkplot.save(root="systems5")
def PlotResampledByAge(resps, **options): samples = [thinkstats2.ResampleRowsWeighted(resp) for resp in resps] sample = pandas.concat(samples, ignore_index=True) groups = sample.groupby('decade') #number of group divisions n = 6 #number of years per group if there are n groups group_size = 30/n #labels age brackets depending on # divs labels = ['{} to {}'.format(int(15 + group_size * i), int(15+(i+1)*group_size)) for i in range(n)] # 0 representing 15-24, 1 being 25-34, and 2 being 35-44 #initilize dictionary of size n, with empty lists prob_dict = {i: [] for i in range(n)} #TODO: Look into not hardcoding this decades = [30,40, 50, 60, 70, 80, 90] for _, group in groups: #calcualates the survival function for each decade _, sf = survival.EstimateSurvival(group) if len(sf.ts) > 1: #iterates through all n age groups to find the probability of marriage for that group for group_num in range(0,n): temp_prob_list = sf.Probs([t for t in sf.ts if (15 + group_size*group_num) <= t <= (15 + (group_num+1)*group_size)]) if len(temp_prob_list) != 0: prob_dict[group_num].append(sum(temp_prob_list)/len(temp_prob_list)) else: pass for key in prob_dict: xs = decades[0:len(prob_dict[key])] thinkplot.plot(xs, prob_dict[key], label=labels[key], **options)
def plot(self, **options): """Plots the wave. """ n = len(self.ys) ts = numpy.linspace(0, self.duration, n) thinkplot.plot(ts, self.ys, **options)
def plot_bass(): wave = thinkdsp.read_wave('328878__tzurkan__guitar-phrase-tzu.wav') wave.normalize() wave.plot() wave.make_spectrum(full=True).plot() sampled = sample(wave, 4) sampled.make_spectrum(full=True).plot() spectrum = sampled.make_spectrum(full=True) boxcar = make_boxcar(spectrum, 4) boxcar.plot() filtered = (spectrum * boxcar).make_wave() filtered.scale(4) filtered.make_spectrum(full=True).plot() plot_segments(wave, filtered) diff = wave.ys - filtered.ys #thinkplot.plot(diff) np.mean(abs(diff)) sinc = boxcar.make_wave() ys = np.roll(sinc.ys, 50) thinkplot.plot(ys[:100])
def plot_sines(): """Makes figures showing correlation of sine waves with offsets. """ wave1 = make_wave(0) wave2 = make_wave(offset=1) thinkplot.preplot(2) wave1.segment(duration=0.01).plot(label='wave1') wave2.segment(duration=0.01).plot(label='wave2') corr_matrix = numpy.corrcoef(wave1.ys, wave2.ys, ddof=0) print(corr_matrix) thinkplot.save(root='autocorr1', xlabel='time (s)', ylabel='amplitude', ylim=[-1.05, 1.05]) offsets = numpy.linspace(0, PI2, 101) corrs = [] for offset in offsets: wave2 = make_wave(offset) corr = corrcoef(wave1.ys, wave2.ys) corrs.append(corr) thinkplot.plot(offsets, corrs) thinkplot.save(root='autocorr2', xlabel='offset (radians)', ylabel='correlation', xlim=[0, PI2], ylim=[-1.05, 1.05])
def sweep_moves(path, n, frames, num_agents, num_sick, immunity): """Sweeps the moves_per_step parameter and creates a graph of the number of agents who are sick or contagious versus time for each immunity value. """ for moves in np.linspace(0, 20, num=5): grid = MovingWorld(n=n, immunity=immunity, num_agents=num_agents, num_sick=num_sick, moves_per_step=int(moves)) # the World step method must return a value for this to work. In this # case, it returns the number that is either sick or contagious. segs = [grid.step() for i in range(frames)] thinkplot.plot(segs, label='moves_per_step = %d' % moves) thinkplot.config( xlabel='Time steps', ylabel='num_sick + num_contagious', loc='upper right', title="Sweeping Number of Moves per Turn, num_agents=%d, n=%d" % (num_agents, n)) plt.savefig(path + 'moves_per_step_sweep.pdf') plt.clf() print("Moves per step sweep saved!")
def CredIntPlt(sf,kl,kh,ll,lh,house,mk,ml,Title): """Given 90 credible values of k and lambda, the mean values of k and lambda, the survival function, the house color scheme to use, and the plot title, this function plots the 90 percent credible interval, the best line, and the data we have""" listcol=colordict[house] Dark=listcol[0] Mid=listcol[1] Light=listcol[2] arr=np.linspace(0,7,num=100) weibSurv2 = exponweib.cdf(arr, kl, lh) #Lower bound weibSurv4 = exponweib.cdf(arr, kh, ll) #Upper bound weibSurv1 = exponweib.cdf(arr, mk, ml) #Best line p4,=plt.plot(arr, 1-weibSurv2,color=Dark,linewidth=3) p1,=plt.plot(arr, 1-weibSurv2,color=Light,linewidth=4) p2,=plt.plot(arr, 1-weibSurv1,color=Mid,linewidth=3,linestyle='--') p3,=plt.plot(arr, 1-weibSurv4,color=Light,linewidth=4) plt.fill_between(arr,1-weibSurv2,1-weibSurv4, facecolor=Light, alpha=.3) thinkplot.plot(sf,color=Dark) plt.xlabel('Age in Books') plt.ylabel('Probability of Survival') plt.ylim([0,1]) plt.legend([p1,p2,p4],['90 Percent Credible Interval','Best Estimate','Data']) plt.title(Title)
def make_figures(): wave1 = make_wave(0) wave2 = make_wave(offset=1) thinkplot.preplot(2) wave1.segment(duration=0.01).plot(label='wave1') wave2.segment(duration=0.01).plot(label='wave2') numpy.corrcoef(wave1.ys, wave2.ys) thinkplot.save(root='autocorr1', xlabel='time (s)', ylabel='amplitude') offsets = numpy.linspace(0, PI2, 101) corrs = [] for offset in offsets: wave2 = make_wave(offset) corr = numpy.corrcoef(wave1.ys, wave2.ys)[0, 1] corrs.append(corr) thinkplot.plot(offsets, corrs) thinkplot.save(root='autocorr2', xlabel='offset (radians)', ylabel='correlation', xlim=[0, PI2])
def CredIntPlt(sf, kl, kh, ll, lh, house, mk, ml, Title): """Given 90 credible values of k and lambda, the mean values of k and lambda, the survival function, the house color scheme to use, and the plot title, this function plots the 90 percent credible interval, the best line, and the data we have""" listcol = colordict[house] Dark = listcol[0] Mid = listcol[1] Light = listcol[2] arr = np.linspace(0, 7, num=100) weibSurv2 = exponweib.cdf(arr, kl, lh) #Lower bound weibSurv4 = exponweib.cdf(arr, kh, ll) #Upper bound weibSurv1 = exponweib.cdf(arr, mk, ml) #Best line p4, = plt.plot(arr, 1 - weibSurv2, color=Dark, linewidth=3) p1, = plt.plot(arr, 1 - weibSurv2, color=Light, linewidth=4) p2, = plt.plot(arr, 1 - weibSurv1, color=Mid, linewidth=3, linestyle='--') p3, = plt.plot(arr, 1 - weibSurv4, color=Light, linewidth=4) plt.fill_between(arr, 1 - weibSurv2, 1 - weibSurv4, facecolor=Light, alpha=.3) thinkplot.plot(sf, color=Dark) plt.xlabel('Age in Books') plt.ylabel('Probability of Survival') plt.ylim([0, 1]) plt.legend([p1, p2, p4], ['90 Percent Credible Interval', 'Best Estimate', 'Data']) plt.title(Title)
def plot(self, label=''): """Plots the wave. label: string label for the plotted line """ n = len(self.ys) ts = numpy.linspace(0, self.duration, n) thinkplot.plot(ts, self.ys, label=label)
def plot_power(self,low=0,high=None,expo=False,**options): #Plots the integrated spectrum #low: index to start at #high: indext to end at cs=self.cs[low:high] fs=self.fs[low:high] if expo: cs=np.exp(cs) thinkplot.plot(fs,cs,**options)
def plot_series_seq(series_seq, colors, labels): """Plots Series objects. series_seq: list of Series colors: list of string colors labels: list of string labels """ for series, color, label in zip(series_seq, colors, labels): thinkplot.plot(series, color=color, label=label)
def plot (self,high=None,**options): #Plots amplitude vs frequency if self.full: fs,amps=self.render_full(high) thinkplot.plot(fs,amps,**options) else: i=None if high is None else find_index(high,self.fs) thinkplot.plot(self.fs[:i],self.amps[:i],**options)
def generate_pmf(fb, hk): pmf_fb = Pmf(degrees(fb)) pmf_hk = Pmf(degrees(hk)) thinkplot.plot([6, 150], [5e-1, 2e-4], color='gray', linestyle='dashed') thinkplot.Pdf(pmf_hk, style='.', label='RPA') thinkplot.config(xscale='log', yscale='log', xlabel='degree', ylabel='PMF') plt.savefig('PMFGraphs_Modified.png')
def plot_power(self, high=None, **options): """Plots power vs frequency. high: frequency to cut off at """ if self.full: fs, amps = self.render_full(high) thinkplot.plot(fs, amps**2, **options) else: i = None if high is None else find_index(high, self.fs) thinkplot.plot(self.fs[:i], self.power[:i], **options)
def plot(res, index): slices = [[0, None], [400, 1000], [860, 900]] start, end = slices[index] xs, ys = zip(*res[start:end]) thinkplot.plot(xs, ys) thinkplot.save(root='dft%d' % index, xlabel='freq (Hz)', ylabel='cov', formats=['png'])
def plot_pink_autocorr(beta, label): """Makes a plot showing autocorrelation for pink noise. beta: parameter of pink noise label: string label for the plot """ signal = thinkdsp.PinkNoise(beta=beta) wave = signal.make_wave(duration=1.0, framerate=11025) lags, corrs = autocorr(wave) thinkplot.plot(lags, corrs, label=label)
def plot(self, high=None, **options): """Plots amplitude vs frequency. Note: if this is a full spectrum, it ignores low and high high: frequency to cut off at """ if self.full: fs, amps = self.render_full(high) thinkplot.plot(fs, amps, **options) else: i = None if high is None else find_index(high, self.fs) thinkplot.plot(self.fs[:i], self.amps[:i], **options)
def main(): gss = utils.ReadGss('gss_college_religion') diffs = [run(gss) for _ in range(101)] years = diffs[0].index rows = thinkstats2.PercentileRows(diffs, [5, 50, 95]) thinkplot.fill_between(years, rows[0], rows[2], alpha=0.2) thinkplot.plot(years, rows[1]) thinkplot.config(xlabel='Year', ylabel='Difference in fraction with no affiliation', xlim=[1970, 2018]) thinkplot.save(root='college_religion')
def plot_power(self, low=0, high=None, expo=False, **options): """Plots the integrated spectrum. low: int index to start at high: int index to end at """ cs = self.cs[low:high] fs = self.fs[low:high] if expo: cs = np.exp(cs) thinkplot.plot(fs, cs, **options)
def plot_diff_deriv(close): change = thinkdsp.Wave(np.diff(close.ys), framerate=1) deriv_spectrum = close.make_spectrum().differentiate() deriv = deriv_spectrum.make_wave() low, high = 0, 50 thinkplot.preplot(2) thinkplot.plot(change.ys[low:high], label='diff') thinkplot.plot(deriv.ys[low:high], label='derivative') thinkplot.config(xlabel='Time (day)', ylabel='Price change ($)') thinkplot.save('diff_int4')
def PlotNormalProbability(sample, title="", ylabel=""): mu, var = thinkstats2.TrimmedMeanVar(sample, p=0.01) sigma = np.sqrt(var) xs = [-5, 5] fxs, fys = thinkstats2.FitLine(xs, inter=mu, slope=sigma) thinkplot.plot(fxs, fys, color='gray', label=r'model $\mu$={:.2f} $\sigma$={:.2f}'.format( mu, sigma)) xs, ys = thinkstats2.NormalProbability(sample) thinkplot.Plot(xs, ys, label="actual") thinkplot.Config(title=title, xlabel="z", ylabel=ylabel)
def PlotNormalModel(sample, title="", xlabel=""): cdf = thinkstats2.Cdf(sample, label="actual") mu, var = thinkstats2.TrimmedMeanVar(sample, p=0.01) sigma = np.sqrt(var) xmin = mu - 4.0 * sigma xmax = mu + 4.0 * sigma xs, ys = thinkstats2.RenderNormalCdf(mu, sigma, xmin, xmax) thinkplot.Cdf(cdf) thinkplot.plot(xs, ys, label=r'model $\mu$={:.2f} $\sigma$={:.2f}'.format( mu, sigma)) thinkplot.Config(title=title, xlabel=xlabel, ylabel="CDF")
def autocorr(wave): n = len(wave.ys) corrs = [] lags = range(n//2) for lag in lags: y1 = wave.ys[lag:] y2 = wave.ys[:n-lag] corr = numpy.corrcoef(y1, y2)[0, 1] corrs.append(corr) thinkplot.plot(lags, corrs) thinkplot.show()
def overlapping_windows(): """Makes a figure showing overlapping hamming windows. """ n = 256 window = np.hamming(n) thinkplot.preplot(num=5) start = 0 for i in range(5): xs = np.arange(start, start + n) thinkplot.plot(xs, window) start += n / 2 thinkplot.save(root='windowing3', xlabel='Index', axis=[0, 800, 0, 1.05])
def overlapping_windows(): """Makes a figure showing overlapping hamming windows. """ n = 256 window = numpy.hamming(n) thinkplot.preplot(num=5) start = 0 for i in range(5): xs = numpy.arange(start, start + n) thinkplot.plot(xs, window) start += n / 2 thinkplot.save(root="windowing3", xlabel="time (s)", axis=[0, 800, 0, 1.05])
def make_figures(): """Makes figures showing complex signals. """ amps = numpy.array([0.6, 0.25, 0.1, 0.05]) freqs = [100, 200, 300, 400] framerate = 11025 ts = numpy.linspace(0, 1, framerate) ys = synthesize1(amps, freqs, ts) print(ys) thinkplot.preplot(2) n = framerate / 25 thinkplot.plot(ts[:n], ys[:n].real, label='real') thinkplot.plot(ts[:n], ys[:n].imag, label='imag') thinkplot.save(root='dft1', xlabel='time (s)', ylabel='wave', ylim=[-1.05, 1.05]) ys = synthesize2(amps, freqs, ts) amps2 = amps * numpy.exp(1.5j) ys2 = synthesize2(amps2, freqs, ts) thinkplot.preplot(2) thinkplot.plot(ts[:n], ys.real[:n], label=r'$\phi_0 = 0$') thinkplot.plot(ts[:n], ys2.real[:n], label=r'$\phi_0 = 1.5$') thinkplot.save(root='dft2', xlabel='time (s)', ylabel='wave', ylim=[-1.05, 1.05], loc='lower right') framerate = 10000 signal = thinkdsp.SawtoothSignal(freq=500) wave = signal.make_wave(duration=0.1, framerate=framerate) hs = dft(wave.ys) amps = numpy.absolute(hs) N = len(hs) fs = numpy.arange(N) * framerate / N thinkplot.plot(fs, amps) thinkplot.save(root='dft3', xlabel='frequency (Hz)', ylabel='amplitude', legend=False)
def plot_diff_filters(wave): window1 = np.array([1, -1]) window2 = np.array([-1, 4, -3]) / 2.0 window3 = np.array([2, -9, 18, -11]) / 6.0 window4 = np.array([-3, 16, -36, 48, -25]) / 12.0 window5 = np.array([12, -75, 200, -300, 300, -137]) / 60.0 thinkplot.preplot(5) for i, window in enumerate([window1, window2, window3, window4, window5]): padded = thinkdsp.zero_pad(window, len(wave)) fft_window = np.fft.rfft(padded) n = len(fft_window) thinkplot.plot(abs(fft_window)[:], label=i+1) thinkplot.show()
def plot_diff_filters(wave): window1 = np.array([1, -1]) window2 = np.array([-1, 4, -3]) / 2.0 window3 = np.array([2, -9, 18, -11]) / 6.0 window4 = np.array([-3, 16, -36, 48, -25]) / 12.0 window5 = np.array([12, -75, 200, -300, 300, -137]) / 60.0 thinkplot.preplot(5) for i, window in enumerate([window1, window2, window3, window4, window5]): padded = thinkdsp.zero_pad(window, len(wave)) fft_window = np.fft.rfft(padded) n = len(fft_window) thinkplot.plot(abs(fft_window)[:], label=i + 1) thinkplot.show()
def plot_correlate(): """Plots the autocorrelation function computed by np. """ wave = thinkdsp.read_wave('28042__bcjordan__voicedownbew.wav') wave.normalize() segment = wave.segment(start=0.2, duration=0.01) lags, corrs = autocorr(segment) N = len(segment) corrs2 = np.correlate(segment.ys, segment.ys, mode='same') lags = np.arange(-N // 2, N // 2) thinkplot.plot(lags, corrs2) thinkplot.config(xlabel='Lag', ylabel='Correlation', xlim=[-N // 2, N // 2]) thinkplot.save(root='autocorr9')
def SurvivalHaz(introductions, lifetimes, plot=False): """Given lists of ages and lifespans, this function calculates the Hazard and Survial curves. If plot is set True, it will plot them.""" haz = survival.EstimateHazardFunction(lifetimes, introductions) sf = haz.MakeSurvival() if plot: thinkplot.plot(sf, color='Grey') plt.xlabel("Age (books)") plt.ylabel("Probability of Surviving") plt.title('Survial Function') thinkplot.show() thinkplot.plot(haz, color='Grey') plt.title('Hazard Function') plt.xlabel("Age (books)") plt.ylabel("Percent of Lives That End") thinkplot.show() return sf, haz
def plot_correlate(): """Plots the autocorrelation function computed by np. """ wave = thinkdsp.read_wave('28042__bcjordan__voicedownbew.wav') wave.normalize() segment = wave.segment(start=0.2, duration=0.01) lags, corrs = autocorr(segment) N = len(segment) corrs2 = np.correlate(segment.ys, segment.ys, mode='same') lags = np.arange(-N//2, N//2) thinkplot.plot(lags, corrs2) thinkplot.config(xlabel='Lag', ylabel='Correlation', xlim=[-N//2, N//2]) thinkplot.save(root='autocorr9')
def SurvivalHaz(introductions,lifetimes,plot=False): """Given lists of ages and lifespans, this function calculates the Hazard and Survial curves. If plot is set True, it will plot them.""" haz=survival.EstimateHazardFunction(lifetimes, introductions) sf=haz.MakeSurvival() if plot: thinkplot.plot(sf,color='Grey') plt.xlabel("Age (books)") plt.ylabel("Probability of Surviving") plt.title('Survial Function') thinkplot.show() thinkplot.plot(haz,color='Grey') plt.title('Hazard Function') plt.xlabel("Age (books)") plt.ylabel("Percent of Lives That End") thinkplot.show() return sf,haz
def NormalProbabilityPlot(sample, label, data_color='blue', fit_color='gray'): """Makes a normal probability plot with a fitted line. sample: sequence of numbers label: string data_color: color string for the data data_color: color string for the fitted line """ data = NormalPlot(sample) fit = FitLine(*data) thinkplot.plot(*fit, color=fit_color, alpha=0.5) thinkplot.plot(*data, label=label, color=data_color, marker='.', markersize=5, alpha=0.5)
def NormalProbabilityPlot(sample, label, data_color='blue', fit_color='gray'): """Makes a normal probability plot with a fitted line. sample: sequence of numbers label: string data_color: color string for the data fit_color: color string for the fitted line """ data = NormalProbability(sample) fit = FitLine(*data) thinkplot.plot(*fit, color=fit_color, alpha=0.5) thinkplot.plot(*data, label=label, color=data_color, marker='.', markersize=5, alpha=0.5)
def plot(self, low=0, high=None, **options): """Plots amplitude vs frequency. Note: if this is a full spectrum, it ignores low and high low: int index to start at high: int index to end at """ # TODO: get rid of low, make high in frequency, and # make it work for full, too if self.full: hs = np.fft.fftshift(self.hs) amps = np.abs(hs) fs = np.fft.fftshift(self.fs) thinkplot.plot(fs, amps, **options) else: thinkplot.plot(self.fs[low:high], self.amps[low:high], **options)
def plot_serial_corr(): """Makes a plot showing serial correlation for pink noise. """ numpy.random.seed(19) betas = numpy.linspace(0, 2, 21) corrs = [] for beta in betas: signal = thinkdsp.PinkNoise(beta=beta) wave = signal.make_wave(duration=1.0, framerate=11025) corr = serial_corr(wave) corrs.append(corr) thinkplot.plot(betas, corrs) thinkplot.config(xlabel=r'pink noise parameter, $\beta$', ylabel='serial correlation', ylim=[0, 1.05]) thinkplot.save(root='autocorr3')
def test_make_dct(): N = 32 framerate = N amps = numpy.zeros(N) amps[2] = 1.0 dct = thinkdsp.Dct(amps, framerate) wave = dct.make_wave() print wave.ys cos_sig = thinkdsp.CosSignal(freq=1, offset=math.pi/N) wave = cos_sig.make_wave(duration=1, start=0, framerate=framerate) print wave.ys dct = wave.make_dct() dct.plot() print dct.fs iv = dct_iv(wave.ys) thinkplot.plot(dct.fs, iv) thinkplot.show()
def plot_fft_convolve(): """Makes a plot showing that FFT-based convolution works. """ names = ['date', 'open', 'high', 'low', 'close', 'volume'] df = pd.read_csv('fb.csv', header=0, names=names, parse_dates=[0]) close = df.close.values[::-1] # compute a 30-day average using np.convolve window = scipy.signal.gaussian(M=30, std=6) window /= window.sum() smoothed = np.convolve(close, window, mode='valid') # compute the same thing using fft_convolve N = len(close) padded = thinkdsp.zero_pad(window, N) M = len(window) smoothed4 = fft_convolve(close, padded)[M-1:] # check for the biggest difference diff = smoothed - smoothed4 print(max(abs(diff))) # compute autocorrelation using np.correlate corrs = np.correlate(close, close, mode='same') corrs2 = fft_autocorr(close) # check for the biggest difference diff = corrs - corrs2 print(max(abs(diff))) # plot the results lags = np.arange(N) - N//2 thinkplot.plot(lags, corrs, color=GRAY, linewidth=7, label='np.convolve') thinkplot.plot(lags, corrs2.real, linewidth=2, label='fft_convolve') thinkplot.config(xlabel='Lag', ylabel='Correlation', xlim=[-N//2, N//2]) thinkplot.save(root='convolution9')
def plot_fft_convolve(): """Makes a plot showing that FFT-based convolution works. """ df = pandas.read_csv("coindesk-bpi-USD-close.csv", nrows=1625, parse_dates=[0]) ys = df.Close.values # compute a 30-day average using numpy.convolve window = scipy.signal.gaussian(M=30, std=6) window /= window.sum() smoothed = numpy.convolve(ys, window, mode="valid") # compute the same thing using fft_convolve padded = zero_pad(window, len(ys)) smoothed2 = fft_convolve(ys, padded) M = len(window) smoothed2 = smoothed2[M - 1 :] # check for the biggest difference diff = smoothed - smoothed2 print(max(abs(diff))) # compute autocorrelation using numpy.correlate N = len(ys) corrs = numpy.correlate(ys, ys, mode="same") corrs = corrs[N // 2 :] corrs2 = fft_autocorr(ys) corrs2 = corrs2[N // 2 :] # check for the biggest difference diff = corrs - corrs2 print(max(abs(diff))) # plot the results thinkplot.preplot(1) thinkplot.plot(corrs, color="0.7", linewidth=7, label="numpy.convolve") thinkplot.plot(corrs2.real, linewidth=2, label="fft_convolve") thinkplot.config(xlabel="lags", ylabel="correlation", xlim=[0, N // 2]) thinkplot.save(root="convolution9")
def plot_bitcoin(): nrows = 1625 df = pandas.read_csv("coindesk-bpi-USD-close.csv", nrows=nrows, parse_dates=[0]) ys = df.Close.values window = numpy.ones(30) window /= sum(window) smoothed = numpy.convolve(ys, window, mode="valid") N = len(window) smoothed = thinkdsp.shift_right(smoothed, N // 2) thinkplot.plot(ys, color="0.7", label="daily") thinkplot.plot(smoothed, label="30 day average") thinkplot.config( xlabel="time (days)", ylabel="price", xlim=[0, nrows], # ylim=[-60, 60], loc="lower right", ) thinkplot.save(root="convolution1")