Beispiel #1
0
    def Run(self, params):
        # Get filenames for inputs.
        deps = self.Dependencies(params)

        fig = plt.figure(figsize=[20, 10])

        for dep in (dep for dep in deps if dep.task == 'sample'):
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]

            with h5py.File(dep_output) as hf:
                samples = np.array(hf['/samples'])
                min_x = np.min(samples)
                max_x = np.max(samples)
                plt.hist(samples, bins=100, density=True)

        for dep in (dep for dep in deps if dep.task == 'approximation'):
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]
            with h5py.File(dep_output) as hf:
                mode = np.array(hf['/mode'])
                covariance = np.array(hf['/covariance'])

                xs = np.linspace(min_x, max_x, num=100, endpoint=True)
                plt.plot(xs,
                         scipy.stats.norm.pdf(xs, loc=mode[0], scale=np.sqrt(covariance[0, 0])),
                         label='approximation')

        plt.legend()
        plt.savefig(self.Outputs(params)[0],
                    bbox_inches='tight',
                    pad_inches=0)
Beispiel #2
0
    def Run(self, params):
        # Get filenames for inputs.
        deps = self.Dependencies(params)

        fig = plt.figure(figsize=[20, 10])

        min_x_all = np.inf
        max_x_all = -np.inf

        for dep in (dep for dep in deps if dep.task == 'sample'):
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]

            with h5py.File(dep_output) as hf:
                samples = np.array(hf['/samples'])
                print(dep.params)
                print(len(samples))
                print(samples)

                label = nice_alg_name(dep.params)

                label += str(dep.params['alg']['params'])
                if 'proposal' in dep.params:
                    label += str(dep.params['proposal'])

                kde = scipy.stats.gaussian_kde(samples[len(samples) // 2:])

                min_x = np.min(samples)
                max_x = np.max(samples)
                xs = np.linspace(min_x, max_x, num=100, endpoint=True)

                plt.plot(xs, kde(xs), label=label)

                min_x_all = min(min_x_all, min_x)
                max_x_all = max(max_x_all, max_x)

        for dep in (dep for dep in deps if dep.task == 'approximation'):
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]
            with h5py.File(dep_output) as hf:
                mode = np.array(hf['/mode'])
                covariance = np.array(hf['/covariance'])

                xs = np.linspace(min_x_all, max_x_all, num=100, endpoint=True)
                plt.plot(xs,
                         scipy.stats.norm.pdf(xs, loc=mode[0], scale=np.sqrt(covariance[0, 0])),
                         '--',
                         linewidth=5,
                         alpha=0.5,
                         label='approximation')

        plt.legend()
        plt.savefig(self.Outputs(params)[0],
                    bbox_inches='tight',
                    pad_inches=0)
Beispiel #3
0
    def Run(self, params):
        # Get filename for samples.
        deps = self.Dependencies(params)
        dep_outputs = [jobs.Outputs(j)[0] for j in deps]

        datas = []
        for output in dep_outputs:
            with h5py.File(output) as hf:
                data = {
                    'iact_iterations': np.array(hf['/iact_iterations'])[0],
                    'iact_likelihood_evaluations': np.array(hf['/iact_likelihood_evaluations'])[0],
                    'iact_seconds': np.array(hf['/iact_seconds'])[0],
                }
            datas.append(data)

        pickle.dump(
            {
                'data': datas,
                'params': params,
            },
            open(self.Outputs(params)[0], 'wb')
        )
Beispiel #4
0
    def Run(self, params):
        print('running AcceptancePcn: %s' % jobs.params_to_string(params))

        # Get filenames for inputs.
        deps = self.Dependencies(params)

        df = pandas.DataFrame(columns=[
            'alg',
            'N',
            'rho',
            'iact_seconds',
        ])

        for dep in deps:
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]

            with h5py.File(dep_output) as hf:
                samples = np.array(hf['/samples'])
                acceptances = sum(samples[1:] != samples[:-1])
                rate = acceptances / (len(samples) - 1)

                label = nice_alg_name(dep.params)

                df = df.append({
                    'alg': label,
                    'N': dep.params['dataset']['N'],
                    'rho': dep.params['proposal']['params']['rho'],
                    'acceptance_rate': rate,
                }, ignore_index=True)

        print(df)

        hue_order = [
            'MH',
            'SMH-1',
            'SMH-2',
            'FlyMC',
        ]

        plt.figure(figsize=[6.4, 3.2])
        fig, subplots = plt.subplots(1, len(self.rhos))
        for i in range(len(self.rhos)):
            ax = subplots[i]
            rho = self.rhos[i]
            sns.pointplot(
                ax=ax,
                x='N',
                y='acceptance_rate',
                data=df[df['rho'] == rho],
                hue='alg',
                hue_order=hue_order,
                markers=[markers[k] for k in hue_order],
                linestyles=[linestyles[k] for k in hue_order],
                palette=palette,
            )
            current_axes = ax.axis()
            ax.axis([current_axes[0], current_axes[1], 0.0, 1.0])
            if i == 0:
                legend = ax.legend()
                legend.set_title('')
            else:
                ax.get_legend().remove()
                ax.set(ylabel='')
                ax.yaxis.set_ticks([])
                ax.yaxis.set_ticklabels([])

            ticklabels = ax.xaxis.get_ticklabels()
            num_ticks = len(ticklabels)
            ax.xaxis.set_ticks([0, num_ticks-1])
            ax.xaxis.set_ticklabels([ticklabels[0], ticklabels[-1]])

            for i, l in enumerate(ax.xaxis.get_ticklabels()):
                if i != 0 and i != len(ax.xaxis.get_ticklabels()) - 1:
                    l.set_visible(False)

            ax.set(xlabel=r'$n$')
            ax.set(ylabel=i == 0 and 'Acceptance Rate' or '')
            ax.set_title(r'$\rho=%0.1f$' % rho)

        plt.savefig(self.Outputs(params)[0],
                    bbox_inches='tight',
                    pad_inches=0)
Beispiel #5
0
    def Run(self, params):
        print('running EssPcn: %s' % jobs.params_to_string(params))

        # Get filenames for inputs.
        deps = self.Dependencies(params)

        df = pandas.DataFrame(columns=[
            'alg',
            'N',
            'rho',
            'ess_seconds',
        ])

        for dep in deps:
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]

            label = nice_alg_name(dep.params)

            pickle_data = pickle.load(open(dep_output, 'rb'))

            print('pickle_data:')
            print(pickle_data)

            # TODO rename this 'run' and restructure the pickled data
            for run in pickle_data['data']:
                df = df.append({
                    'alg': label,
                    'N': dep.params['dataset']['N'],
                    'rho': dep.params['proposal']['params']['rho'],
                    'ess_seconds': 1.0 / run['iact_seconds'],
                }, ignore_index=True)

        print(df)

        hue_order = [
            'MH',
            'SMH-1',
            'SMH-2',
            'FlyMC',
        ]

        plt.figure(figsize=[6.4, 3.2])
        fig, subplots = plt.subplots(1, len(self.rhos))
        for i in range(len(self.rhos)):
            ax = subplots[i]
            rho = self.rhos[i]
            sns.pointplot(
                ax=ax,
                x='N',
                y='ess_seconds',
                data=df[df['rho'] == rho],
                hue='alg',
                hue_order=hue_order,
                markers=[markers[k] for k in hue_order],
                linestyles=[linestyles[k] for k in hue_order],
                palette=palette,
            )
            if i == 0:
                legend = ax.legend()
                legend.set_title('')
                ax.set(ylabel='First moment ESS per second')
            else:
                ax.get_legend().remove()
                ax.set(ylabel='')
                ax.yaxis.set_ticklabels([])
                ax.yaxis.set_ticklabels([], minor=True)

            ax.set_title(r'$\rho = %0.1f$' % rho)
            ax.set(xlabel=r'$n$')
            ax.set(yscale='log')

            ticklabels = ax.xaxis.get_ticklabels()
            num_ticks = len(ticklabels)
            ax.xaxis.set_ticks([0, num_ticks - 1])
            ax.xaxis.set_ticklabels([ticklabels[0], ticklabels[-1]])

            for j, l in enumerate(ax.xaxis.get_ticklabels()):
                if j != 0 and j != len(ax.xaxis.get_ticklabels()) - 1:
                    l.set_visible(False)

        # Square up axes.
        min_y = np.inf
        max_y = -np.inf
        for ax in subplots:
            axis = ax.axis()
            min_y = min(min_y, axis[2])
            max_y = max(max_y, axis[3])
        for ax in subplots:
            axis = ax.axis()
            ax.axis([axis[0], axis[1], min_y, max_y])

        for i, ax in enumerate(subplots):
            if i != 0:
                ax.yaxis.set_ticklabels([])
                ax.yaxis.set_ticklabels([], minor=True)

        plt.savefig(self.Outputs(params)[0],
                    bbox_inches='tight',
                    pad_inches=0)
Beispiel #6
0
    def Run(self, params):
        print('running EssRandomWalk: %s' % jobs.params_to_string(params))

        # Get filenames for inputs.
        deps = self.Dependencies(params)

        df = pandas.DataFrame(columns=[
            'alg',
            'N',
            'ess_seconds',
        ])

        for dep in deps:
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]

            label = nice_alg_name(dep.params)

            pickle_data = pickle.load(open(dep_output, 'rb'))

            # TODO rename this 'run' and restructure the pickled data
            for run in pickle_data['data']:
                df = df.append({
                    'alg': label,
                    'N': dep.params['dataset']['N'],
                    'ess_seconds': 1.0 / run['iact_seconds'],
                }, ignore_index=True)

        print(df)

        hue_order = [
            'MH',
            'SMH-1',
            'SMH-2',
            'FlyMC',
        ]
        if params['model_type'] == 'lr':
            hue_order.append('Zig-Zag')

        plt.figure(figsize=[6.4, 3.2])
        ax = sns.pointplot(
            x='N',
            y='ess_seconds',
            data=df,
            hue='alg',
            hue_order=hue_order,
            markers=[markers[k] for k in hue_order],
            linestyles=[linestyles[k] for k in hue_order],
            palette=palette,
        )
        ax.set(ylabel=r'Effective sample size per second')
        legend = ax.legend()
        legend.set_title('')
        ax.set(xlabel=r'$n$')
        ax.set(yscale='log')

        min_n = df['N'].min()
        max_n = df['N'].max()

        new_ticklabels = ax.xaxis.get_ticklabels()
        new_ticklabels[-2] = ''
        new_ticklabels[-4] = ''
        ax.xaxis.set_ticklabels(new_ticklabels)

        plt.savefig(self.Outputs(params)[0],
                    bbox_inches='tight',
                    pad_inches=0)
Beispiel #7
0
    def Run(self, params):
        print('running LikelihoodsPerIterationPlot: %s' % jobs.params_to_string(params))

        # Get filenames for inputs.
        deps = self.Dependencies(params)

        df = pandas.DataFrame(columns=[
            'alg',
            'N',
            'likelihood_evaluations',
            'iterations',
        ])

        for dep in deps:
            dep_outputs = jobs.Outputs(dep)
            assert len(dep_outputs) == 1
            dep_output = dep_outputs[0]

            label = nice_alg_name(dep.params)

            with h5py.File(dep_output) as hf:
                df = df.append({
                    'alg': label,
                    'N': dep.params['dataset']['N'],
                    'iterations': np.array(hf['/samples']).shape[0],
                    'likelihood_evaluations': np.array(hf['/likelihood_evaluations'])[0],
                }, ignore_index=True)

        df['likelihoods_per_iteration'] = df['likelihood_evaluations'] / df['iterations']

        print(df)

        hue_order = [
            'MH',
            'SMH-1',
            'SMH-2',
        ]

        poster = False
        if poster:
            context = {
                'text.usetex': True,
                'text.latex.preamble': '\\RequirePackage{cmbright}\n\\RequirePackage[default]{lato}',
                'axes.facecolor': 'ffffff',
                'axes.edgecolor': '000000',
                'axes.labelcolor': 'ffffff',
                'xtick.color': 'ffffff',
                'ytick.color': 'ffffff',
                'legend.edgecolor': '000000',
                'legend.facecolor': 'ffffff',
                'text.color': '000000',
                'savefig.facecolor': '002147',
            }
        else:
            context = {}

        with plt.rc_context(context):
            fig = plt.figure(figsize=[6.4, 3.2])
            ax = sns.pointplot(
                x='N',
                y='likelihoods_per_iteration',
                data=df,
                hue='alg',
                hue_order=hue_order,
                markers=[markers[k] for k in hue_order],
                linestyles=[linestyles[k] for k in hue_order],
                palette=palette,
            )
            ax.set(ylabel=r'Likelihood evaluations per iteration')
            legend = ax.legend()
            legend.set_title('')
            ax.set(xlabel=r'$n$')
            ax.set(yscale='log')

            min_n = df['N'].min()
            max_n = df['N'].max()

            new_ticklabels = ax.xaxis.get_ticklabels()
            new_ticklabels[-2] = ''
            new_ticklabels[-4] = ''
            ax.xaxis.set_ticklabels(new_ticklabels)

            plt.savefig(self.Outputs(params)[0],
                        bbox_inches='tight',
                        pad_inches=0)