Exemplo n.º 1
0
def MCMC_PSPVP(MCMC_List):
    MCMCInit=MCMC_List
    
    L = int(MCPAR[1])
    Stepnumber= int(MCPAR[2])
        
    SampleMatrix=np.zeros([Stepnumber,L+1]) 
    SampleMatrix[0,:]=MCMCInit
    Move = np.zeros([L+1])
    
    ChiPrior = MCMCInit[L]
    for step in np.arange(1,Stepnumber,1): 
        Temp = SampleMatrix[step-1,:]
        print(Temp)
        for p in range(L-3):
            StepControl = MCPAR[5]+MCPAR[6]*np.random.random_sample()
            Move[p] = (FITPARUB[p]-FITPARLB[p])/StepControl*(np.random.random_sample()-0.5) # need out of bounds check
        Temp=Temp+Move
        
        SimPost=SimInt_PSPVP(Temp)
        ChiPost=np.sum(CD.Misfit(Intensity,SimPost))
        if ChiPost < ChiPrior:
            SampleMatrix[step,0:L]=Temp[0:L]
            SampleMatrix[step,L]=ChiPost
        else:
            MoveProb = np.exp(-0.5*np.power(ChiPost-ChiPrior,2))
            if np.random.random_sample() < MoveProb:
                SampleMatrix[step,0:L]=Temp[0:L]
                SampleMatrix[step,L]=ChiPost
            else:
                SampleMatrix[step,:]=SampleMatrix[step-1,:]
                
    return SampleMatrix
def MCMCInit_ID1(FITPAR,FITPARLB,FITPARUB,MCPAR):
    
    MCMCInit=np.zeros([int(MCPAR[0]),int(MCPAR[1])+1])
    for i in range(int(MCPAR[0])):
        if i <MCPAR[3]: #reversed from matlab code assigns all chains below randomnumber as random chains
            for c in range(int(MCPAR[1])):
                MCMCInit[i,c]=FITPARLB[c]+(FITPARUB[c]-FITPARLB[c])*np.random.random_sample()
                (SimInt,Amplitude)=SimInt_ID1(MCMCInit[i,:])
            C=np.sum(CD.Misfit(Intensity2,SimInt))
            MCMCInit[i,int(MCPAR[1])]=C
            
        else:
            MCMCInit[i,0:int(MCPAR[1])]=FITPAR
            (SimInt,Amplitude)=SimInt_ID1(MCMCInit[i,:])
            C=np.sum(CD.Misfit(Intensity2,SimInt))
            MCMCInit[i,int(MCPAR[1])]=C
            
           
    return MCMCInit
Exemplo n.º 3
0
def MCMC_LAM1(MCMC_List):

    MCMCInit = MCMC_List

    L = int(MCPAR[1])
    Stepnumber = int(MCPAR[2])

    SampledMatrix = np.zeros([Stepnumber, L + 1])
    SampledMatrix[0, :] = MCMCInit
    Move = np.zeros([L + 1])

    ChiPrior = MCMCInit[L]
    for step in np.arange(1, Stepnumber, 1):
        Temp = SampledMatrix[step - 1, :].copy()
        for p in range(L - 1):
            StepControl = MCPAR[5] + MCPAR[6] * np.random.random_sample()
            Move[p] = (FITPARUB[p] - FITPARLB[p]) / StepControl * (
                np.random.random_sample() - 0.5)  # need out of bounds check
            Temp[p] = Temp[p] + Move[p]
            if Temp[p] < FITPARLB[p]:
                Temp[p] = FITPARLB[p] + (FITPARUB[p] - FITPARLB[p]) / 1000
            elif Temp[p] > FITPARUB[p]:
                Temp[p] = FITPARUB[p] - (FITPARUB[p] - FITPARLB[p]) / 1000
        SimPost = SimInt_LAM1(Temp)
        ChiPost = np.sum(CD.Misfit(Intensity, SimPost))
        if ChiPost < ChiPrior:
            SampledMatrix[step, 0:L] = Temp[0:L]
            SampledMatrix[step, L] = ChiPost
            ChiPrior = ChiPost

        else:
            MoveProb = np.exp(-0.5 * np.power(ChiPost - ChiPrior, 2))
            if np.random.random_sample() < MoveProb:
                SampledMatrix[step, 0:L] = Temp[0:L]
                SampledMatrix[step, L] = ChiPost
                ChiPrior = ChiPost
            else:
                SampledMatrix[step, :] = SampledMatrix[step - 1, :]
    AcceptanceNumber = 0
    Acceptancetotal = len(SampledMatrix[:, 1])

    for i in np.arange(1, len(SampledMatrix[:, 1]), 1):
        if SampledMatrix[i, 0] != SampledMatrix[i - 1, 0]:
            AcceptanceNumber = AcceptanceNumber + 1
    AcceptanceProbability = AcceptanceNumber / Acceptancetotal
    print(AcceptanceProbability)
    ReSampledMatrix = np.zeros(
        [int(MCPAR[2]) / int(MCPAR[4]),
         len(SampledMatrix[1, :])])

    c = -1
    for i in np.arange(0, len(SampledMatrix[:, 1]), MCPAR[4]):
        c = c + 1
        ReSampledMatrix[c, :] = SampledMatrix[i, :]
    return (ReSampledMatrix)
Exemplo n.º 4
0
def MCMC_ID1(MCMC_List):

    MCMCInit = MCMC_List

    L = int(MCPAR[1])
    Stepnumber = int(MCPAR[2])

    SampledMatrixI = np.zeros([Stepnumber, L + 1])
    SampledMatrixI[0, :] = MCMCInit

    Move = np.zeros([L + 1])

    ChiPrior = MCMCInit[L]
    for step in np.arange(1, Stepnumber, 1):
        Temp = SampledMatrixI[step - 1, :].copy()
        for p in range(L - 1):
            StepControl = MCPAR[5] + MCPAR[6] * np.random.random_sample()
            Move[p] = (FITPARUB[p] - FITPARLB[p]) / StepControl * (
                np.random.random_sample() - 0.5)  # need out of bounds check
            Temp[p] = Temp[p] + Move[p]
            if Temp[p] < FITPARLB[p]:
                Temp[p] = FITPARLB[p] + (FITPARUB[p] - FITPARLB[p]) / 1000
            elif Temp[p] > FITPARUB[p]:
                Temp[p] = FITPARUB[p] - (FITPARUB[p] - FITPARLB[p]) / 1000
        (SimPost, AmpPost) = SimInt_ID1(Temp)
        ChiPost = np.sum(CD.Misfit(Intensity2, SimPost))
        if ChiPost < ChiPrior:
            SampledMatrixI[step, 0:L] = Temp[0:L]
            SampledMatrixI[step, L] = ChiPost
            ChiPrior = ChiPost

        else:
            MoveProb = np.exp(-0.5 * np.power(ChiPost - ChiPrior, 2))
            if np.random.random_sample() < MoveProb:
                SampledMatrixI[step, 0:L] = Temp[0:L]
                SampledMatrixI[step, L] = ChiPost
                ChiPrior = ChiPost
            else:
                SampledMatrixI[step, :] = SampledMatrixI[step - 1, :]

    ReSampledMatrixI = np.zeros(
        [int(MCPAR[2]) / int(MCPAR[4]),
         len(SampledMatrixI[1, :])])

    c = -1
    for i in np.arange(0, len(SampledMatrixI[:, 1]), MCPAR[4]):
        c = c + 1
        ReSampledMatrixI[c, :] = SampledMatrixI[i, :]
    (UNCT_Param) = Uncertainty1T(ReSampledMatrixI)
    return (UNCT_Param)  #ReSampledMatrixI
Exemplo n.º 5
0
def MCMCInit_PSPVPUniform(FITPAR,FITPARLB,FITPARUB,MCPAR):
    
    MCMCInit=np.zeros([int(MCPAR[0]),int(MCPAR[1])+1])
    
    for i in range(int(MCPAR[1])-3):
        if FITPARUB[i]==FITPARLB[i]:
            MCMCInit[:,i]=FITPAR[i]
        else:
            A= np.arange(FITPARLB[i],FITPARUB[i]+0.0001,(FITPARUB[i]-FITPARLB[i])/(int(MCPAR[0])-1))
            R=np.random.rand(int(MCPAR[0]))
            ind=R.argsort()
            A=A[ind]
            MCMCInit[:,i]=A
    MCMCInit[:,int(MCPAR[1])-3:int(MCPAR[1])]=FITPAR[int(MCPAR[1])-3:int(MCPAR[1])]        
    
    for i in range(int(MCPAR[0])):
       SimInt=SimInt_PSPVP(MCMCInit[i,:])
       C=np.sum(CD.Misfit(Intensity,SimInt))
       MCMCInit[i,int(MCPAR[1])]=C
        
    return MCMCInit
Exemplo n.º 6
0
    SLD[2, 0] = SLD1
    SLD[3, 0] = SLD1

    Coord = CD.ID1CoordAssign(TPAR, SLD, Trapnumber, Pitch)
    #CDp.plotID1(Coord,Trapnumber,Pitch)
    (FITPAR, FITPARLB, FITPARUB) = CD.PBA_ID1(TPAR, SPAR, Trapnumber)

    R = np.random.normal(0, 0.225, [len(Qx[:, 0]), len(Qx[0, :])])
    (Intensity, Amplitude) = SimInt_ID1(FITPAR)

    N = (1 / (np.power(Intensity, 0.5))) * Intensity  # Generates  noise
    N = N * R
    Intensity2 = Intensity + R
    # Applies Noise

    C = CD.Misfit(Intensity, Intensity2)
    Chi2 = np.sum((C))

    MCPAR = np.zeros([7])
    MCPAR[0] = 1  # Chainnumber
    MCPAR[1] = len(FITPAR)
    MCPAR[2] = 5000  #stepnumber
    MCPAR[3] = 0  #randomchains
    MCPAR[4] = 20  # Resampleinterval
    MCPAR[5] = 100  # stepbase
    MCPAR[6] = 100  # steplength

    MCMCInitial = MCMCInit_ID1(FITPAR, FITPARLB, FITPARUB, MCPAR)
    Acceptprob = 0
    while Acceptprob < 0.3 or Acceptprob > 0.45:
        L = int(MCPAR[1])
    SLD[2, 0] = SLD1
    SLD[3, 0] = SLD1

    Coord = CD.ID1CoordAssign(TPAR, SLD, Trapnumber, Pitch)
    #CDp.plotID1(Coord,Trapnumber,Pitch)
    (FITPAR, FITPARLB, FITPARUB) = CD.PBA_ID1(TPAR, SPAR, Trapnumber)

    R = np.random.normal(0, 0.225, [len(Qx[:, 0]), len(Qx[0, :])])
    (Intensity, Amplitude) = SimInt_ID1(FITPAR)

    N = (1 / (np.power(Intensity, 0.5))) * Intensity  # Generates  noise
    N = N * R
    Intensity2 = Intensity + R
    # Applies Noise

    C = CD.Misfit(Intensity, Intensity2)
    Chi2 = np.sum((C))

    MCPAR = np.zeros([7])
    MCPAR[0] = 6  # Chainnumber
    MCPAR[1] = len(FITPAR)
    MCPAR[2] = 1000  #stepnumber
    MCPAR[3] = 0  #randomchains
    MCPAR[4] = 20  # Resampleinterval
    MCPAR[5] = 350  # stepbase
    MCPAR[6] = 350  # steplength

    #    MCMCInitial=MCMCInit_ID1(FITPAR,FITPARLB,FITPARUB,MCPAR)
    #    Acceptprob=0;
    #    while Acceptprob < 0.3 or Acceptprob > 0.45:
    #        L = int(MCPAR[1])
Exemplo n.º 8
0
    Move = np.zeros([L + 1])

    ChiPrior = MCMCInitial[0, L]
    for step in np.arange(1, Stepnumber, 1):
        Temp = SampledMatrix[step - 1, :].copy()
        for p in range(L - 1):
            StepControl = MCPAR[5] + MCPAR[6] * np.random.random_sample()
            Move[p] = (FITPARUB[p] - FITPARLB[p]) / StepControl * (
                np.random.random_sample() - 0.5)  # need out of bounds check
            Temp[p] = Temp[p] + Move[p]
            if Temp[p] < FITPARLB[p]:
                Temp[p] = FITPARLB[p] + (FITPARUB[p] - FITPARLB[p]) / 1000
            elif Temp[p] > FITPARUB[p]:
                Temp[p] = FITPARUB[p] - (FITPARUB[p] - FITPARLB[p]) / 1000
        (SimPost) = SimInt_LAM1(Temp)
        ChiPost = np.sum(CD.Misfit(Intensity, SimPost))
        if ChiPost < ChiPrior:
            SampledMatrix[step, 0:L] = Temp[0:L]
            SampledMatrix[step, L] = ChiPost
            ChiPrior = ChiPost

        else:
            MoveProb = np.exp(-0.5 * np.power(ChiPost - ChiPrior, 2))
            if np.random.random_sample() < MoveProb:
                SampledMatrix[step, 0:L] = Temp[0:L]
                SampledMatrix[step, L] = ChiPost
                ChiPrior = ChiPost
            else:
                SampledMatrix[step, :] = SampledMatrix[step - 1, :]
    AcceptanceNumber = 0
    Acceptancetotal = len(SampledMatrix[:, 1])
Exemplo n.º 9
0
    Offset = FITPAR[T * 5:T * 5 + 7]
    SPAR = FITPAR[T * 5 + 7:T * 5 + 11]
    Coord = CD.PSPVPCoord(Spline, MCoord, Trapnumber, Disc, Pitch, Offset)
    F1 = CD.FreeFormTrapezoid(Coord[:, :, 0], Qx, Qz, Disc + 1)
    F2 = CD.FreeFormTrapezoid(Coord[:, :, 1], Qx, Qz, Disc + 1)
    F3 = CD.FreeFormTrapezoid(Coord[:, :, 2], Qx, Qz, Disc + 1)
    F4 = CD.FreeFormTrapezoid(Coord[:, :, 3], Qx, Qz, Disc + 1)
    F5 = CD.FreeFormTrapezoid(Coord[:, :, 4], Qx, Qz, Disc + 1)
    F6 = CD.FreeFormTrapezoid(Coord[:, :, 5], Qx, Qz, Disc + 1)
    F7 = CD.FreeFormTrapezoid(Coord[:, :, 6], Qx, Qz, Disc + 1)
    F8 = CD.FreeFormTrapezoid(Coord[:, :, 7], Qx, Qz, Disc + 1)
    Formfactor = (F1 + F2 + F3 + F4 + F5 + F6 + F7 + F8)
    M = np.power(
        np.exp(-1 * (np.power(Qx, 2) + np.power(Qz, 2)) *
               np.power(SPAR[0], 2)), 0.5)
    Formfactor = Formfactor * M
    SimInt = np.power(abs(Formfactor), 2) * SPAR[1] + SPAR[2]
    return SimInt


(FITPAR, FITPARLB, FITPARUB) = CD.PSPVP_PB(Offset, Spline, SPAR, Trapnumber,
                                           Disc)

F2 = np.zeros([len(FITPAR) + 1])
F2[0:len(FITPAR)] = FITPAR
Sim = SimInt_PSPVP(F2)
C = np.sum(CD.Misfit(Intensity, Sim))

plt.semilogy(Qz[:, 4], Intensity[:, 4], '.')
plt.semilogy(Qz[:, 4], Sim[:, 4])
Exemplo n.º 10
0
ppar[2,0]=0.5;  ppar[2,1]=0.2;  ppar[2,2]=0;

Pitch = 135.7;

DW = 1.5
I0 = 0.005
Bk = 33.4
SPAR=np.zeros([4,1])
SPAR[0,0]=DW; SPAR[1,0]=I0; SPAR[2,0]=Bk;SPAR[3,0]=Pitch

Coord=CD.SCNCoordAssign(tpar,Trapnumber,X1,X2,X3,Pitch)
SimInt = CD.SCNIntensitySim(Coord,Qx,Qz,Trapnumber,DW,I0,Bk)
Coordp=CD.SCNParabolaCoord(tpar,ppar,Discretization,Trapnumber, X1, X2, Pitch)
SimIntp = CD.SCNIntensitySim(Coordp,Qx,Qz,Trapnumber+Discretization-1,DW,I0,Bk)

CD.PlotQzCut(Qz,SimIntp,Intensity,26)

Chi2=CD.Misfit(Intensity,SimInt)
C=Chi2.sum()
#%%
[a, b, c,d,e,f,g,h]=CD.ParBoundSCN(tpar,ppar,SPAR,X)

#%%

MCPAR=np.zeros[4,1]
MCPAR[0,0] = 10 # Chainnumber
MCPAR[1,0] = len(FITPAR)
MCPAR[1,0] = 10 #stepnumber
MCPAR[2,0] = 0 #randomchains