<!--- Filename should be: topic_story_anwserformat.Rmd --->

```{r data generation, echo = FALSE, results = "hide"}
library("exams")
include_supplement('lebensfuchs.xlsx', recursive=TRUE)
library("exams.forge")
instruction <- NULL
suppressPackageStartupMessages(library("rio"))
pq     <- import("lebensfuchs.xlsx")
n <- 3
p <- q <- matrix(NA, n, ncol=2)
repeat {
  repeat {
    ind <- sample(1:nrow(pq), n)
    if (length(unique(substr(pq$produkt[ind], 1, 3)))==3) break 
  }
  g     <- pq$produkt[ind]
  p[,1] <- pq$preis[ind]
  p[,2] <- p[,1]+sample((-2:15)/10, n, replace=TRUE)
  q[,1] <- sample(3:15, n, replace=TRUE)
  q[,2] <- q[,1]+sample(-2:2, n, replace=TRUE)
  inter <- cbind(p1q0 = p[,2]*q[,1],
                 p0q0 = p[,1]*q[,1],
                 p1q1 = p[,2]*q[,2],
                 p0q1 = p[,1]*q[,2])
  inter <- rbind(inter, colSums(inter))
  las  <- inter[n+1,1]/inter[n+1,2]
  paa  <- inter[n+1,3]/inter[n+1,4]
  if (abs(las-paa)>1e-2) break
}
#
m <- cbind(p[,1], q[,1], p[,2], q[,2])
colnames(m) <- c("$p_0(i)$", "$q_0(i)$", "$p_1(i)$", "$q_1(i)$")
rownames(m) <- g  # gsub(" - ", "<br>", g, fixed=TRUE)
pindex <- (runif(1)>0.5)
hxy    <- html_matrix_sk(m, title="Gut", fmt=c("%.2f", "%.0f", "%.2f", "%.0f"))
rownames(inter) <- c(g, "$\\sum$") # gsub(" - ", "<br>", g, fixed=TRUE)
colnames(inter) <- c("$p_1(i)q_0(i)$", "$p_0(i)q_0(i)$", "$p_1(i)q_1(i)$", "$p_0(i)q_1(i)$")
hsol   <- html_matrix_sk(inter, fmt=c("%.2f", "%.2f", "%.2f", "%.2f"), title="Gut")
sc     <- num_result(if (pindex) las else paa, digits=3, tolmult=2)
```

Question
========

```{r, error=TRUE, child=instruction}
```

Für einen Warenkorb mit `r n` Gütern wurden für die Basiszeit ($t=0$) und die Berichtszeit ($t=1$) Preise ($p_0(i)$, $p_1(i)$) und Mengen ($q_0(i)$, $q_1(i)$) erhoben. Berechnen Sie den Preisindex nach `r if(pindex) "Laspeyres" else "Paasche"`.

```{r, echo=FALSE, results='asis'}
cat(toHTMLorLatex(hxy))
```

`r round_de(sc$digits)`.

Solution
=========

Mit den Daten aus der Tabelle ($t=1$) ergibt sich

$$I_{La,0,t}=\frac{\sum_{i=1}^n p_t(i)q_0(i)}{\sum_{i=1}^n p_0(i)q_0(i)}=\frac{`r inter[n+1,1]`}{`r inter[n+1,2]`}=`r las`$$ bzw.
$$I_{Pa,0,t}=\frac{\sum_{i=1}^n p_t(i)q_t(i)}{\sum_{i=1}^n p_0(i)q_t(i)}=\frac{`r inter[n+1,3]`}{`r inter[n+1,4]`}=`r paa`$$

```{r, echo=FALSE, results='asis'}
cat("<br>\n", toHTMLorLatex(hsol))
```

Meta-information
================
extype: num
exsolution: `r sc$fx[1]`
extol: `r sc$tolerance`
exname: `r if(exists("story")) story else knitr::current_input()`
