Logistic curve fitting in epidemiology: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 77:
{{trans|C++}}
 
<langsyntaxhighlight lang="11l">-V K = 7.8e9
-V n0 = 27
-V actual = [
Line 122:
V r = solve(f)
V R0 = exp(12 * r)
print(‘r = #.6, R0 = #.6’.format(r, R0))</langsyntaxhighlight>
 
{{out}}
Line 131:
=={{header|Ada}}==
{{trans|C++}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Numerics.Generic_Elementary_Functions;
 
Line 221:
Put (", R0 = "); Real_Io.Put (R0, Exp => 0, Aft => 7);
New_Line;
end Curve_Fitting;</langsyntaxhighlight>
{{out}}
<pre>r = 0.1123022, R0 = 3.8482793</pre>
Line 227:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
 
Line 289:
printf("r = %f, R0 = %f\n", r, R0);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>r = 0.112302, R0 = 3.848279</pre>
Line 295:
=={{header|C++}}==
{{trans|Phix}}
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <functional>
#include <iostream>
Line 351:
std::cout << "r = " << r << ", R0 = " << R0 << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 360:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 416:
double R0 = exp(12 * r);
writeln("r = ", r, ", R0 = ", R0);
}</langsyntaxhighlight>
{{out}}
<pre>r = 0.112302, R0 = 3.84828</pre>
Line 423:
=={{header|FreeBASIC}}==
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">Const K = 7.8e9 ' approx world population
Const n0 = 27 ' number of cases at day 0
Dim Shared As Integer actual(1 To ...) => { _
Line 475:
Dim As Double R0 = Exp(12 * r)
Print Using "r = #.##########, R0 = #.########"; r; R0
Sleep</langsyntaxhighlight>
{{out}}
<pre>r = 0.1123021757, R0 = 3.84827928</pre>
Line 483:
{{libheader|lm}}
This uses the Levenberg-Marquardt method.
<langsyntaxhighlight lang="go">package main
 
import (
Line 538:
fmt.Printf("The logistic curve r for the world data is %.8f\n", r)
fmt.Printf("R0 is then approximately equal to %.7f\n", math.Exp(12*r))
}</langsyntaxhighlight>
 
{{out}}
Line 548:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.List;
import java.util.function.Function;
 
Line 621:
System.out.printf("r = %.16f, R0 = %.16f\n", r, r0);
}
}</langsyntaxhighlight>
{{out}}
<pre>r = 0.1123021756975501, R0 = 3.8482792809167194</pre>
Line 630:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def K: 7800000000; # approx world population
def n0: 27; # number of cases at day 0
Line 678:
((solve(f; 0.5; 0) * 1e10)|round / 1e10 ) as $r
| ((((12 * $r)|exp) * 1e8)|round / 1e8) as $R0
| "r = \($r), R0 = \($R0)"</langsyntaxhighlight>
{{out}}
<pre>
Line 687:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using LsqFit
 
const K = 7_800_000_000 # approximate world population
Line 723:
confidence_interval(fit, 0.05))
println("Since R0 ≈ exp(G * r), and G ≈ 12 days, R0 ≈ ", exp(12r[1]))
</langsyntaxhighlight>{{out}}
<pre>
The logistic curve r for the world data is: [0.11230217572265622]
Line 732:
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import kotlin.math.exp
 
const val K = 7.8e9
Line 793:
val r0 = exp(12.0 * r)
println("r = $r, R0 = $r0")
}</langsyntaxhighlight>
{{out}}
<pre>r = 0.11230217569755005, R0 = 3.8482792809167194</pre>
Line 800:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import math, sugar
 
const
Line 854:
let r = f.solve()
let r0 = exp(12 * r)
echo "r = ", r, ", R0 = ", r0</langsyntaxhighlight>
 
{{out}}
Line 861:
=={{header|Perl}}==
{{trans|Phix}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 909:
my $r = solve(\&logistic_func, 0.5, 0);
my $R0 = exp(12 * $r);
printf "r = %%(%.3f), R0 = %%(%.3f)\n", $r, $R0;</langsyntaxhighlight>
{{out}}
<pre>r = %(0.112), R0 = %(3.848)</pre>
Line 915:
=={{header|Phix}}==
Simplified, my interpretation of shift-cutting (from [[wp:Non-linear_least_squares]])
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Curve_fit.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 974:
<span style="color: #000000;">R0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"r = %.10f, R0 = %.8f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">R0</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 982:
=={{header|Python}}==
Uses NumPy/SciPy's optimize package.
<langsyntaxhighlight lang="python">import numpy as np
import scipy.optimize as opt
 
Line 1,012:
", with covariance of", cov)
print("The calculated R0 is then", np.exp(12 * r))
</langsyntaxhighlight>{{out}}
<pre>
The r for the world Covid-19 data is: [0.11230218] , with covariance of [[2.46164331e-08]]
Line 1,019:
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
library(minpack.lm)
 
Line 1,054:
 
cat("Therefore, R0 is approximately: ", exp(12 * coef(nls.out)))
</langsyntaxhighlight>{{out}}
<pre>
The r for the model is: 0.1123022
Line 1,066:
Original task numbers of cases per day as of April 05 plus updated, as of today, May 11 numbers.
 
<syntaxhighlight lang="raku" perl6line>my $K = 7_800_000_000; # population
my $n0 = 27; # cases @ day 0
 
Line 1,142:
say "Reproductive rate: R0 = ", $R0.fmt('%08f');
}
</syntaxhighlight>
</lang>
{{out}}
<pre>For period: Dec 31 - Apr 5
Line 1,154:
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">K = 7.9e9
N0 = 27
ACTUAL = [
Line 1,212:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>r = 0.11230215850810021, R0 = 3.8482784871191575</pre>
Line 1,218:
=={{header|Vlang}}==
{{trans|wren}}
<langsyntaxhighlight lang="vlang">import math
const (
k = 7800000000 // approx world population
Line 1,274:
r0 := math.round(math.exp(12 * r) * 1e8) / 1e8
println("r = $r, R0 = $r0")
}</langsyntaxhighlight>
{{out}}
<pre>r = 0.1123021757, R0 = 3.84827928</pre>
Line 1,280:
=={{header|Wren}}==
{{trans|Phix}}
<langsyntaxhighlight lang="ecmascript">var K = 7800000000 // approx world population
var n0 = 27 // number of cases at day 0
 
Line 1,333:
var r = (solve.call(f, 0.5, 0) * 1e10).round / 1e10
var R0 = ((12 * r).exp * 1e8).round / 1e8
System.print("r = %(r), R0 = %(R0)")</langsyntaxhighlight>
 
{{out}}
10,333

edits