def create_component_model(column_metadata, column_hypers, suffstats): modeltype = column_metadata['modeltype'] if modeltype == 'normal_inverse_gamma': component_model = CCM.p_ContinuousComponentModel( column_hypers, count=suffstats.get(b'N', 0), sum_x=suffstats.get(b'sum_x', None), sum_x_squared=suffstats.get(b'sum_x_squared', None)) elif modeltype == 'symmetric_dirichlet_discrete': # TODO Can we change the suffstats data structure not to # include the total count in the dictionary of per-item # counts, please? suffstats = copy.copy(suffstats) count = suffstats.pop(b'N', 0) component_model = MCM.p_MultinomialComponentModel(column_hypers, count=count, counts=suffstats) elif modeltype == 'vonmises': component_model = CYCM.p_CyclicComponentModel( column_hypers, count=suffstats.get(b'N', 0), sum_sin_x=suffstats.get(b'sum_sin_x', None), sum_cos_x=suffstats.get(b'sum_cos_x', None)) else: raise ValueError('unknown modeltype: %r' % (modeltype, )) return component_model
def create_component_model(column_metadata, column_hypers, suffstats): modeltype = column_metadata['modeltype'] if modeltype == 'normal_inverse_gamma': component_model = CCM.p_ContinuousComponentModel( column_hypers, count=suffstats.get(b'N', 0), sum_x=suffstats.get(b'sum_x', None), sum_x_squared=suffstats.get(b'sum_x_squared', None)) elif modeltype == 'symmetric_dirichlet_discrete': # TODO Can we change the suffstats data structure not to # include the total count in the dictionary of per-item # counts, please? suffstats = copy.copy(suffstats) count = suffstats.pop(b'N', 0) component_model = MCM.p_MultinomialComponentModel( column_hypers, count=count, counts=suffstats) elif modeltype == 'vonmises': component_model = CYCM.p_CyclicComponentModel( column_hypers, count=suffstats.get(b'N', 0), sum_sin_x=suffstats.get(b'sum_sin_x', None), sum_cos_x=suffstats.get(b'sum_cos_x', None)) else: raise ValueError('unknown modeltype: %r' % (modeltype,)) return component_model
def plot(results, filename=None): n_samples = results['config']['n_samples'] samples = sorted(results['samples']) conf = results['conf'] X_L = results['X_L_list'][0] X_D = results['X_D_list'][0] hgrm, _ = np.histogram(X_D[0], len(set(X_D[0]))) max_mass_mode = np.argmax(hgrm) suffstats = X_L['view_state'][0]['column_component_suffstats'][0][max_mass_mode] counts = suffstats['N'] sum_x = suffstats['sum_x'] sum_x_sq = suffstats['sum_x_squared'] scale = counts/results['config']['n_samples'] component_model = ccm.p_ContinuousComponentModel( X_L['column_hypers'][0], counts, sum_x, sum_x_sq) plt.figure(facecolor='white') ax = plt.subplot(1, 2, 1) ax.hist(samples, min(31, int(n_samples/10)), normed=True, label='Samples', ec='none', fc='gray') T = [[x] for x in samples] M_c = du.gen_M_c_from_T(T, cctypes=['continuous']) xvals = np.linspace(np.min(samples), np.max(samples), 300) Q = [(n_samples, 0, x) for i, x in enumerate(xvals)] p = [su.simple_predictive_probability(M_c, X_L, X_D, [], [q]) for q in Q] p = np.array(p) ax.plot(xvals, np.exp(p), c='#bbbbbb', label='Predicitive probability', lw=3) p = [component_model.calc_element_predictive_logp(x) for x in xvals] ax.plot(xvals, np.exp(p)*scale, c='#222222', label='Summary mode', lw=3) plt.xlabel('Samples') plt.legend(loc=0) ax = plt.subplot(1, 2, 2) ax.bar([0, 1], [conf, 1.0-conf], fc='#333333', ec='none') ax.set_ylim([0, 1]) ax.set_xlim([-.25, 2]) ax.set_xticks([.5, 1.5]) plt.ylabel('Probability mass') ax.set_xticklabels(['Summary mode', 'All other modes']) if filename is None: plt.show() else: plt.savefig(filename)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from __future__ import print_function import numpy import crosscat.cython_code.ContinuousComponentModel as CCM import crosscat.cython_code.MultinomialComponentModel as MCM import crosscat.cython_code.State as State c_hypers = dict(r=10, nu=10, s=10, mu=10) ccm = CCM.p_ContinuousComponentModel(c_hypers) print("empty component model") print(ccm) # for element in [numpy.nan, 0, 1, numpy.nan, 2]: print() ccm.insert_element(element) print("inserted %s" % element) print(ccm) m_hypers = dict(dirichlet_alpha=10, K=3) mcm = MCM.p_MultinomialComponentModel(m_hypers) print("empty component model") print(mcm) for element in [numpy.nan, 0, 1, numpy.nan, 2]:
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import numpy import crosscat.cython_code.ContinuousComponentModel as CCM import crosscat.cython_code.MultinomialComponentModel as MCM import crosscat.cython_code.State as State c_hypers = dict(r=10,nu=10,s=10,mu=10) ccm = CCM.p_ContinuousComponentModel(c_hypers) print "empty component model" print ccm # for element in [numpy.nan, 0, 1, numpy.nan, 2]: print ccm.insert_element(element) print "inserted %s" % element print ccm m_hypers = dict(dirichlet_alpha=10,K=3) mcm = MCM.p_MultinomialComponentModel(m_hypers) print "empty component model" print mcm for element in [numpy.nan, 0, 1, numpy.nan, 2]: