-
Notifications
You must be signed in to change notification settings - Fork 2
/
divinds.c
202 lines (189 loc) · 5.18 KB
/
divinds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* divinds.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "prim.h"
#include "divinds.h"
#define DB_STAT if(DB[17])
#define DB_DIVIN if(DB[18])
/****************************************************************************
* nI = Total number of individuals, sI = number of classes
*/
double BrillouinIndexD(int nI, int *nAI, int sI)
{
int iI;
double divD, fD;
DB_DIVIN DB_PrI(">> BrillouinIndexD nI %d, sI %d\n",nI,sI);
divD = LogFactorialD(nI);
DB_DIVIN DB_PrI("+ div %f\n");
for(iI = 0; iI < sI; iI++)
{
fD = LogFactorialD(nAI[iI]);
divD -= fD;
DB_DIVIN DB_PrI("+ - %9.5f => div %f\n",fD,divD);
}
divD /= DNUM(nI);
DB_DIVIN DB_PrI("<< BrillouinIndexD %f\n",divD);
return(divD);
}
/****************************************************************************/
double BrillouinEvenIndexD(int nI, int *nAI, int sI)
{
double evD, hD, mD;
DB_DIVIN DB_PrI(">> BrillouinEvenIndexD nI %d, sI %d\n",nI,sI);
hD = BrillouinIndexD(nI, nAI, sI);
mD = BrillouinMaxIndexD(nI, sI);
DB_DIVIN DB_PrI("+ H %f Hmax %f\n",hD,mD);
evD = hD/mD;
DB_DIVIN DB_PrI("<< BrillouinEvenIndexD %f\n",evD);
return(evD);
}
/****************************************************************************/
double BrillouinMaxIndexD(int nI, int sI)
{
int rI, nsI;
double divD, rD, d1D, d2D;
DB_DIVIN DB_PrI(">> BrillouinMaxIndexD nI %d, sI %d\n",nI,sI);
divD = LogFactorialD(nI);
nsI = INT(nI/sI);
rI = nI - (sI * nsI);
DB_DIVIN DB_PrI("+ div %f, ns %d, r %d\n",divD,nsI,rI);
rD = LogFactorialD(nsI);
d1D = rD * DNUM(sI-rI);
rD = LogFactorialD(nsI+1);
d2D = rD * DNUM(rI);
DB_DIVIN DB_PrI("+ d1 %f, d2 %f ",d1D,d2D);
divD -= (d1D + d2D);
DB_DIVIN DB_PrI("==> %f\n",divD);
divD /= DNUM(nI);
DB_DIVIN DB_PrI("<< BrillouinMaxIndexD %f\n",divD);
return(divD);
}
/****************************************************************************
* Shannon index
* nI = total number of items (array sum)
* nAI = array of counts for each of [sI] items
*/
double ShannonWeaverIndexD(int n, int *nAI, int s)
{
int i;
double pD,divD;
divD = 0.0;
for(i = 0; i < s; i++)
{
if(nAI[i] > 0)
{
pD = DNUM(nAI[i])/DNUM(n);
pD *= LOG_2(pD);
/*
printf("[%d] = %d p=%f lg2p=%f pD=%f\n",i,nAI[i], DNUM(nAI[i])/DNUM(n),
LOG_2(DNUM(nAI[i])/DNUM(n)) ,pD);
*/
divD += pD;
}
}
if(divD<0.0)
{
divD = -divD;
}
return(divD);
}
/****************************************************************************/
double ShannonWeMaxIndexD(int sI)
{
double mD;
mD = LOG_2(sI);
return(mD);
}
/****************************************************************************/
double ShannonWeEvenIndexD(int nI, int *nAI, int sI)
{
double evD, hD, mD;
hD = ShannonWeaverIndexD(nI, nAI, sI);
mD = ShannonWeMaxIndexD(sI);
evD = hD/mD;
return(evD);
}
/****************************************************************************
* Shannon index
* Log 10 version
* nI = total number of items (array sum)
* nAI = array of counts for each of [sI] items
*/
double ShannonWeaver10IndexD(int n, int *nAI, int s)
{
int i;
double pD,divD;
divD = 0.0;
for(i = 0; i < s; i++)
{
if(nAI[i] > 0 )
{
pD = DNUM(nAI[i])/DNUM(n);
pD *= LOG_10(pD);
divD += pD;
}
}
if(divD<0.0)
{
divD = -divD;
}
return(divD);
}
/****************************************************************************
* Log 10 version
*/
double ShannonWe10MaxIndexD(int sI)
{
double mD;
mD = LOG_10(sI);
return(mD);
}
/****************************************************************************
* Log 10 version
*/
double ShannonWe10EvenIndexD(int nI, int *nAI, int sI)
{
double evD, hD, mD;
hD = ShannonWeaver10IndexD(nI, nAI, sI);
mD = ShannonWe10MaxIndexD(sI);
evD = hD/mD;
return(evD);
}
/****************************************************************************
* Simpson's diversity index
*/
double SimpsonIndexD(int n, int *nAI, int s)
{
int i;
double divD, pD, numD, denD;
DB_DIVIN DB_PrI(">> SimpsonIndexD n %d, s %d ",n,s);
denD = DNUM(n)*DNUM(n-1);
DB_DIVIN DB_PrI("den %3.3f\n",denD);
divD = 0.0;
for(i = 0; i < s; i++)
{
numD = DNUM(nAI[i])*DNUM(nAI[i]-1);
pD = numD/denD;
divD += pD;
}
DB_DIVIN DB_PrI("<< SimpsonIndexD %3.3f\n",divD);
return(divD);
}