Skip to content

Commit 308217f

Browse files
author
eltrompetero
committed
new functions for transforming Ising multipliers
added vec2mat and mat2vec updated doc
1 parent 45dcc51 commit 308217f

File tree

4 files changed

+98
-22
lines changed

4 files changed

+98
-22
lines changed

coniii/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,16 @@ def __init__(self, multipliers, rng=None, n_cpus=None, verbose=False):
123123
Parameters
124124
----------
125125
multipliers : list of ndarray or ndarray
126-
Can be a list of vectors [fields, couplings], a vector of fields and couplings
127-
concatenated together, or a matrix of parameters where the diagonal entries
128-
are the fields.
126+
Can be an integer (all parameters are set to zero), list of vectors [fields,
127+
couplings], a vector of fields and couplings concatenated together, or a
128+
matrix of parameters where the diagonal entries are the fields.
129129
"""
130130

131131
# intelligently read in multipliers by handling multiple use cases
132+
if type(multipliers) is int:
133+
assert multipliers>1, "System size must be greater than 1."
134+
self.n = multipliers
135+
multipliers = np.zeros(self.n+self.n*(self.n-1)//2)
132136
if len(multipliers)==2:
133137
# case where two vectors are given
134138
self.n = len(multipliers[0])

coniii/solvers.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
# SOFTWARE.
2626
# =============================================================================================== #
27-
from scipy.optimize import minimize,fmin_ncg,minimize_scalar,root
27+
from scipy.optimize import minimize, fmin_ncg, minimize_scalar, root
2828
import multiprocess as mp
2929
import copy
3030
from . import mean_field_ising
@@ -147,7 +147,8 @@ def solve(self,
147147
Returns
148148
-------
149149
ndarray
150-
Solved multipliers (parameters).
150+
Solved multipliers (parameters). For Ising problem, these can be converted
151+
into matrix format using utils.vec2mat.
151152
dict, optional
152153
Output from scipy.optimize.root.
153154
"""
@@ -417,7 +418,8 @@ def solve(self,
417418
Returns
418419
-------
419420
ndarray
420-
Solution.
421+
Solved multipliers (parameters). For Ising problem, these can be converted
422+
into matrix format using utils.vec2mat.
421423
dict (optional)
422424
Output from scipy.optimize.minimize returned if full_output is True.
423425
"""
@@ -568,7 +570,8 @@ def learn_settings(i):
568570
Returns
569571
-------
570572
ndarray
571-
Found solution to inverse problem.
573+
Solved multipliers (parameters). For Ising problem, these can be converted
574+
into matrix format using utils.vec2mat.
572575
int
573576
Error flag.
574577
0, converged within given criterion
@@ -1116,7 +1119,8 @@ def solve(self, force_general=False, **kwargs):
11161119
Returns
11171120
-------
11181121
ndarray
1119-
multipliers
1122+
Solved multipliers (parameters). For Ising problem, these can be converted
1123+
into matrix format using utils.vec2mat.
11201124
"""
11211125

11221126
if type(self.model) is Ising and not force_general:
@@ -1690,26 +1694,31 @@ def solve(self, threshold,
16901694
----------
16911695
threshold : float
16921696
meanFieldRef : bool, False
1693-
Expand about mean-field reference
1697+
Expand about mean-field reference.
16941698
independentRef : bool, True
1695-
Expand about independent reference
1699+
Expand about independent reference.
16961700
priorLmbda : float, 0.
1697-
Strength of non-interacting prior
1701+
Strength of non-interacting prior.
16981702
meanFieldPriorLmbda : float, None
16991703
Strength of non-interacting prior in mean field calculation (defaults to
1700-
priorLmbda)
1704+
priorLmbda).
17011705
17021706
Returns
17031707
-------
1704-
With full_output=False, returns
1705-
J : Estimated interaction matrix
1706-
1707-
With full_output=True, returns
1708-
ent : Estimated entropy
1709-
J : Estimated interaction matrix
1710-
clusters : List of clusters
1711-
deltaSdict :
1712-
deltaJdict :
1708+
ndarray
1709+
Solved multipliers (parameters). For Ising problem, these can be converted
1710+
into matrix format using utils.vec2mat.
1711+
float (optional, only if full_output=True)
1712+
Estimated entropy.
1713+
ndarray
1714+
Solved multipliers (parameters). For Ising problem, these can be converted
1715+
into matrix format using utils.vec2mat.
1716+
list (optional, only if full_output=True)
1717+
List of clusters.
1718+
dict (optional, only if full_output=True)
1719+
deltaSdict
1720+
dict (optional, only if full_output=True)
1721+
deltaJdict
17131722
"""
17141723

17151724
# convert input to coocMat
@@ -1776,7 +1785,7 @@ def solve(self, threshold,
17761785
self.multipliers = convert_params( h, squareform(J)*2, '11', concat=True )
17771786

17781787
if full_output:
1779-
return ent, self.multipliers, clusters, deltaSdict, deltaJdict
1788+
return self.multipliers, ent, clusters, deltaSdict, deltaJdict
17801789
else:
17811790
return self.multipliers
17821791
# end ClusterExpansion
@@ -1840,6 +1849,8 @@ def solve(self,
18401849
"""Varies the strength of regularization on the mean field J to best fit given
18411850
cooccurrence data.
18421851
1852+
Parameters
1853+
----------
18431854
n_grid_points : int, 200
18441855
If bracket is given, first test at n_grid_points points evenly spaced in the
18451856
bracket interval, then give the lowest three points to
@@ -1862,6 +1873,12 @@ def solve(self,
18621873
priorLmbda : float,0.
18631874
** As of v1.0.3, not currently implemented **
18641875
Strength of noninteracting prior.
1876+
1877+
Returns
1878+
-------
1879+
ndarray
1880+
Solved multipliers (parameters). For Ising problem, these can be converted
1881+
into matrix format using utils.vec2mat.
18651882
"""
18661883

18671884
from scipy import transpose

coniii/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,12 @@ def test_base_repr():
195195
for power in range(1,7):
196196
assert ''.join(base_repr(i**power,i))=='1'+'0'*power
197197

198+
def test_vec2mat():
199+
for n in range(3,100):
200+
x = np.random.rand(n+n*(n-1)//2)
201+
assert np.array_equal(x, mat2vec(vec2mat(x)))
202+
xvec, xmat = vec2mat(x, True)
203+
assert np.array_equal(xvec, x[:n])
204+
198205
if __name__=='__main__':
199206
test_convert_params()

coniii/utils.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,3 +1140,51 @@ def coarse_grain_with_func(X, n_times, sim_func, coarse_func):
11401140
binsix = originalIx
11411141

11421142
return coarseX, binsix
1143+
1144+
def vec2mat(multipliers, separate_fields=False):
1145+
"""Convert vector of parameters containing fields and couplings to a matrix where the
1146+
diagonal elements are the fields and the remaining elements are the couplings. Fields
1147+
can be returned separately with the separate_fields keyword argument.
1148+
1149+
This is specific to the Ising model.
1150+
1151+
Parameters
1152+
----------
1153+
multipliers : ndarray
1154+
Vector of fields and couplings.
1155+
separate_fields : bool, False
1156+
1157+
Returns
1158+
-------
1159+
ndarray
1160+
n x n matrix. Diagonal elements are fields *unless* separate_fields keyword
1161+
argument is True, in which case the diagonal elements are 0.
1162+
ndarray (optional)
1163+
Fields if separate_fields keyword argument is True.
1164+
"""
1165+
1166+
n = (np.sqrt(1+8*multipliers.size)-1)//2
1167+
assert (n%1)==0, "Must be n fields and (n choose 2) couplings."
1168+
n = int(n)
1169+
1170+
if separate_fields:
1171+
return multipliers[:n], squareform(multipliers[n:])
1172+
return replace_diag(squareform(multipliers[n:]), multipliers[:n])
1173+
1174+
def mat2vec(multipliers):
1175+
"""Convert matrix form of Ising parameters to a vector.
1176+
1177+
This is specific to the Ising model.
1178+
1179+
Parameters
1180+
----------
1181+
multipliers : ndarray
1182+
Matrix of couplings with diagonal elements as fields.
1183+
1184+
Returns
1185+
-------
1186+
ndarray
1187+
Vector of fields and couplings, respectively.
1188+
"""
1189+
1190+
return np.concatenate([multipliers.diagonal(), squareform(zero_diag(multipliers))])

0 commit comments

Comments
 (0)