Example #1
0
	def observe(self, intrinsicLC, noiseSeed = None, tnum = None):
		if tnum is None:
			tnum = 0
		randSeed = np.zeros(1, dtype = 'uint32')
		if noiseSeed is None:
			rand.rdrand(randSeed)
			noiseSeed = randSeed[0]
		self._taskCython.add_ObservationNoise(intrinsicLC.numCadences, intrinsicLC.dt, intrinsicLC.fracNoiseToSignal, intrinsicLC.t, intrinsicLC.x, intrinsicLC.y, intrinsicLC.yerr, intrinsicLC.mask, noiseSeed, threadNum = tnum)

		count = int(np.sum(intrinsicLC.mask))
		y_meanSum = 0.0
		yerr_meanSum = 0.0
		for i in xrange(intrinsicLC.numCadences):
			y_meanSum += intrinsicLC.mask[i]*intrinsicLC.y[i]
			yerr_meanSum += intrinsicLC.mask[i]*intrinsicLC.yerr[i]
		if count > 0.0:
			intrinsicLC._mean = y_meanSum/count
			intrinsicLC._meanerr = yerr_meanSum/count
		else:
			intrinsicLC._mean = 0.0
			intrinsicLC._meanerr = 0.0
		y_stdSum = 0.0
		yerr_stdSum = 0.0
		for i in xrange(intrinsicLC.numCadences):
			y_stdSum += math.pow(intrinsicLC.mask[i]*intrinsicLC.y[i] - intrinsicLC._mean, 2.0)
			yerr_stdSum += math.pow(intrinsicLC.mask[i]*intrinsicLC.yerr[i] - intrinsicLC._meanerr, 2.0)
		if count > 0.0:
			intrinsicLC._std = math.sqrt(y_stdSum/count)
			intrinsicLC._stderr = math.sqrt(yerr_stdSum/count)
		else:
			intrinsicLC._std = 0.0
			intrinsicLC._stderr = 0.0
Example #2
0
 def sample(self, **kwargs):
     returnLC = self.lcObj.copy()
     probVal = kwargs.get('probability', 1.0)
     sampleSeedVal = kwargs.get('sampleSeed', rand.rdrand(np.array([0], dtype='uint32')))
     np.random.seed(seed=sampleSeedVal)
     keepArray = spstats.bernoulli.rvs(probVal, size=self.lcObj.numCadences)
     newNumCadences = np.sum(keepArray)
     tNew = np.require(np.zeros(newNumCadences), requirements=['F', 'A', 'W', 'O', 'E'])
     xNew = np.require(np.zeros(newNumCadences), requirements=['F', 'A', 'W', 'O', 'E'])
     yNew = np.require(np.zeros(newNumCadences), requirements=['F', 'A', 'W', 'O', 'E'])
     yerrNew = np.require(np.zeros(newNumCadences), requirements=['F', 'A', 'W', 'O', 'E'])
     maskNew = np.require(np.zeros(newNumCadences), requirements=['F', 'A', 'W', 'O', 'E'])
     counter = 0
     for i in xrange(self.lcObj.numCadences):
         if keepArray[i] == 1:
             tNew[counter] = self.lcObj.t[i]
             xNew[counter] = self.lcObj.x[i]
             yNew[counter] = self.lcObj.y[i]
             yerrNew[counter] = self.lcObj.yerr[i]
             maskNew[counter] = self.lcObj.mask[i]
             counter += 1
     returnLC.t = tNew
     returnLC.x = xNew
     returnLC.y = yNew
     returnLC.yerr = yerrNew
     returnLC.mask = maskNew
     returnLC._numCadences = newNumCadences
     returnLC._checkIsRegular()
     returnLC._times()
     returnLC._statistics()
     return returnLC
Example #3
0
 def sample(self, **kwargs):
     returnLC = self.lcObj.copy()
     widthVal = kwargs.get('width', returnLC.T/10.0)
     centerVal = kwargs.get('center', returnLC.T/2.0)
     sampleSeedVal = kwargs.get('sampleSeed', rand.rdrand(np.array([0], dtype='uint32')))
     np.random.seed(seed=sampleSeedVal)
     del returnLC.t
     del returnLC.x
     del returnLC.y
     del returnLC.yerr
     del returnLC.mask
     tList = list()
     xList = list()
     yList = list()
     yerrList = list()
     maskList = list()
     for i in xrange(self.lcObj.numCadences):
         keepYN = np.random.binomial(1, self.normalizedSincSq(widthVal, centerVal, self.lcObj.t[i]))
         if keepYN == 1:
             tList.append(self.lcObj.t[i])
             xList.append(self.lcObj.x[i])
             yList.append(self.lcObj.y[i])
             yerrList.append(self.lcObj.yerr[i])
             maskList.append(self.lcObj.mask[i])
     newNumCadences = len(tList)
     returnLC.t = np.require(np.array(tList), requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.x = np.require(np.array(xList), requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.y = np.require(np.array(yList), requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.yerr = np.require(np.array(yerrList), requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.mask = np.require(np.array(maskList), requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC._numCadences = newNumCadences
     returnLC._checkIsRegular()
     returnLC._times()
     returnLC._statistics()
     return returnLC
Example #4
0
    def fit(self, observedLC, zSSeed=None, walkerSeed=None, moveSeed=None, xSeed=None):
        randSeed = np.zeros(1, dtype='uint32')
        if zSSeed is None:
            rand.rdrand(randSeed)
            zSSeed = randSeed[0]
        if walkerSeed is None:
            rand.rdrand(randSeed)
            walkerSeed = randSeed[0]
        if moveSeed is None:
            rand.rdrand(randSeed)
            moveSeed = randSeed[0]
        if xSeed is None:
            rand.rdrand(randSeed)
            xSeed = randSeed[0]
        xStart = np.require(np.zeros(self.ndims*self.nwalkers), requirements=['F', 'A', 'W', 'O', 'E'])

        fluxEst, periodEst, eccentricityEst, omega1Est, tauEst, a2sinInclinationEst = self.estimate(
            observedLC)

        for walkerNum in xrange(self.nwalkers):
            noSuccess = True
            while noSuccess:
                a1Guess, a2Guess, inclinationGuess = self.guess(a2sinInclinationEst)
                ThetaGuess = np.array(
                    [a1Guess, a2Guess, periodEst, eccentricityEst, omega1Est, inclinationGuess, tauEst,
                        fluxEst])
                res = self.set(ThetaGuess)
                lnPrior = self.logPrior(observedLC)
                if res == 0 and lnPrior == 0.0:
                    noSuccess = False
            for dimNum in xrange(self.ndims):
                xStart[dimNum + walkerNum*self.ndims] = ThetaGuess[dimNum]

        lowestFlux = np.min(observedLC.y[np.where(observedLC.mask == 1.0)[0]])
        highestFlux = np.max(observedLC.y[np.where(observedLC.mask == 1.0)[0]])
        res = self._taskCython.fit_MBHBModel(
            observedLC.numCadences, observedLC.dt, lowestFlux, highestFlux, observedLC.t, observedLC.x,
            observedLC.y, observedLC.yerr, observedLC.mask, self.nwalkers, self.nsteps, self.maxEvals,
            self.xTol, self.mcmcA, zSSeed, walkerSeed, moveSeed, xSeed, xStart, self._Chain,
            self._LnPrior, self._LnLikelihood)

        meanTheta = list()
        for dimNum in xrange(self.ndims):
            meanTheta.append(np.mean(self.Chain[dimNum, :, self.nsteps/2:]))
        meanTheta = np.require(meanTheta, requirements=['F', 'A', 'W', 'O', 'E'])
        self.set(meanTheta)
        devianceThetaBar = -2.0*self.logLikelihood(observedLC)
        barDeviance = np.mean(-2.0*self.LnLikelihood[:, self.nsteps/2:])
        self._pDIC = barDeviance - devianceThetaBar
        self._dic = devianceThetaBar + 2.0*self.pDIC
        return res
Example #5
0
	def fit(self, observedLC, zSSeed = None, walkerSeed = None, moveSeed = None, xSeed = None):
		randSeed = np.zeros(1, dtype = 'uint32')
		if zSSeed is None:
			rand.rdrand(randSeed)
			zSSeed = randSeed[0]
		if walkerSeed is None:
			rand.rdrand(randSeed)
			walkerSeed = randSeed[0]
		if moveSeed is None:
			rand.rdrand(randSeed)
			moveSeed = randSeed[0]
		if xSeed is None:
			rand.rdrand(randSeed)
			xSeed = randSeed[0]
		xStart = np.require(np.zeros(self.ndims*self.nwalkers), requirements=['F', 'A', 'W', 'O', 'E'])

		intrinsicFluxEst, periodEst, eccentricityEst, omega1Est, tauEst, a2sinInclinationEst = self.estimate(observedLC)

		''''ThetaGuess = np.array([0.001, 75.0, 10.0, 0.0, 0.0, 90.0, 0.0, 100.0, 0.5])
		for dimNum in xrange(self.ndims):
			xStart[dimNum] = ThetaGuess[dimNum]
		for walkerNum in xrange(1, self.nwalkers):'''
		for walkerNum in xrange(self.nwalkers):
			noSuccess = True
			while noSuccess:
				sinInclinationGuess = random.uniform(-1.0, 1.0)
				inclinationGuess = (180.0/math.pi)*math.asin(sinInclinationGuess)
				m1Guess = math.pow(10.0, random.uniform(-1.0, 3.0)) # m1 in 1e6*SolarMass
				m2Guess = math.pow(10.0, random.uniform(-1.0, math.log10(m1Guess))) # m1 in 1e6SolarMass
				rS1Guess = ((2.0*self.G*m1Guess*1.0e6*self.SolarMass)/math.pow(self.c, 2.0)/self.Parsec) # rS1 in Parsec
				rS2Guess = ((2.0*self.G*m2Guess*1.0e6*self.SolarMass)/math.pow(self.c, 2.0)/self.Parsec) # rS2 in Parsec
				rPeriEst = math.pow(((self.G*math.pow(periodEst*self.Day, 2.0)*((m1Guess + m2Guess)*1.0e6*self.SolarMass)*math.pow(1.0 + eccentricityEst, 3.0))/math.pow(self.twoPi, 2.0)),1.0/3.0)/self.Parsec
				a1Est = (m2Guess*rPeriEst)/((m1Guess + m2Guess)*(1.0 - eccentricityEst));
				a2Est = (m1Guess*rPeriEst)/((m1Guess + m2Guess)*(1.0 - eccentricityEst));


				ThetaGuess = np.array([rPeriEst, m1Guess, m2Guess, eccentricityGuess, omega1Est, inclinationGuess, tauGuess, totalFluxGuess])
				res = self.set(ThetaGuess)
				lnPrior = self.logPrior(observedLC)
				if res == 0 and lnPrior == 0.0:
					noSuccess = False
			for dimNum in xrange(self.ndims):
				xStart[dimNum + walkerNum*self.ndims] = ThetaGuess[dimNum]

		res = self._taskCython.fit_BinarySMBHModel(observedLC.numCadences, observedLC.dt, lowestFlux, highestFlux, observedLC.t, observedLC.x, observedLC.y, observedLC.yerr, observedLC.mask, self.nwalkers, self.nsteps, self.maxEvals, self.xTol, self.mcmcA, zSSeed, walkerSeed, moveSeed, xSeed, xStart, self._Chain, self._LnPosterior)
		return res
Example #6
0
 def sample(self, **kwargs):
     returnLC = self.lcObj.copy()
     widthVal = kwargs.get('width', returnLC.T / 10.0)
     centerVal = kwargs.get('center', returnLC.T / 2.0)
     sampleSeedVal = kwargs.get('sampleSeed',
                                rand.rdrand(np.array([0], dtype='uint32')))
     np.random.seed(seed=sampleSeedVal)
     del returnLC.t
     del returnLC.x
     del returnLC.y
     del returnLC.yerr
     del returnLC.mask
     tList = list()
     xList = list()
     yList = list()
     yerrList = list()
     maskList = list()
     for i in range(self.lcObj.numCadences):
         keepYN = np.random.binomial(
             1, self.normalizedSincSq(widthVal, centerVal, self.lcObj.t[i]))
         if keepYN == 1:
             tList.append(self.lcObj.t[i])
             xList.append(self.lcObj.x[i])
             yList.append(self.lcObj.y[i])
             yerrList.append(self.lcObj.yerr[i])
             maskList.append(self.lcObj.mask[i])
     newNumCadences = len(tList)
     returnLC.t = np.require(np.array(tList),
                             requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.x = np.require(np.array(xList),
                             requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.y = np.require(np.array(yList),
                             requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.yerr = np.require(np.array(yerrList),
                                requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC.mask = np.require(np.array(maskList),
                                requirements=['F', 'A', 'W', 'O', 'E'])
     returnLC._numCadences = newNumCadences
     returnLC._checkIsRegular()
     returnLC._times()
     returnLC._statistics()
     return returnLC
Example #7
0
 def sample(self, **kwargs):
     returnLC = self.lcObj.copy()
     probVal = kwargs.get('probability', 1.0)
     sampleSeedVal = kwargs.get('sampleSeed',
                                rand.rdrand(np.array([0], dtype='uint32')))
     np.random.seed(seed=sampleSeedVal)
     keepArray = spstats.bernoulli.rvs(probVal, size=self.lcObj.numCadences)
     newNumCadences = np.sum(keepArray)
     tNew = np.require(np.zeros(newNumCadences),
                       requirements=['F', 'A', 'W', 'O', 'E'])
     xNew = np.require(np.zeros(newNumCadences),
                       requirements=['F', 'A', 'W', 'O', 'E'])
     yNew = np.require(np.zeros(newNumCadences),
                       requirements=['F', 'A', 'W', 'O', 'E'])
     yerrNew = np.require(np.zeros(newNumCadences),
                          requirements=['F', 'A', 'W', 'O', 'E'])
     maskNew = np.require(np.zeros(newNumCadences),
                          requirements=['F', 'A', 'W', 'O', 'E'])
     counter = 0
     for i in range(self.lcObj.numCadences):
         if keepArray[i] == 1:
             tNew[counter] = self.lcObj.t[i]
             xNew[counter] = self.lcObj.x[i]
             yNew[counter] = self.lcObj.y[i]
             yerrNew[counter] = self.lcObj.yerr[i]
             maskNew[counter] = self.lcObj.mask[i]
             counter += 1
     returnLC.t = tNew
     returnLC.x = xNew
     returnLC.y = yNew
     returnLC.yerr = yerrNew
     returnLC.mask = maskNew
     returnLC._numCadences = newNumCadences
     returnLC._checkIsRegular()
     returnLC._times()
     returnLC._statistics()
     return returnLC
Example #8
0
#!/usr/bin/env python
"""	Module to draw hardware random numbers.

    For a demonstration of the module, please run the module as a command line program eg.
    bash-prompt$ python randDemo.py --help
    and
    bash-prompt$ python randDemo.py -n 100
"""

import numpy as np
import rand

if __name__ == '__main__':
    import argparse as argparse
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-n',
        '--numRand',
        type=int,
        default=10,
        help=r'Number of hardware random numbers to generate...')
    args = parser.parse_args()

    A = np.zeros(args.numRand, dtype='uint32')
    success = rand.rdrand(A)
    for i in xrange(args.numRand):
        print '%s' % (str(A[i]))
Example #9
0
#!/usr/bin/env python
"""	Module to draw hardware random numbers.

    For a demonstration of the module, please run the module as a command line program eg.
    bash-prompt$ python randDemo.py --help
    and
    bash-prompt$ python randDemo.py -n 100
"""

import numpy as np
import rand

if __name__ == '__main__':
    import argparse as argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('-n', '--numRand', type=int, default=10,
                        help=r'Number of hardware random numbers to generate...')
    args = parser.parse_args()

    A = np.zeros(args.numRand, dtype='uint32')
    success = rand.rdrand(A)
    for i in xrange(args.numRand):
        print '%s'%(str(A[i]))