#### Othko97

• SotK Beta
• Patrikios
Voting Member
• Posts: 3243
• Karma: 9
« on: November 11, 2014, 07:13:04 PM »
A general thread for help with coding.

To start off: I'm trying to write a parser for statistical distributions, however I have run into a minor problem. When I use the parser to find the expected value of the distribution, it gives me the variance. I honestly cannot see my fault, especially seeing as how just using my modules for any distribution works like a charm.

Code: [Select]
`"""Parser for Statistical Distributions"""from PoiDist import * #Imports Poisson functionsfrom BinDist import * #Imports Binomial functionsfrom UniDist import * #Imports Uniform Continuous functionsglobal DistDict #Dictionary to store distributionsDistDict = {} #Sets up dictionaryclass Parser(): def __init__(self): pass def parse(self, string): #Parse function for parser if "~" in string: #If input contains a tilde key, denoting a new distribution listA = string.split("~") #Creates a list split on the tilde, allowing separation of name name = listA[0] #Declares variable name listB = listA[1].split("(") #Splits ListA[1] to allow the type of distribution to be found type = str(listB[0]) #Declares variable type if type == "B": #If a binomial distribution listC = listB[1].split(",") #Splits listC to show number of trials and probability of success n = str(listC[0]) #Sets n to number of trials n = int(n) #Converts n to integer p = str(listC[1][:-1]) #Sets p to probability of success p = float(p) #Converts p to float DistDict[name] = B(n, p) #Adds distribution to the dictionary if type == "U": #If a continuous uniform distribution listC = listB[1].split(",") #Splits listC to show upper and lower bounds a = str(listC[0]) #Sets a to lower bound a = float(a) #Converts a to float b = str(listC[1][:-1]) #Sets b to upper bound b = float(b) #Converts b to float DistDict[name] = U(a, b) #Adds distribution to the dictionary if type == "Po": #If a poisson distribution l = str(listB[1][0:-1]) #Sets l to average/variance l = float(l) #Converts l to float DistDict[name] = Po(l) #Adds distribution to dictionary elif "Var" or "var" in string: #If asking for a variance listA = string.split("(") #Splits to find distribution wanted print(DistDict[listA[1][:-1]].Var()) #Prints the variance of the requested distribution elif "E" or "e" in string: #If asking for an expected value listA = string.split("(") #Splits to find distribution wanted print(DistDict[listA[1][:-1]].E()) #Prints the expected value of the requested distribution`
Alcohol and Calculus don't mix. Never Drink and Derive.

I am Othko, the master of the 23 techniques of death, murder of the 8 popes, victor of the never ending war, he who fell from the highest of places, second of his cloning, most noble swords-bearer, Ninety-Seventh of His Name, Lord of That Bit Between High Places and Low Places Through Which One Falls In Transit Between them! Apparently, at any rate.

#### Jubal

Executive Officer
• Posts: 30757
• Karma: 123
• Awards
« Reply #1 on: November 11, 2014, 07:29:44 PM »
So to get this straight, each distribution has a set of functions which you just imported that include a var and an e function?
The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...

#### Glaurung

• Sakellarios
Financial Officer
• Posts: 4791
• Karma: 12
« Reply #2 on: November 11, 2014, 08:01:39 PM »
Also, how do you call this code? At a rough guess, you call it (at least) once to initialise your distribution dictionary, passing in a string containing a tilde and distribution name, and then again to get the desired data, passing in a string containing 'e' or 'var'. Is that right?

#### Othko97

• SotK Beta
• Patrikios
Voting Member
• Posts: 3243
• Karma: 9
« Reply #3 on: November 11, 2014, 08:33:03 PM »
Each distribution is a class containing functions for expected values and variance. The dictionary contains a variable name and the object it represents. This part certainly works correctly, as just printing parameters for the object gives them correctly. Eventually I intend to create a main program which just passes input through the parser or quits. A sample of where things go wrong looks somewhat like so:

Code: [Select]
`>>>from Parser import *>>>p = Parser()>>>p.parse("X~B(10,0.5)")>>>print(DistDict["X"].n)10>>>print(DistDict["X"].p)0.5>>>p.parse("E(X)")2.5  #This should be 5.0>>>p.parse("Var(X)")2.5>>>print(DistDict["X"].E())5.0`
As you can see, this is somewhat problematic. The functions themselves also work correctly, as is testified by the last line in the above demonstration. So yeah, pass it through once to set up the variable, and again to call a function.
Alcohol and Calculus don't mix. Never Drink and Derive.

I am Othko, the master of the 23 techniques of death, murder of the 8 popes, victor of the never ending war, he who fell from the highest of places, second of his cloning, most noble swords-bearer, Ninety-Seventh of His Name, Lord of That Bit Between High Places and Low Places Through Which One Falls In Transit Between them! Apparently, at any rate.

#### Othko97

• SotK Beta
• Patrikios
Voting Member
• Posts: 3243
• Karma: 9
« Reply #4 on: November 12, 2014, 08:37:53 AM »
Solved!
Alcohol and Calculus don't mix. Never Drink and Derive.

I am Othko, the master of the 23 techniques of death, murder of the 8 popes, victor of the never ending war, he who fell from the highest of places, second of his cloning, most noble swords-bearer, Ninety-Seventh of His Name, Lord of That Bit Between High Places and Low Places Through Which One Falls In Transit Between them! Apparently, at any rate.

#### Glaurung

• Sakellarios
Financial Officer
• Posts: 4791
• Karma: 12
« Reply #5 on: November 12, 2014, 08:51:28 AM »

#### Cuddly Khan

• Silhouette in Disguise
• Patrikios
Voting Member
• Posts: 7821
• Karma: 34
• PURGE THE BOTS!!
• Awards
« Reply #6 on: November 12, 2014, 08:55:33 AM »
It's 42!
Most effective elected official. Ever. (not counting Jubal)

He is Jubal the modder, Jubal the wayfarer, Jubal the admin. And he has come to me now, at the turning of the tide.

#### Othko97

• SotK Beta
• Patrikios
Voting Member
• Posts: 3243
• Karma: 9
« Reply #7 on: November 12, 2014, 09:31:45 AM »
I still have no idea why it didn't work, but I rewrote it so that anything not containing a tilde is considered together, with a split used to find out what is wanted. This for some reason worked.
Code: [Select]
`if "~" in string:    codeelse:    listA = string.split("(")    if listA[0] == "E" or listA[0] == "e":        print(DistDict[str(listA[1][:-1])].E())    elif listA[0] == "Var" or listA[0] == "var":        print(DistDict[str(listA[1][:-1])].Var())`
Alcohol and Calculus don't mix. Never Drink and Derive.

I am Othko, the master of the 23 techniques of death, murder of the 8 popes, victor of the never ending war, he who fell from the highest of places, second of his cloning, most noble swords-bearer, Ninety-Seventh of His Name, Lord of That Bit Between High Places and Low Places Through Which One Falls In Transit Between them! Apparently, at any rate.

#### Glaurung

• Sakellarios
Financial Officer
• Posts: 4791
• Karma: 12
« Reply #8 on: November 12, 2014, 10:09:20 AM »
Strange. I was going to suggest swapping the 'elif' sections in the original code to see what happened. Anyway, I'm glad to know it's fixed.

#### Son of the King

• Megas Domestikos
Voting Member
• Posts: 3357
• Karma: 29
• Awards
« Reply #9 on: November 12, 2014, 07:52:21 PM »

Code: [Select]
` elif "Var" or "var" in string: #If asking for a variance listA = string.split("(") #Splits to find distribution wanted print(DistDict[listA[1][:-1]].Var()) #Prints the variance of the requested distribution elif "E" or "e" in string: #If asking for an expected value listA = string.split("(") #Splits to find distribution wanted print(DistDict[listA[1][:-1]].E()) #Prints the expected value of the requested distribution`

These conditions are always true. Python will see them as `if ("Var") or ("var" in string)` and since "Var" is always True you'll always enter the first elif. You could do

Code: [Select]
`elif "Var" in string or "var" in string`
or better

Code: [Select]
`elif any(f in string for f in ("var", "Var"))`
or even better in my eyes

Code: [Select]
`elif "var" in string.lower()`

#### Othko97

• SotK Beta
• Patrikios
Voting Member
• Posts: 3243
• Karma: 9