Game Design and Project Resources: The Workshops Quarter > Non-game Programs - The Tinkers' Workshop

The Coding Help Thread

(1/3) > >>

Othko97:
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: ---"""Parser for Statistical Distributions"""

from PoiDist import * #Imports Poisson functions
from BinDist import * #Imports Binomial functions
from UniDist import * #Imports Uniform Continuous functions

global DistDict #Dictionary to store distributions

DistDict = {} #Sets up dictionary

class 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

--- End code ---

Jubal:
So to get this straight, each distribution has a set of functions which you just imported that include a var and an e function?

Glaurung:
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:
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: --->>>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

--- End code ---

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.

Othko97:
Solved!

Navigation

[0] Message Index

[#] Next page

Go to full version