The n+1 Bernstein polynomials that comprise a basis are: b(nu, n, x) = choose (n, nu) * x^nu * (1-x)^(n-nu)
Bernstein polynomials are a partition of unity.
Sum(from nu=0 to nu=n, b(nu,n,x)) = 1 ; for an arbitraty (!!!) x
this can be used to assess the error of calculations of the polynomials themself.
Bernstein expansion of an arbitraty function is:
B(n, f) at x is equal to Sum(from nu=0 to nu=n, f(nu/n) * b(nu,n,x)
Let’s define the function
## Bernstein polynomial Bern(nu, n, x)
# defined for nu <n and 0<x<1 ## add ifelse sometime
Bern <- function (nu, n, x) {
f <- choose (n, nu) * x^nu * (1-x)^(n-nu)
}
#it's just dbinom(nu, n, x, log = FALSE)
Let’s stage the scene and plot the first polynomials, that are well known
require('ggplot2')
# is a vector of a 101 values between 0 and 1 including the end points
x <- seq(0,1, .01)
n <- 0; nu <- 0
y <- Bern(nu,n, x)
z <- dbinom(nu, n, x, log = FALSE)
qplot(x, y, color = I("red"),
size = I(1), alpha = I(1/2), geom = c("point"))
TRUE. b(0,0,x) is identically equal to 1 (because there is just one polynomial and the partition of unity principle stands)
For n=1 (order one) the TWO polynomials must be x and 1-x (for the same reason)
n <- 1; nu <- 0 #change to nu = 1 if you want to see the second one
y <- Bern(nu,n, x)
qplot(x, y, color = I("red"),
size = I(1), alpha = I(1/2), geom = c("point"))
Order 2 (with THREE polynomials: (1-x)^2 ; 2x(1-x) ; x^2)
n <- 2; nu <- 2 #change to nu = 1,2 if you want to see the second one
y <- Bern(nu,n, x)
z <- dbinom(nu, n, x, log = FALSE)
qplot(x, y, color = I("brown"),
size = I(1), alpha = I(1/2), geom = c("point"))
Let’ check the precision at the end points
z <- c(Bern(0,2,0), Bern(1,2,0), Bern(2,2,0))
z
u <- c(Bern(0,2,1), Bern(1,2,1), Bern(2,2,1))
u
So far so good.
Now something more elaborate
n <- 10; nu <- 5 # similar to a binomial with size=10 and p=0.5
y <- Bern(nu,n, x)
qplot(x, y, color = I("blue"),
size = I(1), alpha = I(1/2), geom = c("point", "line"))
Very understandable. We are changing probabilities for the same structure of a discrete relationship.
LS0tDQp0aXRsZTogIkJlcm5zdGVpbiBQb2x5bm9taWFscyINCm91dHB1dDogaHRtbF9ub3RlYm9vaw0KLS0tDQoNClRoZSBuKzEgQmVybnN0ZWluIHBvbHlub21pYWxzIHRoYXQgY29tcHJpc2UgYSBiYXNpcyANCmFyZToNCmIobnUsIG4sIHgpID0gY2hvb3NlIChuLCBudSkgKiB4Xm51ICogKDEteCleKG4tbnUpDQoNCkJlcm5zdGVpbiBwb2x5bm9taWFscyBhcmUgYSBwYXJ0aXRpb24gb2YgdW5pdHkuDQoNClN1bShmcm9tIG51PTAgdG8gbnU9biwgYihudSxuLHgpKSA9IDEgOyBmb3IgYW4gYXJiaXRyYXR5ICghISEpIHgNCg0KdGhpcyBjYW4gYmUgdXNlZCB0byBhc3Nlc3MgdGhlIGVycm9yIG9mIGNhbGN1bGF0aW9ucyBvZiANCnRoZSBwb2x5bm9taWFscyB0aGVtc2VsZi4NCg0KQmVybnN0ZWluIGV4cGFuc2lvbiBvZiBhbiBhcmJpdHJhdHkgZnVuY3Rpb24gaXM6DQoNCkIobiwgZikgYXQgeCBpcyBlcXVhbCB0byBTdW0oZnJvbSBudT0wIHRvIG51PW4sIGYobnUvbikgKiBiKG51LG4seCkNCg0KTGV0J3MgZGVmaW5lIHRoZSBmdW5jdGlvbg0KDQpgYGB7cn0NCiMjIEJlcm5zdGVpbiBwb2x5bm9taWFsIEJlcm4obnUsIG4sIHgpDQoNCiMgZGVmaW5lZCBmb3IgbnUgPG4gYW5kIDA8eDwxICMjIGFkZCBpZmVsc2Ugc29tZXRpbWUNCg0KQmVybiA8LSBmdW5jdGlvbiAobnUsIG4sIHgpIHsNCiAgZiA8LSBjaG9vc2UgKG4sIG51KSAqIHhebnUgKiAoMS14KV4obi1udSkgDQp9DQoNCiNpdCdzIGp1c3QgZGJpbm9tKG51LCBuLCB4LCBsb2cgPSBGQUxTRSkNCmBgYA0KDQpMZXQncyBzdGFnZSB0aGUg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