-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make two node example graph #16
base: main
Are you sure you want to change the base?
Conversation
mscroggs
commented
Mar 14, 2025
•
edited
Loading
edited
- Refactored node and graph so that a graph can be more easily created using functions as in Helper class for creating graph #7.
- Added functions to estimate mean and standard deviation from a graph
- Added test that one and two node graphs containing normal distributions give the correct mean and stdev
class NormalDistribution(Distribution): | ||
"""Normal distribution.""" | ||
|
||
def __init__(self, mean: str | float = 0.0, std_dev: str | float = 1.0) -> None: | ||
"""Initialise.""" | ||
self.mean = mean | ||
self.std_dev = std_dev | ||
|
||
def sample( | ||
self, sampled_dependencies: dict[str, npt.NDArray[float]], samples: int | ||
) -> npt.NDArray[float]: | ||
"""Sample a normal distribution with mean 1.""" | ||
values = np.random.normal(0.0, 1.0, samples) # noqa: NPY002 | ||
if isinstance(self.std_dev, str): | ||
values *= sampled_dependencies[self.std_dev] | ||
else: | ||
values *= self.std_dev | ||
if isinstance(self.mean, str): | ||
values += sampled_dependencies[self.mean] | ||
else: | ||
values += self.mean | ||
return values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a placeholder class that should be replaced with an actual distribution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working example 🥳 🚀
Couple of small ruff
-please-ers in the Graph
class.
For the algorithms
module, I'm not convinced as to why the methods in there can't be part of the Graph
class itself, but I'm open to being convinced.
The test comments are mostly on a reduction in the number of cases. The two example tests have 16 & 32 test cases, respectively (which is not a bad thing, but some are likely redundant in terms of what they check).
from .iteration import roots_down_to_outcome | ||
|
||
|
||
def sample( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of interest, why not make these methods of the Graph
class? We can always then do something like
# algorithms/expectation.py
@wraps(graph.sample)
def sample(graph, ...):
return graph.sample(...)
if we really want to.
I'm guessing because we may expand the number of algorithms we have, and so it's going to be nicer in the long-term to have a separate module for algorithms? (Python needs header files...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, long term I'm expecting more algorithms (and expect them to be more complex or potentially take more than just a graph as input). Buy may be that using @wraps
is nicer (adds reading about @wraps to today's to do list)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If only Python had header / body separation, so long algorithms didn't have to be such an issue 😢
@wraps
doesn't solve anything by itself, it just recycles the docstring and other "help" info (which effectively lets us expose a class method as a function in another module, without needing to repeat the docstring etc)
|
||
|
||
class DistributionNode(Labelled): | ||
"""A node containing a distribution family that depends on its parents.""" | ||
class DistributionNode(Node): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More generally a node containing a DistributionFamily
? (This is something we can leave on the bench till our hack tomorrow when we get these two things to talk to each other, so feel free to ignore for now)
Co-authored-by: Will Graham <[email protected]>
Co-authored-by: Will Graham <[email protected]>
…nto mscroggs/normal-example
Co-authored-by: Will Graham <[email protected]>
…nto mscroggs/normal-example