sirzento
sirzento

Reputation: 707

translation error from python to lua. Where is my error?

After asking and deleting a few questions, I dont know where the error is in my code. the expected output is:

[0.6759289682539686, 0.6759289682539686, 0.6759289682539686, 0.6759289682539686, 0.6759289682539686, 0.31500873015873027, 0.12156230158730162, 0.5246873015873018, 0.5989928571428574, 0.060103968253968264]

But my lua code only generates:

3.2328
3.2328
3.2328
3.2328
3.2328
3.2328
3.2328
3.2328
3.2328
3.2328

I really dont know where I made an error. Maybe you guys can spot it. I tried a few changes but it always mades no different. I also dont get why every row has the same number.

python code that works:

from math import prod
from fractions import Fraction
def bitstrings(n) :
    """Return all possible bitstrings of length n"""
    if n == 0 :
        yield []
        return
    else :
        for b in [0,1] :
            for x in bitstrings(n-1) :
                yield [b] + x


def prob_selected(weights, num_selected = 5) :

    # P(n generated, including e)*P(e of n selected | n generated including e)
    # i.e. Sum_n (n generated, including e) * #num_selections / #generated
    # num_selected = how many will be drawn out of the hat (at most)

    n = len(weights)
    final_probability = [0] * n
    
    for bits in bitstrings(n) :
        num_generated = sum(bits)
        prob_generated = prod([w if b else (1-w) for (w,b) in zip(weights, bits)])
        
        for i in range(n) :
            if bits[i] :
                final_probability[i] += prob_generated * min(num_selected, num_generated) / num_generated
    return final_probability


print(prob_selected([1, 1, 1, 1, 1,
                     0.5, 0.2, 0.8, 0.9, 0.1]))

My lua code:

-- python len()
function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

-- python sum()
table.reduce = function (list, fn) 
    local acc
    for k, v in ipairs(list) do
        if 1 == k then
            acc = v
        else
            acc = fn(acc, v)
        end 
    end 
    return acc 
end

globalArr = {}
function generateBitstrings (n, arr, i)
    if i == n then
        table.insert(globalArr, {table.unpack(arr)})
        return
    end
    
    arr[i] = 0
    generateBitstrings(n, arr, i + 1)
    
    arr[i] = 1
    generateBitstrings(n, arr, i + 1)
end

function prob_selected (weights, num_selected)
    local n = tablelength(weights)
    final_probability = {}
    
    for i=1, n do
        final_probability[i] = 0
    end
    
    globalArr = {}
    generateBitstrings(n + 1, {}, 1)
    for ibots, bits in ipairs(globalArr) do 
        num_generated = table.reduce(
            bits,
            function(a, b)
                return a + b
            end
        )

        prob_generated = 1
        bitsLength = tablelength(bits)
        for i=1,bitsLength do
            if bits[i] then
                prob_generated = prob_generated * weights[i]
            else
                prob_generated = prob_generated * 1 - weights[i]
            end
        end
        
        for i=1,n do
            if bits[i] == 1 then
                final_probability[i] = final_probability[i] + (prob_generated * math.min(num_selected, num_generated) / num_generated)
            end
        end
    end
    return final_probability
end

for i, value in ipairs(prob_selected({1, 1, 1, 1, 1,0.5, 0.2, 0.8, 0.9, 0.1}, 5)) do
    print(value)
end

Upvotes: 0

Views: 46

Answers (1)

Alexander Mashin
Alexander Mashin

Reputation: 4617

  1. As @Egor Skriptunoff said, if bits[i] will not work as you expect in Lua: 0 is not falsy; only false and nil are. You need if bits[i] == 1.
  2. You forgot the parentheses in prob_generated = prob_generated * (1 - weights[i]),
  3. I added several local's, although that was not critical,
  4. I made globalArr local; you may want to rename it,
  5. I made the test output more compact.
-- python len()
local function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

-- python sum()
table.reduce = function (list, fn) 
    local acc
    for k, v in ipairs(list) do
        if 1 == k then
            acc = v
        else
            acc = fn(acc, v)
        end 
    end 
    return acc 
end

local function generateBitstrings (global_arr, n, arr, i)
    if i == n then
        table.insert(global_arr, {table.unpack(arr)})
        return
    end
    
    arr[i] = 0
    generateBitstrings(global_arr, n, arr, i + 1)
    
    arr[i] = 1
    generateBitstrings(global_arr, n, arr, i + 1)
end

local function prob_selected (weights, num_selected)
    local n = tablelength(weights)
    local final_probability = {}
    
    for i=1, n do
        final_probability[i] = 0
    end
    
    local globalArr = {}
    generateBitstrings(globalArr, n + 1, {}, 1)
    for ibots, bits in ipairs(globalArr) do 
        local num_generated = table.reduce(
            bits,
            function(a, b)
                return a + b
            end
        )

        local prob_generated = 1
        local bitsLength = tablelength(bits)
        for i=1,bitsLength do
            if bits[i] == 1 then
                prob_generated = prob_generated * weights[i]
            else
                prob_generated = prob_generated * (1 - weights[i])
            end
        end
        
        for i=1,n do
            if bits[i] == 1 then
                final_probability[i] = final_probability[i] + (prob_generated * math.min(num_selected, num_generated) / num_generated)
            end
        end
    end
    return final_probability
end

print (table.concat (prob_selected({1, 1, 1, 1, 1,0.5, 0.2, 0.8, 0.9, 0.1}, 5), ', '))

Upvotes: 2

Related Questions