Skip to content

Commit 2d32376

Browse files
committed
feat: working and reasonably fast version!!
1 parent 60be886 commit 2d32376

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

index.js

+36-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {nelderMead} from './nelderMead.js';
1+
import {nelderMead} from './nelderMead/nelderMead.js';
22
import betainc from '@stdlib/math-base-special-betainc/lib/index.js'
33

44
/*
@@ -18,7 +18,7 @@ const beta_cdf = ({x,a,b}) => betainc(x,a,b)
1818
// https://www.squiggle-language.com/playground#code=eNqrVirOyC8PLs3NTSyqVLIqKSpN1QELuaZkluQXwURSUtMSS3NKnPNTUpWslJJT0jSSUksSNQz1DHQUjEz1DBQ0dRQM9Aw1lWoBnIcZzA%3D%3D
1919
*/
2020

21-
const find_beta_from_ci = ({ci_lower, ci_upper}) => {
21+
export const find_beta_from_ci = ({ci_lower, ci_upper}) => {
2222

2323
function loss(x){
2424
let a = x[0]
@@ -49,21 +49,42 @@ const find_beta_from_ci = ({ci_lower, ci_upper}) => {
4949

5050
return [df_da, df_db]
5151
}
52+
53+
// Try a sensible default
54+
let x0_init = [ 50, 50 ]
55+
let nelderMead_init = nelderMead(loss, x0_init);
56+
let result_init = [nelderMead_init.x[0], nelderMead_init.x[1]]
57+
let loss_init = loss(result_init)
58+
// console.log(loss_init)
5259

53-
let x0 = [ 10, 5]
54-
let result = nelderMead(loss, x0);
55-
return [result.x[0], result.x[1]]
60+
if(loss < 10e-8){
61+
return result_init
62+
}
5663

57-
}
64+
// if the sensible default doesn't work,
65+
// try a grid search
66+
let min_loss = Infinity
67+
let result = null
68+
for(let a=1; a<40; a=a+2){
69+
for(let b=1; b<40; b=b+2){
70+
let x0 = [ a, b ]
71+
72+
let nelderMead_output = nelderMead(loss, x0);
73+
let new_result = [nelderMead_output.x[0], nelderMead_output.x[1]]
74+
let new_loss = loss(new_result)
5875

59-
let ci_lower = 0.3
60-
let ci_upper = 0.8
76+
if(loss < 10e-8){
77+
return new_result
78+
}
6179

62-
let result = find_beta_from_ci({ci_lower, ci_upper})
63-
console.log(result)
64-
/* console.log([a,b])
65-
console.log(`beta(${a}, ${b})`)
80+
if(new_loss < min_loss){
81+
min_loss = new_loss
82+
result = new_result
83+
}
84+
}
85+
}
86+
// console.log(min_loss)
87+
return result
88+
89+
}
6690

67-
console.log(beta_cdf({x: ci_lower, a, b}))
68-
console.log(beta_cdf({x: ci_upper, a, b}))
69-
*/

nelderMead/LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2016, Ben Frederickson
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the author nor the names of contributors may be used to
15+
endorse or promote products derived from this software without specific prior
16+
written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

nelderMead/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This function was extracted from here: <https://github.com/benfred/fmin/blob/master/src/nelderMead.js>.
2+
3+
See also the LICENSE in this folder.

nelderMead.js nelderMead/nelderMead.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function norm2(a) {
1212
return Math.sqrt(dot(a, a));
1313
}
1414

15-
1615
function weightedSum(ret, w1, v1, w2, v2) {
1716
for (var j = 0; j < ret.length; ++j) {
1817
ret[j] = w1 * v1[j] + w2 * v2[j];

test.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {find_beta_from_ci} from "./index.js"
2+
let ci_lower = 0.3
3+
let ci_upper = 0.8
4+
5+
let result = find_beta_from_ci({ci_lower, ci_upper})
6+
console.log(result)
7+
/* console.log([a,b])
8+
console.log(`beta(${a}, ${b})`)
9+
10+
console.log(beta_cdf({x: ci_lower, a, b}))
11+
console.log(beta_cdf({x: ci_upper, a, b}))
12+
*/

0 commit comments

Comments
 (0)