File Coverage

blib/lib/Statistics/SocialNetworks.pm
Criterion Covered Total %
statement 57 81 70.3
branch 12 24 50.0
condition 1 2 50.0
subroutine 7 9 77.7
pod 5 6 83.3
total 82 122 67.2


line stmt bran cond sub pod time code
1             package Statistics::SocialNetworks;
2              
3 1     1   51499 use 5.008008;
  1         4  
  1         44  
4 1     1   5 use strict;
  1         2  
  1         36  
5 1     1   5 use warnings;
  1         7  
  1         1061  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10              
11             # Items to export into callers namespace by default. Note: do not export
12             # names by default without a very good reason. Use EXPORT_OK instead.
13             # Do not simply export all your public functions/methods/constants.
14              
15             # This allows declaration use Statistics::SocialNetworks ':all';
16             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17             # will save memory.
18             our %EXPORT_TAGS = ( 'all' => [ qw(
19             constraint
20             c
21             p
22             CTdi
23             flushCache
24             ) ] );
25              
26             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27              
28             our @EXPORT = qw(
29            
30             );
31              
32             our $VERSION = '0.03';
33              
34              
35             # Preloaded methods go here.
36              
37             my %p = (); # cache for p() results
38              
39             sub m {
40 0     0 0 0 my $g = shift;
41 0         0 my $i = shift;
42 0         0 my $j = shift;
43 0         0 my %g = %{$g};
  0         0  
44              
45 0 0       0 return $g{$i}{$j} if defined $g{$i}{$j};
46 0         0 return 0;
47             }
48              
49             sub flushCache {
50 1     1 1 6 %p = ();
51             }
52              
53             sub CTdi {
54             #Coleman-Theil disorder index
55             #a measure of diversity of contacts
56             #0 = same for all contacts
57             #1 = all constraint is measured in a single relationship
58              
59 0     0 1 0 my $g = shift;
60 0         0 my $i = shift;
61 0         0 my %g = %{$g};
  0         0  
62              
63 0         0 my @N = keys %{$g{$i}};
  0         0  
64              
65 0         0 my $N = $#N + 1;
66              
67 0 0       0 return 1 if ( $N == 1 );
68              
69 0         0 my $sum = 0;
70 0         0 my $C = constraint($g,$i);
71 0 0       0 if ( $C ) {
72 0         0 foreach my $j ( @N ) {
73 0 0       0 next if ($j eq $i);
74 0         0 my $factor = c($g, $i, $j)/($C/$N);
75 0 0       0 $sum += $factor * log($factor) if ( $factor );
76             }
77 0         0 $sum /= ($N * log($N));
78             }
79 0         0 return $sum;
80             }
81              
82             sub p {
83 45     45 1 43 my $g = shift; # graph
84 45         40 my $i = shift;
85 45         43 my $j = shift;
86 45         42 my %g = %{$g};
  45         112  
87              
88 45 100       161 return $p{$i}{$j} if (defined $p{$i}{$j});
89              
90 18         16 my @v = keys %{$g{$i}};
  18         47  
91 18         24 my $z = 0;
92            
93 18         19 my $total_w = 0;
94 18         21 my $n;
95 18         19 my $w = undef;
96 18         21 foreach $n ( @v ) {
97 42   50     81 $w = $g{$i}{$n} || 1;
98 42         59 $total_w += $w;
99 42 100       102 next unless ( $n eq $j );
100 12         16 $z += $w;
101             }
102              
103 18 100       67 return $p{$i}{$j} = 0 unless ($z > 0);
104              
105 12         22 $p{$i}{$j} = ($z/($total_w));
106 12         44 return $p{$i}{$j};
107            
108             }
109              
110             sub c {
111 9     9 1 12 my $g = shift; # graph
112 9         12 my $i = shift;
113 9         10 my $j = shift;
114 9         9 my %g = %{$g};
  9         29  
115              
116 9         19 my $sum = p($g,$i,$j);
117              
118 9         13 my @v = keys %{$g{$i}};
  9         23  
119 9         12 my $key;
120 9         11 foreach $key ( @v ) {
121 27 50       148 next if $key eq $i;
122 27 100       56 next if $key eq $j;
123 18         29 $sum += p($g, $i, $key) * p($g, $key, $j);
124             }
125              
126 9         32 return $sum * $sum;
127             }
128              
129             sub constraint {
130 4     4 1 24 my $g = shift; # graph
131 4         5 my $i = shift;
132 4         7 my %g = %{$g};
  4         16  
133              
134 4         7 my $sum = 0;
135              
136 4         5 my @v = keys %{$g{$i}};
  4         12  
137 4 100       15 return 1 if ($#v == 0);
138 3         2 my $node;
139 3         8 foreach $node ( @v ) {
140 9 50       19 next if ( $node eq $i );
141 9         19 $sum += c($g, $i, $node);
142             }
143            
144 3         20 return $sum;
145             }
146              
147             1;
148             __END__