Esempio n. 1
0
SGVector<float64_t> CLogitLikelihood::get_log_zeroth_moments(
		SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels* lab) const
{
	SGVector<float64_t> y;

	if (lab)
	{
		REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
				"Length of the vector of means (%d), length of the vector of "
				"variances (%d) and number of labels (%d) should be the same\n",
				mu.vlen, s2.vlen, lab->get_num_labels())
		REQUIRE(lab->get_label_type()==LT_BINARY,
				"Labels must be type of CBinaryLabels\n")

		y=((CBinaryLabels*)lab)->get_labels();
	}
	else
	{
		REQUIRE(mu.vlen==s2.vlen, "Length of the vector of means (%d) and "
				"length of the vector of variances (%d) should be the same\n",
				mu.vlen, s2.vlen)

		y=SGVector<float64_t>(mu.vlen);
		y.set_const(1.0);
	}

	// create an object of normal pdf function
	CNormalPDF* f=new CNormalPDF();

	// create an object of sigmoid function
	CSigmoidFunction* g=new CSigmoidFunction();

	// create an object of product of sigmoid and normal pdf functions
	CProductFunction* h=new CProductFunction(f, g);
	SG_REF(h);

	// compute probabilities using numerical integration
	SGVector<float64_t> r(mu.vlen);

	for (index_t i=0; i<mu.vlen; i++)
	{
		// set normal pdf parameters
		f->set_mu(mu[i]);
		f->set_sigma(CMath::sqrt(s2[i]));

		// set sigmoid parameters
		g->set_a(y[i]);

		// evaluate integral on (-inf, inf)
		r[i]=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+
			CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY);
	}

	SG_UNREF(h);

	r.log();

	return r;
}
Esempio n. 2
0
SGVector<float64_t> CProbitLikelihood::get_log_zeroth_moments(
		SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels* lab) const
{
	SGVector<float64_t> y;

	if (lab)
	{
		REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
				"Length of the vector of means (%d), length of the vector of "
				"variances (%d) and number of labels (%d) should be the same\n",
				mu.vlen, s2.vlen, lab->get_num_labels())
		REQUIRE(lab->get_label_type()==LT_BINARY,
				"Labels must be type of CBinaryLabels\n")

		y=((CBinaryLabels*)lab)->get_labels();
	}
	else
	{
		REQUIRE(mu.vlen==s2.vlen, "Length of the vector of means (%d) and "
				"length of the vector of variances (%d) should be the same\n",
				mu.vlen, s2.vlen)

		y=SGVector<float64_t>(mu.vlen);
		y.set_const(1.0);
	}

	Map<VectorXd> eigen_y(y.vector, y.vlen);
	Map<VectorXd> eigen_mu(mu.vector, mu.vlen);
	Map<VectorXd> eigen_s2(s2.vector, s2.vlen);

	SGVector<float64_t> r(y.vlen);
	Map<VectorXd> eigen_r(r.vector, r.vlen);

	// compute: lp=log(normal_cdf((mu.*y)./sqrt(1+sigma^2)))
	eigen_r=eigen_mu.array()*eigen_y.array()/((1.0+eigen_s2.array()).sqrt());

	for (index_t i=0; i<eigen_r.size(); i++)
		eigen_r[i]=CStatistics::lnormal_cdf(eigen_r[i]);

	return r;
}
Esempio n. 3
0
SGVector<float64_t> CSoftMaxLikelihood::predictive_helper(SGVector<float64_t> mu,
	SGVector<float64_t> s2, const CLabels *lab, EMCSamplerType option) const
{
	const index_t C=s2.vlen/mu.vlen;
	const index_t n=mu.vlen/C;

	REQUIRE(n*C==mu.vlen, "Number of labels (%d) times number of classes (%d) must match "
		"number of elements(%d) in mu\n", n, C, mu.vlen);

	REQUIRE(n*C*C==s2.vlen, "Number of labels (%d) times second power of number of classes (%d*%d) must match "
		"number of elements(%d) in s2\n",n, C, C, s2.vlen);

	SGVector<index_t> y;

	if (lab)
	{
		REQUIRE(lab->get_label_type()==LT_MULTICLASS,
			"Labels must be type of CMulticlassLabels\n");

		const index_t n1=lab->get_num_labels();
		REQUIRE(n==n1, "Number of samples (%d) learned from mu and s2 must match "
			"number of labels(%d) in lab\n",n,n1);

		y=((CMulticlassLabels*) lab)->get_int_labels();
		for (index_t i=0;i<y.vlen;i++)
			REQUIRE(y[i]<C,"Labels must be between 0 and C(ie %d here). Currently lab[%d] is"
				"%d\n",C,i,y[i]);
	}
	else
	{
		y=SGVector<index_t>(n);
		y.set_const(C);
	}

	SGVector<float64_t> ret(mu.vlen);

	for(index_t idx=0; idx<n; idx++)
	{
		SGMatrix<float64_t> Sigma(s2.vector+idx*C*C, C, C, false);
		SGVector<float64_t> mean(mu.vector+idx*C, C, false);
		SGVector<float64_t> label(C);
		if (y[idx]<C)
		{
			label.set_const(0);
			label[y[idx]]=1.0;
		}
		else
		{
			label.set_const(1.0);
		}

		Map<VectorXd> eigen_ret_sub(ret.vector+idx*C, C);
		SGVector<float64_t> tmp=mc_sampler(m_num_samples,mean,Sigma,label);
		Map<VectorXd> eigen_tmp(tmp.vector, tmp.vlen);
		eigen_ret_sub=eigen_tmp;

		if (option==1)
		{
			Map<VectorXd> eigen_label(label.vector, label.vlen);
			eigen_ret_sub=eigen_ret_sub.array()*eigen_label.array()+(1-eigen_ret_sub.array())*(1-eigen_label.array());
		}
	}

	if (option==2)
	{
		Map<VectorXd> eigen_ret(ret.vector, ret.vlen);
		eigen_ret=eigen_ret.array()*(1-eigen_ret.array());
	}

	return ret;
}