Error TypeError: a float is required

3

This is my code:

### BIBLIOTECAS

import scipy.special as sps
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
from scipy.stats import norm
from scipy.stats import gamma
from math import exp
import operator
import csv


########################################

############### GERAR VALORES DA DISTRIBUIO GE ################

def rge(n, alpha, beta):
    u = np.random.uniform(low=0, high=1, size=n)
    x = -beta * np.log((-1 / alpha) * np.log(u))
    return (x)


teste = rge(100, 1, 3)
plt.hist(teste)
plt.show()

temp = pd.DataFrame(teste)


########################################

### FUNCAO DE DISTRIBUICAO

#   t[0] = alfa
#   t[1] = beta

def fx(x, t):
    prod = 1.0
    for i in range(0, len(x)):
        prod *= (t[0] / t[1]) * exp(- (x[i] / t[1])) * exp(-t[0] * exp(-(x[i] / t[1])))
    return prod

##########################################

##### FUNCAO DE VEROSSIMILHANCA


def L(x, t):
    return fx(x, t)

##########################################

### FUNCAO MCMC

def mcmc(N, k={"t0": 1, "t1": 1}, x=[]):
    chute = {"t0": [1], "t1": [1]}
    M = chute
    hiper = {"t0": [0.1, 0.1], "t1": [0.1, 0.1]}  # VALORES DOS HIPERPARAMETROS
    contador = {"t0": [], "t1": []}  # CONTADOR PARA TAXA DE ACEITACAO

    thetas = M.keys()
    for i in range(N - 1):
        for j in thetas:

            if j == "t0":

                M[j].append(np.random.gamma(shape=M[j][-1], scale=k[j]))

                lista = [[M[l][-1] for l in thetas], [M[l][-1] if l != j else M[l][-2] for l in thetas]]
                t1 = gamma.pdf(M[j][-1], a=hiper[j][0], scale=hiper[j][1]) * L(x, lista[0]) * gamma.pdf(M[j][-2],
                                                                                                           a=M[j][-1],
                                                                                                           scale=k[j])
                t2 = gamma.pdf(M[j][-2], a=hiper[j][0], scale=hiper[j][1]) * L(x, lista[1]) * gamma.pdf(M[j][-1],

                                                                                                           loc=M[j][-2],
                                                                                                           scale=k[j])
                teste = (t1 / t2)

            else:

                M[j].append(np.random.gamma(shape=M[j][-1], scale=k[j]))

                lista = [[M[l][-1] for l in thetas], [M[l][-1] if l != j else M[l][-2] for l in thetas]]
                t1 = gamma.pdf(M[j][-1], a=hiper[j][0], scale=hiper[j][1]) * L(x, lista[0]) * gamma.pdf(M[j][-2],
                                                                                                           a=M[j][-1],
                                                                                                           scale=k[j])
                t2 = gamma.pdf(M[j][-2], a=hiper[j][0], scale=hiper[j][1]) * L(x, lista[1]) * gamma.pdf(M[j][-1],
                                                                                                           a=M[j][-2],
                                                                                                           scale=k[j])

                teste = (t1 / t2)

            if (min(1, teste) < np.random.uniform(low=0, high=1)) or (np.isinf(teste)) or (np.isnan(teste)):
                M[j][-1] = M[j][-2]
                contador[j].append(0)
            else:
                contador[j].append(1)

    print("Tamanho do theta 0 : " + str(len(M["t0"])))
    print("\nTamanho do theta 1 : " + str(len(M["t1"])))

    M = pd.DataFrame.from_dict(M)
    contador = pd.DataFrame.from_dict(contador)
    cont = contador.apply(sum)
    print(cont)

    return (M)


N = int(input("Entre com o N: "))

MP = mcmc(N=N, x=temp)

print(MP)

And you are generating the following error:

Traceback (most recent call last):
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 116, in <module>
    MP = mcmc(N=N, x=temp)
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 74, in mcmc
    t1 = gamma.pdf(M[j][-1], a=hiper[j][0], scale=hiper[j][1]) * L(x, lista[0]) * gamma.pdf(M[j][-2],
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 53, in L
    return fx(x, t)
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 44, in fx
    prod *= (t[0] / t[1]) * exp(- (x[i] / t[1])) * exp(-t[0] * exp(-(x[i] / t[1])))
  File "/home/karlla/anaconda2/lib/python2.7/site-packages/pandas/core/series.py", line 78, in wrapper
    "{0}".format(str(converter)))
TypeError: cannot convert the series to <type 'float'>

I can not fix this.

    
asked by anonymous 23.12.2016 / 13:16

1 answer

5

The error that is occurring is due to the fact that you are using an object DataFrame of Pandas and not a list of values (in the contents of the x variable). If you put, right there at the beginning of the mcmc function as follows:

print(x) 

You will see something like this result (in the test I used N = 5):

Entre com o N: 5
            0
0   -1.056996
1    2.015035
2    1.401659
3    3.210307
..        ...
98   5.822160
99  -4.474483

[100 rows x 1 columns]

That is, its x variable is a DataFrame with 100 rows and 1 column. The column has label 0 (which appears right there at the top of the column), so when you do x[i] (in the calculation inside the fx function) you are trying to get the label column i . Your code would give error when i was different from 0, but it is already giving error at the beginning because you are trying to use the entire x[0] column (make print(x[0]) and you will notice that it returns the entire column) in place of an expected actual value (that is, a float ).

That is, try switching your code to the following:

def fx(x, t):
    prod = 1.0
    for i in range(0, len(x)):
        prod *= (t[0] / t[1]) * exp(- (x[0][i] / t[1])) * exp(-t[0] * exp(-(x[0][i] / t[1])))
    return prod
  

Note the use of x[0][i] (that is, the value at the i position of the single   column 0 ) instead of x[i] .

After "fixing" this, your code gives another error later (it complains that on the line with t2 = gamma.pdf ... you are not reporting the required a argument), but it works (that is, it runs without errors - I do not know if it produces the result you want).

Finally, if you are going to use DataFrame, use it correctly, or convert it to a list by doing (and in that case your code in fx can be kept as originally):

MP = mcmc(N=N, x=temp[0].tolist())
    
26.12.2016 / 13:54