logmem

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package logmem is an implementation of bayes.NodeLogger for memory-based logging.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NodeLog

type NodeLog struct {
	// FromAtoB is the number of accesses from node A to node B as map[A]map[B].
	// A is the incoming access and B is the outgoing access.
	FromAToB map[uint64]map[uint64]int
	// FromA is the number of incoming accesses from node A as map[A].
	FromA map[uint64]int
	// ToB is the number of outgoing accesses to node B as map[B].
	ToB map[uint64]int

	// TotalAccesses is the total number of accesses to the node.
	TotalAccesses int
	// contains filtered or unexported fields
}

NodeLog holds the records of a node. It is an implementation of bayes.NodeLogger for memory-based logging.

func New

func New(nodeID uint64) *NodeLog

New returns a new NodeLog instance.

func (NodeLog) ID

func (n NodeLog) ID() uint64

ID returns the node ID of the current node.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-bayes/pkg/nodelogger/logmem"
)

func main() {
	n := logmem.New(12345)

	fmt.Println(n.ID())

}
Output:

12345

func (NodeLog) Predict

func (n NodeLog) Predict(fromNodeA, toNodeB uint64) float64

Predict returns the probability of the next node to be toNodeB if the incoming node is fromNodeA.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-bayes/pkg/nodelogger/logmem"
)

func main() {
	const (
		x = uint64(1) // Node ID of node x
		y = uint64(2) // Node ID of node y
		z = uint64(3) // Node ID of node z
	)

	nodeY := logmem.New(y) // Create a new node y

	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.
	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.

	fmt.Println("Prediction of outgoing node to be x, on incoming node as x:", nodeY.Predict(x, x))
	fmt.Println("Prediction of outgoing node to be y, on incoming node as x:", nodeY.Predict(x, y))
	fmt.Println("Prediction of outgoing node to be z, on incoming node as x:", nodeY.Predict(x, z))

}
Output:

Prediction of outgoing node to be x, on incoming node as x: 0
Prediction of outgoing node to be y, on incoming node as x: 0
Prediction of outgoing node to be z, on incoming node as x: 1

func (NodeLog) PriorPNotFromAtoB

func (n NodeLog) PriorPNotFromAtoB(fromA, toB uint64) float64

PriorPNotFromAtoB returns the prior probability of the node not to be B if the previous node is A.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-bayes/pkg/nodelogger/logmem"
)

func main() {
	const (
		x = uint64(1) // Node ID of node x
		y = uint64(2) // Node ID of node y
		z = uint64(3) // Node ID of node z
	)

	nodeY := logmem.New(y) // Create a new node y

	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.
	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.

	fmt.Println("Prior probability of outgoing node is not x, on incoming node as x (x -> y -> not x):",
		nodeY.PriorPNotFromAtoB(x, x))
	fmt.Println("Prior probability of outgoing node is not y, on incoming node as x (x -> y -> not y):",
		nodeY.PriorPNotFromAtoB(x, y))
	fmt.Println("Prior probability of outgoing node is not z, on incoming node as x (x -> y -> not z):",
		nodeY.PriorPNotFromAtoB(x, z))

}
Output:

Prior probability of outgoing node is not x, on incoming node as x (x -> y -> not x): 0.5
Prior probability of outgoing node is not y, on incoming node as x (x -> y -> not y): 0.5
Prior probability of outgoing node is not z, on incoming node as x (x -> y -> not z): 0

func (NodeLog) PriorPfromAtoB

func (n NodeLog) PriorPfromAtoB(fromA, toB uint64) float64

PriorPfromAtoB returns the prior probability of the node to be B if the previous node is A.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-bayes/pkg/nodelogger/logmem"
)

func main() {
	const (
		x = uint64(1) // Node ID of node x
		y = uint64(2) // Node ID of node y
		z = uint64(3) // Node ID of node z
	)

	nodeY := logmem.New(y) // Create a new node y

	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.
	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.

	fmt.Println("Prior probability of outgoing node x, on incoming node as x (x -> y -> x):",
		nodeY.PriorPfromAtoB(x, x))
	fmt.Println("Prior probability of outgoing node y, on incoming node as x (x -> y -> y):",
		nodeY.PriorPfromAtoB(x, y))
	fmt.Println("Prior probability of outgoing node z, on incoming node as x (x -> y -> z):",
		nodeY.PriorPfromAtoB(x, z))

}
Output:

Prior probability of outgoing node x, on incoming node as x (x -> y -> x): 0
Prior probability of outgoing node y, on incoming node as x (x -> y -> y): 0
Prior probability of outgoing node z, on incoming node as x (x -> y -> z): 0.5

func (NodeLog) PriorPtoB

func (n NodeLog) PriorPtoB(nodeB uint64) float64

PriorPtoB returns the prior probability of the outgoing node to be nodeB.

Which is the number of outgoing accesses to the node B out of the total number of accesses of current node.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-bayes/pkg/nodelogger/logmem"
)

func main() {
	const (
		x = uint64(1)
		y = uint64(2)
		z = uint64(3)
	)

	nodeY := logmem.New(y)

	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.
	nodeY.Update(x, z) // from x to z (x -> y -> z) This should be called from node z.
	nodeY.Update(z, x) // from z to x (z -> y -> x) This should be called from node x.

	fmt.Println("Prior probability of outgoing node x (y -> x):", nodeY.PriorPtoB(x))
	fmt.Println("Prior probability of outgoing node y (y -> y):", nodeY.PriorPtoB(y))
	fmt.Println("Prior probability of outgoing node z (y -> z):", nodeY.PriorPtoB(z))

}
Output:

Prior probability of outgoing node x (y -> x): 0.5
Prior probability of outgoing node y (y -> y): 0
Prior probability of outgoing node z (y -> z): 0.5

func (NodeLog) String

func (n NodeLog) String() string

String returns a string representation of the NodeLog which is the node ID.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-bayes/pkg/nodelogger/logmem"
)

func main() {
	n := logmem.New(12345)

	// Stringer implementation for the NodeLog type.
	fmt.Println(n)

}
Output:

12345

func (*NodeLog) Update

func (n *NodeLog) Update(fromA, toB uint64)

Update updates the records of a node. It must be called by the next node accessed.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-bayes/pkg/nodelogger/logmem"
)

func main() {
	const (
		x = uint64(1)
		y = uint64(2)
		z = uint64(3)
	)

	nodeY := logmem.New(y)

	// Update the access log of nodeY.
	//
	// It will update the access log as "x -> y -> z".
	// Which means that node x is the predecessor of node y, and node z is the
	// successor of node y. In other words, node x is the incoming node and node
	// z is the outgoing node of node y.
	//
	// Note that it must be called by the next node accessed. In this case,
	// node z should call this function.
	nodeY.Update(x, z)

	fmt.Println("Total access:", nodeY.TotalAccesses)
	fmt.Println("Number of access from node x:", nodeY.FromA[x])
	fmt.Println("Number of access from node z:", nodeY.FromA[z])
	fmt.Println("Number of outgoing node x:", nodeY.ToB[x])
	fmt.Println("Number of outgoing node z:", nodeY.ToB[z])

}
Output:

Total access: 1
Number of access from node x: 1
Number of access from node z: 0
Number of outgoing node x: 0
Number of outgoing node z: 1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL