Invalid Syntax in else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RivalDealer
    New Member
    • Dec 2013
    • 3

    Invalid Syntax in else statement

    As part of a real coded genetic algorithm, I have to generate a random number beta that follows Laplace Distribution.

    The give relationship is -
    beta(i)
    = a-b*log(u(i)) [if r(i) <= 0.5]
    = a+b*log(u(i)) [if r(i) > 0.5]

    Where i varies from 1 to n (n is the length of the chromosome). r(i) and u(i) are uniform random numbers belonging to the interval [0,1]. a and b are constants.

    I want to make a list of beta values for each i and hence wrote the following lines of code:
    Code:
        bet = []
        for i in range(chromlen):                                   
            if (random.uniform(0,1) <= 0.5):
                bet.append((a-b*(math.log(random.uniform(0,1)+0.000000000001)))
            else:
                bet.append((a+b*(math.log(random.uniform(0,1)+0.000000000001)))
        beta = bet
    This way I have a list of beta values that have been generated randomly and follow Laplace Distribution

    I'm receiving a message when I run the program that says "There's an error in your program: invalid syntax" and it highlights 'else' in red.

    I can't figure out where I made a mistake. Please help. Thanks.

    I've imported 'random' and 'math' earlier in the code. As I mentioned before, this is just part of the code.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You are missing a closing parens, ), on the previous line. Also, you are using two different random numbers here and the second one does not have to be < 0.5.
    Code:
            if (random.uniform(0,1) <= 0.5):
                 bet.append((a-b*(math.log(random.uniform(0,1)+0.000000000001)))
    
    ## use instead
            this_random = random.uniform(0,1)
            if (this_random <= 0.5):
                 bet.append((a-b*(math.log(this_random+0.000000000001))))    ## parens added

    Comment

    • RivalDealer
      New Member
      • Dec 2013
      • 3

      #3
      Thank you for your reply! The 2 numbers are supposed to be different as can be noticed from the relation I mentioned above. However, the parenthesis bit helped :)

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Then the if/else statements make no sense as you append a new/different/second value whether the first random was < 0.5 or not. See the code example above to use the original, tested random to append to the list.
        Code:
                first = random.uniform(0,1)
                print "testing %f for <= 0.5" % (first)
                if first <= 0.5:
                     second = random.uniform(0,1)
                     print "appending %f" % (second)
                     bet.append((a-b*(math.log(second+0.000000000001)))

        Comment

        • RivalDealer
          New Member
          • Dec 2013
          • 3

          #5
          Hey, I'm sorry if there's any confusion been created from my side but the if and else statement is required because different expressions for beta(i) are obtained depending on the value of 'first'.

          beta(i) = a - b*log(u(i)) if first <= 0.5
          beta(i) = a + b*log(u(i)) if first > 0.5

          I understand that 'second' comes into play only when the conditions concerning first are satisfied.

          Comment

          Working...