Function Repository Resource:

AssociationNormalize

Source Notebook

Normalizes the values of an Association

Contributed by: Seth J. Chandler

ResourceFunction["AssociationNormalize"][assoc]

divides each value in assoc by the Norm of its values.

ResourceFunction["AssociationNormalize"][assoc,f]

divides each value in assoc by a function f of its values.

Details

Consistent with the way Normalize behaves, if all the values in an association are zero, ResourceFunction["AssociationNormalize"] returns its first argument unchanged.
ResourceFunction["AssociationNormalize"] evaluates all values in assoc, including those stored with RuleDelayed .

Examples

Basic Examples (2) 

Normalize an Association with three values:

In[1]:=
ResourceFunction["AssociationNormalize"][
 Association["a" -> 2, "b" -> 3, "c" -> 4]]
Out[1]=

Normalize an Association with three values so that the values total to 1:

In[2]:=
ResourceFunction["AssociationNormalize"][
 Association["a" -> 2, "b" -> 3, "c" -> 4], Total]
Out[2]=

Scope (4) 

Symbolic values:

In[3]:=
ResourceFunction["AssociationNormalize"][
 Association["a" -> x, "b" -> y]]
Out[3]=

Use an arbitrary norm function:

In[4]:=
ResourceFunction["AssociationNormalize"][
 Association["a" -> x, "b" -> y], f]
Out[4]=

An association with complex values:

In[5]:=
v = <|"a" -> 1, "b" -> 2 I, "c" -> 3, "d" -> 4 I, "e" -> 5, "f" -> 6 I|>
Out[5]=

Normalize using exact arithmetic:

In[6]:=
ResourceFunction["AssociationNormalize"][v]
Out[6]=

An association where all the values are zero:

In[7]:=
ResourceFunction["AssociationNormalize"][
 Association["a" -> 0, "b" -> 0]]
Out[7]=

Applications (4) 

Here is an association in which the keys are various attributes and the values are probabilities (the problem is taken from the Probabilistic Graphical Models book contained in the Sources section). The goal is to figure out the probability that the value of "sat" is "s1" given that the grade is "s2":

In[8]:=
jpd = {<|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l0"|> -> 1197/100000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l1"|> -> 10773/100000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l0"|> -> 399/6250, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l1"|> -> 1197/12500, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l0"|> -> 118503/1000000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l1"|> -> 1197/1000000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l0"|> -> 133/125000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l1"|> -> 1197/125000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l0"|> -> 133/5000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l1"|> -> 399/10000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l0"|> -> 92169/500000, <|"intelligence" -> "i0", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l1"|> -> 931/500000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l0"|> -> 63/100000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l1"|> -> 567/100000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l0"|> -> 21/6250, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l1"|> -> 63/12500, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l0"|> -> 6237/1000000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l1"|> -> 63/1000000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l0"|> -> 7/125000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l1"|> -> 63/125000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l0"|> -> 7/5000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l1"|> -> 21/10000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l0"|> -> 4851/500000, <|"intelligence" -> "i0", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l1"|> -> 49/500000, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l0"|> -> 81/25000, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l1"|> -> 729/25000, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l0"|> -> 18/15625, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l1"|> -> 27/15625, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l0"|> -> 891/1250000, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l1"|> -> 9/1250000, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l0"|> -> 3/2500, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l1"|> -> 27/2500, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l0"|> -> 9/3125, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l1"|> -> 27/6250, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l0"|> -> 297/62500, <|"intelligence" -> "i1", "sat" -> "s0", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l1"|> -> 3/62500, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l0"|> -> 81/6250, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g1", "letter" -> "l1"|> -> 729/6250, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l0"|> -> 72/15625, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g2", "letter" -> "l1"|> -> 108/15625, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l0"|> -> 891/312500, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d0", "grade" -> "g3", "letter" -> "l1"|> -> 9/312500, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l0"|> -> 3/625, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g1", "letter" -> "l1"|> -> 27/625, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l0"|> -> 36/3125, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g2", "letter" -> "l1"|> -> 54/3125, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l0"|> -> 297/15625, <|"intelligence" -> "i1", "sat" -> "s1", "difficulty" -> "d1", "grade" -> "g3", "letter" -> "l1"|> -> 3/15625};

Eliminate any associations in which the value of "grade" is not "g2":

In[9]:=
Select[Not[#[[1]]["grade"] =!= "g2"] &][jpd]
Out[9]=

Normalize the remaining probabilities:

In[10]:=
ResourceFunction["AssociationNormalize"][Association[%], Total]
Out[10]=

Use KeySelect to compute the probability that the "sat" value is "s1":

In[11]:=
Total[KeySelect[#["sat"] == "s1" &][%]]
Out[11]=

Possible Issues (1) 

Delayed values are evaluated:

In[12]:=
assoc = <|"a" -> 100, "b" :> (Print["Hello"]; RandomReal[])|>
Out[12]=
In[13]:=
ResourceFunction["AssociationNormalize"][assoc]
Out[13]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 06 July 2021

Source Metadata

Related Resources

License Information