Real gmm_objective(std::vector<VectorD> const& x, Vector const& alphas, std::vector<VectorD> const& means, std::vector<VectorD> const& qs, std::vector<Vector> const& ls, Real wishart_gamma, Real wishart_m) { size_t n = x.size(); size_t d = x[0].size(); size_t K = alphas.size(); // assert (K = rows means) // assert (d = cols means) // assert (K = rows qs) // assert (d = cols qs) // assert (K = rows ls) // assert (auto di = (cardToInt d) in di*(di-1)/2 = cardToInt (cols ls)) double out = 0.0; Vector tmp{ K }; for (size_t i = 0; i < n; ++i) { for (size_t k = 0; k < K; ++k) tmp[k] = alphas[k] + sum(qs[k]) - 0.5 * sumsq(Qtimesv(qs[k], ls[k], x[i] - means[k])); out += logsumexp(tmp); } out -= n * logsumexp(alphas); for (size_t k = 0; k < K; ++k) out += sumsq(exp(qs[k])) + sumsq(ls[k]); return out; }
/* * Display the shortest sums of squares for long integer "d1". * * Return the minimum number of summed squares required to represent "d1". */ int findsq(long d1) { if (sumsq(d1, 1)) return 1; if (sumsq(d1, 2)) return 2; if (sumsq(d1, 3)) return 3; if (sumsq(d1, 4)) return 4; fprintf(stderr, "Whoops! Can't find the sum of four squares that equal %ld.\n", d1); exit(EXIT_FAILURE); }
SpdMatrix WMS::center_sumsq(const Vector &mu)const{ SpdMatrix ans = sumsq(); // sum wyy^T ans.add_outer(mu, sumw()); // wyyT + w.mu.muT ans -= as_symmetric(mu.outer(sum_, 2)); return ans; }
int main() { std::uniform_real_distribution<Real> dist(0, 1); auto rand = [&dist] { return dist(rng); }; Vec<Real> theta{ 26 }; size_t n_bones = 15; size_t n_verts = 500; fillrand(&theta, std::uniform_real_distribution<Real>(-1, 1)); Mat<Real, 3, N_VERTS> base_positions{ 3, n_verts }; for (int i = 0; i < n_verts; ++i) fillrand(&base_positions[i], std::uniform_real_distribution<Real>(1, 10)); std::vector<Real4x4> base_relatives{ n_bones }; for (int i = 0; i < n_bones; ++i) { Vec<Real, 3> rvec; fillrand(&rvec, std::uniform_real_distribution<Real>(-2, 2)); Vec<Real, 3> tvec; fillrand(&tvec, std::uniform_real_distribution<Real>(1, 2)); base_relatives[i] = Rt_to_transform(angle_axis_to_rotation_matrix(rvec), tvec); } std::vector<int> parents{ -1, 0, 1, -1, 3, 4, -1, 6, 7, -1, 9, 10, -1, 12, 13, }; Mat<Real, N_VERTS, N_BONES> weights{ n_verts, n_bones }; for (int i = 0; i < n_verts; ++i) for (int j = 0; j < n_bones; ++j) { if (rand() < 4.0 / n_bones) weights[j][i] = rand(); } boost::timer::auto_cpu_timer t; // Debug 150s // Release 1s double total = 0; size_t N = 1000; #ifdef _DEBUG N = N / 10; // Debug is roughly this much slower than release -- multiply timings. #endif for (size_t count = 0; count < N; ++count) { std::vector<Vec3<Real>> pose_params = to_pose_params(theta, n_bones); Mat<Real, 3, N_VERTS> verts = get_skinned_vertex_positions(base_relatives, parents, base_positions, weights, pose_params); total += sumsq(verts[1]); } std::cout << "total =" << total << ", time per call = " << t.elapsed().wall / double(N) / 1e6 << "ms" << std::endl; return 0; }
// Pre-condition: start <= end // Post-condition: Returns the sum of the squares of the integers starting from // start and ending at end. int sumsq(int start, int end) { // Base case were one value is being summed. if (start == end) return start*start; // This is the recursive solution for larger cases. return start*start + sumsq(start+1, end); }
void experiment_normal::get_state(libbase::vector<double>& state) const { assert(count() == sum.size()); assert(count() == sumsq.size()); state.init(2 * count()); for (int i = 0; i < count(); i++) { state(i) = sum(i); state(count() + i) = sumsq(i); } }
void SLLPS::draw() { for (int i = 0; i < variance_samplers_.size(); ++i) { Ptr<ZeroMeanGaussianModel> innovation_model = model_->innovation_model(i); const auto suf = innovation_model->suf(); double sigsq = variance_samplers_[i].draw( rng(), suf->n(), suf->sumsq()); innovation_model->set_sigsq(sigsq); } observation_coefficient_sampler_->draw_Beta(); model_->impose_identifiability_constraint(); }
WM::WishartModel(double pri_df, const SpdMatrix &PriVarEst) : ParamPolicy(new UnivParams(pri_df), new SpdParams(PriVarEst*pri_df)), DataPolicy(new WS(PriVarEst.nrow())), PriorPolicy() { Chol chol(sumsq()); if (!chol.is_pos_def()) { report_error("Sum of squares matrix must be positive definite in " "WishartModel constructor"); } }
void Qtimesv_test() { auto q = vec(0.1, -1.0, 0.3); auto l = vec(5.0, -2.0, 7.1); auto v = vec(1.4, -7.0, 3.1); auto ans0 = exp(q[0]) * v[0]; auto ans1 = l[0] * v[0] + exp(q[1]) * v[1]; auto ans2 = l[1] * v[0] + l[2] * v[1] + exp(q[2]) * v[2]; auto ans = vec(ans0, ans1, ans2); auto qv = Qtimesv(q, l, v); auto nrm = sumsq(qv - ans); assert(nrm < 0.0001); }
void main() { long s; int i; for(i=11, s=0; i<=1000; i++) { if(palindrome(i)&&odd(i)&&sumsq(i)) { s+=(long)i; } } printf("\nSum=%ld", s); }
void experiment_normal::estimate(libbase::vector<double>& estimate, libbase::vector<double>& stderror) const { assert(count() == sum.size()); assert(count() == sumsq.size()); // estimate is the mean value assert(get_samplecount() > 0); estimate = sum / double(get_samplecount()); // standard error is sigma/sqrt(n) stderror.init(count()); if (get_samplecount() > 1) for (int i = 0; i < count(); i++) stderror(i) = sqrt((sumsq(i) / double(get_samplecount()) - estimate(i) * estimate(i)) / double(get_samplecount() - 1)); else stderror = std::numeric_limits<double>::max(); }
int main() { // Very basic, NOT comprensive, tests of the recursive methods. int vals[4]; printf("7! = %d\n", fact(7)); printf("31^2 + 32^2 + ... + 200^2 = %d\n", sumsq(31, 200)); vals[0] = 37; vals[1] = 48; vals[2] = 56; vals[3] = 63; printf("vals has %d Odd values.\n", ArrayOdd(vals, 4)); print_reverse("writethisbackwards", 18); printf("\n"); printf("3^10 = %d\n", powerA(3,10)); printf("3^11 = %d\n", powerB(3,11)); if (Rbinary(33, vals, 0, 3) == -1) printf("33 was not found in vals.\n"); dectobin(179); printf("\n"); if (check("madamimadam", 11)) printf("madamimadam is a palindrome.\n"); printf("The 27th Fibonacci number is %d\n", fibonacci(27)); // Test of fast exponentiation vs. regular version int start = time(0); int ans1 = slowModPow(6874, 1000000000, 13713); int end1 = time(0); int ans2 = modPow(6874, 1000000000, 13713); int end2 = time(0); printf("ans1 = %d, ans2 = %d.\n", ans1, ans2); printf("slowModExp took %d sec.\n", end1-start); printf("modPow took %d sec.\n", end2-end1); system("PAUSE"); return 0; }
Real3x3 angle_axis_to_rotation_matrix(Real3 const& angle_axis) { auto n = sqrt(sumsq(angle_axis)); if (n < 0.0001) return vec( vec(1., 0., 0.), vec(0., 1., 0.), vec(0., 0., 1.) ); Real x = angle_axis[0] / n; Real y = angle_axis[1] / n; Real z = angle_axis[2] / n; Real s = sin(n); Real c = cos(n); return vec( vec(x*x + (1. - x*x)*c, x*y*(1. - c) - z*s, x*z*(1. - c) + y*s), vec(x*y*(1. - c) + z*s, y*y + (1. - y*y)*c, y*z*(1. - c) - x*s), vec(x*z*(1. - c) - y*s, z*y*(1. - c) + x*s, z*z + (1. - z*z)*c)); }
SpdMatrix WishartModel::simdat(){ return rWish(nu(), sumsq()); }
double WishartModel::logp(const SpdMatrix &W) const{ return dWish(W, sumsq(), nu(), true); }
int dim()const {return sumsq().nrow();}
SpdMatrix WMS::var_hat()const{ if(sumw()==0) return sumsq()*0.0; return center_sumsq()/sumw(); }