Skip to content
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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Make two node example graph #16

wants to merge 21 commits into from

Conversation

mscroggs
Copy link
Collaborator

@mscroggs mscroggs commented Mar 14, 2025

  • 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

@mscroggs mscroggs marked this pull request as draft March 14, 2025 15:30
Comment on lines +26 to +47
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
Copy link
Collaborator Author

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

@mscroggs mscroggs linked an issue Mar 19, 2025 that may be closed by this pull request
3 tasks
@mscroggs mscroggs requested a review from willGraham01 March 19, 2025 09:24
@mscroggs mscroggs marked this pull request as ready for review March 19, 2025 09:24
Copy link
Collaborator

@willGraham01 willGraham01 left a 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(
Copy link
Collaborator

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...)

Copy link
Collaborator Author

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)

Copy link
Collaborator

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):
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Helper class for creating graph
2 participants