# 24.11.2010
x<-c(0,0,1,1,2)
y<-c(1,2,0,1,0)
f<-function(x,y) (x+2*y)/12
z<-f(x,y)
xyz<-cbind(x,y,z)

cloud(z~x*y,data=xyz,xlim=c(-1,3),ylim=c(-1,3),type=c("p","h"),
scales=list(arrows=F,distance=1.65,y=list(at=0:2), x=list(at=0:2)),
screen=list(x=-35,y=-35,z=-45))

f.cloud<-cloud(z~x*y,data=xyz,xlim=c(-1,3),ylim=c(-1,3),type=c("p","h"),
scales=list(arrows=F,distance=1.65,y=list(at=0:2), x=list(at=0:2)),
screen=list(x=-35,y=-35,z=-45))

print(f.cloud)

# Tällä tnf, reunajakaumat, plot
m <- matrix(1:4,2)
prop.table(m)
prop.table(m,1)
prop.table(m,2)

============
# Piirtofunktio

function.draw<-function(f,low=-1,hi=1,n=30)
{
r<-seq(low,hi,length=n)
z<-outer(r,r,f)
persp(r,r,z,xlab="x",ylab="y",zlab="z",theta = -90, phi = 30,ticktype="detailed")
}

# f(x,y)=8*x*y
f3<-function(x,y){ifelse(x>=y,8*x*y,0)}
function.draw(f3,0,1,25)

# f(x,y)=1
fTas<-function(x,y) x*y/(x*y)
function.draw(fTas,0,1,25)
######################
# Tasajakauma Tas(0,1)
######################
# Kf # theta = 45, phi = 30
# FTas(x,y)=x*y
FTas<-function(x,y){ifelse(x>=0 & x<=1 & y>=0 & y<=1,x*y,0)}
function.draw(FTas,0,1,25)

##################
# Tas(0,1) tiheysfunktio

x <- seq(0, 2, length= 30)
y <- x
f <- function(x,y) {ifelse(x>=0 & x<=1 & y>=0 & y<=1,1,0) }
z <- outer(x, y, f)
#z[is.na(z)] <- 1
#op <- par(bg = "white")
#persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",ticktype="detailed")

#################
# wireframe
#################
g <- expand.grid(x = 1:10, y = 5:15, gr = 1:2)
g$z <- log((g$x^g$g + g$y^2) * g$gr)
wireframe(z ~ x * y, data = g, groups = gr,
          scales = list(arrows = FALSE),
          drape = TRUE, colorkey = TRUE,
          screen = list(z = 30, x = -60))
###############

# Bivariate Normal Distribution
function1.draw <- function(f, low = -1, hi = 1, n = 50){
r <- seq(low, hi, length = n)
z <- outer(r, r, f)
persp(r, r, z, axes=FALSE, box=TRUE)}
#par(mfrow=c(1,3), pty="s")
f1 <- function(x,y){
exp( (x^2-2*0.5*x*y+y^2) / (-2*(1-0.5^2)) )/
(2*pi*sqrt(1-0.5^2))}
x <- seq(-3,3,length=100)
y <- x
function1.draw(f1,-3,3,20)
#####################
contour(x,y,outer(x,y,f1),nlevels=10)
image(x,y,outer(x,y,f1),zlim=range(outer(x,y,f1)),add = FALSE)
par(mfrow=c(1,1))
#####################
# 2Normaalijakauma
######################
function2.draw <- function(f, low = -1, hi = 1, n = 50){
r <- seq(low, hi, length = n)
z <- outer(r, r, f)
persp(r, r, z, axes=FALSE, box=TRUE)}
##############
f2N <- function(x,y,rho=0.9){
exp( (x^2-2*rho*x*y+y^2) / (-2*(1-rho^2)) )/
(2*pi*sqrt(1-rho^2))}
################
x <- seq(-3,3,length=100)
y <- x
function2.draw(f2N,-3,3,50)


#####################
function.draw<-function(f,low=-1,hi=1,n=30)
{
r<-seq(low,hi,length=n)
z<-outer(r,r,f)
persp(r,r,z,xlab="x",ylab="y",zlab="z",theta = 45, phi = 30,ticktype="detailed")
}





