/*
    Copyright (C) 2011 Brendon J. Brewer
    This file is part of DNest, the Diffusive Nested Sampler.

    DNest is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DNest is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DNest.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "TestModel.h"
#include "Utils.h"
#include "RandomNumberGenerator.h"
#include <cmath>

using namespace std;
using namespace DNest;

	TestModel::TestModel()
	:x(20)
	{

	}

	TestModel::~TestModel()
	{

	}

	Model* TestModel::factory() const
	{
		return new TestModel;
	}

	Model* TestModel::clone() const
	{
		return new TestModel(*this);
	}

	void TestModel::copyFrom(const Model* other)
	{
		*this = *((TestModel*)other);
	}

	void TestModel::fromPrior()
	{
		for(size_t i=0; i<x.size(); i++)
			x[i] = -0.5 + randomU();
		Model::fromPrior();
		calculateLogLikelihood();
	}

	double TestModel::perturb()
	{
		int which = randInt(x.size());
		x[which] += pow(10.0, 1-6*randomU())*randn();
		x[which] = mod(x[which] + 0.5, 1.0) - 0.5;
		Model::perturb();
		calculateLogLikelihood();
		return 0;
	}

	void TestModel::calculateLogLikelihood()
	{
		static const double u = 0.01;
		static const double v = 0.1;

		double C = log(1.0/sqrt(2*pi));
		double logl1 = 0;
		double logl2 = 0;

		for(size_t i=0; i<x.size(); i++)
		{
			logl1 += C - log(u) - 0.5*pow((x[i] - 0.031)/u, 2);
			logl2 += C - log(v) - 0.5*pow(x[i]/v, 2);
		}
		logl1 += log(100.0);

		logl.logl = logsumexp(logl1, logl2);
	}

	void TestModel::print(ostream& out) const
	{
		for(size_t i=0; i<x.size(); i++)
			out<<x[i]<<' ';
	}

